using System.Collections.Generic; using ColdMint.scripts.utils; using Godot; namespace ColdMint.scripts.item; /// /// Item manager /// 物品管理器 /// public static class ItemTypeManager { 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. /// 创建给定物品id的新物品实例 /// /// /// Returns null when the id is not registered. /// 当物品id没有注册时返回null /// /// public static IItem? NewItem(string id) => Registry.TryGetValue(id, out var itemType) ? itemType.NewItemFunc() : null; /// /// Creates new instances in given amount of the item registered to the given id. /// 创建给定数量的注册到给定 id 的物品的新实例。 /// /// /// public static IList NewItems(string id, int amount) { IList result = []; for (int i = 0; i < amount; i++) { if (NewItem(id) is { } item) result.Add(item); } return result; } /// /// Creates new instance of the item registered to the given id, and put it into given position in both node tree and 2D space /// 创建以给定 id 注册的物品的新实例,并将其放到节点树和二维空间中的给定位置 /// /// /// /// /// Position in global coordinate /// 全局坐标中的位置 /// /// public static IItem? CreateItem(string id, Node? parent = null, Vector2? position = null) { var item = NewItem(id); parent?.CallDeferred("add_child", (item as Node)!); if (item is not Node2D node) return item; if (position is { } pos) node.GlobalPosition = pos; return item; } /// /// Creates new instances in given amount of the item registered to the given id, and put them into given position in both node tree and 2D space /// 创建以给定 id 注册的物品的给定数量的新实例,并将其放到节点树和二维空间中的给定位置 /// /// /// /// /// /// Position in global coordinate /// 全局坐标中的位置 /// /// public static IList CreateItems(string id, int amount, Node? parent = null, Vector2? position = null) { IList result = []; for (int i = 0; i < amount; i++) { if (CreateItem(id, parent, position) is { } item) result.Add(item); } return result; } /// /// Get the translated default name of the item type for the given id /// 获取指定物品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 /// 获取指定物品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 /// 获取指定物品id的默认图标 /// /// /// Default icon of the item id if it exists. Else, return a /// 当前物品id的默认图标,若无则返回一个 /// 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; }