Backpacks are no longer allowed in backpacks.

不允许背包内放置背包了。
This commit is contained in:
Cold-Mint 2024-09-29 22:12:54 +08:00
parent 31a1d292d8
commit 2d92a92faf
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
8 changed files with 63 additions and 28 deletions

View File

@ -17,6 +17,7 @@ public partial class HotBar : HBoxContainer
base._Ready(); base._Ready();
var universalItemContainer = new UniversalItemContainer(Config.HotBarSize); var universalItemContainer = new UniversalItemContainer(Config.HotBarSize);
_itemContainer = universalItemContainer; _itemContainer = universalItemContainer;
_itemContainer.CanContainContainer = true;
_itemContainer.SupportSelect = true; _itemContainer.SupportSelect = true;
_itemContainerDisplay = new ItemSlotContainerDisplay(this); _itemContainerDisplay = new ItemSlotContainerDisplay(this);
_itemContainerDisplay.BindItemContainer(_itemContainer); _itemContainerDisplay.BindItemContainer(_itemContainer);

View File

@ -51,6 +51,16 @@ public interface IItem
/// </summary> /// </summary>
bool IsSelect { get; set; } bool IsSelect { get; set; }
/// <summary>
/// <para>Whether this item can also hold other items</para>
/// <para>此物品是否还能容纳其他物品</para>
/// </summary>
/// <remarks>
///<para>For example, a backpack is an object that can hold other objects.</para>
///<para>例如,背包是一个物品,他可以容纳其他物品。</para>
/// </remarks>
bool CanContainItems { get; set; }
/// <summary> /// <summary>
/// <para>The container in which the item is located</para> /// <para>The container in which the item is located</para>
/// <para>物品所在的物品容器</para> /// <para>物品所在的物品容器</para>

View File

@ -44,7 +44,13 @@ public interface IItemContainer
/// <para>Whether this item container supports checking</para> /// <para>Whether this item container supports checking</para>
/// <para>此物品容器是否支持选中</para> /// <para>此物品容器是否支持选中</para>
/// </summary> /// </summary>
public bool SupportSelect { get; set; } bool SupportSelect { get; set; }
/// <summary>
/// <para>Whether this item container supports items that CanContainItems=true, such as backpacks</para>
/// <para>此物品容器是否支持容纳CanContainItems=true的物品例如背包</para>
/// </summary>
bool CanContainContainer { get; set; }
/// <summary> /// <summary>
/// <para>Gets a placeholder object</para> /// <para>Gets a placeholder object</para>
@ -92,6 +98,15 @@ public interface IItemContainer
/// <returns></returns> /// <returns></returns>
bool ReplaceItem(int index, IItem item); bool ReplaceItem(int index, IItem item);
/// <summary>
/// <para>Whether items are replaceable</para>
/// <para>是否可替换物品</para>
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
/// <returns></returns>
bool CanReplaceItem(int index, IItem item);
/// <summary> /// <summary>
/// <para>ClearItem</para> /// <para>ClearItem</para>
/// <para>清理物品</para> /// <para>清理物品</para>

View File

@ -60,18 +60,24 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
{ {
return false; return false;
} }
var itemSlotNode = data.As<ItemSlotNode>();
var sourceItem = itemSlotNode.Item;
if (sourceItem == null)
{
return false;
}
switch (Item) switch (Item)
{ {
case null: case null:
case PlaceholderItem:
return true; return true;
default: case PlaceholderItem placeholderItem:
var itemSlotNode = data.As<ItemSlotNode>(); var placeholderItemContainer = placeholderItem.ItemContainer;
var sourceItem = itemSlotNode.Item; if (placeholderItemContainer == null)
if (sourceItem == null)
{ {
return false; return true;
} }
return placeholderItemContainer.CanReplaceItem(placeholderItem.Index, sourceItem);
default:
return Item.MergeableItemCount(sourceItem, sourceItem.Quantity) > 0; return Item.MergeableItemCount(sourceItem, sourceItem.Quantity) > 0;
} }
} }
@ -101,11 +107,12 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
var placeholderItemContainer = placeholderItem.ItemContainer; var placeholderItemContainer = placeholderItem.ItemContainer;
var sourceItemContainer = sourceItem.ItemContainer; var sourceItemContainer = sourceItem.ItemContainer;
var sourceItemIndex = sourceItem.Index; var sourceItemIndex = sourceItem.Index;
var replaceResult = false;
if (placeholderItemContainer != null) if (placeholderItemContainer != null)
{ {
placeholderItemContainer.ReplaceItem(placeholderItem.Index, sourceItem); replaceResult = placeholderItemContainer.ReplaceItem(placeholderItem.Index, sourceItem);
} }
if (sourceItemContainer != null) if (replaceResult && sourceItemContainer != null)
{ {
sourceItemContainer.ClearItem(sourceItemIndex); sourceItemContainer.ClearItem(sourceItemIndex);
} }

View File

@ -13,21 +13,7 @@ public partial class Packsack : PickAbleTemplate
{ {
private const string Path = "res://prefab/ui/packsackUI.tscn"; private const string Path = "res://prefab/ui/packsackUI.tscn";
[Export] public int NumberSlots { get; set; } [Export] public int NumberSlots { get; set; }
public override bool CanContainItems { get; set; } = true;
/// <summary>
/// <para>Whether to allow backpacks</para>
/// <para>是否允许放置背包</para>
/// </summary>
/// <remarks>
///<para>Can a new backpack be placed in the slot of the backpack?</para>
///<para>即此背包的槽位内是否可以再放置新的背包?</para>
/// </remarks>
[Export]
public bool BackpackAllowed { get; set; }
public override bool CanPutInPack => false;
public override void Use(Node2D? owner, Vector2 targetGlobalPosition) public override void Use(Node2D? owner, Vector2 targetGlobalPosition)
{ {
GameSceneDepend.DynamicUiGroup?.ShowControl(Path, control => GameSceneDepend.DynamicUiGroup?.ShowControl(Path, control =>

View File

@ -16,6 +16,7 @@ public class PlaceholderItem : IItem
public int Quantity { get; set; } = 1; public int Quantity { get; set; } = 1;
public int MaxQuantity { get; } = 1; public int MaxQuantity { get; } = 1;
public bool IsSelect { get; set; } public bool IsSelect { get; set; }
public bool CanContainItems { get; set; } = false;
public IItemContainer? ItemContainer { get; set; } public IItemContainer? ItemContainer { get; set; }
public int MergeableItemCount(IItem other, int unallocatedQuantity) public int MergeableItemCount(IItem other, int unallocatedQuantity)

View File

@ -37,6 +37,12 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
public bool CanAddItem(IItem item) 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 the capacity is not full, directly return to add items
//如果未占满容量,直接返回可添加物品 //如果未占满容量,直接返回可添加物品
if (GetUsedCapacity() < totalCapacity) if (GetUsedCapacity() < totalCapacity)
@ -184,6 +190,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
} }
public bool SupportSelect { get; set; } public bool SupportSelect { get; set; }
public bool CanContainContainer { get; set; }
public IItem GetPlaceHolderItem(int index) public IItem GetPlaceHolderItem(int index)
@ -228,6 +235,15 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
return true; return true;
} }
public bool CanReplaceItem(int index, IItem item)
{
if (item.CanContainItems && !CanContainContainer)
{
return false;
}
return true;
}
public bool ClearItem(int index) public bool ClearItem(int index)
{ {

View File

@ -31,8 +31,6 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
} }
} }
public virtual bool CanPutInPack => true;
/// <summary> /// <summary>
/// <para>Owner</para> /// <para>Owner</para>
/// <para>主人</para> /// <para>主人</para>
@ -79,6 +77,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
public int MaxQuantity { get; set; } = 1; public int MaxQuantity { get; set; } = 1;
public bool IsSelect { get; set; } public bool IsSelect { get; set; }
public virtual bool CanContainItems { get; set; }
public IItemContainer? ItemContainer { get; set; } public IItemContainer? ItemContainer { get; set; }
private Label? _tipLabel; private Label? _tipLabel;