From 3c33b055ab6a5f79df6952f79df91edcdb8c6147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=A7=E9=9B=A8=E7=83=A8?= Date: Thu, 13 Jun 2024 23:49:59 +0800 Subject: [PATCH] Add the feature to load simple item type information from yaml files --- data/itemRegs/packsacks.yaml | 4 ++ data/itemRegs/weapons.yaml | 4 ++ locals/Log.csv | 6 ++- scripts/item/ItemTypeManager.cs | 63 +++++++++++++++++++---- scripts/loader/uiLoader/MainMenuLoader.cs | 7 ++- 5 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 data/itemRegs/packsacks.yaml create mode 100644 data/itemRegs/weapons.yaml diff --git a/data/itemRegs/packsacks.yaml b/data/itemRegs/packsacks.yaml new file mode 100644 index 0000000..2ff0af2 --- /dev/null +++ b/data/itemRegs/packsacks.yaml @@ -0,0 +1,4 @@ +- id: packsack + scene_path: res://prefab/packsacks/packsack.tscn + max_stack_value: 1 + icon_path: res://sprites/Player.png \ No newline at end of file diff --git a/data/itemRegs/weapons.yaml b/data/itemRegs/weapons.yaml new file mode 100644 index 0000000..171c341 --- /dev/null +++ b/data/itemRegs/weapons.yaml @@ -0,0 +1,4 @@ +- id: staff_of_the_undead + scene_path: res://prefab/weapons/staffOfTheUndead.tscn + icon_path: res://sprites/weapon/staffOfTheUndead.png + max_stack_value: 1 \ No newline at end of file diff --git a/locals/Log.csv b/locals/Log.csv index 7a74051..cbd7af2 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -25,4 +25,8 @@ enter_the_room_debug,节点{0}进入房间{1}。,"Node {0} enters room {1}.",ノ death_info,生物{0}被{1}击败。,"Creature {0} was defeated by {1}.",生物{0}が{1}によって打ち負かされました。 loot_list_has_no_entries,ID为{0}的战利品表,没有指定条目。,"Loot list with ID {0}, no entry specified.",ID{0}の戦利品テーブルは、エントリ指定されていません。 not_within_the_loot_spawn_range,给定的数值{0}没有在战利品{1}的生成范围{2}内。,The given value {0} is not within the spawn range {2} of loot {1}.,与えられた数値{0}は戦利品{1}の生成範囲{2}内にありません。 -loot_data_quantity,有{0}个战利品数据被返回。,{0} loot data was returned.,{0}個の戦利品データが返されます。 \ No newline at end of file +loot_data_quantity,有{0}个战利品数据被返回。,{0} loot data was returned.,{0}個の戦利品データが返されます。 + +start_item_register_from_file,开始从文件注册物品信息,Start registering item information from files,アイテム情報をファイルから登録開始 +item_register_from_file,从文件{0}中注册物品信息,Registering item information from file {0},ファイル{0}からアイテム情報を登録する +item_register_find_item_in_file,注册发现的物品{0},Register discovered item {0},見つかったアイテム{0}を登録 \ No newline at end of file diff --git a/scripts/item/ItemTypeManager.cs b/scripts/item/ItemTypeManager.cs index 4d35cfa..210a782 100644 --- a/scripts/item/ItemTypeManager.cs +++ b/scripts/item/ItemTypeManager.cs @@ -1,28 +1,71 @@ using System.Collections.Generic; +using System.Linq; +using ColdMint.scripts.debug; using ColdMint.scripts.utils; using Godot; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + 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, () => scene.Instantiate(), icon, typeInfo.MaxStackValue); + Register(itemType); + } + } + } + /// /// Register items statically here /// public static void StaticRegister() { - var staffOfTheUndeadScene = ResourceLoader.Load("res://prefab/weapons/staffOfTheUndead.tscn"); - var staffOfTheUndeadIcon = ResourceLoader.Load("res://sprites/weapon/staffOfTheUndead.png"); - var staffOfTheUndead = - new ItemType("staff_of_the_undead", () => staffOfTheUndeadScene.Instantiate(), staffOfTheUndeadIcon, 1); - Register(staffOfTheUndead); - - var packsackScene = ResourceLoader.Load("res://prefab/packsacks/packsack.tscn"); - var packsackIcon = ResourceLoader.Load("res://sprites/Player.png"); - var packsack = new ItemType("packsack", () => packsackScene.Instantiate(), packsackIcon, 1); - Register(packsack); + } private static Dictionary Registry { get; } = []; diff --git a/scripts/loader/uiLoader/MainMenuLoader.cs b/scripts/loader/uiLoader/MainMenuLoader.cs index 4eede2e..5bad091 100644 --- a/scripts/loader/uiLoader/MainMenuLoader.cs +++ b/scripts/loader/uiLoader/MainMenuLoader.cs @@ -95,8 +95,11 @@ public partial class MainMenuLoader : UiLoaderTemplate CampManager.AddCamp(aborigines); _gameScene = (PackedScene)GD.Load("res://scenes/game.tscn"); - //Temp: Register ItemType - //临时:注册物品类型 + //Register ItemTypes from file + //从文件注册物品类型 + ItemTypeManager.RegisterFromFile(); + //Hardcoded ItemTypes Register + //硬编码注册物品类型 ItemTypeManager.StaticRegister(); }