diff --git a/Assets/Scripts/BeiBao/Inventory.cs b/Assets/Scripts/BeiBao/Inventory.cs index 03c278c..361f5b0 100644 --- a/Assets/Scripts/BeiBao/Inventory.cs +++ b/Assets/Scripts/BeiBao/Inventory.cs @@ -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 { -/// -/// 背包类 -/// 背包是由多个格子组成的,每个格子都可以存放不同的物品。 -/// + /// + /// 背包类 + /// 背包是由多个格子组成的,每个格子都可以存放不同的物品。 + /// public class Inventory { - public int SlotCount { get; set; } // 背包格子的数量 + private int _slotCount; + + public int SlotCount => _slotCount; // 背包格子的数量 + private List slots; public Inventory(int slotCount) { - SlotCount = slotCount; + _slotCount = slotCount; slots = new List(); - 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()); } } - } \ No newline at end of file diff --git a/Assets/Scripts/BeiBao/InventorySlot.cs b/Assets/Scripts/BeiBao/InventorySlot.cs index f482187..4f236d6 100644 --- a/Assets/Scripts/BeiBao/InventorySlot.cs +++ b/Assets/Scripts/BeiBao/InventorySlot.cs @@ -6,7 +6,13 @@ /// public class InventorySlot { - public Item Item { get; set; } + private Item _item; + + /// + /// 物品 + /// + 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; } diff --git a/Assets/Scripts/BeiBao/Item.cs b/Assets/Scripts/BeiBao/Item.cs index 59c80a3..329f9f2 100644 --- a/Assets/Scripts/BeiBao/Item.cs +++ b/Assets/Scripts/BeiBao/Item.cs @@ -11,12 +11,24 @@ private int _maxStack; private int _quantity; + /// + /// 名字 + /// public string ItemName => _itemName; + /// + /// ID + /// public int ItemID => _itemID; + /// + /// 最大堆叠数量 + /// public int MaxStack => _maxStack; + /// + /// 当前数量 + /// public int Quantity => _quantity; public Item(string itemName, int itemID, int maxStack) @@ -45,6 +57,17 @@ return remaining; } + /// + /// 仅作为检查,并不发生实际的数据改变 + /// + /// + /// + 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; } + + /// + /// 仅作为检查,并不发生实际的数据改变 + /// + /// + /// + public int CheckRemoveQuantity(int quantity) + { + int tmp = quantity - _quantity; + return tmp <= 0 ? 0 : tmp; + } } } \ No newline at end of file