using Core.Economy; using KTGMGemClient; using TMPro; using TowerDefense.Level; using UnityEngine; using DG.Tweening; using Core.Utilities; namespace TowerDefense.UI.HUD { /// /// A class for controlling the displaying the currency /// public class EndlessCurrencyUI : MonoBehaviour { /// /// TextMeshPro对应的金币数据 /// public TextMeshProUGUI DisplayText; protected Currency currency; private ParticleSystem goldGetPs;//金币获得动画 /// /// 每秒增加的金币数量 /// private int addCurrencyPerSecond; private float duration; /// /// 一秒前金币 /// private int lastCurrency; [SerializeField] private GameObject addCurrencyPrefab; private void Start() { goldGetPs = transform.Find("CurrencyImage/Effect_UI_JinBi_HuoDe/Particle System (6)").GetComponent(); if (EndlessLevelManager.instanceExists) { currency = EndlessLevelManager.instance.Currency; UpdateDisplay(); currency.currencyChanged += UpdateDisplay; } else Debug.LogError("[UI] No EndlessLevelManager to get currency from"); EventCenter.Ins.Add((int)KTGMGemClient.EventType.PlayGetGoldPS, PlayGetGoldPS); } /// /// Unsubscribe from events /// protected virtual void OnDestroy() { if (currency != null) currency.currencyChanged -= UpdateDisplay; } /// /// A method for updating the display based on the current currency /// protected void UpdateDisplay() { int current = currency.currentCurrency; int lastCurrency = currency.lastCurrency; DisplayText.text = current.ToString(); if (lastCurrency > 0 && current - lastCurrency > 0) ToBig().OnComplete(ToSmall); } private Tweener ToBig() { return DOTween.To( () => DisplayText.transform.localScale, (Vector3 v) => DisplayText.transform.localScale = v, new Vector3(1.15f, 1.15f, 1.15f), 0.15f); } private void ToSmall() { DOTween.To( () => DisplayText.transform.localScale, (Vector3 v) => DisplayText.transform.localScale = v, new Vector3(1f, 1f, 1f), 0.1f); } public void PlayGetGoldPS() { goldGetPs.Play(); } /// /// 在数字中加入逗号 /// /// protected string AddNumberSemi(string str) { int len = str.Length; for (int ipos = len - 3; ipos > 0; ipos -= 3) { str = str.Insert(ipos, ","); } return str; } private void Update() { FloatAddCurrency(); } /// /// 飘1s内增加的金币 /// private void FloatAddCurrency() { if (duration <= 0.0001f) { lastCurrency = currency.currentCurrency; } duration += Time.deltaTime; if (duration >= 1f) { int add = currency.currentCurrency - lastCurrency; if (add > 0) { GameObject obj = Instantiate(addCurrencyPrefab); obj.transform.SetParent(GameObject.Find("UICamera/BottomCanvas/Panel/Bottom/CurrencyContainer").transform, false); obj.transform.localPosition = new Vector3(148f, -30f, 0f); obj.transform.localScale = new Vector3(0.66f, 0.66f, 0.66f); TextMeshProUGUI textMeshProUGUI = obj.GetComponent(); textMeshProUGUI.text = $"+{add}"; DOTween.To( () => obj.transform.localPosition.y, (float v) => { Vector3 pos = obj.transform.localPosition; pos.y = v; obj.transform.localPosition = pos; }, -20f, 0.2f); Destroy(obj, 0.5f); } duration = 0f; } } } }