diff --git a/scripts/item/IItem.cs b/scripts/item/IItem.cs index d155aa0..6ed7bbb 100644 --- a/scripts/item/IItem.cs +++ b/scripts/item/IItem.cs @@ -26,6 +26,14 @@ public interface IItem /// 当前项目的描述 /// string? Description { get; } + /// + /// + /// Whether the current item can be put into item containers like packsack.
+ /// This attribute is usually set to false for items of the backpack class to avoid pack nesting. + ///
+ /// 当前物品是否可以放入背包类的容器中。一般将背包类物品的该属性设为false来避免背包嵌套。 + ///
+ bool CanPutInPack { get; } /// /// Execute when current item is used
e.g. when player clicks left mouse button with current item in hand
diff --git a/scripts/item/Packsack.cs b/scripts/item/Packsack.cs index 8654563..a8f0bf4 100644 --- a/scripts/item/Packsack.cs +++ b/scripts/item/Packsack.cs @@ -1,6 +1,7 @@ using ColdMint.scripts.inventory; using ColdMint.scripts.pickable; using ColdMint.scripts.utils; + using Godot; namespace ColdMint.scripts.item; @@ -14,6 +15,8 @@ public partial class Packsack : PickAbleTemplate private PackedScene? _packedScene; private PacksackUi? _packsackUi; + public override bool CanPutInPack => false; + public override void Destroy() { if (ItemContainer == null) return; @@ -31,10 +34,12 @@ public partial class Packsack : PickAbleTemplate { return; } + if (_packsackUi == null) { - _packsackUi = NodeUtils.InstantiatePackedScene(_packedScene,this); + _packsackUi = NodeUtils.InstantiatePackedScene(_packedScene, this); } + _packsackUi?.Show(); } @@ -45,6 +50,5 @@ public partial class Packsack : PickAbleTemplate base._Ready(); ItemContainer = new UniversalItemContainer(); _packedScene = GD.Load("res://prefab/ui/packsackUI.tscn"); - } } \ No newline at end of file diff --git a/scripts/item/itemStacks/PacksackStack.cs b/scripts/item/itemStacks/PacksackStack.cs index 0e78007..3e96266 100644 --- a/scripts/item/itemStacks/PacksackStack.cs +++ b/scripts/item/itemStacks/PacksackStack.cs @@ -17,17 +17,16 @@ public class PacksackStack(Packsack packsack) : IItemStack public Texture2D Icon => packsack.Icon; public string Name => packsack.Name; public string? Description => packsack.Description; - - //todo: 只拒绝是背包的物品是权宜之计,应该为物品加入一个“是否可以放入背包”的属性来实现这个判断。 + public bool CanAddItem(IItem item) { - if (item is Packsack) return false; + if (!item.CanPutInPack) return false; return packsack.ItemContainer?.CanAddItem(item) ?? false; } public bool AddItem(IItem item) { - if (item is Packsack) return false; + if (!item.CanPutInPack) return false; return packsack.ItemContainer?.AddItem(item) ?? false; } diff --git a/scripts/pickable/PickAbleTemplate.cs b/scripts/pickable/PickAbleTemplate.cs index 0de8019..5d0dd9d 100644 --- a/scripts/pickable/PickAbleTemplate.cs +++ b/scripts/pickable/PickAbleTemplate.cs @@ -21,6 +21,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem [Export] protected string? UniqueName { get; set; } public new string Name => UniqueName ?? ItemTypeManager.DefaultNameOf(Id); [Export] protected string? UniqueDescription { get; set; } + public virtual bool CanPutInPack => true; /// /// Owner