122 lines
4.0 KiB
C#
122 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BlueWater.Items;
|
|
using UnityEngine;
|
|
|
|
namespace BlueWater.Players.Combat
|
|
{
|
|
public enum InventorySortingType
|
|
{
|
|
None = 0,
|
|
Recent,
|
|
Name,
|
|
Category,
|
|
Count,
|
|
}
|
|
|
|
[Serializable]
|
|
public class CombatInventory
|
|
{
|
|
[field: SerializeField]
|
|
public List<ItemSlot> ItemSlotList { get; private set; } = new();
|
|
|
|
[field: SerializeField]
|
|
public float WeightLimit { get; private set; } = float.PositiveInfinity;
|
|
|
|
[field: SerializeField]
|
|
public float CurrentTotalWeight { get; private set; }
|
|
|
|
[field: SerializeField]
|
|
public bool IsOverWeight { get; private set; }
|
|
|
|
public event Action<ItemSlot, bool> OnChangeItemSlot;
|
|
|
|
public ItemSlot GetItemByIdx(int idx)
|
|
{
|
|
return ItemSlotList.Find(i => i.Idx == idx);
|
|
}
|
|
|
|
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.Instance.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.Instance.ItemDictionary[x.Idx].Name,
|
|
ItemManager.Instance.ItemDictionary[y.Idx].Name, StringComparison.Ordinal));
|
|
break;
|
|
case InventorySortingType.Category:
|
|
ItemSlotList.Sort((x, y) =>
|
|
ItemManager.Instance.ItemDictionary[x.Idx].Category.CompareTo(ItemManager.Instance.ItemDictionary[y.Idx].Category));
|
|
break;
|
|
case InventorySortingType.Count:
|
|
ItemSlotList.Sort((x, y) => y.Count.CompareTo(x.Count));
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
} |