using System.Collections.Generic; using ColdMint.scripts.item.weapon; using ColdMint.scripts.utils; using Godot; namespace ColdMint.scripts.item; /// /// Item manager /// 物品管理器 /// public static class ItemTypeManager { /// /// Register items 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", () => NodeUtils.InstantiatePackedScene(staffOfTheUndeadScene), 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", () => NodeUtils.InstantiatePackedScene(packsackScene), packsackIcon, 1); Register(packsack); } private static Dictionary Registry { get; } = []; private static Texture2D DefaultTexture { get; } = new PlaceholderTexture2D(); /// /// Register an item type. /// Return false if the item id already exist. /// 注册一个物品类型 /// 如果项目id已经存在,则返回false。 /// /// 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; /// /// Gets the maximum number of stacks for an item /// 获取某个物品的最大堆叠数量 /// /// ///id ///物品ID /// /// public static int MaxStackQuantityOf(string id) => Registry.TryGetValue(id, out var itemType) ? itemType.MaxStackQuantity : 0; }