From c1c3fce58af1e2036e87f2ea2c5c2848480fc4f4 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Sun, 29 Sep 2024 23:30:42 +0800 Subject: [PATCH] =?UTF-8?q?Allows=20players=20to=20drag=20items=20onto=20t?= =?UTF-8?q?he=20item=20container=20for=20quick=20storage.=20=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E7=8E=A9=E5=AE=B6=E5=B0=86=E7=89=A9=E5=93=81=E6=8B=96?= =?UTF-8?q?=E5=88=B0=E7=89=A9=E5=93=81=E5=AE=B9=E5=99=A8=E4=B8=8A=E4=BB=A5?= =?UTF-8?q?=E4=BE=BF=E5=BF=AB=E9=80=9F=E5=85=A5=E5=BA=93=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/inventory/IItem.cs | 20 ++++---- scripts/inventory/ItemSlotNode.cs | 56 +++++++++++++++++++++ scripts/inventory/Packsack.cs | 2 - scripts/inventory/PlaceholderItem.cs | 1 + scripts/inventory/UniversalItemContainer.cs | 13 ++--- scripts/pickable/PickAbleTemplate.cs | 2 +- 6 files changed, 75 insertions(+), 19 deletions(-) diff --git a/scripts/inventory/IItem.cs b/scripts/inventory/IItem.cs index 1a085d6..9a16648 100644 --- a/scripts/inventory/IItem.cs +++ b/scripts/inventory/IItem.cs @@ -51,21 +51,21 @@ public interface IItem /// bool IsSelect { get; set; } - /// - /// Whether this item can also hold other items - /// 此物品是否还能容纳其他物品 - /// - /// - ///For example, a backpack is an object that can hold other objects. - ///例如,背包是一个物品,他可以容纳其他物品。 - /// - bool CanContainItems { get; set; } - /// /// The container in which the item is located /// 物品所在的物品容器 /// IItemContainer? ItemContainer { get; set; } + + /// + /// Its own container of items + /// 自身的物品容器 + /// + /// + /// Returns a non-null value if the item itself can hold other items + /// 物品本身可容纳其他物品,则返回非空值 + /// + public IItemContainer? SelfItemContainer { get; set; } /// /// Calculate how many items can be merged with other items diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index fb16305..48fa874 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -78,6 +78,20 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay } return placeholderItemContainer.CanReplaceItem(placeholderItem.Index, sourceItem); default: + var sourceItemSelfContainer = sourceItem.SelfItemContainer; + if (sourceItemSelfContainer != null) + { + //Place the container on the item. + //将容器放在物品上。 + return sourceItemSelfContainer.CanAddItem(Item); + } + var itemSelfContainer = Item.SelfItemContainer; + if (itemSelfContainer != null) + { + //Drag the item onto the container. + //将物品拖到容器上。 + return itemSelfContainer.CanAddItem(sourceItem); + } return Item.MergeableItemCount(sourceItem, sourceItem.Quantity) > 0; } } @@ -102,6 +116,48 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay return; } + if (Item.SelfItemContainer != null) + { + //Use items and place them on the container. + //用物品,在物品容器上放置。 + var oldIndex = sourceItem.Index; + var oldItemContainer = sourceItem.ItemContainer; + var addNumber = Item.SelfItemContainer.AddItem(sourceItem); + if (addNumber >= 0) + { + if (addNumber == sourceItem.Quantity) + { + oldItemContainer?.ClearItem(oldIndex); + } + else + { + oldItemContainer?.RemoveItem(oldIndex, addNumber); + } + } + return; + } + + if (sourceItem.SelfItemContainer != null) + { + //Use containers and place on top of items. + //用容器物品,在物品上放置。 + var oldIndex = Item.Index; + var oldItemContainer = Item.ItemContainer; + var addNumber = sourceItem.SelfItemContainer.AddItem(Item); + if (addNumber >= 0) + { + if (addNumber == Item.Quantity) + { + oldItemContainer?.ClearItem(oldIndex); + } + else + { + oldItemContainer?.RemoveItem(oldIndex, addNumber); + } + } + return; + } + if (Item is PlaceholderItem placeholderItem) { var placeholderItemContainer = placeholderItem.ItemContainer; diff --git a/scripts/inventory/Packsack.cs b/scripts/inventory/Packsack.cs index 0eac4cb..e02532f 100644 --- a/scripts/inventory/Packsack.cs +++ b/scripts/inventory/Packsack.cs @@ -13,7 +13,6 @@ public partial class Packsack : PickAbleTemplate { private const string Path = "res://prefab/ui/packsackUI.tscn"; [Export] public int NumberSlots { get; set; } - public override bool CanContainItems { get; set; } = true; public override void Use(Node2D? owner, Vector2 targetGlobalPosition) { GameSceneDepend.DynamicUiGroup?.ShowControl(Path, control => @@ -35,7 +34,6 @@ public partial class Packsack : PickAbleTemplate } } - public IItemContainer? SelfItemContainer { get; set; } public override void _Ready() { diff --git a/scripts/inventory/PlaceholderItem.cs b/scripts/inventory/PlaceholderItem.cs index 998bc88..1037615 100644 --- a/scripts/inventory/PlaceholderItem.cs +++ b/scripts/inventory/PlaceholderItem.cs @@ -18,6 +18,7 @@ public class PlaceholderItem : IItem public bool IsSelect { get; set; } public bool CanContainItems { get; set; } = false; public IItemContainer? ItemContainer { get; set; } + public IItemContainer? SelfItemContainer { get; set; } public int MergeableItemCount(IItem other, int unallocatedQuantity) { diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index d6958e0..4f1cd2d 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -37,7 +37,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer public bool CanAddItem(IItem item) { - if (item.CanContainItems && !CanContainContainer) + if (item.SelfItemContainer != null && !CanContainContainer) { //The item to be added can hold other items, and this item container does not allow item containers. //要添加的物品能够容纳其他物品,且此物品容器不允许放置物品容器。 @@ -132,16 +132,17 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer //There can be more than one item, try to share equally. //物品可有多个,尝试均摊。 var originalQuantity = item.Quantity; + var temporarilyQuantity = item.Quantity; var index = 0; foreach (var unitItem in _itemDictionary.Values) { - var number = unitItem.MergeableItemCount(item, item.Quantity); + var number = unitItem.MergeableItemCount(item, temporarilyQuantity); if (number == 0) { continue; } - item.Quantity -= number; + temporarilyQuantity -= number; unitItem.Quantity += number; ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent { @@ -165,7 +166,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer { //The capacity is full. The remaining capacity cannot be stored. //容量已满,无法存放剩余。 - return originalQuantity - item.Quantity; + return originalQuantity - temporarilyQuantity; } //Add the rest to the container. @@ -237,7 +238,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer public bool CanReplaceItem(int index, IItem item) { - if (item.CanContainItems && !CanContainContainer) + if (item.SelfItemContainer != null && !CanContainContainer) { return false; } @@ -261,7 +262,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } return result; } - + public int RemoveSelectItem(int number) { diff --git a/scripts/pickable/PickAbleTemplate.cs b/scripts/pickable/PickAbleTemplate.cs index 9c335cb..6b9ce6f 100644 --- a/scripts/pickable/PickAbleTemplate.cs +++ b/scripts/pickable/PickAbleTemplate.cs @@ -77,8 +77,8 @@ public partial class PickAbleTemplate : RigidBody2D, IItem public int MaxQuantity { get; set; } = 1; public bool IsSelect { get; set; } - public virtual bool CanContainItems { get; set; } public IItemContainer? ItemContainer { get; set; } + public IItemContainer? SelfItemContainer { get; set; } private Label? _tipLabel;