2024-06-03 18:26:03 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using BlueWater.Items;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2024-07-15 16:20:39 +00:00
|
|
|
namespace BlueWater
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
2024-07-15 16:20:39 +00:00
|
|
|
public enum CombatInventorySortingType
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
Recent,
|
|
|
|
Name,
|
|
|
|
Category,
|
|
|
|
Count,
|
|
|
|
}
|
|
|
|
|
|
|
|
[Serializable]
|
2024-07-15 16:20:39 +00:00
|
|
|
public class Inventory
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
|
|
|
[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)
|
|
|
|
{
|
2024-07-15 16:20:39 +00:00
|
|
|
existingItemIdx.AddItemQuantity(newItemSlot.Quantity);
|
|
|
|
OnChangeItemSlot?.Invoke(existingItemIdx, true);
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ItemSlotList.Add(newItemSlot);
|
2024-07-15 16:20:39 +00:00
|
|
|
OnChangeItemSlot?.Invoke(newItemSlot, true);
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 추가된 아이템에 맞게 인벤토리 내의 무게 계산
|
|
|
|
CalculateInventoryWeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveItem(ItemSlot removeItemSlot, int removeCount)
|
|
|
|
{
|
|
|
|
var existingItem = ItemSlotList.Find(i => i.Idx == removeItemSlot.Idx);
|
|
|
|
|
|
|
|
if (existingItem != null)
|
|
|
|
{
|
2024-07-15 16:20:39 +00:00
|
|
|
existingItem.RemoveItemQuantity(removeCount);
|
|
|
|
if (existingItem.Quantity <= 0)
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2024-06-21 22:11:53 +00:00
|
|
|
var elementWeight = ItemManager.Instance.GetItemDataByIdx(element.Idx).Weight;
|
2024-07-15 16:20:39 +00:00
|
|
|
CurrentTotalWeight += elementWeight * element.Quantity;
|
2024-06-03 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IsOverWeight = CurrentTotalWeight >= WeightLimit;
|
|
|
|
}
|
|
|
|
|
2024-07-15 16:20:39 +00:00
|
|
|
public void SortItem(CombatInventorySortingType sortingType)
|
2024-06-03 18:26:03 +00:00
|
|
|
{
|
|
|
|
switch (sortingType)
|
|
|
|
{
|
2024-07-15 16:20:39 +00:00
|
|
|
case CombatInventorySortingType.None:
|
2024-06-03 18:26:03 +00:00
|
|
|
return;
|
2024-07-15 16:20:39 +00:00
|
|
|
case CombatInventorySortingType.Recent:
|
2024-06-03 18:26:03 +00:00
|
|
|
ItemSlotList.Sort((x, y) => y.AcquisitionTime.CompareTo(x.AcquisitionTime));
|
|
|
|
break;
|
2024-07-15 16:20:39 +00:00
|
|
|
case CombatInventorySortingType.Name:
|
2024-06-03 18:26:03 +00:00
|
|
|
ItemSlotList.Sort((x, y) =>
|
2024-06-21 22:11:53 +00:00
|
|
|
string.Compare(ItemManager.Instance.GetItemDataByIdx(x.Idx).Name,
|
|
|
|
ItemManager.Instance.GetItemDataByIdx(y.Idx).Name, StringComparison.Ordinal));
|
2024-06-03 18:26:03 +00:00
|
|
|
break;
|
2024-07-15 16:20:39 +00:00
|
|
|
case CombatInventorySortingType.Category:
|
2024-06-03 18:26:03 +00:00
|
|
|
ItemSlotList.Sort((x, y) =>
|
2024-07-15 16:20:39 +00:00
|
|
|
ItemManager.Instance.GetItemDataByIdx(x.Idx).Type.CompareTo(ItemManager.Instance.GetItemDataByIdx(y.Idx).Type));
|
2024-06-03 18:26:03 +00:00
|
|
|
break;
|
2024-07-15 16:20:39 +00:00
|
|
|
case CombatInventorySortingType.Count:
|
|
|
|
ItemSlotList.Sort((x, y) => y.Quantity.CompareTo(x.Quantity));
|
2024-06-03 18:26:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|