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;