Add CanPutInPack property of IItem

This commit is contained in:
霧雨烨 2024-06-16 18:41:26 +08:00
parent ced2618a5e
commit 3063680786
4 changed files with 18 additions and 6 deletions

View File

@ -26,6 +26,14 @@ public interface IItem
/// <para>当前项目的描述</para> /// <para>当前项目的描述</para>
/// </summary> /// </summary>
string? Description { get; } string? Description { get; }
/// <summary>
/// <para>
/// Whether the current item can be put into item containers like packsack.<br/>
/// This attribute is usually set to false for items of the backpack class to avoid pack nesting.
/// </para>
/// <para>当前物品是否可以放入背包类的容器中。一般将背包类物品的该属性设为false来避免背包嵌套。</para>
/// </summary>
bool CanPutInPack { get; }
/// <summary> /// <summary>
/// <para>Execute when current item is used <br/> e.g. when player clicks left mouse button with current item in hand</para> /// <para>Execute when current item is used <br/> e.g. when player clicks left mouse button with current item in hand</para>

View File

@ -1,6 +1,7 @@
using ColdMint.scripts.inventory; using ColdMint.scripts.inventory;
using ColdMint.scripts.pickable; using ColdMint.scripts.pickable;
using ColdMint.scripts.utils; using ColdMint.scripts.utils;
using Godot; using Godot;
namespace ColdMint.scripts.item; namespace ColdMint.scripts.item;
@ -14,6 +15,8 @@ public partial class Packsack : PickAbleTemplate
private PackedScene? _packedScene; private PackedScene? _packedScene;
private PacksackUi? _packsackUi; private PacksackUi? _packsackUi;
public override bool CanPutInPack => false;
public override void Destroy() public override void Destroy()
{ {
if (ItemContainer == null) return; if (ItemContainer == null) return;
@ -31,10 +34,12 @@ public partial class Packsack : PickAbleTemplate
{ {
return; return;
} }
if (_packsackUi == null) if (_packsackUi == null)
{ {
_packsackUi = NodeUtils.InstantiatePackedScene<PacksackUi>(_packedScene,this); _packsackUi = NodeUtils.InstantiatePackedScene<PacksackUi>(_packedScene, this);
} }
_packsackUi?.Show(); _packsackUi?.Show();
} }
@ -45,6 +50,5 @@ public partial class Packsack : PickAbleTemplate
base._Ready(); base._Ready();
ItemContainer = new UniversalItemContainer(); ItemContainer = new UniversalItemContainer();
_packedScene = GD.Load<PackedScene>("res://prefab/ui/packsackUI.tscn"); _packedScene = GD.Load<PackedScene>("res://prefab/ui/packsackUI.tscn");
} }
} }

View File

@ -18,16 +18,15 @@ public class PacksackStack(Packsack packsack) : IItemStack
public string Name => packsack.Name; public string Name => packsack.Name;
public string? Description => packsack.Description; public string? Description => packsack.Description;
//todo: 只拒绝是背包的物品是权宜之计,应该为物品加入一个“是否可以放入背包”的属性来实现这个判断。
public bool CanAddItem(IItem item) public bool CanAddItem(IItem item)
{ {
if (item is Packsack) return false; if (!item.CanPutInPack) return false;
return packsack.ItemContainer?.CanAddItem(item) ?? false; return packsack.ItemContainer?.CanAddItem(item) ?? false;
} }
public bool AddItem(IItem item) public bool AddItem(IItem item)
{ {
if (item is Packsack) return false; if (!item.CanPutInPack) return false;
return packsack.ItemContainer?.AddItem(item) ?? false; return packsack.ItemContainer?.AddItem(item) ?? false;
} }

View File

@ -21,6 +21,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
[Export] protected string? UniqueName { get; set; } [Export] protected string? UniqueName { get; set; }
public new string Name => UniqueName ?? ItemTypeManager.DefaultNameOf(Id); public new string Name => UniqueName ?? ItemTypeManager.DefaultNameOf(Id);
[Export] protected string? UniqueDescription { get; set; } [Export] protected string? UniqueDescription { get; set; }
public virtual bool CanPutInPack => true;
/// <summary> /// <summary>
/// <para>Owner</para> /// <para>Owner</para>