using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; /// /// 库存插槽 /// public class InventorySlot { /// 数量 public int count; /// 名字 public string name; /// 物品 public InventoryItem item; /// 限制类型 public List limits = new List(); /// 图片 public Sprite Sprite => item != null ? item.sprite : null; /// 堆叠数量 public int MaxStack => item != null ? item.stack : 0; /// 添加物品数量 public virtual void Settings(ref int count) { // 剩余容量 int canAdd = MaxStack - count; // 剩余容量和余数取最小值 int addCount = Mathf.Min(canAdd, count); this.count += addCount; count -= addCount; } /// 设置物品 public virtual void Settings(InventoryItem item, int count) { this.item = item; this.count = count; } /// 替换物品 public virtual (InventoryItem, int) Replace(InventoryItem item, int count) { int remain = this.count; this.count = count; InventoryItem old = this.item; this.item = item; return (old, remain); } /// 验证类型 public bool Verify(InventoryItem item) { string[] types = item.type.Split("/"); return limits.All(item => types.Contains(item)); } /// 是否可堆叠 public virtual bool IsStackable(InventoryItem obj) { return item != null && item.IsStackable(obj) && count < MaxStack; } }