CapersProject/Assets/02.Scripts/Ui/Combat/CombatItemInventoryUi.cs

255 lines
7.8 KiB
C#
Raw Normal View History

2024-06-03 18:26:03 +00:00
using System;
using System.Collections.Generic;
using BlueWater.Items;
2024-06-03 18:26:03 +00:00
using BlueWater.Players.Combat;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
namespace BlueWater.Uis
2024-06-03 18:26:03 +00:00
{
public class CombatItemInventoryUi : SwitchActionPopupUi
2024-06-03 18:26:03 +00:00
{
// Variables
#region Variables
// Components
[SerializeField, Required]
protected TMP_Dropdown SortingDropdown;
[SerializeField, Required]
protected TMP_Text CurrentWeight;
[SerializeField, Required]
protected Transform InstantiateLocation;
[SerializeField, Required]
protected DiscardPopupUi DiscardPopupUi;
[SerializeField, Required]
protected GameObject ItemSlotUiPrefab;
// Variables
[SerializeField]
private List<ItemSlotUi> _itemSlotUiList = new();
private List<ItemSlotUi> _selectedList = new();
private CombatInventory _combatInventory;
#endregion
// Unity events
#region Unity events
private void Awake()
{
InitializeInventory();
InventorySynchronization();
CalculateWeight();
}
private void OnEnable()
{
DiscardPopupUi.Close();
}
private void Start()
{
_combatInventory.OnChangeItemSlot += ChangedData;
}
private void OnDestroy()
{
_combatInventory.OnChangeItemSlot -= ChangedData;
}
#endregion
// Initialize methods
#region Initialize methods
[Button("컴포넌트 초기화")]
private void InitializeComponents()
{
SortingDropdown = transform.Find("ItemInventoryPanel/Popup Base - Concave/Top/SortingDropdown").GetComponent<TMP_Dropdown>();
CurrentWeight = transform.Find("ItemInventoryPanel/Popup Base - Concave/Top/CurrentWeight").GetComponent<TMP_Text>();
InstantiateLocation = transform.Find("ItemInventoryPanel/ScrollView/Viewport/Content");
DiscardPopupUi = transform.Find("DiscardPopupUi").GetComponent<DiscardPopupUi>();
}
private void InitializeInventory()
{
if (DataManager.Instance)
{
_combatInventory = DataManager.Instance.CombatInventory;
}
_itemSlotUiList.Clear();
foreach (Transform element in InstantiateLocation)
{
Destroy(element.gameObject);
}
}
private void InventorySynchronization()
{
foreach (var element in _combatInventory.ItemSlotList)
{
var newItemContent = Instantiate(ItemSlotUiPrefab, InstantiateLocation).GetComponent<ItemSlotUi>();
newItemContent.InitializeData(element);
_itemSlotUiList.Add(newItemContent);
}
}
#endregion
// Methods
#region Methods
public override void Close()
{
if (DiscardPopupUi.gameObject.activeSelf)
{
DiscardPopupUi.Close();
}
base.Close();
}
private void CalculateWeight()
{
if (float.IsPositiveInfinity(_combatInventory.WeightLimit))
{
CurrentWeight.text = _combatInventory.CurrentTotalWeight + " / ∞Kg";
}
else
{
CurrentWeight.text = _combatInventory.CurrentTotalWeight + " / " + _combatInventory.WeightLimit + "Kg";
}
CurrentWeight.color = _combatInventory.IsOverWeight ? Color.red : Color.white;
}
public void ChangedData(ItemSlot changedItemSlot, bool added)
{
if (added)
{
AddItem(changedItemSlot);
}
else
{
RemoveItem(changedItemSlot);
}
CalculateWeight();
}
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.InitializeData(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;
_combatInventory.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.Instance.GetItemDataByIdx(x.ItemSlot.Idx).Name,
ItemManager.Instance.GetItemDataByIdx(y.ItemSlot.Idx).Name, StringComparison.Ordinal));
2024-06-03 18:26:03 +00:00
break;
case InventorySortingType.Category:
_itemSlotUiList.Sort((x, y) =>
ItemManager.Instance.GetItemDataByIdx(x.ItemSlot.Idx).Category.CompareTo(ItemManager.Instance.GetItemDataByIdx(y.ItemSlot.Idx).Category));
2024-06-03 18:26:03 +00:00
break;
case InventorySortingType.Count:
_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);
}
}
#endregion
}
}