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

81 lines
2.6 KiB
C#
Raw Normal View History

2024-06-03 18:26:03 +00:00
using System.Collections;
using System.Collections.Generic;
using BlueWater.Items;
using BlueWater.Utility;
using Sirenix.OdinInspector;
2024-06-03 18:26:03 +00:00
using UnityEngine;
namespace BlueWater.Uis
{
public class ItemLootUi : MonoBehaviour
{
[SerializeField]
private List<ItemLootInfoUi> _itemLootInfoUis = new(3);
[ShowInInspector]
private Queue<(ItemData, int, int)> _waitingLootInfoUis = new();
2024-06-03 18:26:03 +00:00
private Coroutine _waitingQueueCoroutineInstance;
2024-06-03 18:26:03 +00:00
private readonly WaitForSeconds _coroutineRestartTime = new(0.5f);
private void Awake()
{
var itemLootInfoUis = GetComponentsInChildren<ItemLootInfoUi>();
foreach (var element in itemLootInfoUis)
{
_itemLootInfoUis.Add(element);
}
}
public void ShowLootInfoUi(ItemData itemData, int count)
{
var totalItemCount = DataManager.Instance.Inventory.GetItemByIdx(itemData.Idx).Quantity;
if (_waitingLootInfoUis.Count > 0)
{
_waitingLootInfoUis.Enqueue((itemData, count, totalItemCount));
return;
}
2024-06-03 18:26:03 +00:00
foreach (var element in _itemLootInfoUis)
{
if (element.IsShowing) continue;
SetItemLootInfo(element, itemData, count, totalItemCount);
return;
2024-06-03 18:26:03 +00:00
}
_waitingLootInfoUis.Enqueue((itemData, count, totalItemCount));
Utils.StartUniqueCoroutine(this, ref _waitingQueueCoroutineInstance, WaitingQueueCoroutine());
2024-06-03 18:26:03 +00:00
}
private void SetItemLootInfo(ItemLootInfoUi element, ItemData itemData, int count, int totalItemCount)
2024-06-03 18:26:03 +00:00
{
var itemNameText = $"+{count} {itemData.Name}";
var totalItemCountText = $"({totalItemCount})";
element.SetInfo(itemData.Sprite, itemNameText, totalItemCountText);
element.ShowUi();
2024-06-03 18:26:03 +00:00
}
private IEnumerator WaitingQueueCoroutine()
2024-06-03 18:26:03 +00:00
{
while (_waitingLootInfoUis.Count > 0)
2024-06-03 18:26:03 +00:00
{
foreach (var element in _itemLootInfoUis)
{
if (element.IsShowing) continue;
2024-06-03 18:26:03 +00:00
var lootInfoUi = _waitingLootInfoUis.Dequeue();
SetItemLootInfo(element, lootInfoUi.Item1, lootInfoUi.Item2, lootInfoUi.Item3);
2024-06-03 18:26:03 +00:00
break;
2024-06-03 18:26:03 +00:00
}
2024-06-03 18:26:03 +00:00
yield return _coroutineRestartTime;
}
_waitingQueueCoroutineInstance = null;
2024-06-03 18:26:03 +00:00
}
}
}