2024-07-20 11:32:54 +00:00
|
|
|
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<int> _goldQueue = new();
|
|
|
|
private bool _isGoldAnimating;
|
|
|
|
private bool _isQuitting;
|
2024-07-22 00:42:29 +00:00
|
|
|
|
|
|
|
// Hashes
|
|
|
|
private static readonly int _highlightTriggerHash = Animator.StringToHash("highlightTrigger");
|
2024-07-20 11:32:54 +00:00
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
DataManager.Instance.OnChangeGold += ChangeGold;
|
|
|
|
}
|
|
|
|
|
2024-07-22 00:42:29 +00:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
//_goldAnimator.GetComponent<GraphicMaterialOverride>().SetMaterialDirty();
|
|
|
|
}
|
|
|
|
|
2024-07-20 11:32:54 +00:00
|
|
|
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;
|
2024-07-22 00:42:29 +00:00
|
|
|
_goldAnimator.SetTrigger(_highlightTriggerHash);
|
2024-07-20 11:32:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|