From 16b3a5a10653b32f3f2a73e572b0cab7c8487da7 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 01:18:55 +0800 Subject: [PATCH] =?UTF-8?q?UniversalItemContainer=20refactored=20done=20Un?= =?UTF-8?q?iversalItemContainer=E8=B0=83=E6=95=99=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/inventory/CommonItem.cs | 4 +- scripts/inventory/IItemContainer.cs | 128 ++++++++++++++++++-- scripts/inventory/ItemSlotNode.cs | 18 +-- scripts/inventory/UniversalItemContainer.cs | 111 +++++++++++------ scripts/item/IItemStack.cs | 11 +- 5 files changed, 216 insertions(+), 56 deletions(-) diff --git a/scripts/inventory/CommonItem.cs b/scripts/inventory/CommonItem.cs index 884383d..fbaccfc 100644 --- a/scripts/inventory/CommonItem.cs +++ b/scripts/inventory/CommonItem.cs @@ -3,6 +3,7 @@ using Godot; namespace ColdMint.scripts.inventory; +/* /// /// Common goods /// 普通的物品 @@ -17,4 +18,5 @@ public class CommonItem : IItem public string? Description { get; set; } public Action? OnUse { get; set; } public 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 5fb1cc7..1cb88fc 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -1,3 +1,7 @@ +using System; +using System.Collections; +using System.Collections.Generic; + using ColdMint.scripts.item; using Godot; @@ -30,6 +34,17 @@ public interface IItemContainer /// bool AddItem(IItem_New item); + /// + /// Add an stack of items to this container + /// 向当前容器中存入一堆物品 + /// + /// + /// + /// If the source item stack is empty after the operation is completed + /// 操作完成后,源物品堆是否被取空 + /// + bool AddItemStack(IItemStack itemStack); + /// /// Gets the selected location /// 获取选中的位置 @@ -43,14 +58,42 @@ public interface IItemContainer /// /// ItemSlotNode? GetSelectItemSlotNode(); + + /// + /// If present, remove an item from the slot at the currently selected location and return it. + /// 如果存在,移除当前选中位置的槽位中的一个物品并将其返回 + /// + /// + IItem_New? PickItemFromItemSlotBySelectIndex(); + + /// + /// Remove the specified number of items from the item slot at the currently selected location, and return them as a new item stack + /// 取出当前选中位置的物品槽中指定数量的物品,并作为新的物品堆返回 + /// + /// + /// Quantity to be taken out, inputs below zero represent all items + /// 要取出的数量,小于0的输入代表全部物品 + /// + /// + IItemStack? PickItemsFromItemSlotBySelectIndex( int value); /// /// Removes an item from the inventory at the currently selected location /// 移除当前选中位置物品栏内的物品 /// - /// - /// - bool RemoveItemFromItemSlotBySelectIndex(int number); + /// + /// Quantity to be removed, inputs below zero represent all items + /// 要删除的数量,小于0的输入代表全部物品 + /// + /// + /// The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0 + /// 若物品槽内物品少于指定的数量,返回相差的数量。否则返回0 + /// + /// + /// Will remove the removed items from the game, if that is not the intent, consider using the + /// 会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用 + /// + int RemoveItemFromItemSlotBySelectIndex(int number); /// /// Gets the number of item slots @@ -67,25 +110,90 @@ public interface IItemContainer /// ItemSlotNode? GetItemSlotNode(int index); + /// + /// If present, remove an item from the slot in the specified location and return it. + /// 如果存在,移除指定位置的槽位中的一个物品并将其返回 + /// + /// + IItem_New? 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 + /// 取出指定位置的物品槽中指定数量的物品,并作为新的物品堆返回 + /// + /// + /// + /// Quantity to be taken out, inputs below zero represent all items + /// 要取出的数量,小于0的输入代表全部物品 + /// + /// + IItemStack? PickItemsFromItemSlot(int itemSlotIndex, int value); + /// /// Removes an item from the item slot in the specified location /// 在指定位置的物品槽内移除物品 /// /// - /// - /// - bool RemoveItemFromItemSlot(int itemSlotIndex, int number); + /// + /// Quantity to be removed, inputs below zero represent all items + /// 要删除的数量,小于0的输入代表全部物品 + /// + /// + /// The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0 + /// 若物品槽内物品少于指定的数量,返回相差的数量。否则返回0 + /// + /// + /// Will remove the removed items from the game, if that is not the intent, consider using the + /// 会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用 + /// + int RemoveItemFromItemSlot(int itemSlotIndex, int number); /// - /// Based on the given item, match the item slots where it can be placed + /// Based on the given item, match the item slots where it can be added to /// 根据给定的物品,匹配可放置它的物品槽 /// /// /// - ///Return null if there is no slot to place the item in - ///若没有槽可放置此物品,则返回null + /// Return null if there is no slot to place the item in + /// 若没有槽可放置此物品,则返回null /// - ItemSlotNode? Matching(IItem_New item); + ItemSlotNode? Match(IItem_New item); + + /// + /// Based on the given item stack, match the item slots where it can be added to + /// 根据给定的物品堆,匹配可放置它的物品槽 + /// + /// + /// + /// Return null if there is no slot to add the item slot in + /// 若没有槽可放置此物品堆,则返回null + /// + ItemSlotNode? Match(IItemStack stack); + + /// + /// Match the first item slot that has item stack that satisfies the predicate + /// 匹配首个拥有满足指定条件的物品堆的物品槽 + /// + /// + /// + /// Return null if there is no slot satisfies the predicate + /// 若没有满足条件的槽位,返回null + /// + /// + ItemSlotNode? Match(Func predicate); + + /// + /// Match all item slots that has item stack that satisfies the predicate + /// 匹配所有拥有满足指定条件的物品堆的物品槽 + /// + /// + /// + /// IEnumerable for the item slot matched to, will be empty if there's no slot satisfies the predicate + /// 包含匹配到的槽位的IEnumerable,当没有满足条件的槽位时为空 + /// + /// + IEnumerable MatchAll(Func predicate); + /// /// AddItemSlot diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index ea526a6..b129e1c 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -90,13 +90,17 @@ public partial class ItemSlotNode : MarginContainer /// Removes the specified number of items from the item slot /// 在物品槽内移除指定数量的物品 /// - /// + /// + /// Quantity to be removed, inputs below zero represent all items + /// 要删除的数量,小于0的输入代表全部物品 + /// /// /// The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0 /// 若物品槽内物品少于指定的数量,返回相差的数量。否则返回0 /// /// - /// 会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用 + /// Will remove the removed items from the game, if that is not the intent, consider using the + /// 会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用 /// public int RemoveItem(int number) { @@ -200,16 +204,16 @@ public partial class ItemSlotNode : MarginContainer /// 尝试将一个物品堆合并至该槽位中 /// /// - /// Number of items remaining in the source item pile after the operation is completed - /// 操作完成后,源物品堆中剩余的物品数 + /// If the source item stack is empty after the operation is completed + /// 操作完成后,源物品堆是否被取空 /// - public int AddItemStack(IItemStack itemStack) + public bool AddItemStack(IItemStack itemStack) { - int result; + bool result; if (_itemStack is null) { _itemStack = itemStack; - result = 0; + result = false; } else { diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 538b695..975f837 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -1,6 +1,11 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; + using ColdMint.scripts.character; +using ColdMint.scripts.item; using ColdMint.scripts.utils; + using Godot; namespace ColdMint.scripts.inventory; @@ -30,20 +35,34 @@ public class UniversalItemContainer : IItemContainer //_selectIndex默认为0. private int _selectIndex; - public bool CanAddItem(IItem item) + public bool CanAddItem(IItem_New item) { - return Matching(item) != null; + return Match(item) != null; } - public bool AddItem(IItem item) + public bool AddItem(IItem_New item) { - var itemSlotNode = Matching(item); + var itemSlotNode = Match(item); if (itemSlotNode == null) { return false; } - return itemSlotNode.SetItem(item); + return itemSlotNode.AddItem(item); + } + + public bool AddItemStack(IItemStack itemStack) + { + while (true) + { + var itemSlotNode = Match(itemStack); + + if (itemSlotNode == null) + return false; + + if (itemSlotNode.AddItemStack(itemStack)) + return true; + } } public int GetSelectIndex() @@ -68,10 +87,11 @@ public class UniversalItemContainer : IItemContainer return null; } - public bool RemoveItemFromItemSlotBySelectIndex(int number) - { - return RemoveItemFromItemSlot(_selectIndex, number); - } + public IItem_New? PickItemFromItemSlotBySelectIndex() => PickItemFromItemSlot(_selectIndex); + + public IItemStack? PickItemsFromItemSlotBySelectIndex(int value) => PickItemsFromItemSlot(_selectIndex, value); + + public int RemoveItemFromItemSlotBySelectIndex(int number) => RemoveItemFromItemSlot(_selectIndex, number); public int GetItemSlotCount() { @@ -94,42 +114,65 @@ public class UniversalItemContainer : IItemContainer return _itemSlotNodes[safeIndex]; } - public bool RemoveItemFromItemSlot(int itemSlotIndex, int number) + public IItem_New? PickItemFromItemSlot(int itemSlotIndex) { - if (_itemSlotNodes == null) - { - return false; - } - + if (_itemSlotNodes == null) return null; var safeIndex = GetSafeIndex(itemSlotIndex); if (safeIndex == UnknownIndex) { - return false; + return null; + } + + var itemSlot = _itemSlotNodes[safeIndex]; + return itemSlot.PickItem(); + } + + public IItemStack? PickItemsFromItemSlot(int itemSlotIndex, int value) + { + if (_itemSlotNodes == null) return null; + var safeIndex = GetSafeIndex(itemSlotIndex); + if (safeIndex == UnknownIndex) + { + return null; + } + + var itemSlot = _itemSlotNodes[safeIndex]; + return itemSlot.PickItems(value); + } + + public int RemoveItemFromItemSlot(int itemSlotIndex, int number) + { + if (_itemSlotNodes == null) return number; + var safeIndex = GetSafeIndex(itemSlotIndex); + if (safeIndex == UnknownIndex) + { + return number; } var itemSlot = _itemSlotNodes[safeIndex]; return itemSlot.RemoveItem(number); } - public ItemSlotNode? Matching(IItem item) + public ItemSlotNode? Match(IItem_New item) { - if (_itemSlotNodes == null || _itemSlotNodes.Count == 0) - { - return null; - } + //Find and return the first slot that can hold this item, if the list is null or not found, return null + //寻找并返回第一个遇到的可放置此物品的物品槽,若列表为空或不存在,将返回null + return _itemSlotNodes?.FirstOrDefault(itemSlotNode => itemSlotNode.CanAddItem(item)); + } + public ItemSlotNode? Match(IItemStack stack) + { + throw new NotImplementedException(); + } - foreach (var itemSlotNode in _itemSlotNodes) - { - if (itemSlotNode.CanSetItem(item)) - { - //If there is an item slot to put this item in, then we return it. - //如果有物品槽可放置此物品,那么我们返回它。 - return itemSlotNode; - } - } + public ItemSlotNode? Match(Func predicate) + { + throw new NotImplementedException(); + } - return null; + public IEnumerable MatchAll(Func predicate) + { + throw new NotImplementedException(); } @@ -139,8 +182,8 @@ public class UniversalItemContainer : IItemContainer /// /// /// - ///-1 is returned on failure, and the index that does not result in an out-of-bounds subscript is returned on success - ///失败返回-1,成功返回不会导致下标越界的索引 + /// -1 is returned on failure, and the index that does not result in an out-of-bounds subscript is returned on success + /// 失败返回-1,成功返回不会导致下标越界的索引 /// private int GetSafeIndex(int itemSlotIndex) { diff --git a/scripts/item/IItemStack.cs b/scripts/item/IItemStack.cs index 1ebfba7..8507679 100644 --- a/scripts/item/IItemStack.cs +++ b/scripts/item/IItemStack.cs @@ -70,9 +70,9 @@ public interface IItemStack /// 被移入当前堆的物品堆 /// /// - /// 操作结束后原物品堆中剩余的物品数 + /// 操作结束后原物品堆是否为空 /// - public int TakeFrom(IItemStack itemStack); + public bool TakeFrom(IItemStack itemStack); /// /// Get item instance at the top of current stack without removing it from stack @@ -112,10 +112,13 @@ public interface IItemStack /// /// /// 在当前物品堆移除指定数量的物品,被移除的物品应当从游戏中移除。
- /// 如果您不想将它们从游戏中移除,请考虑: + /// 如果您并不打算将它们从游戏中移除,请考虑使用 ///
///
- /// + /// + /// Quantity to be removed, inputs below zero represent all items + /// 要删除的数量,小于0的输入代表全部物品 + /// /// /// The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0 /// 若物品槽内物品少于指定的数量,返回相差的数量。否则返回0