chenxin
2020-10-19 69d78dba3b74f57708d0a5c04d3f0d8606f3ae9f
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
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Core.Utilities;
using DG.Tweening;
 
/**
 * 无尽模式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;
 
        private Color[] colorArr =
        {
            new Color(1, 0, 0),                   // "#FF0000"
            new Color(1, 165 / 255f, 0),          // "#FFA500"
            new Color(1, 1, 0),                   // "#FFFF00"
            new Color(0, 1, 0),                   // "#00FF00"
            new Color(0, 1, 1),                   // "#00FFFF"
            new Color(0, 0, 1),                   // "#0000FF"
            new Color(128 / 255f, 0, 128 / 255f)  // "#800080"
        };
 
        /// <summary>
        /// 当前的进度值
        /// </summary>
        private float current;
 
        /// <summary>
        /// 目标进度值
        /// </summary>
        private float target;
 
        /// <summary>
        /// 血条消失速度
        /// </summary>
        public float Speed = 50f;
 
        /// <summary>
        /// 当前颜色索引
        /// </summary>
        private int index;
 
        private Tween tween;
 
        public Text WaveNumText;
 
        // Start is called before the first frame update
        private void Start()
        {
            InitHP();
        }
 
        /// <summary>
        /// 显示血条
        /// </summary>
        public void ShowHP()
        {
            gameObject.SetActive(true);
        }
 
        /// <summary>
        /// 隐藏血条
        /// </summary>
        public void HideHP()
        {
            gameObject.SetActive(false);
        }
 
        /// <summary>
        /// 设置当前进度百分比
        /// </summary>
        /// <param name="progress"></param>
        public void SetCurrentProgress(float progress)
        {
            target = progress;
            tween = DOTween.To(() => HPImage2.fillAmount, (v) => HPImage2.fillAmount = v, target, 0.3f);
            tween.Play();
        }
 
        /// <summary>
        /// 重置进度
        /// </summary>
        private void ResetProgress()
        {
            current = 1f;
            target = 1f;
            HPImage1.fillAmount = 1;
            HPImage2.fillAmount = 1;
            index = index % colorArr.Length;
        }
 
        /// <summary>
        /// 切换血条显示
        /// </summary>
        /// <param name="end"></param>
        public void SwitchHP(bool end = false)
        {
            tween.Pause();
            HPImage2.fillAmount = 0;
            index = ++index % colorArr.Length;
            ChangeHPImageIndex();
            ResetProgress();
            SetHPColor(end);
        }
 
        /// <summary>
        /// 初始化血条
        /// </summary>
        public void InitHP()
        {
            index = 0;
            ResetProgress();
            SetHPColor();
        }
 
        /// <summary>
        /// 更新波次
        /// </summary>
        /// <param name="wave"></param>
        public void UpdateWave(int wave)
        {
            WaveNumText.text = $"×{wave}";
        }
 
        /// <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.color = colorArr[index];
                HPImage1.color = Color.white;
            }
            else
            {
                HPImage2.color = colorArr[index];
                HPImage1.color = colorArr[(index + 1) % colorArr.Length];
            }
        }
    }
}