From 0880feb2988416d1b9203d8609da971a94f51505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=A7=E9=9B=A8=E7=83=A8?= Date: Mon, 10 Jun 2024 23:08:48 +0800 Subject: [PATCH] Add new item interfaces and manager --- scripts/item/IItemStack.cs | 36 ++++++++++++++++++++ scripts/item/IItem_New.cs | 24 ++++++++++++++ scripts/item/ItemType.cs | 25 ++++++++++++++ scripts/item/ItemTypeManager.cs | 59 +++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 scripts/item/IItemStack.cs create mode 100644 scripts/item/IItem_New.cs create mode 100644 scripts/item/ItemType.cs create mode 100644 scripts/item/ItemTypeManager.cs diff --git a/scripts/item/IItemStack.cs b/scripts/item/IItemStack.cs new file mode 100644 index 0000000..23c0bae --- /dev/null +++ b/scripts/item/IItemStack.cs @@ -0,0 +1,36 @@ +using Godot; + +namespace ColdMint.scripts.item; + +public interface IItemStack +{ + /// + /// ID of items inside current stack + /// + string Id { get; } + + /// + /// Max number of current stack + /// + int MaxQuantity { get; } + + /// + /// Quantity of current stack + /// + int Quantity { get; set; } + + /// + /// Icon of current item + /// + Texture2D Icon { get; } + + /// + /// Display name of current item + /// + string Name { get; } + + /// + /// Description of current item, which may show in inventory + /// + string? Description { get; } +} \ No newline at end of file diff --git a/scripts/item/IItem_New.cs b/scripts/item/IItem_New.cs new file mode 100644 index 0000000..520a995 --- /dev/null +++ b/scripts/item/IItem_New.cs @@ -0,0 +1,24 @@ +using Godot; + +namespace ColdMint.scripts.item; + +//Todo: Merge this with IItem (and, then change this ugly name +public interface IItem_New +{ + /// + /// ID of current item + /// + string Id { get; } + /// + /// Icon of current item + /// + Texture2D Icon { get; } + /// + /// Display name of current item + /// + string Name { get; } + /// + /// Description of current item, which may show in inventory + /// + string? Description { get; } +} \ No newline at end of file diff --git a/scripts/item/ItemType.cs b/scripts/item/ItemType.cs new file mode 100644 index 0000000..4e33524 --- /dev/null +++ b/scripts/item/ItemType.cs @@ -0,0 +1,25 @@ +using System; + +using Godot; + +namespace ColdMint.scripts.item; + +public readonly struct ItemType +{ + /// + /// Item id of this type + /// + public string Id { get; init; } + /// + /// A function returns a new item instance of this type + /// + public Func Getter { get; init; } + /// + /// Default icon of items of this type + /// + public Texture2D? Texture { get; init; } + /// + /// Max number in item stack of this type + /// + public int MaxStackQuantity { get; init; } +} \ No newline at end of file diff --git a/scripts/item/ItemTypeManager.cs b/scripts/item/ItemTypeManager.cs new file mode 100644 index 0000000..d46be7b --- /dev/null +++ b/scripts/item/ItemTypeManager.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; + +using ColdMint.scripts.utils; + +using Godot; + +namespace ColdMint.scripts.item; + +public static class ItemTypeManager +{ + // Register items statically here + static ItemTypeManager() { } + + private static Dictionary Registry { get; } = []; + private static Texture2D DefaultTexture { get; } = new PlaceholderTexture2D(); + + + /// + /// Register a 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_New? NewItem(string id) => + Registry.TryGetValue(id, out var itemType) ? itemType.Getter() : 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.Texture ?? DefaultTexture + : DefaultTexture; +} \ No newline at end of file