Add the feature to load simple item type information from yaml files

This commit is contained in:
霧雨烨 2024-06-13 23:49:59 +08:00
parent c735851cc3
commit 3c33b055ab
5 changed files with 71 additions and 13 deletions

View File

@ -0,0 +1,4 @@
- id: packsack
scene_path: res://prefab/packsacks/packsack.tscn
max_stack_value: 1
icon_path: res://sprites/Player.png

View File

@ -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

View File

@ -26,3 +26,7 @@ death_info,生物{0}被{1}击败。,"Creature {0} was defeated by {1}.",生物{0
loot_list_has_no_entries,ID为{0}的战利品表,没有指定条目。,"Loot list with ID {0}, no entry specified.",ID{0}の戦利品テーブルは、エントリ指定されていません。 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}内にありません。 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}個の戦利品データが返されます。 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}を登録
1 id zh en ja
26 loot_list_has_no_entries ID为{0}的战利品表,没有指定条目。 Loot list with ID {0}, no entry specified. ID{0}の戦利品テーブルは、エントリ指定されていません。
27 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}内にありません。
28 loot_data_quantity 有{0}个战利品数据被返回。 {0} loot data was returned. {0}個の戦利品データが返されます。
29 start_item_register_from_file 开始从文件注册物品信息 Start registering item information from files アイテム情報をファイルから登録開始
30 item_register_from_file 从文件{0}中注册物品信息 Registering item information from file {0} ファイル{0}からアイテム情報を登録する
31 item_register_find_item_in_file 注册发现的物品{0} Register discovered item {0} 見つかったアイテム{0}を登録
32

View File

@ -1,28 +1,71 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using ColdMint.scripts.debug;
using ColdMint.scripts.utils; using ColdMint.scripts.utils;
using Godot; using Godot;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace ColdMint.scripts.item; namespace ColdMint.scripts.item;
public static class ItemTypeManager public static class ItemTypeManager
{ {
//Use for yaml deserialization
private record struct ItemTypeInfo(string Id, string ScenePath, string IconPath, int MaxStackValue) { }
/// <summary>
/// Register items from yaml file
/// </summary>
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<IEnumerable<ItemTypeInfo>>(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<PackedScene>(typeInfo.ScenePath);
var icon = ResourceLoader.Load<Texture2D>(typeInfo.IconPath);
var itemType = new ItemType(typeInfo.Id, () => scene.Instantiate<IItem>(), icon, typeInfo.MaxStackValue);
Register(itemType);
}
}
}
/// <summary> /// <summary>
/// Register items statically here /// Register items statically here
/// </summary> /// </summary>
public static void StaticRegister() public static void StaticRegister()
{ {
var staffOfTheUndeadScene = ResourceLoader.Load<PackedScene>("res://prefab/weapons/staffOfTheUndead.tscn");
var staffOfTheUndeadIcon = ResourceLoader.Load<Texture2D>("res://sprites/weapon/staffOfTheUndead.png");
var staffOfTheUndead =
new ItemType("staff_of_the_undead", () => staffOfTheUndeadScene.Instantiate<IItem>(), staffOfTheUndeadIcon, 1);
Register(staffOfTheUndead);
var packsackScene = ResourceLoader.Load<PackedScene>("res://prefab/packsacks/packsack.tscn");
var packsackIcon = ResourceLoader.Load<Texture2D>("res://sprites/Player.png");
var packsack = new ItemType("packsack", () => packsackScene.Instantiate<IItem>(), packsackIcon, 1);
Register(packsack);
} }
private static Dictionary<string, ItemType> Registry { get; } = []; private static Dictionary<string, ItemType> Registry { get; } = [];

View File

@ -95,8 +95,11 @@ public partial class MainMenuLoader : UiLoaderTemplate
CampManager.AddCamp(aborigines); CampManager.AddCamp(aborigines);
_gameScene = (PackedScene)GD.Load("res://scenes/game.tscn"); _gameScene = (PackedScene)GD.Load("res://scenes/game.tscn");
//Temp: Register ItemType //Register ItemTypes from file
//临时:注册物品类型 //从文件注册物品类型
ItemTypeManager.RegisterFromFile();
//Hardcoded ItemTypes Register
//硬编码注册物品类型
ItemTypeManager.StaticRegister(); ItemTypeManager.StaticRegister();
} }