2024-06-12 19:04:12 +00:00
|
|
|
|
using System.Collections.Generic;
|
2024-06-13 14:29:18 +00:00
|
|
|
|
using ColdMint.scripts.item.weapon;
|
2024-06-10 15:08:48 +00:00
|
|
|
|
using ColdMint.scripts.utils;
|
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.item;
|
|
|
|
|
|
2024-06-13 14:29:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Item manager</para>
|
|
|
|
|
/// <para>物品管理器</para>
|
|
|
|
|
/// </summary>
|
2024-06-10 15:08:48 +00:00
|
|
|
|
public static class ItemTypeManager
|
|
|
|
|
{
|
2024-06-13 06:13:55 +00:00
|
|
|
|
/// <summary>
|
2024-06-13 14:29:18 +00:00
|
|
|
|
/// <para>Register items here</para>
|
|
|
|
|
/// <para>在这里注册物品</para>
|
2024-06-13 06:13:55 +00:00
|
|
|
|
/// </summary>
|
2024-06-12 19:04:12 +00:00
|
|
|
|
public static void StaticRegister()
|
|
|
|
|
{
|
|
|
|
|
var staffOfTheUndeadScene = ResourceLoader.Load<PackedScene>("res://prefab/weapons/staffOfTheUndead.tscn");
|
2024-06-13 03:04:51 +00:00
|
|
|
|
var staffOfTheUndeadIcon = ResourceLoader.Load<Texture2D>("res://sprites/weapon/staffOfTheUndead.png");
|
|
|
|
|
var staffOfTheUndead =
|
2024-06-13 14:29:18 +00:00
|
|
|
|
new ItemType("staff_of_the_undead",
|
|
|
|
|
() => NodeUtils.InstantiatePackedScene<ProjectileWeapon>(staffOfTheUndeadScene), staffOfTheUndeadIcon,
|
|
|
|
|
1);
|
2024-06-12 19:04:12 +00:00
|
|
|
|
Register(staffOfTheUndead);
|
2024-06-13 03:04:51 +00:00
|
|
|
|
|
|
|
|
|
var packsackScene = ResourceLoader.Load<PackedScene>("res://prefab/packsacks/packsack.tscn");
|
|
|
|
|
var packsackIcon = ResourceLoader.Load<Texture2D>("res://sprites/Player.png");
|
2024-06-13 14:29:18 +00:00
|
|
|
|
var packsack = new ItemType("packsack", () => NodeUtils.InstantiatePackedScene<Packsack>(packsackScene),
|
|
|
|
|
packsackIcon, 1);
|
2024-06-13 03:04:51 +00:00
|
|
|
|
Register(packsack);
|
2024-06-12 19:04:12 +00:00
|
|
|
|
}
|
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-13 14:29:18 +00:00
|
|
|
|
/// <para>Register an item type.</para>
|
|
|
|
|
/// <para>Return false if the item id already exist.</para>
|
|
|
|
|
/// <para>注册一个物品类型</para>
|
|
|
|
|
/// <para>如果项目id已经存在,则返回false。</para>
|
2024-06-10 15:08:48 +00:00
|
|
|
|
/// </summary>
|
2024-06-13 14:29:18 +00:00
|
|
|
|
/// <returns><para>Whether the registration was successful.</para>
|
|
|
|
|
/// <para>注册是否成功。</para>
|
|
|
|
|
/// </returns>
|
2024-06-10 15:08:48 +00:00
|
|
|
|
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
|
|
|
|
|
2024-06-13 14:29:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Gets the maximum number of stacks for an item</para>
|
|
|
|
|
/// <para>获取某个物品的最大堆叠数量</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">
|
|
|
|
|
///<para>id</para>
|
|
|
|
|
///<para>物品ID</para>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int MaxStackQuantityOf(string id) =>
|
|
|
|
|
Registry.TryGetValue(id, out var itemType) ? itemType.MaxStackQuantity : 0;
|
2024-06-10 15:08:48 +00:00
|
|
|
|
}
|