完成背包系统基础功能并验证成功

master
zxl 2025-02-12 10:52:39 +08:00
parent 3539d614a9
commit a965a36598
3 changed files with 77 additions and 23 deletions

View File

@ -1,24 +1,27 @@
 using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace BeiBao
namespace BeiBao
{
/// <summary>
/// 背包类
/// 背包是由多个格子组成的,每个格子都可以存放不同的物品。
/// </summary>
/// <summary>
/// 背包类
/// 背包是由多个格子组成的,每个格子都可以存放不同的物品。
/// </summary>
public class Inventory
{
public int SlotCount { get; set; } // 背包格子的数量
private int _slotCount;
public int SlotCount => _slotCount; // 背包格子的数量
private List<InventorySlot> slots;
public Inventory(int slotCount)
{
SlotCount = slotCount;
_slotCount = slotCount;
slots = new List<InventorySlot>();
for (int i = 0; i < SlotCount; i++)
for (int i = 0; i < _slotCount; i++)
{
slots.Add(new InventorySlot());
}
@ -28,24 +31,30 @@
public bool AddItem(Item item, int quantity)
{
int remainingQuantity = quantity;
int tmpQuantity = quantity;
foreach (var slot in slots)
{
if (remainingQuantity == 0) break;
var tmpItem = new Item(item.ItemName, item.ItemID, item.MaxStack);
if (slot.IsEmpty)
{
// 如果格子为空,直接放入物品
remainingQuantity = item.AddQuantity(remainingQuantity);
slot.AddItem(item, quantity - remainingQuantity);
remainingQuantity = tmpItem.CheckAddQuantity(remainingQuantity);
slot.AddItem(tmpItem, tmpQuantity - remainingQuantity);
}
else if (slot.Item.ItemID == item.ItemID && slot.Item.Quantity < slot.Item.MaxStack)
else if (slot.Item.ItemID == tmpItem.ItemID && slot.Item.Quantity < slot.Item.MaxStack)
{
// 如果格子内物品与新物品相同,并且未满堆叠,堆叠物品
remainingQuantity = slot.Item.AddQuantity(remainingQuantity);
remainingQuantity = slot.Item.CheckAddQuantity(remainingQuantity);
}
tmpQuantity = remainingQuantity;
}
Debug.Log($"向背包中添加{item.ItemName} 数量为:{quantity}");
return remainingQuantity == 0;
}
@ -53,17 +62,22 @@
public bool RemoveItem(int itemID, int quantity)
{
int remainingQuantity = quantity;
int tmpQuantity = quantity;
foreach (var slot in slots)
for (var i = slots.Count - 1; i >= 0; i--)
{
if (remainingQuantity == 0) break;
var slot = slots[i];
if (!slot.IsEmpty && slot.Item.ItemID == itemID)
{
remainingQuantity -= slot.Item.RemoveQuantity(remainingQuantity) ? remainingQuantity : 0;
remainingQuantity = slot.Item.CheckRemoveQuantity(remainingQuantity);
slot.RemoveItem(tmpQuantity - remainingQuantity);
tmpQuantity = remainingQuantity;
}
}
Debug.Log($"向背包中取出物品id{itemID} 数量为:{quantity}");
return remainingQuantity == 0;
}
@ -78,8 +92,8 @@
sb.AppendLine($"{slot.Item.ItemName} - {slot.Item.Quantity}/{slot.Item.MaxStack}");
}
}
Debug.Log(sb.ToString());
}
}
}

View File

@ -6,7 +6,13 @@
/// </summary>
public class InventorySlot
{
public Item Item { get; set; }
private Item _item;
/// <summary>
/// 物品
/// </summary>
public Item Item => _item;
public bool IsEmpty => Item == null;
// 将物品添加到格子中
@ -14,7 +20,7 @@
{
if (IsEmpty)
{
Item = item;
_item = item;
return Item.AddQuantity(quantity) == 0;
}
@ -33,7 +39,7 @@
{
if (Item.Quantity == 0)
{
Item = null; // 如果物品数量为零,则清空格子
_item = null; // 如果物品数量为零,则清空格子
}
return true;
}

View File

@ -11,12 +11,24 @@
private int _maxStack;
private int _quantity;
/// <summary>
/// 名字
/// </summary>
public string ItemName => _itemName;
/// <summary>
/// ID
/// </summary>
public int ItemID => _itemID;
/// <summary>
/// 最大堆叠数量
/// </summary>
public int MaxStack => _maxStack;
/// <summary>
/// 当前数量
/// </summary>
public int Quantity => _quantity;
public Item(string itemName, int itemID, int maxStack)
@ -45,6 +57,17 @@
return remaining;
}
/// <summary>
/// 仅作为检查,并不发生实际的数据改变
/// </summary>
/// <param name="quantity"></param>
/// <returns></returns>
public int CheckAddQuantity(int quantity)
{
int remaining = quantity;
return Quantity + remaining > MaxStack ? remaining = (Quantity + quantity) - MaxStack : 0;
}
// 尝试移除数量,返回是否成功
public bool RemoveQuantity(int quantity)
{
@ -56,5 +79,16 @@
return false;
}
/// <summary>
/// 仅作为检查,并不发生实际的数据改变
/// </summary>
/// <param name="quantity"></param>
/// <returns></returns>
public int CheckRemoveQuantity(int quantity)
{
int tmp = quantity - _quantity;
return tmp <= 0 ? 0 : tmp;
}
}
}