62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
![]() |
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace DDD
|
||
|
{
|
||
|
[Serializable]
|
||
|
public class Inventory
|
||
|
{
|
||
|
[field: SerializeField]
|
||
|
public List<InventoryItemSlot> InventoryItemSlots { get; private set; } = new();
|
||
|
|
||
|
/// <summary>
|
||
|
/// true : 추가, false = 제거
|
||
|
/// </summary>
|
||
|
public event Action<InventoryItemSlot, bool> OnChangedInventoryItemSlot;
|
||
|
|
||
|
public InventoryItemSlot GetItemByIdx(string idx)
|
||
|
{
|
||
|
return InventoryItemSlots.Find(i => i.Idx == idx);
|
||
|
}
|
||
|
|
||
|
public void AddItem(InventoryItemSlot newItemSlot)
|
||
|
{
|
||
|
// 현재 인벤토리 내에 같은 idx인 아이템 찾기
|
||
|
InventoryItemSlot existingItemIdx = InventoryItemSlots.Find(i => i.Idx == newItemSlot.Idx);
|
||
|
|
||
|
// 같은 idx가 있으면, 갯수만 추가
|
||
|
// 같은 idx가 없으면, 리스트에 아이템 새로 추가
|
||
|
if (existingItemIdx != null)
|
||
|
{
|
||
|
existingItemIdx.AddItemCount(newItemSlot.Count);
|
||
|
OnChangedInventoryItemSlot?.Invoke(existingItemIdx, true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
InventoryItemSlots.Add(newItemSlot);
|
||
|
OnChangedInventoryItemSlot?.Invoke(newItemSlot, true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void RemoveItem(InventoryItemSlot removeItemSlot, int removeCount)
|
||
|
{
|
||
|
InventoryItemSlot existingItem = InventoryItemSlots.Find(i => i.Idx == removeItemSlot.Idx);
|
||
|
|
||
|
if (existingItem != null)
|
||
|
{
|
||
|
existingItem.RemoveItemCount(removeCount);
|
||
|
if (existingItem.Count <= 0)
|
||
|
{
|
||
|
InventoryItemSlots.Remove(existingItem);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.Log("제거할 아이템을 찾을 수 없습니다.");
|
||
|
}
|
||
|
|
||
|
OnChangedInventoryItemSlot?.Invoke(removeItemSlot, false);
|
||
|
}
|
||
|
}
|
||
|
}
|