Commit e814b21e authored by hex's avatar hex

auto download field

parent a4e1c2ca
...@@ -79,6 +79,9 @@ public class GameField : OCGobject ...@@ -79,6 +79,9 @@ public class GameField : OCGobject
// 场地禁用容器 // 场地禁用容器
private readonly List<FieldDisabledContainer> field_disabled_containers = new List<FieldDisabledContainer>(); private readonly List<FieldDisabledContainer> field_disabled_containers = new List<FieldDisabledContainer>();
private readonly int[] _currentFieldCode = new int[2] { 0, 0 };
private readonly int[] _targetFieldCode = new int[2] { 0, 0 };
public ph currentPhase; public ph currentPhase;
#endregion #endregion
...@@ -315,6 +318,7 @@ public class GameField : OCGobject ...@@ -315,6 +318,7 @@ public class GameField : OCGobject
UpdatePHoleEffects(); UpdatePHoleEffects();
UpdatePNumberPositions(); UpdatePNumberPositions();
UpdateDisabledContainers(); UpdateDisabledContainers();
UpdateFieldTextures();
} }
private void UpdateFieldSize() private void UpdateFieldSize()
...@@ -371,6 +375,63 @@ public class GameField : OCGobject ...@@ -371,6 +375,63 @@ public class GameField : OCGobject
UpdatePHolePositions(); UpdatePHolePositions();
} }
private void UpdateFieldTextures()
{
for (int player = 0; player < 2; player++)
{
// 只有当目标和当前不一致时,才需要处理
if (_targetFieldCode[player] != _currentFieldCode[player])
{
ApplyFieldCode(player, _targetFieldCode[player]);
}
}
}
private void ApplyFieldCode(int player, int targetCode)
{
var fieldObject = UIHelper.getByName(gameObject, $"obj_{player}");
// Case 1: 目标是隐藏场地 (code为0)
if (targetCode <= 0)
{
AnimateFieldObject(fieldObject, Vector3.zero);
_currentFieldCode[player] = 0; // 更新当前状态
return;
}
// Case 2: 目标是显示某个场地,向 GameTextureManager 请求纹理
// get的第三个参数是当图片正在加载时返回的值,我们用null来判断
Texture2D tex = GameTextureManager.get(targetCode, GameTextureType.field_picture, null);
if (tex != null)
{
// 纹理已经加载好了!
// 检查它是否是“未知”纹理(加载失败的标志)
if (tex == GameTextureManager.unknown)
{
// 加载失败,隐藏场地
AnimateFieldObject(fieldObject, Vector3.zero);
}
else
{
// 加载成功,应用纹理并显示
UIHelper.getByName<UITexture>(gameObject, $"field_{player}").mainTexture = tex;
AnimateFieldObject(fieldObject, Vector3.one);
}
// 无论成功还是失败,只要处理过了,就更新当前状态,防止重复处理
_currentFieldCode[player] = targetCode;
}
else
{
// 纹理返回 null,意味着它仍在后台加载中。
// 我们这一帧什么都不做,下一帧Update时会再次检查。
// 可以选择在这里先隐藏旧的场地,以提供更好的过渡效果。
if (_currentFieldCode[player] != 0)
{
AnimateFieldObject(fieldObject, Vector3.zero);
}
}
}
private void HandlePHoleCreation() private void HandlePHoleCreation()
{ {
HandleMePHole(); HandleMePHole();
...@@ -566,62 +627,68 @@ public class GameField : OCGobject ...@@ -566,62 +627,68 @@ public class GameField : OCGobject
public void set(int player, int code) public void set(int player, int code)
{ {
if (player < 0 || player >= 2) return; if (player < 0 || player >= 2) return;
if (fieldCode[player] == code) return;
fieldCode[player] = code; // 如果目标已经是这个code,就没必要重复设置
if (_targetFieldCode[player] == code) return;
if (code > 0)
{ _targetFieldCode[player] = code;
SetFieldCode(player, code); // if (player < 0 || player >= 2) return;
} // if (fieldCode[player] == code) return;
else
{ // fieldCode[player] = code;
HideFieldObject(player);
} // if (code > 0)
} // {
// SetFieldCode(player, code);
private void SetFieldCode(int player, int code) // }
{ // else
Texture2D tex = LoadFieldCodeTexture(code); // {
var obj = UIHelper.getByName(gameObject, $"obj_{player}"); // HideFieldObject(player);
// }
if (tex != null) }
{
UIHelper.getByName<UITexture>(gameObject, $"field_{player}").mainTexture = tex; // private void SetFieldCode(int player, int code)
AnimateFieldObject(obj, Vector3.one); // {
} // Texture2D tex = LoadFieldCodeTexture(code);
else // var obj = UIHelper.getByName(gameObject, $"obj_{player}");
{
AnimateFieldObject(obj, Vector3.zero); // if (tex != null)
} // {
} // UIHelper.getByName<UITexture>(gameObject, $"field_{player}").mainTexture = tex;
// AnimateFieldObject(obj, Vector3.one);
private Texture2D LoadFieldCodeTexture(int code) // }
{ // else
string[] paths = { // {
$"picture/field/{code}.png", // AnimateFieldObject(obj, Vector3.zero);
$"expansions/pics/field/{code}.png", // }
$"pics/field/{code}.png", // }
$"picture/field/{code}.jpg",
$"expansions/pics/field/{code}.jpg", // private Texture2D LoadFieldCodeTexture(int code)
$"pics/field/{code}.jpg" // {
}; // string[] paths = {
// $"picture/field/{code}.png",
foreach (string path in paths) // $"expansions/pics/field/{code}.png",
{ // $"pics/field/{code}.png",
if (File.Exists(path)) // $"picture/field/{code}.jpg",
{ // $"expansions/pics/field/{code}.jpg",
return UIHelper.getTexture2D(path); // $"pics/field/{code}.jpg"
} // };
}
return null; // foreach (string path in paths)
} // {
// if (File.Exists(path))
private void HideFieldObject(int player) // {
{ // return UIHelper.getTexture2D(path);
var obj = UIHelper.getByName(gameObject, $"obj_{player}"); // }
AnimateFieldObject(obj, Vector3.zero); // }
} // return null;
// }
// private void HideFieldObject(int player)
// {
// var obj = UIHelper.getByName(gameObject, $"obj_{player}");
// AnimateFieldObject(obj, Vector3.zero);
// }
private void AnimateFieldObject(GameObject obj, Vector3 targetScale) private void AnimateFieldObject(GameObject obj, Vector3 targetScale)
{ {
......
...@@ -12,6 +12,7 @@ public enum GameTextureType ...@@ -12,6 +12,7 @@ public enum GameTextureType
card_picture = 0, card_picture = 0,
card_verticle_drawing = 1, card_verticle_drawing = 1,
card_feature = 3, card_feature = 3,
field_picture = 4,
} }
public class GameTextureManager public class GameTextureManager
...@@ -243,11 +244,29 @@ public class GameTextureManager ...@@ -243,11 +244,29 @@ public class GameTextureManager
pic.rawData = null; // 确保失败时rawData为null pic.rawData = null; // 确保失败时rawData为null
} }
} }
else if (pic.type == GameTextureType.card_picture && pic.code != 0 && AutoPicDownload) else if ((pic.type == GameTextureType.card_picture || pic.type == GameTextureType.field_picture) && pic.code != 0 && AutoPicDownload)
{ {
// 文件不存在,且是卡图,则尝试下载 // 文件不存在,且是卡图 或 场地,则尝试下载
string url = "https://cdntx.moecube.com/images/ygopro-images-zh-CN/" + pic.code.ToString() + ".jpg"; string url = "";
string finalPath = "picture/card/" + pic.code.ToString() + ".jpg"; string finalPath = "";
if (pic.type == GameTextureType.card_picture)
{
url = "https://cdntx.moecube.com/images/ygopro-images-zh-CN/" + pic.code.ToString() + ".jpg";
finalPath = "picture/card/" + pic.code.ToString() + ".jpg";
}
// + ADD: 为场地图片提供下载 URL 和保存路径
else if (pic.type == GameTextureType.field_picture)
{
url = $"https://cdntx.moecube.com/images/ygopro-images-zh-CN/field/{pic.code}.jpg";
finalPath = $"picture/field/{pic.code}.jpg"; // 保存到约定的文件夹
// 确保目录存在
string dir = Path.GetDirectoryName(finalPath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
lock (_lock) lock (_lock)
{ {
...@@ -294,6 +313,14 @@ public class GameTextureManager ...@@ -294,6 +313,14 @@ public class GameTextureManager
if (!File.Exists(path)) path = "picture/cardIn8thEdition/" + pic.code + ".jpg"; if (!File.Exists(path)) path = "picture/cardIn8thEdition/" + pic.code + ".jpg";
} }
break; break;
case GameTextureType.field_picture:
path = $"picture/field/{pic.code}.png";
if (!File.Exists(path)) path = $"expansions/pics/field/{pic.code}.png";
if (!File.Exists(path)) path = $"pics/field/{pic.code}.png";
if (!File.Exists(path)) path = $"picture/field/{pic.code}.jpg";
if (!File.Exists(path)) path = $"expansions/pics/field/{pic.code}.jpg";
if (!File.Exists(path)) path = $"pics/field/{pic.code}.jpg";
break;
} }
return path; return path;
} }
...@@ -349,7 +376,8 @@ public class GameTextureManager ...@@ -349,7 +376,8 @@ public class GameTextureManager
switch (pic.type) switch (pic.type)
{ {
case GameTextureType.card_picture: case GameTextureType.card_picture:
// 卡图直接使用,无需额外处理 case GameTextureType.field_picture:
// 卡图和场地直接使用,无需额外处理
texture = tempTexture; texture = tempTexture;
// 因为我们直接用了tempTexture,所以不要销毁它 // 因为我们直接用了tempTexture,所以不要销毁它
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment