wangguan
2020-11-18 60571ce1c77c5c323dff0d13e0fcd1ae9c22b40a
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using UnityEngine.UI;
using UnityEngine;
using Core.Utilities;
using DG.Tweening;
using TMPro;
 
/**
 * 无尽模式boss血量管理器
 * @Author: chenxin
 * @Date: 2020-10-16 20:00:56
 */
namespace KTGMGemClient
{
    public class EndlessBossHPManager : Singleton<EndlessBossHPManager>
    {
        /// <summary>
        /// 血条1
        /// </summary>
        public Image HPImage1;
 
        /// <summary>
        /// 血条2
        /// </summary>
        public Image HPImage2;
 
        /// <summary>
        /// boss形象
        /// </summary>
        public Image BossImage;
 
        private string path = "UI/Endless/Blood/";
 
        /// <summary>
        /// 血条底图
        /// </summary>
        public Sprite BaseBlood;
 
        /// <summary>
        /// 目标进度值
        /// </summary>
        private float target;
 
        /// <summary>
        /// 当前颜色索引
        /// </summary>
        private int index;
 
        public TextMeshProUGUI WaveNumText;
 
        /// <summary>
        /// 血条7种颜色
        /// </summary>
        private int count = 7;
 
        /// <summary>
        /// boss信息,名字和等级
        /// </summary>
        public Text BossInfo;
 
        private Sequence progressSequence;
 
        public Text HPInfoText;
 
        private float totalHP;
 
        /// <summary>
        /// boss总血量
        /// </summary>
        /// <value></value>
        private float TotalHP { get; set; }
 
        private float hp;
 
        /// <summary>
        /// 当前血量
        /// </summary>
        /// <value></value>
        public float CurrentHP
        {
            get { return hp; }
            private set
            {
                hp = value;
 
                if (hp < 0.00001f) hp = 0;
            }
        }
 
        // Start is called before the first frame update
        private void Start()
        {
            Init();
        }
 
        /// <summary>
        /// 显示血条
        /// </summary>
        public void ShowHP()
        {
            gameObject.SetActive(true);
        }
 
        /// <summary>
        /// 隐藏血条
        /// </summary>
        public void HideHP()
        {
            gameObject.SetActive(false);
        }
 
        /// <summary>
        /// 设置boss的当前血量
        /// </summary>
        /// <param name="hp"></param>
        public void SetCurrentHP(float hp)
        {
            CurrentHP = hp;
            target = CurrentHP / TotalHP;
            HPInfoText.text = $"HP:{Mathf.Floor(CurrentHP)}/{Mathf.Floor(TotalHP)}";
            DOTween.To(() => HPImage2.fillAmount, (float v) => HPImage2.fillAmount = v, target, 0.2f);
        }
 
        /// <summary>
        /// 重置进度
        /// </summary>
        private void ResetProgress()
        {
            target = 1f;
            HPImage1.fillAmount = 1;
            HPImage2.fillAmount = 1;
            index = index % count;
        }
 
        /// <summary>
        /// 切换血条显示
        /// </summary>
        /// <param name="end"></param>
        public void SwitchHP(bool end = false)
        {
            HPImage2.fillAmount = 0;
            index = ++index % count;
            ChangeHPImageIndex();
            ResetProgress();
            SetHPColor(end);
        }
 
        /// <summary>
        /// 初始化血条
        /// </summary>
        public void Init()
        {
            index = 0;
            ResetProgress();
            SetHPColor();
        }
 
        /// <summary>
        /// 初始化血量数据
        /// </summary>
        /// <param name="totalHP"></param>
        public void InitHP(float totalHP)
        {
            CurrentHP = TotalHP = totalHP;
            HPInfoText.text = $"HP:{Mathf.Floor(CurrentHP)}/{Mathf.Floor(TotalHP)}";
        }
 
        /// <summary>
        /// 更新波次
        /// </summary>
        /// <param name="wave"></param>
        public void UpdateWave(int wave)
        {
            WaveNumText.text = $"x{wave}";
            if (wave == 0)
            {
                AudioSourceManager.Ins.Play(AudioEnum.BossDie);
            }
        }
 
        /// <summary>
        /// 设置boss信息
        /// </summary>
        /// <param name="info"></param>
        public void SetBossInfo(string info)
        {
            BossInfo.text = info;
        }
 
        /// <summary>
        /// 切换两血条的显示层级
        /// </summary>
        private void ChangeHPImageIndex()
        {
            int index1 = HPImage1.transform.GetSiblingIndex();
            int index2 = HPImage2.transform.GetSiblingIndex();
            HPImage1.transform.SetSiblingIndex(index2);
            HPImage2.transform.SetSiblingIndex(index1);
            // 始终保持 HPImage2在上面
            Image tmp = HPImage1;
            HPImage1 = HPImage2;
            HPImage2 = tmp;
        }
 
        /// <summary>
        /// 设置血条颜色
        /// </summary>
        /// <param name="end">是否是最终的血条显示</param>
        private void SetHPColor(bool end = false)
        {
            if (end)
            {
                HPImage2.sprite = GetSprite(index);
                HPImage1.sprite = Resources.Load($"{path}di", typeof(Sprite)) as Sprite;
            }
            else
            {
                HPImage2.sprite = GetSprite(index);
                HPImage1.sprite = GetSprite((index + 1) % count);
            }
        }
 
        /// <summary>
        /// 根据索引获得sprite
        /// </summary>
        /// <param name="index"></param>
        private Sprite GetSprite(int index)
        {
            return Resources.Load($"{path}{index}", typeof(Sprite)) as Sprite;
        }
    }
}