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

141 lines
4.2 KiB
C#
Raw Normal View History

2024-11-07 09:13:54 +00:00
using System;
2024-10-10 05:44:37 +00:00
using System.Collections;
2024-10-08 13:44:20 +00:00
using System.Collections.Generic;
2024-10-15 18:01:16 +00:00
using System.Linq;
2024-10-08 13:44:20 +00:00
using BlueWater.Npcs.Customers;
2024-11-28 23:07:50 +00:00
using BlueWater.Utility;
2024-10-08 13:44:20 +00:00
using Sirenix.OdinInspector;
using Spine.Unity;
using UnityEngine;
2024-10-15 18:01:16 +00:00
using UnityEngine.Rendering;
2024-10-08 13:44:20 +00:00
2024-10-15 18:01:16 +00:00
namespace BlueWater.Uis
2024-10-08 13:44:20 +00:00
{
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);
2024-10-15 18:01:16 +00:00
private ObservableList<KeyValuePair<Customer, Bill>> _customerBills = new();
2024-11-28 23:07:50 +00:00
private Coroutine _updateInstance;
private WaitForSeconds _waitForSeconds = new(0.1f);
2024-10-08 13:44:20 +00:00
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()
{
2024-10-15 18:01:16 +00:00
_customerBills.ItemAdded += UpdateBillInfo;
_customerBills.ItemRemoved += UpdateBillInfo;
2024-10-08 13:44:20 +00:00
EventManager.OnOrderedCocktail += OrderedCocktail;
EventManager.OnOrderResult += OrderResult;
}
private void OnDestroy()
{
2024-10-15 18:01:16 +00:00
_customerBills.ItemAdded -= UpdateBillInfo;
_customerBills.ItemRemoved -= UpdateBillInfo;
2024-10-08 13:44:20 +00:00
EventManager.OnOrderedCocktail -= OrderedCocktail;
EventManager.OnOrderResult -= OrderResult;
}
private void OrderedCocktail(Customer customer)
{
var instance = Instantiate(_billPrefab, _spawnLocation);
2024-10-15 18:01:16 +00:00
var newKeyValuePair = new KeyValuePair<Customer, Bill>(customer, instance);
_customerBills.Add(newKeyValuePair);
2024-11-07 09:13:54 +00:00
instance.Initialize(customer, _spawnPosition, _billInfos[0].Position, () => _customerBills.Remove(newKeyValuePair));
customer.SetCurrentBill(instance);
2024-10-08 13:44:20 +00:00
}
2024-11-15 07:28:13 +00:00
private void OrderResult(Customer customer, bool isCorrected)
2024-10-08 13:44:20 +00:00
{
2024-10-15 18:01:16 +00:00
var keyValue = _customerBills.FirstOrDefault((element) => element.Key == customer);
2024-11-15 07:28:13 +00:00
keyValue.Value.OrderResult(isCorrected);
2024-10-08 13:44:20 +00:00
}
2024-10-15 18:01:16 +00:00
private void UpdateBillInfo(ObservableList<KeyValuePair<Customer, Bill>> sender, ListChangedEventArgs<KeyValuePair<Customer, Bill>> e)
2024-10-08 13:44:20 +00:00
{
2024-11-28 23:07:50 +00:00
Utils.StartUniqueCoroutine(this, ref _updateInstance, UpdateBillInfoAfterDelay(sender));
}
private IEnumerator UpdateBillInfoAfterDelay(ObservableList<KeyValuePair<Customer, Bill>> sender)
{
yield return _waitForSeconds;
2024-10-15 18:01:16 +00:00
foreach (var element in sender)
2024-10-08 13:44:20 +00:00
{
for (var i = 0; i < _billInfos.Count; i++)
{
2024-10-15 18:01:16 +00:00
if (element.Value.CurrentBillInfo == _billInfos[i]) break;
2024-11-28 23:07:50 +00:00
2024-10-08 13:44:20 +00:00
if (_billInfos[i].IsEmpty)
{
PlayChainAnimation();
2024-11-28 23:07:50 +00:00
element.Value?.Move(_billInfos[i]);
2024-10-08 13:44:20 +00:00
break;
}
}
}
2024-10-10 05:44:37 +00:00
StopChainAnimation();
2024-11-28 23:07:50 +00:00
_updateInstance = null;
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
}
}
}