125 lines
3.4 KiB
C#
125 lines
3.4 KiB
C#
using System.Collections;
|
|
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;
|
|
private bool _isActivating;
|
|
|
|
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);
|
|
instance.Initialize(customer, _spawnPosition, _billInfos[0].Position);
|
|
_customerBillDictionary.Add(customer, instance);
|
|
UpdateBillInfo();
|
|
}
|
|
|
|
private void OrderResult(Customer customer, bool isSucceed)
|
|
{
|
|
if (_customerBillDictionary.TryGetValue(customer, out var bill))
|
|
{
|
|
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++)
|
|
{
|
|
if (element.CurrentBillInfo == _billInfos[i]) break;
|
|
|
|
if (_billInfos[i].IsEmpty)
|
|
{
|
|
PlayChainAnimation();
|
|
element.Move(_billInfos[i]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
StopChainAnimation();
|
|
}
|
|
|
|
private void PlayChainAnimation()
|
|
{
|
|
_chain.AnimationState.SetAnimation(0, Move, true);
|
|
_isMovedChain = true;
|
|
}
|
|
|
|
private void StopChainAnimation()
|
|
{
|
|
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;
|
|
}
|
|
|
|
_chain.AnimationState.ClearTrack(0);
|
|
_isMovedChain = false;
|
|
_isActivating = false;
|
|
}
|
|
}
|
|
}
|