using System; using System.Collections; using System.Collections.Generic; using Sirenix.OdinInspector; using TMPro; using UnityEngine; namespace BlueWater.Uis { public class GoldUi : MonoBehaviour { [SerializeField, Required] private Animator _goldAnimator; [SerializeField, Required] private TMP_Text _goldText; private Queue _goldQueue = new(); private bool _isGoldAnimating; private bool _isQuitting; private const string GoldAnimation = "Gold"; private void OnEnable() { DataManager.Instance.OnChangeGold += ChangeGold; } private void OnDisable() { if (_isQuitting) return; DataManager.Instance.OnChangeGold -= ChangeGold; } private void OnApplicationQuit() { _isQuitting = true; } private void ChangeGold(int newGoldAmount) { _goldQueue.Enqueue(newGoldAmount); if (!_isGoldAnimating) { StartCoroutine(AnimateGoldChange()); } } private IEnumerator AnimateGoldChange() { _isGoldAnimating = true; while (_goldQueue.Count > 0) { var targetGold = _goldQueue.Dequeue(); var currentGold = int.Parse(_goldText.text.Replace(",", "")); var elapsedTime = 0f; _goldAnimator.Play(GoldAnimation, -1, 0f); while (elapsedTime < 1f) { elapsedTime += Time.deltaTime; var newGold = (int)Mathf.Lerp(currentGold, targetGold, elapsedTime / 1f); _goldText.text = newGold.ToString("N0"); yield return null; } _goldText.text = targetGold.ToString("N0"); } _isGoldAnimating = false; } } }