From 2d92a92faff05c98417dd5631176919cc51f1d58 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Sun, 29 Sep 2024 22:12:54 +0800 Subject: [PATCH] =?UTF-8?q?Backpacks=20are=20no=20longer=20allowed=20in=20?= =?UTF-8?q?backpacks.=20=E4=B8=8D=E5=85=81=E8=AE=B8=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E5=86=85=E6=94=BE=E7=BD=AE=E8=83=8C=E5=8C=85=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/inventory/HotBar.cs | 1 + scripts/inventory/IItem.cs | 10 +++++++++ scripts/inventory/IItemContainer.cs | 19 +++++++++++++++-- scripts/inventory/ItemSlotNode.cs | 23 ++++++++++++++------- scripts/inventory/Packsack.cs | 16 +------------- scripts/inventory/PlaceholderItem.cs | 1 + scripts/inventory/UniversalItemContainer.cs | 16 ++++++++++++++ scripts/pickable/PickAbleTemplate.cs | 5 ++--- 8 files changed, 63 insertions(+), 28 deletions(-) diff --git a/scripts/inventory/HotBar.cs b/scripts/inventory/HotBar.cs index 3a041ff..3515b0f 100644 --- a/scripts/inventory/HotBar.cs +++ b/scripts/inventory/HotBar.cs @@ -17,6 +17,7 @@ public partial class HotBar : HBoxContainer base._Ready(); var universalItemContainer = new UniversalItemContainer(Config.HotBarSize); _itemContainer = universalItemContainer; + _itemContainer.CanContainContainer = true; _itemContainer.SupportSelect = true; _itemContainerDisplay = new ItemSlotContainerDisplay(this); _itemContainerDisplay.BindItemContainer(_itemContainer); diff --git a/scripts/inventory/IItem.cs b/scripts/inventory/IItem.cs index d5f18af..1a085d6 100644 --- a/scripts/inventory/IItem.cs +++ b/scripts/inventory/IItem.cs @@ -51,6 +51,16 @@ 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 /// 物品所在的物品容器 diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index da46460..42bed97 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -44,7 +44,13 @@ public interface IItemContainer /// Whether this item container supports checking /// 此物品容器是否支持选中 /// - public bool SupportSelect { get; set; } + bool SupportSelect { get; set; } + + /// + /// Whether this item container supports items that CanContainItems=true, such as backpacks + /// 此物品容器是否支持容纳CanContainItems=true的物品,例如背包 + /// + bool CanContainContainer { get; set; } /// /// Gets a placeholder object @@ -91,6 +97,15 @@ public interface IItemContainer /// /// bool ReplaceItem(int index, IItem item); + + /// + /// Whether items are replaceable + /// 是否可替换物品 + /// + /// + /// + /// + bool CanReplaceItem(int index, IItem item); /// /// ClearItem @@ -99,7 +114,7 @@ public interface IItemContainer /// /// bool ClearItem(int index); - + /// /// Gets the item in the specified location, equivalent to /// 获取指定位置的物品,等同于 diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index 0d81db8..fb16305 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -60,18 +60,24 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay { return false; } + var itemSlotNode = data.As(); + var sourceItem = itemSlotNode.Item; + if (sourceItem == null) + { + return false; + } switch (Item) { case null: - case PlaceholderItem: return true; - default: - var itemSlotNode = data.As(); - var sourceItem = itemSlotNode.Item; - if (sourceItem == null) + case PlaceholderItem placeholderItem: + var placeholderItemContainer = placeholderItem.ItemContainer; + if (placeholderItemContainer == null) { - return false; + return true; } + return placeholderItemContainer.CanReplaceItem(placeholderItem.Index, sourceItem); + default: return Item.MergeableItemCount(sourceItem, sourceItem.Quantity) > 0; } } @@ -101,11 +107,12 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay var placeholderItemContainer = placeholderItem.ItemContainer; var sourceItemContainer = sourceItem.ItemContainer; var sourceItemIndex = sourceItem.Index; + var replaceResult = false; if (placeholderItemContainer != null) { - placeholderItemContainer.ReplaceItem(placeholderItem.Index, sourceItem); + replaceResult = placeholderItemContainer.ReplaceItem(placeholderItem.Index, sourceItem); } - if (sourceItemContainer != null) + if (replaceResult && sourceItemContainer != null) { sourceItemContainer.ClearItem(sourceItemIndex); } diff --git a/scripts/inventory/Packsack.cs b/scripts/inventory/Packsack.cs index 5a351f0..0eac4cb 100644 --- a/scripts/inventory/Packsack.cs +++ b/scripts/inventory/Packsack.cs @@ -13,21 +13,7 @@ public partial class Packsack : PickAbleTemplate { private const string Path = "res://prefab/ui/packsackUI.tscn"; [Export] public int NumberSlots { get; set; } - - /// - /// Whether to allow backpacks - /// 是否允许放置背包 - /// - /// - ///Can a new backpack be placed in the slot of the backpack? - ///即此背包的槽位内是否可以再放置新的背包? - /// - [Export] - public bool BackpackAllowed { get; set; } - - public override bool CanPutInPack => false; - - + public override bool CanContainItems { get; set; } = true; public override void Use(Node2D? owner, Vector2 targetGlobalPosition) { GameSceneDepend.DynamicUiGroup?.ShowControl(Path, control => diff --git a/scripts/inventory/PlaceholderItem.cs b/scripts/inventory/PlaceholderItem.cs index 7826eb4..998bc88 100644 --- a/scripts/inventory/PlaceholderItem.cs +++ b/scripts/inventory/PlaceholderItem.cs @@ -16,6 +16,7 @@ public class PlaceholderItem : IItem public int Quantity { get; set; } = 1; public int MaxQuantity { get; } = 1; public bool IsSelect { get; set; } + public bool CanContainItems { get; set; } = false; public IItemContainer? ItemContainer { get; set; } public int MergeableItemCount(IItem other, int unallocatedQuantity) diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 889f9fc..d6958e0 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -37,6 +37,12 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer public bool CanAddItem(IItem item) { + if (item.CanContainItems && !CanContainContainer) + { + //The item to be added can hold other items, and this item container does not allow item containers. + //要添加的物品能够容纳其他物品,且此物品容器不允许放置物品容器。 + return false; + } //If the capacity is not full, directly return to add items //如果未占满容量,直接返回可添加物品 if (GetUsedCapacity() < totalCapacity) @@ -184,6 +190,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } public bool SupportSelect { get; set; } + public bool CanContainContainer { get; set; } public IItem GetPlaceHolderItem(int index) @@ -228,6 +235,15 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer return true; } + public bool CanReplaceItem(int index, IItem item) + { + if (item.CanContainItems && !CanContainContainer) + { + return false; + } + return true; + } + public bool ClearItem(int index) { diff --git a/scripts/pickable/PickAbleTemplate.cs b/scripts/pickable/PickAbleTemplate.cs index d70a31a..9c335cb 100644 --- a/scripts/pickable/PickAbleTemplate.cs +++ b/scripts/pickable/PickAbleTemplate.cs @@ -30,9 +30,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem return TranslationServerUtils.Translate(key) ?? key; } } - - public virtual bool CanPutInPack => true; - + /// /// Owner /// 主人 @@ -79,6 +77,7 @@ 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; } private Label? _tipLabel;