2024-01-02 21:39:53 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
2024-04-30 00:11:59 +00:00
|
|
|
public enum InventorySortingType
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
RECENT,
|
|
|
|
NAME,
|
|
|
|
CATEGORY,
|
|
|
|
QUANTITY,
|
|
|
|
}
|
|
|
|
|
2024-01-02 21:39:53 +00:00
|
|
|
[Serializable]
|
|
|
|
public class PlayerInventory
|
|
|
|
{
|
2024-04-30 00:11:59 +00:00
|
|
|
[field: SerializeField] public List<ItemSlot> 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);
|
|
|
|
}
|
2024-01-02 21:39:53 +00:00
|
|
|
|
2024-04-30 00:11:59 +00:00
|
|
|
public void RemoveItem(ItemSlot removeItemSlot, int removeCount)
|
2024-01-02 21:39:53 +00:00
|
|
|
{
|
2024-04-30 00:11:59 +00:00
|
|
|
var existingItem = ItemSlotList.Find(i => i.Idx == removeItemSlot.Idx);
|
2024-01-21 17:42:31 +00:00
|
|
|
|
|
|
|
if (existingItem != null)
|
2024-01-02 21:39:53 +00:00
|
|
|
{
|
2024-04-30 00:11:59 +00:00
|
|
|
existingItem.RemoveItemCount(removeCount);
|
|
|
|
if (existingItem.Count <= 0)
|
|
|
|
{
|
|
|
|
ItemSlotList.Remove(existingItem);
|
|
|
|
}
|
2024-01-02 21:39:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-30 00:11:59 +00:00
|
|
|
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();
|
2024-01-02 21:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|