Add new item interfaces and manager
This commit is contained in:
parent
fcffdf3720
commit
0880feb298
36
scripts/item/IItemStack.cs
Normal file
36
scripts/item/IItemStack.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
using Godot;
|
||||
|
||||
namespace ColdMint.scripts.item;
|
||||
|
||||
public interface IItemStack
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>ID of items inside current stack</para>
|
||||
/// </summary>
|
||||
string Id { get; }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Max number of current stack</para>
|
||||
/// </summary>
|
||||
int MaxQuantity { get; }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Quantity of current stack</para>
|
||||
/// </summary>
|
||||
int Quantity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Icon of current item</para>
|
||||
/// </summary>
|
||||
Texture2D Icon { get; }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Display name of current item</para>
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Description of current item, which may show in inventory</para>
|
||||
/// </summary>
|
||||
string? Description { get; }
|
||||
}
|
24
scripts/item/IItem_New.cs
Normal file
24
scripts/item/IItem_New.cs
Normal file
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>ID of current item</para>
|
||||
/// </summary>
|
||||
string Id { get; }
|
||||
/// <summary>
|
||||
/// <para>Icon of current item</para>
|
||||
/// </summary>
|
||||
Texture2D Icon { get; }
|
||||
/// <summary>
|
||||
/// <para>Display name of current item</para>
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
/// <summary>
|
||||
/// <para>Description of current item, which may show in inventory</para>
|
||||
/// </summary>
|
||||
string? Description { get; }
|
||||
}
|
25
scripts/item/ItemType.cs
Normal file
25
scripts/item/ItemType.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
|
||||
using Godot;
|
||||
|
||||
namespace ColdMint.scripts.item;
|
||||
|
||||
public readonly struct ItemType
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>Item id of this type</para>
|
||||
/// </summary>
|
||||
public string Id { get; init; }
|
||||
/// <summary>
|
||||
/// <para>A function returns a new item instance of this type</para>
|
||||
/// </summary>
|
||||
public Func<IItem_New> Getter { get; init; }
|
||||
/// <summary>
|
||||
/// <para>Default icon of items of this type</para>
|
||||
/// </summary>
|
||||
public Texture2D? Texture { get; init; }
|
||||
/// <summary>
|
||||
/// <para>Max number in item stack of this type</para>
|
||||
/// </summary>
|
||||
public int MaxStackQuantity { get; init; }
|
||||
}
|
59
scripts/item/ItemTypeManager.cs
Normal file
59
scripts/item/ItemTypeManager.cs
Normal file
|
@ -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<string, ItemType> Registry { get; } = [];
|
||||
private static Texture2D DefaultTexture { get; } = new PlaceholderTexture2D();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Register a item type.
|
||||
/// Return false if the item id already exist.
|
||||
/// </summary>
|
||||
/// <returns>Whether the registration was successful.</returns>
|
||||
public static bool Register(ItemType itemType) => Registry.TryAdd(itemType.Id, itemType);
|
||||
|
||||
/// <summary>
|
||||
/// <para>Creates a new instance of the item registered to the given id.</para>
|
||||
/// <para>Returns null when the id is not registered.</para>
|
||||
/// </summary>
|
||||
public static IItem_New? NewItem(string id) =>
|
||||
Registry.TryGetValue(id, out var itemType) ? itemType.Getter() : null;
|
||||
|
||||
/// <summary>
|
||||
/// Get the translated default name of the item type for the given id
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Translated default name of the item id if it exists. Else, return the id itself
|
||||
/// </returns>
|
||||
public static string DefaultNameOf(string id) => TranslationServerUtils.Translate($"item_{id}") ?? id;
|
||||
|
||||
/// <summary>
|
||||
/// Get the translated default description of the item type for the given id
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Translated default description of the item id if it exists. Else, return null
|
||||
/// </returns>
|
||||
public static string? DefaultDescriptionOf(string id) => TranslationServerUtils.Translate($"item_{id}_desc");
|
||||
|
||||
/// <summary>
|
||||
/// Get the default icon of the item type for the given id
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Translated default icon of the item id if it exists. Else, return a placeholder
|
||||
/// </returns>
|
||||
public static Texture2D DefaultIconOf(string id) =>
|
||||
Registry.TryGetValue(id, out var itemType)
|
||||
? itemType.Texture ?? DefaultTexture
|
||||
: DefaultTexture;
|
||||
}
|
Loading…
Reference in New Issue
Block a user