From 197930f4466592e4985c7d30bda154bc5907725e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=A7=E9=9B=A8=E7=83=A8?= Date: Sat, 15 Jun 2024 17:07:50 +0800 Subject: [PATCH] =?UTF-8?q?Support=20for=20optional=20custom=20parameters?= =?UTF-8?q?=20for=20registering=20item=20information=20from=20yaml=20?= =?UTF-8?q?=E4=B8=BA=E4=BB=8Eyaml=E6=B3=A8=E5=86=8C=E7=89=A9=E5=93=81?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=8F=90=E4=BE=9B=E4=BA=86=E5=8F=AF=E9=80=89?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=8F=82=E6=95=B0=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locals/Log.csv | 3 +- scripts/item/ICommonItem.cs | 4 +- scripts/item/ItemTypeRegister.cs | 71 ++++++++++++++++++++-- scripts/item/itemStacks/CommonItemStack.cs | 4 +- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/locals/Log.csv b/locals/Log.csv index 76bc83f..63ca9e8 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -32,4 +32,5 @@ log_warning_node_cannot_cast_to,创建的物品{0}无法被转型为类型{1},Cr log_start_item_register_from_file,开始从文件注册物品信息,Start registering item information from files,アイテム情報をファイルから登録開始 log_found_files,找到{0}个文件,Found {0} files,{0}ファイルが見つかりました log_found_item_types,从文件中找到{0}个物品类型,Found {0} item types in files,ファイルから{0}個のアイテム・タイプが見つかった -log_error_when_open_item_regs_dir,尝试打开物品信息目录时发生错误,Error when opening itemRegs dir,アイテム情報カタログを開こうとしてエラーが発生しました。 \ No newline at end of file +log_error_when_open_item_regs_dir,尝试打开物品信息目录时发生错误,Error when opening itemRegs dir,アイテム情報カタログを開こうとしてエラーが発生しました。 +log_wrong_custom_arg,不匹配的参数:类型为{0}而值为{1},Mismatched parameter: type {0} and value {1},パラメータの不一致:型{0}と値{1}。 \ No newline at end of file diff --git a/scripts/item/ICommonItem.cs b/scripts/item/ICommonItem.cs index b223a92..a41a60e 100644 --- a/scripts/item/ICommonItem.cs +++ b/scripts/item/ICommonItem.cs @@ -16,9 +16,9 @@ namespace ColdMint.scripts.item; public interface ICommonItem : IItem { /// - /// Method to copy an instance same with self. Will be used to pick out item instance from a + /// Method to clone an instance same with self. Will be used to pick out item instance from a /// 复制与自身相同的实例的方法。将用于从 中拿取新的物品实例。 /// /// - ICommonItem CopyInstance(); + ICommonItem CloneInstance(); } \ No newline at end of file diff --git a/scripts/item/ItemTypeRegister.cs b/scripts/item/ItemTypeRegister.cs index 3e93c51..9b22422 100644 --- a/scripts/item/ItemTypeRegister.cs +++ b/scripts/item/ItemTypeRegister.cs @@ -56,25 +56,88 @@ public static class ItemTypeRegister private static IList ParseFile(IDeserializer deserializer, string filePath) { - string itemRegsDirPath; - string file; var yamlFile = FileAccess.Open(filePath, FileAccess.ModeFlags.Read); + //Read & deserialize var yamlString = yamlFile.GetAsText(); var typeInfos = deserializer.Deserialize>(yamlString); + yamlFile.Close(); return typeInfos; } private static void RegisterTypeInfo(ItemTypeInfo typeInfo) { + //Load scene and icon var scene = ResourceLoader.Load(typeInfo.ScenePath); var icon = ResourceLoader.Load(typeInfo.IconPath); + + //Create init delegate + Func newItemFunc; + if (typeInfo.CustomArgs is null or []) + { + newItemFunc = () => NodeUtils.InstantiatePackedScene(scene); + } + else + { + Action? setArgs = null; + foreach (var arg in typeInfo.CustomArgs) + { + setArgs += + node => node?.SetDeferred(arg.Name, arg.ParseValue()); + } + + newItemFunc = () => + { + var newItem = NodeUtils.InstantiatePackedScene(scene); + setArgs?.Invoke(newItem as Node); + return newItem; + }; + } + + //construct item type, register var itemType = new ItemType(typeInfo.Id, - () => NodeUtils.InstantiatePackedScene(scene), + newItemFunc, icon, typeInfo.MaxStackValue); ItemTypeManager.Register(itemType); } //Use for yaml deserialization - private record struct ItemTypeInfo(string Id, string ScenePath, string IconPath, int MaxStackValue) { } + private record struct ItemTypeInfo( + string Id, string ScenePath, string IconPath, int MaxStackValue, + IList? CustomArgs) { } + + private readonly record struct CustomArg(string Name, CustomArgType Type, string Value) + { + public Variant ParseValue() => + Type switch + { + CustomArgType.String => Value, + CustomArgType.Int => int.Parse(Value), + CustomArgType.Float => double.Parse(Value), + CustomArgType.Vector2 => ParseVector2FromString(Value), + CustomArgType.Texture => ResourceLoader.Load("res://sprites/" + Value), + _ => throw new ArgumentOutOfRangeException($"Unknown Arg Type {Type}") + }; + + private Vector2 ParseVector2FromString(string s) + { + var ss = s.Split(','); + if (ss.Length != 2) + { + LogCat.LogErrorWithFormat("wrong_custom_arg", "Vector2", s); + return Vector2.Zero; + } + + return new Vector2(float.Parse(ss[0]), float.Parse(ss[1])); + } + } + + private enum CustomArgType + { + String, + Int, + Float, + Vector2, + Texture, + } } \ No newline at end of file diff --git a/scripts/item/itemStacks/CommonItemStack.cs b/scripts/item/itemStacks/CommonItemStack.cs index 1887718..0018e04 100644 --- a/scripts/item/itemStacks/CommonItemStack.cs +++ b/scripts/item/itemStacks/CommonItemStack.cs @@ -67,7 +67,7 @@ public class CommonItemStack(ICommonItem innerItem) : IItemStack { if(Empty) return null; Quantity--; - var result = innerItem.CopyInstance(); + var result = innerItem.CloneInstance(); if(Empty) innerItem.Destroy(); return result; } @@ -75,7 +75,7 @@ public class CommonItemStack(ICommonItem innerItem) : IItemStack public IItemStack? PickItems(int value) { if (Empty) return null; - var result = new CommonItemStack(innerItem.CopyInstance()); + var result = new CommonItemStack(innerItem.CloneInstance()); var n = Math.Min(Quantity, value); if (n < 0) {