2025-02-11 18:00:25 +08:00
|
|
|
|
namespace BeiBao
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 背包格子类
|
|
|
|
|
/// 每个背包格子存储一种物品,并管理物品数量和堆叠。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class InventorySlot
|
|
|
|
|
{
|
2025-02-12 10:52:39 +08:00
|
|
|
|
private Item _item;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 物品
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Item Item => _item;
|
|
|
|
|
|
2025-02-11 18:00:25 +08:00
|
|
|
|
public bool IsEmpty => Item == null;
|
|
|
|
|
|
|
|
|
|
// 将物品添加到格子中
|
|
|
|
|
public bool AddItem(Item item, int quantity)
|
|
|
|
|
{
|
|
|
|
|
if (IsEmpty)
|
|
|
|
|
{
|
2025-02-12 10:52:39 +08:00
|
|
|
|
_item = item;
|
2025-02-11 18:00:25 +08:00
|
|
|
|
return Item.AddQuantity(quantity) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Item.ItemID == item.ItemID && Item.Quantity < Item.MaxStack)
|
|
|
|
|
{
|
|
|
|
|
int remaining = Item.AddQuantity(quantity);
|
|
|
|
|
return remaining == 0;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从格子中取出物品
|
|
|
|
|
public bool RemoveItem(int quantity)
|
|
|
|
|
{
|
|
|
|
|
if (Item != null && Item.RemoveQuantity(quantity))
|
|
|
|
|
{
|
|
|
|
|
if (Item.Quantity == 0)
|
|
|
|
|
{
|
2025-02-12 10:52:39 +08:00
|
|
|
|
_item = null; // 如果物品数量为零,则清空格子
|
2025-02-11 18:00:25 +08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|