From 23877e82cc8fcc5ad93bbfcb75d55c54f0875096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=A7=E9=9B=A8=E7=83=A8?= Date: Thu, 13 Jun 2024 03:07:55 +0800 Subject: [PATCH] =?UTF-8?q?Remove=20old=20item=20interfaces,rename=20new?= =?UTF-8?q?=20=E7=A7=BB=E9=99=A4=E6=97=A7=E7=9A=84=E7=89=A9=E5=93=81?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E9=87=8D=E5=91=BD=E5=90=8D=E6=96=B0?= =?UTF-8?q?=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/character/CharacterTemplate.cs | 8 +-- scripts/inventory/CommonItem.cs | 22 -------- scripts/inventory/IItem.cs | 58 --------------------- scripts/inventory/IItemContainer.cs | 10 ++-- scripts/inventory/ItemSlotNode.cs | 8 +-- scripts/inventory/UniversalItemContainer.cs | 10 ++-- scripts/item/{IItem_New.cs => IItem.cs} | 3 +- scripts/item/IItemStack.cs | 10 ++-- scripts/item/ItemType.cs | 4 +- scripts/item/ItemTypeManager.cs | 4 +- scripts/item/SingleItemStack.cs | 12 ++--- scripts/item/weapon/WeaponTemplate.cs | 2 +- scripts/projectile/ProjectileTemplate.cs | 2 +- 13 files changed, 36 insertions(+), 117 deletions(-) delete mode 100644 scripts/inventory/CommonItem.cs delete mode 100644 scripts/inventory/IItem.cs rename scripts/item/{IItem_New.cs => IItem.cs} (92%) diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index b2531af..fabc156 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -271,7 +271,7 @@ public partial class CharacterTemplate : CharacterBody2D { //Empty reference checking is implicitly performed here. //此处隐式的执行了空引用检查。 - if (pickAbleItem is not IItem_New item) + if (pickAbleItem is not IItem item) { return false; } @@ -348,7 +348,7 @@ public partial class CharacterTemplate : CharacterBody2D return false; } - if (_currentItem is IItem_New item) + if (_currentItem is IItem item) { item.Use(this, position); } @@ -537,7 +537,7 @@ public partial class CharacterTemplate : CharacterBody2D /// protected virtual void EnterThePickingRangeBody(Node node) { - if (node is not IItem_New) + if (node is not IItem) { return; } @@ -552,7 +552,7 @@ public partial class CharacterTemplate : CharacterBody2D /// protected virtual void ExitThePickingRangeBody(Node node) { - if (node is not IItem_New) + if (node is not IItem) { return; } diff --git a/scripts/inventory/CommonItem.cs b/scripts/inventory/CommonItem.cs deleted file mode 100644 index fbaccfc..0000000 --- a/scripts/inventory/CommonItem.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using Godot; - -namespace ColdMint.scripts.inventory; - -/* -/// -/// Common goods -/// 普通的物品 -/// -public class CommonItem : IItem -{ - public string? Id { get; set; } - public int Quantity { get; set; } - public int MaxStackQuantity { get; set; } - public Texture2D? Icon { get; set; } - public string? Name { get; set; } - public string? Description { get; set; } - public Action? OnUse { get; set; } - public Func? OnInstantiation { get; set; } -} -*/ \ No newline at end of file diff --git a/scripts/inventory/IItem.cs b/scripts/inventory/IItem.cs deleted file mode 100644 index 6de2d52..0000000 --- a/scripts/inventory/IItem.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using Godot; - -namespace ColdMint.scripts.inventory; - -/* -public interface IItem -{ - /// - /// Item and ID - /// 物品还有ID - /// - string? Id { get; set; } - - /// - /// Represents the quantity of this item - /// 表示此物品的数量 - /// - int Quantity { get; set; } - - /// - /// How many can this item stack up to - /// 这个物品最多叠加到多少个 - /// - int MaxStackQuantity { get; set; } - - /// - /// Items can be set with Icon - /// 物品可以设置图标 - /// - Texture2D? Icon { get; set; } - - /// - /// Item has a name - /// 物品有名称 - /// - string? Name { get; set; } - - /// - /// Description - /// 描述 - /// - string? Description { get; set; } - - - /// - /// When using items - /// 当使用物品时 - /// - Action? OnUse { get; set; } - - /// - /// When removing items from the backpack, instantiate them - /// 当从背包内取出,实例化物品时 - /// - Func? OnInstantiation { get; set; } -} -*/ \ No newline at end of file diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index 1cb88fc..e10b9bd 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -24,7 +24,7 @@ public interface IItemContainer /// /// /// - bool CanAddItem(IItem_New item); + bool CanAddItem(IItem item); /// /// Implement methods for adding items @@ -32,7 +32,7 @@ public interface IItemContainer /// /// /// - bool AddItem(IItem_New item); + bool AddItem(IItem item); /// /// Add an stack of items to this container @@ -64,7 +64,7 @@ public interface IItemContainer /// 如果存在,移除当前选中位置的槽位中的一个物品并将其返回 /// /// - IItem_New? PickItemFromItemSlotBySelectIndex(); + IItem? PickItemFromItemSlotBySelectIndex(); /// /// Remove the specified number of items from the item slot at the currently selected location, and return them as a new item stack @@ -115,7 +115,7 @@ public interface IItemContainer /// 如果存在,移除指定位置的槽位中的一个物品并将其返回 /// /// - IItem_New? PickItemFromItemSlot(int itemSlotIndex); + IItem? PickItemFromItemSlot(int itemSlotIndex); /// /// Remove the specified number of items from the item slot in the specified location, and return them as a new item stack @@ -157,7 +157,7 @@ public interface IItemContainer /// Return null if there is no slot to place the item in /// 若没有槽可放置此物品,则返回null /// - ItemSlotNode? Match(IItem_New item); + ItemSlotNode? Match(IItem item); /// /// Based on the given item stack, match the item slots where it can be added to diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index 58f8524..f6b27d4 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -50,14 +50,14 @@ public partial class ItemSlotNode : MarginContainer /// If present, get the item at the top of the item stack in this slot /// 如果存在,获取该槽位中物品堆顶部的物品 /// - public IItem_New? GetItem() => _itemStack?.GetItem(); + public IItem? GetItem() => _itemStack?.GetItem(); /// /// If present, remove an item in this slot and return it. /// 如果存在,移除该槽位中的一个物品并将其返回 /// /// - public IItem_New? PickItem() + public IItem? PickItem() { if (_itemStack is null) return null; @@ -149,7 +149,7 @@ public partial class ItemSlotNode : MarginContainer /// /// /// - public bool CanAddItem(IItem_New item) + public bool CanAddItem(IItem item) { if (_itemStack == null) return true; return _itemStack.CanAddItem(item); @@ -180,7 +180,7 @@ public partial class ItemSlotNode : MarginContainer /// Try to add an item to this slot, if it can't be added to this slot, return false /// 尝试向当前槽位中加入物品,如果该物品不能被放入该槽位,返回false /// - public bool AddItem(IItem_New item) + public bool AddItem(IItem item) { bool result; if (_itemStack is null) diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 3de0b44..24a9062 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -35,12 +35,12 @@ public class UniversalItemContainer : IItemContainer //_selectIndex默认为0. private int _selectIndex; - public bool CanAddItem(IItem_New item) + public bool CanAddItem(IItem item) { return Match(item) != null; } - public bool AddItem(IItem_New item) + public bool AddItem(IItem item) { var itemSlotNode = Match(item); if (itemSlotNode == null) @@ -87,7 +87,7 @@ public class UniversalItemContainer : IItemContainer return null; } - public IItem_New? PickItemFromItemSlotBySelectIndex() => PickItemFromItemSlot(_selectIndex); + public IItem? PickItemFromItemSlotBySelectIndex() => PickItemFromItemSlot(_selectIndex); public IItemStack? PickItemsFromItemSlotBySelectIndex(int value) => PickItemsFromItemSlot(_selectIndex, value); @@ -114,7 +114,7 @@ public class UniversalItemContainer : IItemContainer return _itemSlotNodes[safeIndex]; } - public IItem_New? PickItemFromItemSlot(int itemSlotIndex) + public IItem? PickItemFromItemSlot(int itemSlotIndex) { if (_itemSlotNodes == null) return null; var safeIndex = GetSafeIndex(itemSlotIndex); @@ -153,7 +153,7 @@ public class UniversalItemContainer : IItemContainer return itemSlot.RemoveItem(number); } - public ItemSlotNode? Match(IItem_New item) + public ItemSlotNode? Match(IItem item) { //Find and return the first slot that can hold this item, if the list is null or not found, return null //寻找并返回第一个遇到的可放置此物品的物品槽,若列表为空或不存在,将返回null diff --git a/scripts/item/IItem_New.cs b/scripts/item/IItem.cs similarity index 92% rename from scripts/item/IItem_New.cs rename to scripts/item/IItem.cs index a046842..8c9efc5 100644 --- a/scripts/item/IItem_New.cs +++ b/scripts/item/IItem.cs @@ -2,8 +2,7 @@ namespace ColdMint.scripts.item; -//Todo: Merge this with IItem (and, then change this ugly name -public interface IItem_New +public interface IItem { /// /// ID of current item diff --git a/scripts/item/IItemStack.cs b/scripts/item/IItemStack.cs index 40a1a32..e5ab606 100644 --- a/scripts/item/IItemStack.cs +++ b/scripts/item/IItemStack.cs @@ -45,14 +45,14 @@ public interface IItemStack /// 判断能否容纳指定物品 /// /// - public bool CanAddItem(IItem_New item); + public bool CanAddItem(IItem item); /// /// Hold a given item /// /// Item to hold by current stack /// Whether successful - public bool AddItem(IItem_New item); + public bool AddItem(IItem item); /// /// 判断能从指定物品堆中接收的物品数量 @@ -81,7 +81,7 @@ public interface IItemStack /// /// /// - public IItem_New? GetItem(); + public IItem? GetItem(); /// /// Pop the item instance at the top of current item stack and return it @@ -89,7 +89,7 @@ public interface IItemStack /// /// /// - public IItem_New? PickItem(); + public IItem? PickItem(); /// /// Remove the specified number of items and return them as a new item stack @@ -134,7 +134,7 @@ public interface IItemStack /// /// Create a new ItemStack with the given item as the first item /// - public static IItemStack FromItem(IItem_New item) => ItemTypeManager.MaxStackQuantityOf(item.Id) switch + public static IItemStack FromItem(IItem item) => ItemTypeManager.MaxStackQuantityOf(item.Id) switch { 1 => new SingleItemStack(item), > 1 => throw new NotImplementedException(), diff --git a/scripts/item/ItemType.cs b/scripts/item/ItemType.cs index 395670c..d12b93a 100644 --- a/scripts/item/ItemType.cs +++ b/scripts/item/ItemType.cs @@ -4,7 +4,7 @@ using Godot; namespace ColdMint.scripts.item; -public readonly struct ItemType(string id, Func newItemFunc, Texture2D? icon, int maxStackQuantity) +public readonly struct ItemType(string id, Func newItemFunc, Texture2D? icon, int maxStackQuantity) { /// /// Item id of this type @@ -13,7 +13,7 @@ public readonly struct ItemType(string id, Func newItemFunc, Texture2 /// /// A function returns a new item instance of this type /// - public Func NewItemFunc { get; init; } = newItemFunc; + public Func NewItemFunc { get; init; } = newItemFunc; /// /// Default icon of items of this type /// diff --git a/scripts/item/ItemTypeManager.cs b/scripts/item/ItemTypeManager.cs index c97aefb..663d736 100644 --- a/scripts/item/ItemTypeManager.cs +++ b/scripts/item/ItemTypeManager.cs @@ -12,7 +12,7 @@ public static class ItemTypeManager public static void StaticRegister() { var staffOfTheUndeadScene = ResourceLoader.Load("res://prefab/weapons/staffOfTheUndead.tscn"); - var staffOfTheUndead = new ItemType("staff_of_the_undead", () => staffOfTheUndeadScene.Instantiate(), null, 1); + var staffOfTheUndead = new ItemType("staff_of_the_undead", () => staffOfTheUndeadScene.Instantiate(), null, 1); Register(staffOfTheUndead); } @@ -31,7 +31,7 @@ public static class ItemTypeManager /// 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) => + public static IItem? NewItem(string id) => Registry.TryGetValue(id, out var itemType) ? itemType.NewItemFunc() : null; /// diff --git a/scripts/item/SingleItemStack.cs b/scripts/item/SingleItemStack.cs index 0a31888..a384172 100644 --- a/scripts/item/SingleItemStack.cs +++ b/scripts/item/SingleItemStack.cs @@ -10,9 +10,9 @@ namespace ColdMint.scripts.item; /// Item stack of single item /// //maybe we'd move this into inventory namespace -public class SingleItemStack(IItem_New item) : IItemStack +public class SingleItemStack(IItem item) : IItemStack { - public IItem_New Item { get; init; } = item; + public IItem Item { get; init; } = item; public string Id => Item.Id; public int MaxQuantity => 1; @@ -21,20 +21,20 @@ public class SingleItemStack(IItem_New item) : IItemStack public string Name => Item.Name; public string? Description => Item.Description; - public bool CanAddItem(IItem_New item) => false; + public bool CanAddItem(IItem item) => false; - public bool AddItem(IItem_New item) => false; + public bool AddItem(IItem item) => false; public int CanTakeFrom(IItemStack itemStack) => 0; public bool TakeFrom(IItemStack itemStack) => false; - public IItem_New? GetItem() + public IItem? GetItem() { return Quantity == 1 ? Item : null; } - public IItem_New? PickItem() + public IItem? PickItem() { Quantity = 0; return Item; diff --git a/scripts/item/weapon/WeaponTemplate.cs b/scripts/item/weapon/WeaponTemplate.cs index 6b5d314..e8bd693 100644 --- a/scripts/item/weapon/WeaponTemplate.cs +++ b/scripts/item/weapon/WeaponTemplate.cs @@ -13,7 +13,7 @@ namespace ColdMint.scripts.item.weapon; /// WeaponTemplate /// 武器模板 /// -public abstract partial class WeaponTemplate : RigidBody2D, IItem_New +public abstract partial class WeaponTemplate : RigidBody2D, IItem { private float _gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle(); diff --git a/scripts/projectile/ProjectileTemplate.cs b/scripts/projectile/ProjectileTemplate.cs index 3079f72..a98d1bf 100644 --- a/scripts/projectile/ProjectileTemplate.cs +++ b/scripts/projectile/ProjectileTemplate.cs @@ -116,7 +116,7 @@ public partial class ProjectileTemplate : CharacterBody2D //Match any item now //现在使它识别任何物品 - if (target is IItem_New) + if (target is IItem) { //Bullets are allowed to strike objects. //允许子弹撞击物品。