CapersProject/Assets/02.Scripts/Ui/Tycoon/BillUi.cs

125 lines
3.4 KiB
C#
Raw Normal View History

2024-10-10 05:44:37 +00:00
using System.Collections;
2024-10-08 13:44:20 +00:00
using System.Collections.Generic;
using BlueWater.Npcs.Customers;
using Sirenix.OdinInspector;
using Spine.Unity;
using UnityEngine;
namespace BlueWater
{
public class BillUi : MonoBehaviour
{
[SerializeField]
private Bill _billPrefab;
[SerializeField]
private SkeletonGraphic _chain;
[Title("계산서")]
[SerializeField]
private Vector3 _spawnPosition;
[SerializeField]
private Transform _spawnLocation;
[SerializeField]
private List<BillInfo> _billInfos = new(5);
private Dictionary<Customer, Bill> _customerBillDictionary = new();
private bool _isMovedChain;
2024-10-10 05:44:37 +00:00
private bool _isActivating;
2024-10-08 13:44:20 +00:00
private const string Move = "Move";
private void Start()
{
EventManager.OnOrderedCocktail += OrderedCocktail;
EventManager.OnOrderResult += OrderResult;
}
private void OnDestroy()
{
EventManager.OnOrderedCocktail -= OrderedCocktail;
EventManager.OnOrderResult -= OrderResult;
}
private void OrderedCocktail(Customer customer)
{
var instance = Instantiate(_billPrefab, _spawnLocation);
2024-10-10 02:37:44 +00:00
instance.Initialize(customer, _spawnPosition, _billInfos[0].Position);
2024-10-08 13:44:20 +00:00
_customerBillDictionary.Add(customer, instance);
UpdateBillInfo();
}
private void OrderResult(Customer customer, bool isSucceed)
{
2024-10-10 05:44:37 +00:00
if (_customerBillDictionary.TryGetValue(customer, out var bill))
2024-10-08 13:44:20 +00:00
{
bill.OrderResult(isSucceed, UpdateBillInfo);
_customerBillDictionary.Remove(customer);
UpdateBillInfo(); // Bill이 제거된 후 빈 자리를 업데이트
}
}
private void UpdateBillInfo()
{
foreach (var element in _customerBillDictionary.Values)
{
for (var i = 0; i < _billInfos.Count; i++)
{
2024-10-10 02:37:44 +00:00
if (element.CurrentBillInfo == _billInfos[i]) break;
2024-10-08 13:44:20 +00:00
if (_billInfos[i].IsEmpty)
{
PlayChainAnimation();
element.Move(_billInfos[i]);
break;
}
}
}
2024-10-10 05:44:37 +00:00
StopChainAnimation();
2024-10-08 13:44:20 +00:00
}
private void PlayChainAnimation()
{
_chain.AnimationState.SetAnimation(0, Move, true);
_isMovedChain = true;
}
private void StopChainAnimation()
{
2024-10-10 05:44:37 +00:00
if (!_isMovedChain || _isActivating) return;
StartCoroutine(nameof(StopChainAnimationCoroutine));
}
private IEnumerator StopChainAnimationCoroutine()
{
_isActivating = true;
while (true)
{
var isMoving = false;
foreach (var element in _billInfos)
{
if (!element.IsMoving) continue;
isMoving = true;
}
if (!isMoving)
{
break;
}
yield return null;
}
2024-10-08 13:44:20 +00:00
_chain.AnimationState.ClearTrack(0);
_isMovedChain = false;
2024-10-10 05:44:37 +00:00
_isActivating = false;
2024-10-08 13:44:20 +00:00
}
}
}