OldBlueWater/BlueWater/Assets/02.Scripts/Item/ItemInventoryUi.cs
NTG 7a2b5e9d02 Closes #255 거대 슬라임 보스 추가
+ UiManager의 CombatUi, OceanUi 모두 분리
각각의 씬에서 CombatUiManager, OceanUiManager로 변경
+ Ocean, OceanUi input action map 추가 및 변경
input action map, uiManager 변경에 따른 Player input 로직 변경
+ CombatPlayer가 죽으면 GameOverUi 추가
+ 재시작 기능 추가
+ 인벤토리 Ui 수정
+ 슬라임 보스 로직 및 애니메이션 수정
2024-05-12 06:29:16 +09:00

234 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class ItemInventoryUi : PopupUi
{
private enum InventoryType
{
NONE = 0,
OCEAN_INVENTORY_IN_OCEAN,
OCEAN_INVENTORY_IN_COMBAT,
COMBAT_INVENTORY_IN_COMBAT,
}
[SerializeField] private InventoryType inventoryType;
[SerializeField, Required] private TMP_Dropdown sortingDropdown;
[SerializeField, Required] private TMP_Text currentWeight;
[SerializeField, Required] private GameObject itemSlotUiPrefab;
[SerializeField, Required] private Transform instantiateLocation;
[SerializeField, Required] private DiscardPopupUi discardPopupUi;
[field: SerializeField] public List<ItemSlotUi> ItemSlotUiList { get; set; } = new();
private PlayerInventory inventory;
private List<ItemSlotUi> selectedList = new();
private void OnEnable()
{
discardPopupUi.Close();
InitAndUpdateInventory();
inventory.OnChangeItemSlot += ChangedData;
}
private void Start()
{
InitAndUpdateInventory();
}
private void OnDisable()
{
inventory.OnChangeItemSlot -= ChangedData;
}
public override void Close()
{
if (discardPopupUi.gameObject.activeSelf)
{
discardPopupUi.Close();
}
base.Close();
}
public void InitAndUpdateInventory()
{
inventory ??= GetPlayerInventory();
InitInventory();
InventorySynchronization();
InitWeight();
}
private PlayerInventory GetPlayerInventory()
{
switch (inventoryType)
{
case InventoryType.NONE:
return null;
case InventoryType.OCEAN_INVENTORY_IN_OCEAN:
case InventoryType.OCEAN_INVENTORY_IN_COMBAT:
return DataManager.Inst.OceanInventory;
case InventoryType.COMBAT_INVENTORY_IN_COMBAT:
return DataManager.Inst.CombatInventory;
default:
throw new ArgumentOutOfRangeException();
}
}
private void InitInventory()
{
ItemSlotUiList.Clear();
foreach (Transform element in instantiateLocation)
{
Destroy(element.gameObject);
}
}
private void InventorySynchronization()
{
foreach (var element in inventory.ItemSlotList)
{
var newItemContent = Instantiate(itemSlotUiPrefab, instantiateLocation).GetComponent<ItemSlotUi>();
newItemContent.InitData(element);
ItemSlotUiList.Add(newItemContent);
}
}
private void InitWeight()
{
if (float.IsPositiveInfinity(inventory.WeightLimit))
{
currentWeight.text = inventory.CurrentTotalWeight + " / ∞Kg";
}
else
{
currentWeight.text = inventory.CurrentTotalWeight + " / " + inventory.WeightLimit + "Kg";
}
currentWeight.color = inventory.IsOverWeight ? Color.red : Color.white;
}
public void ChangedData(ItemSlot changedItemSlot, bool added)
{
if (added)
{
AddItem(changedItemSlot);
}
else
{
RemoveItem(changedItemSlot);
}
InitWeight();
}
private void AddItem(ItemSlot addItemSlot)
{
var existingItemSlotUi = ItemSlotUiList.Find(i => i.ItemSlot.Idx == addItemSlot.Idx);
if (existingItemSlotUi != null)
{
existingItemSlotUi.UpdateData(addItemSlot);
}
else
{
var newItemSlot = Instantiate(itemSlotUiPrefab, instantiateLocation).GetComponent<ItemSlotUi>();
newItemSlot.InitData(addItemSlot);
ItemSlotUiList.Add(newItemSlot);
}
}
private void RemoveItem(ItemSlot removeItemSlot)
{
var existingItemSlotUi = ItemSlotUiList.Find(i => i.ItemSlot.Idx == removeItemSlot.Idx);
if (existingItemSlotUi != null)
{
existingItemSlotUi.UpdateData(removeItemSlot);
if (existingItemSlotUi.ItemSlot.Count <= 0)
{
ItemSlotUiList.Remove(existingItemSlotUi);
Destroy(existingItemSlotUi.gameObject);
}
}
}
public void SelectAll()
{
foreach (var element in ItemSlotUiList)
{
element.ToggleOn();
}
}
public void DeselectAll()
{
foreach (var element in ItemSlotUiList)
{
element.ToggleOff();
}
}
public void DiscardButton()
{
selectedList.Clear();
foreach (var element in ItemSlotUiList)
{
if (!element.ToggleIsOn) continue;
selectedList.Add(element);
}
if (selectedList.Count == 1)
{
discardPopupUi.DiscardMessage(selectedList[0]);
}
else if (selectedList.Count > 1)
{
discardPopupUi.DiscardAllMessage(selectedList);
}
}
public void SortButton()
{
if (sortingDropdown.value == 0) return;
inventory.SortItem((InventorySortingType)sortingDropdown.value);
SortItemSlotUi((InventorySortingType)sortingDropdown.value);
sortingDropdown.value = 0;
}
private void SortItemSlotUi(InventorySortingType sortingType)
{
switch (sortingType)
{
case InventorySortingType.NONE:
return;
case InventorySortingType.RECENT:
ItemSlotUiList.Sort((x, y) => y.ItemSlot.AcquisitionTime.CompareTo(x.ItemSlot.AcquisitionTime));
break;
case InventorySortingType.NAME:
ItemSlotUiList.Sort((x, y) => string.Compare(ItemManager.Inst.ItemDictionary[x.ItemSlot.Idx].name, ItemManager.Inst.ItemDictionary[y.ItemSlot.Idx].name, StringComparison.Ordinal));
break;
case InventorySortingType.CATEGORY:
ItemSlotUiList.Sort((x, y) => ItemManager.Inst.ItemDictionary[x.ItemSlot.Idx].category.CompareTo(ItemManager.Inst.ItemDictionary[y.ItemSlot.Idx].category));
break;
case InventorySortingType.QUANTITY:
ItemSlotUiList.Sort((x, y) => y.ItemSlot.Count.CompareTo(x.ItemSlot.Count));
break;
default:
throw new ArgumentOutOfRangeException();
}
for (var i = 0; i < ItemSlotUiList.Count; i++)
{
ItemSlotUiList[i].transform.SetSiblingIndex(i);
}
}
}
}