using System.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 物品库存 /// public class Inventory { /// 插槽列表 public List slots = new List(); /// 添加物品 public void AddItem(InventorySlot slot) { if (MergeToSlots(slot)) { return; } // 如果还有剩余,按照MaxStack分批放入空位 while (slot.count > 0) { slot.count = AddToSlots(slot); } } /// 添加到插槽 public int AddToSlots(InventorySlot slot) { InventorySlot emptySlot = slots.FirstOrDefault(s => s.item == null); if (emptySlot == null) { Debug.Log("库存没有空位了!"); return 0; } // 填充插槽 int addCount = Mathf.Min(slot.count, slot.MaxStack); emptySlot.Settings(slot.item, addCount); // 计算余量 return slot.count - addCount; } /// 合并到插槽 public bool MergeToSlots(InventorySlot slot) { int remain = slot.count; // 查询所有可合并的插槽 var sameSlots = slots.Where(slot => slot.Merge(slot)).ToList(); // 合并到已有插槽 sameSlots.ForEach(obj => MergeToSlots(obj, ref remain)); slot.count = remain; return remain == 0; } /// 合并到插槽 public void MergeToSlots(InventorySlot slot, ref int remain) { int canAdd = slot.MaxStack - slot.count; int add = Mathf.Min(canAdd, remain); slot.count += add; remain -= add; } } /// /// 库存插槽 /// public class InventorySlot { /// 数量 public int count; /// 物品 public DataItem item; /// 堆叠数量 public virtual int MaxStack => item != null ? item.MaxStack : 0; public InventorySlot() { } public InventorySlot(DataItem item, int count) { this.item = item; this.count = count; } public void Settings(DataItem item, int count) { this.item = item; this.count = count; } /// 是否允许合并 public bool Merge(InventorySlot slot) { if (item == null || slot == null || slot.item == null) { return false; } if (count >= item.MaxStack) { return false; } return slot.item.name == item.name; } }