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)
{