CapersProject/Assets/02.Scripts/BlueWater/Character/Inventory.cs
2025-02-03 19:03:41 +09:00

121 lines
4.2 KiB
C#

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