using System; using System.Collections.Generic; using UnityEngine; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public enum InventorySortingType { NONE = 0, RECENT, NAME, CATEGORY, QUANTITY, } [Serializable] public class PlayerInventory { [field: SerializeField] public List ItemSlotList { get; set; } = new(); [field: SerializeField] public float WeightLimit { get; set; } = float.PositiveInfinity; [field: SerializeField] public float CurrentTotalWeight { get; set; } [field: SerializeField] public bool IsOverWeight { get; set; } public delegate void ChangeItemSlot(ItemSlot changeItemSlot, bool added); public event ChangeItemSlot OnChangeItemSlot; public void AddItem(ItemSlot newItemSlot) { // 현재 인벤토리 내에 같은 idx인 아이템 찾기 var existingItemIdx = ItemSlotList.Find(i => i.Idx == newItemSlot.Idx); // 같은 idx가 있으면, 갯수만 추가 // 같은 idx가 없으면, 리스트에 아이템 새로 추가 if (existingItemIdx != null) { existingItemIdx.AddItemCount(newItemSlot.Count); } else { ItemSlotList.Add(newItemSlot); } // 추가된 아이템에 맞게 인벤토리 내의 무게 계산 CalculateInventoryWeight(); // 인벤토리 UI에 업데이트 OnChangeItemSlot?.Invoke(newItemSlot, true); } public void RemoveItem(ItemSlot removeItemSlot, int removeCount) { var existingItem = ItemSlotList.Find(i => i.Idx == removeItemSlot.Idx); if (existingItem != null) { existingItem.RemoveItemCount(removeCount); if (existingItem.Count <= 0) { ItemSlotList.Remove(existingItem); } } else { Debug.Log("Item not found in inventory to remove."); } CalculateInventoryWeight(); OnChangeItemSlot?.Invoke(removeItemSlot, false); } public void CalculateInventoryWeight() { CurrentTotalWeight = 0f; foreach (var element in ItemSlotList) { var elementWeight = ItemManager.Inst.ItemDictionary[element.Idx].weight; CurrentTotalWeight += elementWeight * element.Count; } IsOverWeight = CurrentTotalWeight >= WeightLimit; } public void SortItem(InventorySortingType sortingType) { switch (sortingType) { case InventorySortingType.NONE: return; case InventorySortingType.RECENT: ItemSlotList.Sort((x, y) => y.AcquisitionTime.CompareTo(x.AcquisitionTime)); break; case InventorySortingType.NAME: ItemSlotList.Sort((x, y) => string.Compare(ItemManager.Inst.ItemDictionary[x.Idx].name, ItemManager.Inst.ItemDictionary[y.Idx].name, StringComparison.Ordinal)); break; case InventorySortingType.CATEGORY: ItemSlotList.Sort((x, y) => ItemManager.Inst.ItemDictionary[x.Idx].category.CompareTo(ItemManager.Inst.ItemDictionary[y.Idx].category)); break; case InventorySortingType.QUANTITY: ItemSlotList.Sort((x, y) => y.Count.CompareTo(x.Count)); break; default: throw new ArgumentOutOfRangeException(); } } } }