Item ids can only be specified in yaml files.

物品的id只能在yaml文件内指定了。
This commit is contained in:
Cold-Mint 2024-08-01 23:30:28 +08:00
parent 074f11e971
commit e54b62cb82
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
11 changed files with 53 additions and 57 deletions

View File

@ -1,5 +1,9 @@
#Register an item container here.
#在这里注册物品容器。
#Note: The id must be the same as the item id in the scene. Otherwise, an ArgumentException will be thrown.
#备注id必须和场景内的物品id保持一致。否则会抛出ArgumentException。
#After you declare the id of the item, add the corresponding localized text to the csv file in the locals' folder. For example, if the id is a corresponding name is item_a and the corresponding description is item_a_desc.
#当您声明物品的id后请在locals文件夹中的csv文件中添加相应的本地化文本。例如id为a则对应的名称为item_a对应的描述为item_a_desc。
#portable_backpacks
#便携式背包

View File

@ -1,5 +1,9 @@
#Register your weapon here.
#在这里注册你的武器。
#Note: The id must be the same as the item id in the scene. Otherwise, an ArgumentException will be thrown.
#备注id必须和场景内的物品id保持一致。否则会抛出ArgumentException。
#After you declare the id of the item, add the corresponding localized text to the csv file in the locals' folder. For example, if the id is a corresponding name is item_a and the corresponding description is item_a_desc.
#当您声明物品的id后请在locals文件夹中的csv文件中添加相应的本地化文本。例如id为a则对应的名称为item_a对应的描述为item_a_desc。
#staff_necromancy
#死灵法杖

View File

@ -1,5 +1,5 @@
id,zh,en,ja
item_staff_of_the_undead,死灵法杖,StaffOfTheUndead,ネクロポリスの杖です
item_staff_of_the_undead_desc,发射诅咒,可将敌人转化为邪恶的怪物。,Cast a curse that transforms enemies into evil monsters.,呪いを発射して、敵を邪悪な怪物に変えることができます。
item_packsack,背包,Packsack,背嚢
item_packsack_desc,可以装载更多物品的背包。,A backpack that can hold more items.,より多くのアイテムを保持できるバックパックです。
item_staff_necromancy,死灵法杖,staffNecromancy,ネクロポリスの杖です
item_staff_necromancy_desc,发射诅咒,可将敌人转化为邪恶的怪物。,Cast a curse that transforms enemies into evil monsters.,呪いを発射して、敵を邪悪な怪物に変えることができます。
item_portable_backpacks,便携式背包,PortableBackpacks,ポータブルバックパック
item_portable_backpacks_desc,为玩家提供9个物品槽。,Provides 9 item slots for the player.,プレイヤーに9つのアイテムスロットを提供します。
1 id zh en ja
2 item_staff_of_the_undead item_staff_necromancy 死灵法杖 StaffOfTheUndead staffNecromancy ネクロポリスの杖です
3 item_staff_of_the_undead_desc item_staff_necromancy_desc 发射诅咒,可将敌人转化为邪恶的怪物。 Cast a curse that transforms enemies into evil monsters. 呪いを発射して、敵を邪悪な怪物に変えることができます。
4 item_packsack item_portable_backpacks 背包 便携式背包 Packsack PortableBackpacks 背嚢 ポータブルバックパック
5 item_packsack_desc item_portable_backpacks_desc 可以装载更多物品的背包。 为玩家提供9个物品槽。 A backpack that can hold more items. Provides 9 item slots for the player. より多くのアイテムを保持できるバックパックです。 プレイヤーに9つのアイテムスロットを提供します。

View File

@ -14,7 +14,6 @@ collision_layer = 8
collision_mask = 38
script = ExtResource("1_slakl")
NumberSlots = 30
Id = "packsack"
[node name="DamageArea2D" type="Area2D" parent="."]
collision_layer = 8

View File

@ -1,9 +1,8 @@
[gd_scene load_steps=8 format=4 uid="uid://du5ldsp613fei"]
[gd_scene load_steps=7 format=4 uid="uid://du5ldsp613fei"]
[ext_resource type="TileSet" uid="uid://c4wpp12rr44hi" path="res://tileSets/dungeon.tres" id="1_rn2om"]
[ext_resource type="Script" path="res://scripts/map/PlayerSpawn.cs" id="2_6p8mv"]
[ext_resource type="Script" path="res://scripts/map/ItemSpawn.cs" id="3_v1tlc"]
[ext_resource type="Texture2D" uid="uid://b2blj0yf4ohx3" path="res://icon.svg" id="4_psvpu"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kiih8"]
size = Vector2(450, 191)
@ -42,11 +41,7 @@ script = ExtResource("2_6p8mv")
[node name="ItemMarker2D" type="Marker2D" parent="."]
position = Vector2(142, 84)
script = ExtResource("3_v1tlc")
ItemId = "staff_of_the_undead"
[node name="Icon" type="Sprite2D" parent="ItemMarker2D"]
scale = Vector2(0.3, 0.3)
texture = ExtResource("4_psvpu")
ItemId = "staff_necromancy"
[node name="NavigationRegion2D" type="NavigationRegion2D" parent="."]
navigation_polygon = SubResource("NavigationPolygon_064c7")

View File

@ -1,8 +1,9 @@
[gd_scene load_steps=7 format=3 uid="uid://dnnn2xyayiehk"]
[gd_scene load_steps=8 format=3 uid="uid://dnnn2xyayiehk"]
[ext_resource type="Texture2D" uid="uid://wt50kx6bup51" path="res://sprites/weapon/StaffNecromancy.png" id="1_ms3us"]
[ext_resource type="Script" path="res://scripts/weapon/ProjectileWeapon.cs" id="1_w8hhv"]
[ext_resource type="PackedScene" uid="uid://c01av43yk1q71" path="res://prefab/projectile/curseOfTheUndead.tscn" id="2_34250"]
[ext_resource type="Texture2D" uid="uid://dg5vwprt66w4j" path="res://sprites/weapon/StaffNecromancy_Icon.png" id="3_31iau"]
[ext_resource type="AudioStream" uid="uid://cak6chjjsu7wo" path="res://sounds/fire.wav" id="4_ffr2k"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_obcq2"]
@ -17,7 +18,7 @@ collision_mask = 34
script = ExtResource("1_w8hhv")
ProjectileScenes = [ExtResource("2_34250")]
FiringIntervalAsMillisecond = 300
Id = "staff_of_the_undead"
UniqueIcon = ExtResource("3_31iau")
[node name="DamageArea2D" type="Area2D" parent="."]
collision_layer = 8

View File

@ -6,9 +6,9 @@ public interface IItem
{
/// <summary>
/// <para>ID of current item</para>
/// <para>当前项目的ID</para>
/// <para>当前物品的ID</para>
/// </summary>
string Id { get; }
string Id { get; set; }
/// <summary>
/// <para>Icon of current item</para>

View File

@ -5,9 +5,10 @@ namespace ColdMint.scripts.inventory;
public readonly struct ItemType(
string id,
Func<Node?,IItem?> createItemFunc,
Texture2D? icon,
int maxStackQuantity)
int maxStackQuantity,
Func<Node?, IItem?> createItemFunc
)
{
/// <summary>
/// <para>Item id of this type</para>
@ -19,7 +20,7 @@ public readonly struct ItemType(
/// <para>A function returns a new item instance of this type</para>
/// <para>用于创建该类型的物品实例的函数</para>
/// </summary>
public Func<Node?,IItem?> CreateItemFunc { get; init; } = createItemFunc;
public Func<Node?, IItem?> CreateItemFunc { get; init; } = createItemFunc;
/// <summary>
/// <para>Default icon of items of this type</para>

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.inventory;
@ -49,7 +48,7 @@ public static class ItemTypeManager
/// <summary>
/// <para>Create multiple new item instances for the given item Id</para>
/// <para>Create multiple new item instances for the given item ID</para>
/// <para>创建多个给定物品Id的新物品实例</para>
/// </summary>
/// <param name="id"></param>
@ -84,24 +83,7 @@ public static class ItemTypeManager
return items.ToArray();
}
/// <summary>
/// <para>Get the translated default name of the item type for the given id</para>
/// <para>获取指定物品id翻译后的物品名</para>
/// </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>
/// <para>Get the translated default description of the item type for the given id</para>
/// <para>获取指定物品id翻译后的描述</para>
/// </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>
/// <para>Get the default icon of the item type for the given id</para>

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using ColdMint.scripts.debug;
using ColdMint.scripts.serialization;
using ColdMint.scripts.utils;
@ -30,7 +29,7 @@ public static class ItemTypeRegister
LogCat.Log("start_item_register_from_file");
//初始化文件目录
//initialize file dir
var itemRegsDirPath = "res://data/itemRegs";
const string itemRegsDirPath = "res://data/itemRegs";
var itemRegsDir = DirAccess.Open(itemRegsDirPath);
var error = DirAccess.GetOpenError();
if (error is not Error.Ok)
@ -103,15 +102,10 @@ public static class ItemTypeRegister
//加载场景和图标
var scene = ResourceLoader.Load<PackedScene>(typeInfo.ScenePath);
var icon = ResourceLoader.Load<Texture2D>(typeInfo.IconPath);
//Create init delegate
//创建初始化委托
Action<Node?>? setArgs = null;
//构造项目类型,寄存器
//construct item type, register
var itemType = new ItemType(typeInfo.Id,
defaultParentNode =>
icon, typeInfo.MaxStackValue, defaultParentNode =>
{
var newItem = NodeUtils.InstantiatePackedScene<IItem>(scene);
if (newItem is not Node node) return newItem;
@ -121,11 +115,10 @@ public static class ItemTypeRegister
return null;
}
setArgs?.Invoke(node);
newItem.Id = typeInfo.Id;
NodeUtils.CallDeferredAddChild(NodeUtils.FindContainerNode(node, defaultParentNode), node);
return newItem;
},
icon, typeInfo.MaxStackValue);
});
var succeed = ItemTypeManager.Register(itemType);
LogCat.LogWithFormat("register_item", label: LogCat.LogLabel.Default, LogCat.UploadFormat, itemType.Id,
succeed);
@ -138,5 +131,4 @@ public static class ItemTypeRegister
string ScenePath,
string IconPath,
int MaxStackValue);
}

View File

@ -4,6 +4,7 @@ using ColdMint.scripts.character;
using ColdMint.scripts.damage;
using ColdMint.scripts.debug;
using ColdMint.scripts.inventory;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.pickable;
@ -14,12 +15,21 @@ namespace ColdMint.scripts.pickable;
/// </summary>
public partial class PickAbleTemplate : RigidBody2D, IItem
{
[Export] public virtual string Id { get; set; } = "ID";
//Do not export this field because the ID is specified within yaml.
//不要导出此字段因为ID是在yaml内指定的。
public virtual string Id { get; set; } = "ID";
[Export] protected Texture2D? UniqueIcon { get; set; }
public Texture2D Icon => UniqueIcon ?? ItemTypeManager.DefaultIconOf(Id);
[Export] protected string? UniqueName { get; set; }
public new string Name => UniqueName ?? ItemTypeManager.DefaultNameOf(Id);
[Export] protected string? UniqueDescription { get; set; }
public new string Name
{
get
{
var key = $"item_{Id}";
return TranslationServerUtils.Translate(key) ?? key;
}
}
public virtual bool CanPutInPack => true;
/// <summary>
@ -37,7 +47,15 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
[Export] private int _minContactInjury = 1;
[Export] private int _maxContactInjury = 2;
public string? Description => UniqueDescription ?? ItemTypeManager.DefaultDescriptionOf(Id);
public string Description
{
get
{
var key = $"item_{Id}_desc";
return TranslationServerUtils.Translate(key) ?? key;
}
}
public int Quantity { get; set; } = 1;
/// <summary>