From e9656d69926ee1fd093b6e915d2f4efc739429fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=A7=E9=9B=A8=E7=83=A8?= Date: Wed, 12 Jun 2024 02:22:04 +0800 Subject: [PATCH] (temp): Just a small refactoring in the huge interaction code (regarding ItemSlot and ItemStack), but I think it's necessary to leave a save point, after all, I have to go to sleep first --- scripts/character/CharacterTemplate.cs | 2 +- scripts/inventory/IItem.cs | 4 +- scripts/inventory/IItemContainer.cs | 10 +-- scripts/inventory/ItemSlotNode.cs | 86 +++++++++++++++----------- scripts/item/IItemStack.cs | 11 ++++ 5 files changed, 71 insertions(+), 42 deletions(-) diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index 5aa1650..8494134 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -277,7 +277,7 @@ public partial class CharacterTemplate : CharacterBody2D { //Empty reference checking is implicitly performed here. //此处隐式的执行了空引用检查。 - if (pickAbleItem is not IItem item) + if (pickAbleItem is not IItem_New item) { return false; } diff --git a/scripts/inventory/IItem.cs b/scripts/inventory/IItem.cs index 4cdbb0d..6de2d52 100644 --- a/scripts/inventory/IItem.cs +++ b/scripts/inventory/IItem.cs @@ -3,6 +3,7 @@ using Godot; namespace ColdMint.scripts.inventory; +/* public interface IItem { /// @@ -53,4 +54,5 @@ public interface IItem /// 当从背包内取出,实例化物品时 /// Func? OnInstantiation { get; set; } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index c275c7d..9678df3 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -1,4 +1,6 @@ -namespace ColdMint.scripts.inventory; +using ColdMint.scripts.item; + +namespace ColdMint.scripts.inventory; /// /// item container @@ -16,7 +18,7 @@ public interface IItemContainer /// /// /// - bool CanAddItem(IItem item); + bool CanAddItem(IItem_New item); /// /// Implement methods for adding items @@ -24,7 +26,7 @@ public interface IItemContainer /// /// /// - bool AddItem(IItem item); + bool AddItem(IItem_New item); /// /// Gets the selected location @@ -81,5 +83,5 @@ public interface IItemContainer ///Return null if there is no slot to place the item in ///若没有槽可放置此物品,则返回null /// - ItemSlotNode? Matching(IItem item); + ItemSlotNode? Matching(IItem_New item); } \ No newline at end of file diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index a5d30be..93a07f5 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -1,4 +1,6 @@ +using ColdMint.scripts.item; using ColdMint.scripts.utils; + using Godot; namespace ColdMint.scripts.inventory; @@ -9,7 +11,8 @@ namespace ColdMint.scripts.inventory; /// public partial class ItemSlotNode : MarginContainer { - private IItem? _item; + //private IItem? _item; + private IItemStack? _itemStack; private TextureRect? _backgroundTextureRect; private TextureRect? _iconTextureRect; private Label? _quantityLabel; @@ -35,56 +38,69 @@ public partial class ItemSlotNode : MarginContainer public TextureRect? BackgroundTextureRect => _backgroundTextureRect; /// - /// Get the items in the item slot - /// 获取物品槽内的物品 + /// Get the item stack in the item slot + /// 获取物品槽内的物品堆 /// /// - public IItem? GetItem() - { - return _item; - } + public IItemStack GetItemStack() => _itemStack; /// /// Removes the specified number of items from the item slot /// 在物品槽内移除指定数量的物品 /// /// - /// - public bool RemoveItem(int number) + /// + /// The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0 + /// 若物品槽内物品少于指定的数量,返回相差的数量。否则返回0 + /// + public int RemoveItem(int number) { - if (_item == null) + if (_itemStack == null) { - return false; + return number; } - var newNumber = _item.Quantity - number; - if (newNumber <= 0) - { - //If the specified number of items is removed, the number of items is less than or equal to 0. Then we return the removal successful and empty the inventory. - //如果移除指定数量的物品后,物品数量小于或等于0。那么我们返回移除成功,并清空物品栏。 - ClearItem(); - return true; - } + var result = _itemStack.RemoveItem(number); + //If the specified number of items is removed, the number of items is less than or equal to 0. Then we empty the inventory. + //如果移除指定数量的物品后,物品数量小于或等于0。那么我们清空物品栏。 + if (_itemStack.Quantity == 0) ClearSlot(); else { - _item.Quantity = newNumber; - UpdateTooltipText(_item); - UpdateQuantityLabel(_item.Quantity); - return true; + UpdateTooltipText(_itemStack); + UpdateQuantityLabel(_itemStack.Quantity); } + + return result; + + // var newNumber = _item.Quantity - number; + // if (newNumber <= 0) + // { + // //If the specified number of items is removed, the number of items is less than or equal to 0. Then we return the removal successful and empty the inventory. + // //如果移除指定数量的物品后,物品数量小于或等于0。那么我们返回移除成功,并清空物品栏。 + // ClearItem(); + // return true; + // } + // else + // { + // _item.Quantity = newNumber; + // UpdateTooltipText(_item); + // UpdateQuantityLabel(_item.Quantity); + // return true; + // } } /// - /// Empty the items in the item slot - /// 清空物品槽内的物品 + /// Empty the item slot + /// 清空物品槽 /// /// - ///This method does not calculate how many items should be left. If you want to remove a specified number of items, call the RemoveItem method. - ///此方法不会计算物品应该剩余多少个。若您希望移除指定数量的物品,请调用RemoveItem方法。 + ///This method does not calculate how many items should be left. If you want to remove a specified number of items, call the method. + ///此方法不会计算物品应该剩余多少个。若您希望移除指定数量的物品,请调用方法。 /// - public void ClearItem() + public void ClearSlot() { - _item = null; + _itemStack = null; + if (_iconTextureRect != null) { _iconTextureRect.Texture = null; @@ -95,12 +111,10 @@ public partial class ItemSlotNode : MarginContainer _control.TooltipText = null; } - if (_quantityLabel != null) - { - _quantityLabel.Hide(); - } + _quantityLabel?.Hide(); } + //Todo: I searched until here. /// /// Can the specified item be placed in the item slot? @@ -186,9 +200,9 @@ public partial class ItemSlotNode : MarginContainer if (debugText != null) { _control.TooltipText = string.Format(debugText, item.Id, - TranslationServerUtils.Translate(item.Name), - item.Quantity, item.MaxStackQuantity, item.GetType().Name, - TranslationServerUtils.Translate(item.Description)); + TranslationServerUtils.Translate(item.Name), + item.Quantity, item.MaxStackQuantity, item.GetType().Name, + TranslationServerUtils.Translate(item.Description)); } } else diff --git a/scripts/item/IItemStack.cs b/scripts/item/IItemStack.cs index f476c63..1ff6bd9 100644 --- a/scripts/item/IItemStack.cs +++ b/scripts/item/IItemStack.cs @@ -37,6 +37,17 @@ public interface IItemStack string? Description { get; } + /// + /// Removes the specified number of items from current item stack + /// 在当前物品堆移除指定数量的物品 + /// + /// + /// + /// The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0 + /// 若物品槽内物品少于指定的数量,返回相差的数量。否则返回0 + /// + public int RemoveItem(int number); + /// /// Create a new ItemStack with the given item as the first item ///