Allows players to drag items onto the item container for quick storage.

允许玩家将物品拖到物品容器上以便快速入库。
This commit is contained in:
Cold-Mint 2024-09-29 23:30:42 +08:00
parent 2d92a92faf
commit c1c3fce58a
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
6 changed files with 75 additions and 19 deletions

View File

@ -51,21 +51,21 @@ public interface IItem
/// </summary>
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>
/// <para>The container in which the item is located</para>
/// <para>物品所在的物品容器</para>
/// </summary>
IItemContainer? ItemContainer { get; set; }
/// <summary>
/// <para>Its own container of items</para>
/// <para>自身的物品容器</para>
/// </summary>
/// <remarks>
/// <para>Returns a non-null value if the item itself can hold other items</para>
/// <para>物品本身可容纳其他物品,则返回非空值</para>
/// </remarks>
public IItemContainer? SelfItemContainer { get; set; }
/// <summary>
/// <para>Calculate how many items can be merged with other items</para>

View File

@ -78,6 +78,20 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
}
return placeholderItemContainer.CanReplaceItem(placeholderItem.Index, sourceItem);
default:
var sourceItemSelfContainer = sourceItem.SelfItemContainer;
if (sourceItemSelfContainer != null)
{
//Place the container on the item.
//将容器放在物品上。
return sourceItemSelfContainer.CanAddItem(Item);
}
var itemSelfContainer = Item.SelfItemContainer;
if (itemSelfContainer != null)
{
//Drag the item onto the container.
//将物品拖到容器上。
return itemSelfContainer.CanAddItem(sourceItem);
}
return Item.MergeableItemCount(sourceItem, sourceItem.Quantity) > 0;
}
}
@ -102,6 +116,48 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
return;
}
if (Item.SelfItemContainer != null)
{
//Use items and place them on the container.
//用物品,在物品容器上放置。
var oldIndex = sourceItem.Index;
var oldItemContainer = sourceItem.ItemContainer;
var addNumber = Item.SelfItemContainer.AddItem(sourceItem);
if (addNumber >= 0)
{
if (addNumber == sourceItem.Quantity)
{
oldItemContainer?.ClearItem(oldIndex);
}
else
{
oldItemContainer?.RemoveItem(oldIndex, addNumber);
}
}
return;
}
if (sourceItem.SelfItemContainer != null)
{
//Use containers and place on top of items.
//用容器物品,在物品上放置。
var oldIndex = Item.Index;
var oldItemContainer = Item.ItemContainer;
var addNumber = sourceItem.SelfItemContainer.AddItem(Item);
if (addNumber >= 0)
{
if (addNumber == Item.Quantity)
{
oldItemContainer?.ClearItem(oldIndex);
}
else
{
oldItemContainer?.RemoveItem(oldIndex, addNumber);
}
}
return;
}
if (Item is PlaceholderItem placeholderItem)
{
var placeholderItemContainer = placeholderItem.ItemContainer;

View File

@ -13,7 +13,6 @@ public partial class Packsack : PickAbleTemplate
{
private const string Path = "res://prefab/ui/packsackUI.tscn";
[Export] public int NumberSlots { get; set; }
public override bool CanContainItems { get; set; } = true;
public override void Use(Node2D? owner, Vector2 targetGlobalPosition)
{
GameSceneDepend.DynamicUiGroup?.ShowControl(Path, control =>
@ -35,7 +34,6 @@ public partial class Packsack : PickAbleTemplate
}
}
public IItemContainer? SelfItemContainer { get; set; }
public override void _Ready()
{

View File

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

View File

@ -37,7 +37,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
public bool CanAddItem(IItem item)
{
if (item.CanContainItems && !CanContainContainer)
if (item.SelfItemContainer != null && !CanContainContainer)
{
//The item to be added can hold other items, and this item container does not allow item containers.
//要添加的物品能够容纳其他物品,且此物品容器不允许放置物品容器。
@ -132,16 +132,17 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
//There can be more than one item, try to share equally.
//物品可有多个,尝试均摊。
var originalQuantity = item.Quantity;
var temporarilyQuantity = item.Quantity;
var index = 0;
foreach (var unitItem in _itemDictionary.Values)
{
var number = unitItem.MergeableItemCount(item, item.Quantity);
var number = unitItem.MergeableItemCount(item, temporarilyQuantity);
if (number == 0)
{
continue;
}
item.Quantity -= number;
temporarilyQuantity -= number;
unitItem.Quantity += number;
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
{
@ -165,7 +166,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
{
//The capacity is full. The remaining capacity cannot be stored.
//容量已满,无法存放剩余。
return originalQuantity - item.Quantity;
return originalQuantity - temporarilyQuantity;
}
//Add the rest to the container.
@ -237,7 +238,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
public bool CanReplaceItem(int index, IItem item)
{
if (item.CanContainItems && !CanContainContainer)
if (item.SelfItemContainer != null && !CanContainContainer)
{
return false;
}
@ -261,7 +262,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
}
return result;
}
public int RemoveSelectItem(int number)
{

View File

@ -77,8 +77,8 @@ 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; }
public IItemContainer? SelfItemContainer { get; set; }
private Label? _tipLabel;