2024-06-12 19:04:12 +00:00
|
|
|
|
using System.Collections.Generic;
|
2024-06-10 15:08:48 +00:00
|
|
|
|
|
|
|
|
|
using ColdMint.scripts.utils;
|
|
|
|
|
|
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.item;
|
|
|
|
|
|
|
|
|
|
public static class ItemTypeManager
|
|
|
|
|
{
|
|
|
|
|
// Register items statically here
|
2024-06-12 19:04:12 +00:00
|
|
|
|
public static void StaticRegister()
|
|
|
|
|
{
|
|
|
|
|
var staffOfTheUndeadScene = ResourceLoader.Load<PackedScene>("res://prefab/weapons/staffOfTheUndead.tscn");
|
2024-06-12 19:07:55 +00:00
|
|
|
|
var staffOfTheUndead = new ItemType("staff_of_the_undead", () => staffOfTheUndeadScene.Instantiate<IItem>(), null, 1);
|
2024-06-12 19:04:12 +00:00
|
|
|
|
Register(staffOfTheUndead);
|
|
|
|
|
}
|
2024-06-10 15:08:48 +00:00
|
|
|
|
|
|
|
|
|
private static Dictionary<string, ItemType> Registry { get; } = [];
|
|
|
|
|
private static Texture2D DefaultTexture { get; } = new PlaceholderTexture2D();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-06-12 19:04:12 +00:00
|
|
|
|
/// Register an item type.
|
2024-06-10 15:08:48 +00:00
|
|
|
|
/// 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>
|
2024-06-12 19:07:55 +00:00
|
|
|
|
public static IItem? NewItem(string id) =>
|
2024-06-11 16:51:40 +00:00
|
|
|
|
Registry.TryGetValue(id, out var itemType) ? itemType.NewItemFunc() : null;
|
2024-06-10 15:08:48 +00:00
|
|
|
|
|
|
|
|
|
/// <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)
|
2024-06-11 16:51:40 +00:00
|
|
|
|
? itemType.Icon ?? DefaultTexture
|
2024-06-10 15:08:48 +00:00
|
|
|
|
: DefaultTexture;
|
2024-06-11 16:51:40 +00:00
|
|
|
|
|
|
|
|
|
public static int MaxStackQuantityOf(string id) => Registry.TryGetValue(id, out var itemType) ? itemType.MaxStackQuantity : 0;
|
2024-06-10 15:08:48 +00:00
|
|
|
|
}
|