using Core.Economy;
|
using KTGMGemClient;
|
using TMPro;
|
using TowerDefense.Level;
|
using UnityEngine;
|
using DG.Tweening;
|
using Core.Utilities;
|
|
namespace TowerDefense.UI.HUD
|
{
|
/// <summary>
|
/// A class for controlling the displaying the currency
|
/// </summary>
|
public class EndlessCurrencyUI : MonoBehaviour
|
{
|
/// <summary>
|
/// TextMeshPro对应的金币数据
|
/// </summary>
|
public TextMeshProUGUI DisplayText;
|
|
protected Currency currency;
|
|
private ParticleSystem goldGetPs;//金币获得动画
|
|
/// <summary>
|
/// 每秒增加的金币数量
|
/// </summary>
|
private int addCurrencyPerSecond;
|
|
private float duration;
|
|
/// <summary>
|
/// 一秒前金币
|
/// </summary>
|
private int lastCurrency;
|
|
[SerializeField]
|
private GameObject addCurrencyPrefab;
|
|
private void Start()
|
{
|
goldGetPs = transform.Find("CurrencyImage/Effect_UI_JinBi_HuoDe/Particle System (6)").GetComponent<ParticleSystem>();
|
|
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);
|
}
|
|
/// <summary>
|
/// Unsubscribe from events
|
/// </summary>
|
protected virtual void OnDestroy()
|
{
|
if (currency != null)
|
currency.currencyChanged -= UpdateDisplay;
|
}
|
|
/// <summary>
|
/// A method for updating the display based on the current currency
|
/// </summary>
|
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();
|
}
|
|
/// <summary>
|
/// 在数字中加入逗号
|
/// </summary>
|
/// <param name="str"></param>
|
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();
|
}
|
|
/// <summary>
|
/// 飘1s内增加的金币
|
/// </summary>
|
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>();
|
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;
|
}
|
}
|
}
|
}
|