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()
{
}
private static Dictionary Registry { get; } = [];
private static Texture2D DefaultTexture { get; } = new PlaceholderTexture2D();
///
/// Register an item type.
/// Return false if the item id already exist.
///
/// Whether the registration was successful.
public static bool Register(ItemType itemType) => Registry.TryAdd(itemType.Id, itemType);
///
/// Creates a new instance of the item registered to the given id.
/// Returns null when the id is not registered.
///
public static IItem? NewItem(string id) =>
Registry.TryGetValue(id, out var itemType) ? itemType.NewItemFunc() : null;
///
/// Get the translated default name of the item type for the given id
///
///
/// Translated default name of the item id if it exists. Else, return the id itself
///
public static string DefaultNameOf(string id) => TranslationServerUtils.Translate($"item_{id}") ?? id;
///
/// Get the translated default description of the item type for the given id
///
///
/// Translated default description of the item id if it exists. Else, return null
///
public static string? DefaultDescriptionOf(string id) => TranslationServerUtils.Translate($"item_{id}_desc");
///
/// Get the default icon of the item type for the given id
///
///
/// Translated default icon of the item id if it exists. Else, return a placeholder
///
public static Texture2D DefaultIconOf(string id) =>
Registry.TryGetValue(id, out var itemType)
? itemType.Icon ?? DefaultTexture
: DefaultTexture;
public static int MaxStackQuantityOf(string id) => Registry.TryGetValue(id, out var itemType) ? itemType.MaxStackQuantity : 0;
}