using System; using System.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 物品库存 /// public class Inventory { /// 背包容量 public int capacity; /// 插槽列表 public List slots; /// 改变事件 public event Action OnChange; /// 物品库存 public Inventory(int capacity) { this.capacity = capacity; IEnumerable enumerable = Enumerable.Range(0, capacity); slots = enumerable.Select(_ => new InventorySlot()).ToList(); } /// 应用修改 public void Apply() => OnChange?.Invoke(this); /// 遍历数组 public void ForEach(Action action) => slots.ForEach(action); /// 获取一个空插槽 public InventorySlot Empty() => slots.FirstOrDefault(s => s.item == null); /// 相同堆叠插槽 public List Stackable(InventoryItem item) => slots.Where(slot => slot.IsStackable(item)).ToList(); /// 添加物品 public void AddItem(InventoryItem item, int count) { // 合并重复可堆叠 int remain = MergeToSlots(item, count); // 如果还有剩余,按照MaxStack分批放入空位 if (remain > 0) { AddToSlots(item, remain, true); } OnChange?.Invoke(this); } /// 合并到插槽 public int MergeToSlots(InventoryItem item, int count) { // 查询所有可合并的插槽 var sameSlots = Stackable(item); // 合并到已有插槽 sameSlots.ForEach(obj => obj.Settings(ref count)); return count; } /// 添加物品 public void AddToSlots(InventoryItem item, int count, bool isWhile) { // 取一个空插槽 InventorySlot emptySlot = Empty(); // 没有空插槽则退出 if (emptySlot == null) { return; } // 堆叠数量 = Min(物品数量 , 最大堆叠数量) int addCount = Mathf.Min(count, item.stack); // 计算剩余物品数量 int remain = count - addCount; // 填充进空插槽 emptySlot.Settings(item, addCount); // 没有剩余或者不循环则退出 if (remain == 0 || !isWhile) { return; } // 还有余量循环执行添加 AddToSlots(item, remain, isWhile); } }