diff --git a/scripts/item/ItemTypeManager.cs b/scripts/item/ItemTypeManager.cs index 5b553a6..9416d07 100644 --- a/scripts/item/ItemTypeManager.cs +++ b/scripts/item/ItemTypeManager.cs @@ -16,56 +16,6 @@ namespace ColdMint.scripts.item; /// public static class ItemTypeManager { - //Use for yaml deserialization - private record struct ItemTypeInfo(string Id, string ScenePath, string IconPath, int MaxStackValue) { } - - /// - /// Register items from yaml file - /// 从文件注册物品 - /// - public static void RegisterFromFile() - { - LogCat.Log("start_item_register_from_file"); - - // initialize yaml deserializer - var deserializer = new DeserializerBuilder() - .WithNamingConvention(UnderscoredNamingConvention.Instance) // convent snake_case - .Build(); - - // initialize file dir - string itemRegsDirPath = "res://data/itemRegs/"; - var itemRegsDir = DirAccess.Open(itemRegsDirPath); - if (DirAccess.GetOpenError() is not Error.Ok) - { - LogCat.LogError("error_when_open_item_regs_dir"); - } - - // traverse the dir, find files to register - foreach (var file in itemRegsDir.GetFiles()) - { - if (file is null) continue; - LogCat.LogWithFormat("item_register_from_file", file); - - // read file, parse to an IEnumerable of type infos - var yamlFile = FileAccess.Open($"{itemRegsDirPath}/{file}", FileAccess.ModeFlags.Read); - var yamlString = yamlFile.GetAsText(); - var typeInfos = deserializer.Deserialize>(yamlString); - yamlFile.Close(); - - // traverse type infos and register them. - foreach (var typeInfo in typeInfos) - { - LogCat.LogWithFormat("item_register_find_item_in_file", typeInfo.Id); - var scene = ResourceLoader.Load(typeInfo.ScenePath); - var icon = ResourceLoader.Load(typeInfo.IconPath); - var itemType = new ItemType(typeInfo.Id, - () => NodeUtils.InstantiatePackedScene(scene), - icon, typeInfo.MaxStackValue); - Register(itemType); - } - } - } - /// /// Register items here /// 在这里注册物品 diff --git a/scripts/item/ItemTypeRegister.cs b/scripts/item/ItemTypeRegister.cs new file mode 100644 index 0000000..933492a --- /dev/null +++ b/scripts/item/ItemTypeRegister.cs @@ -0,0 +1,67 @@ +using System.Collections.Generic; + +using ColdMint.scripts.debug; +using ColdMint.scripts.utils; + +using Godot; + +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace ColdMint.scripts.item; + +/// +/// 负责从文件注册物品 +/// +public static class ItemTypeRegister +{ + /// + /// Register items from yaml file + /// 从文件注册物品 + /// + public static void RegisterFromFile() + { + LogCat.Log("start_item_register_from_file"); + + // initialize yaml deserializer + var deserializer = new DeserializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) // convent snake_case + .Build(); + + // initialize file dir + string itemRegsDirPath = "res://data/itemRegs/"; + var itemRegsDir = DirAccess.Open(itemRegsDirPath); + if (DirAccess.GetOpenError() is not Error.Ok) + { + LogCat.LogError("error_when_open_item_regs_dir"); + } + + // traverse the dir, find files to register + foreach (var file in itemRegsDir.GetFiles()) + { + if (file is null) continue; + LogCat.LogWithFormat("item_register_from_file", file); + + // read file, parse to an IEnumerable of type infos + var yamlFile = FileAccess.Open($"{itemRegsDirPath}/{file}", FileAccess.ModeFlags.Read); + var yamlString = yamlFile.GetAsText(); + var typeInfos = deserializer.Deserialize>(yamlString); + yamlFile.Close(); + + // traverse type infos and register them. + foreach (var typeInfo in typeInfos) + { + LogCat.LogWithFormat("item_register_find_item_in_file", typeInfo.Id); + var scene = ResourceLoader.Load(typeInfo.ScenePath); + var icon = ResourceLoader.Load(typeInfo.IconPath); + var itemType = new ItemType(typeInfo.Id, + () => NodeUtils.InstantiatePackedScene(scene), + icon, typeInfo.MaxStackValue); + ItemTypeManager.Register(itemType); + } + } + } + + //Use for yaml deserialization + private record struct ItemTypeInfo(string Id, string ScenePath, string IconPath, int MaxStackValue) { } +} \ No newline at end of file diff --git a/scripts/loader/uiLoader/MainMenuLoader.cs b/scripts/loader/uiLoader/MainMenuLoader.cs index 5bad091..3659e79 100644 --- a/scripts/loader/uiLoader/MainMenuLoader.cs +++ b/scripts/loader/uiLoader/MainMenuLoader.cs @@ -97,7 +97,7 @@ public partial class MainMenuLoader : UiLoaderTemplate //Register ItemTypes from file //从文件注册物品类型 - ItemTypeManager.RegisterFromFile(); + ItemTypeRegister.RegisterFromFile(); //Hardcoded ItemTypes Register //硬编码注册物品类型 ItemTypeManager.StaticRegister();