227 lines
8.8 KiB
C#
227 lines
8.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BlueWater.Items;
|
|
using Sirenix.OdinInspector;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace BlueWater.Uis
|
|
{
|
|
public class InventoryUi : MonoBehaviour
|
|
{
|
|
[Title("컴포넌트")]
|
|
[SerializeField, Required]
|
|
private TMP_Dropdown _inventoryFilterSortDropdown;
|
|
|
|
[SerializeField, Required]
|
|
private TMP_Dropdown _inventorySortDropdown;
|
|
|
|
[Title("프리팹 생성 위치")]
|
|
[SerializeField, Required]
|
|
private Transform _tycoonItemSlotLocation;
|
|
|
|
[Title("프리팹")]
|
|
[SerializeField, Required]
|
|
private TycoonItemSlotUi _tycoonItemSlotUi;
|
|
|
|
private List<TycoonItemSlotUi> _tycoonItemSlotUis = new();
|
|
|
|
private ItemManager _itemManager;
|
|
private DataManager _dataManager;
|
|
private Inventory _inventory;
|
|
|
|
private void Awake()
|
|
{
|
|
InitializeInventory();
|
|
InventorySynchronization();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_dataManager.Inventory.OnChangeItemSlot += ChangedData;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_dataManager.Inventory.OnChangeItemSlot -= ChangedData;
|
|
}
|
|
|
|
private void InitializeInventory()
|
|
{
|
|
_itemManager = ItemManager.Instance;
|
|
_dataManager = DataManager.Instance;
|
|
_inventory = _dataManager.Inventory;
|
|
|
|
_tycoonItemSlotUis.Clear();
|
|
foreach (Transform element in _tycoonItemSlotLocation)
|
|
{
|
|
Destroy(element.gameObject);
|
|
}
|
|
}
|
|
|
|
private void InventorySynchronization()
|
|
{
|
|
foreach (var element in _inventory.ItemSlotList)
|
|
{
|
|
var newItemContent = Instantiate(_tycoonItemSlotUi, _tycoonItemSlotLocation).GetComponent<TycoonItemSlotUi>();
|
|
newItemContent.SetItemSlot(element);
|
|
_tycoonItemSlotUis.Add(newItemContent);
|
|
}
|
|
}
|
|
|
|
public void ChangedData(ItemSlot changedItemSlot, bool added)
|
|
{
|
|
if (added)
|
|
{
|
|
AddItem(changedItemSlot);
|
|
}
|
|
else
|
|
{
|
|
RemoveItem(changedItemSlot);
|
|
}
|
|
}
|
|
|
|
private void AddItem(ItemSlot addItemSlot)
|
|
{
|
|
var existingItemSlotUi = _tycoonItemSlotUis.Find(i => i.ItemSlot.Idx == addItemSlot.Idx);
|
|
if (existingItemSlotUi)
|
|
{
|
|
existingItemSlotUi.SetItemSlot(addItemSlot);
|
|
}
|
|
else
|
|
{
|
|
var newItemSlot = Instantiate(_tycoonItemSlotUi, _tycoonItemSlotLocation).GetComponent<TycoonItemSlotUi>();
|
|
newItemSlot.SetItemSlot(addItemSlot);
|
|
_tycoonItemSlotUis.Add(newItemSlot);
|
|
}
|
|
}
|
|
|
|
private void RemoveItem(ItemSlot removeItemSlot)
|
|
{
|
|
var existingItemSlotUi = _tycoonItemSlotUis.Find(i => i.ItemSlot.Idx == removeItemSlot.Idx);
|
|
if (existingItemSlotUi)
|
|
{
|
|
existingItemSlotUi.SetItemSlot(removeItemSlot);
|
|
if (existingItemSlotUi.ItemSlot.Quantity <= 0)
|
|
{
|
|
_tycoonItemSlotUis.Remove(existingItemSlotUi);
|
|
Destroy(existingItemSlotUi.gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void InventoryFilterSortButton()
|
|
{
|
|
if (_inventoryFilterSortDropdown.value == 0) return;
|
|
|
|
InventoryFilterSortItemSlotUi((InventoryFilterSortingType)_inventoryFilterSortDropdown.value);
|
|
|
|
_inventoryFilterSortDropdown.value = 0;
|
|
}
|
|
|
|
private void InventoryFilterSortItemSlotUi(InventoryFilterSortingType inventoryFilterSortingType)
|
|
{
|
|
switch (inventoryFilterSortingType)
|
|
{
|
|
case InventoryFilterSortingType.None:
|
|
break;
|
|
case InventoryFilterSortingType.Total:
|
|
foreach (var element in _tycoonItemSlotUis)
|
|
{
|
|
element.ShowUi();
|
|
}
|
|
break;
|
|
case InventoryFilterSortingType.Ingredient:
|
|
foreach (var element in _tycoonItemSlotUis)
|
|
{
|
|
if (_itemManager.ItemDataSo.GetDataByIdx(element.ItemSlot.Idx).Type == ItemType.FoodIngredient)
|
|
{
|
|
element.ShowUi();
|
|
}
|
|
else
|
|
{
|
|
element.HideUi();
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException(nameof(inventoryFilterSortingType), inventoryFilterSortingType, null);
|
|
}
|
|
}
|
|
|
|
public void InventorySortButton()
|
|
{
|
|
if (_inventorySortDropdown.value == 0) return;
|
|
|
|
InventorySortItemSlotUi((InventorySortingType)_inventorySortDropdown.value);
|
|
|
|
_inventorySortDropdown.value = 0;
|
|
}
|
|
|
|
private void InventorySortItemSlotUi(InventorySortingType inventorySortingType)
|
|
{
|
|
switch (inventorySortingType)
|
|
{
|
|
case InventorySortingType.None:
|
|
break;
|
|
case InventorySortingType.PriceUp:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).Price.
|
|
CompareTo(_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).Price));
|
|
break;
|
|
case InventorySortingType.PriceDown:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).Price.
|
|
CompareTo(_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).Price));
|
|
break;
|
|
case InventorySortingType.TypeUp:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).Type.
|
|
CompareTo(_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).Type));
|
|
break;
|
|
case InventorySortingType.TypeDown:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).Type.
|
|
CompareTo(_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).Type));
|
|
break;
|
|
case InventorySortingType.IngredientTypeUp:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).IngredientType.
|
|
CompareTo(_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).IngredientType));
|
|
break;
|
|
case InventorySortingType.IngredientTypeDown:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).IngredientType.
|
|
CompareTo(_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).IngredientType));
|
|
break;
|
|
case InventorySortingType.NameUp:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
string.Compare(_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).Name,
|
|
_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).Name, StringComparison.Ordinal));
|
|
break;
|
|
case InventorySortingType.NameDown:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
string.Compare(_itemManager.ItemDataSo.GetDataByIdx(y.ItemSlot.Idx).Name,
|
|
_itemManager.ItemDataSo.GetDataByIdx(x.ItemSlot.Idx).Name, StringComparison.Ordinal));
|
|
break;
|
|
case InventorySortingType.AcquisitionTimeUp:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
x.ItemSlot.AcquisitionTime.
|
|
CompareTo(y.ItemSlot.AcquisitionTime));
|
|
break;
|
|
case InventorySortingType.AcquisitionTimeDown:
|
|
_tycoonItemSlotUis.Sort((x, y) =>
|
|
y.ItemSlot.AcquisitionTime.
|
|
CompareTo(x.ItemSlot.AcquisitionTime));
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
|
|
for (var i = 0; i < _tycoonItemSlotUis.Count; i++)
|
|
{
|
|
_tycoonItemSlotUis[i].transform.SetSiblingIndex(i);
|
|
}
|
|
}
|
|
}
|
|
} |