CapersProject/Assets/02.Scripts/Character/Player/Combat/CombatInventory.cs
Nam Tae Gun 7fb0da5888 #25, #26 아이템 직접 획득 방식 추가 및 선형적인 맵 구조 변경
+ Item관련 Excel, Json, So 수정
+ DropItemTable 로직 수정
+ 아이템 프리팹에서 Enable Interaction 체크하면 직접 룻팅, 해제하면 자동 룻팅
+ 체력회복 아이템 추가
+ 개발자 메뉴 상호작용 "F1" 키를 통해 접근 가능
+ 보스 맵은 마법진을 상호작용하면 보스전 시작
+ 맵 안에서 교전 중일 때, 투명 벽 쉐이더 추가
+ 맵 마다의 통로를 통해서 이동 가능
+ 선형적인 맵 구조에 맞게 리소스 및 위치 수정
+ 타이틀 화면으로 이동할 때 나타나는 오류 수정(CombatUiManager OnDisable 싱글톤 문제)

Closes #25, #26
2024-06-22 07:11:53 +09:00

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.GetItemDataByIdx(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.GetItemDataByIdx(x.Idx).Name,
ItemManager.Instance.GetItemDataByIdx(y.Idx).Name, StringComparison.Ordinal));
break;
case InventorySortingType.Category:
ItemSlotList.Sort((x, y) =>
ItemManager.Instance.GetItemDataByIdx(x.Idx).Category.CompareTo(ItemManager.Instance.GetItemDataByIdx(y.Idx).Category));
break;
case InventorySortingType.Count:
ItemSlotList.Sort((x, y) => y.Count.CompareTo(x.Count));
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}