chenxin
2020-12-22 d185e655cd926e45ed7f7ce3f59af7dc23b4c372
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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;
            }
        }
    }
}