少年修仙传客户端代码仓库
hch
2 天以前 600733c8f592cb9e65f2b7a3e110ac1d686e6bfe
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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Monday, September 11, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
using System.Text;
 
namespace vnxbqy.UI
{
 
    public class BossLifeBar : MonoBehaviour
    {
        [SerializeField] Sprite[] m_LifeBarSprites;
 
        [SerializeField] Image m_BossIcon;
        [SerializeField] Image m_BackGround;
        [SerializeField] Image m_MiddleGround;
        [SerializeField] Image m_PrefaceGround;
 
        [SerializeField] Slider m_SliderMiddleground;
        [SerializeField] Slider m_SliderForeground;
 
        [SerializeField] Text m_Surplus;
        [SerializeField] Image m_Realm;
        [SerializeField] Text m_BossName;
        [SerializeField] Text m_BossLevel;
 
        [SerializeField] float m_SmoothTime = 0.3f;
        public float smoothTime {
            get { return m_SmoothTime; }
        }
 
        Pattern pattern = Pattern.Reduce;
        float targetValue = 0f;
        int totalSegments = 1;
        int surplusSegments = -1;
 
        float refValue = 0f;
        float timer = 0f;
        float behaviourStartValue = 0f;
 
        float m_CurrentBehaviourValue = 0f;
        float currentBehaviourValue {
            get { return m_CurrentBehaviourValue; }
            set {
                m_CurrentBehaviourValue = value;
                UpdateSurplusSegments(currentBehaviourValue);
                var behaviourDecimalValue = m_CurrentBehaviourValue - (int)m_CurrentBehaviourValue;
                var trueDecimalValue = targetValue - (int)targetValue;
                switch (pattern)
                {
                    case Pattern.Add:
                        m_SliderForeground.value = behaviourDecimalValue;
                        m_SliderMiddleground.value = behaviourDecimalValue > trueDecimalValue ? 1f : trueDecimalValue;
                        break;
                    case Pattern.Reduce:
                        m_SliderMiddleground.value = behaviourDecimalValue;
                        m_SliderForeground.value = behaviourDecimalValue < trueDecimalValue ? 0 : trueDecimalValue;
                        break;
                    case Pattern.None:
                        m_SliderMiddleground.value = behaviourDecimalValue;
                        m_SliderForeground.value = behaviourDecimalValue;
                        break;
                }
            }
        }
 
        public void SetBaseInfo(int _npcId, ulong _hp, ulong _maxHp, int _level)
        {
            var npcConfig = NPCConfig.Get(_npcId);
            m_BossName.text = npcConfig.charName;
            m_BossLevel.text = _level.ToString();
            m_BossIcon.SetSprite(npcConfig.HeadPortrait);
 
            if (m_Realm != null)
            {
                if (npcConfig.Realm > 0 && RealmConfig.Has(npcConfig.Realm))
                {
                    m_Realm.SetActive(true);
                    var realmConfig = RealmConfig.Get(npcConfig.Realm);
                    m_Realm.SetSprite(realmConfig.Img);
                }
                else
                {
                    m_Realm.SetActive(false);
                }
            }
 
            surplusSegments = -1;
            totalSegments = npcConfig.LifeBarCount;
            targetValue = currentBehaviourValue = ((float)_hp / _maxHp) * totalSegments - 0.0001f;
            var behaviourDecimalValue = currentBehaviourValue - (int)currentBehaviourValue;
            m_SliderForeground.value = m_SliderMiddleground.value = behaviourDecimalValue;
 
            refValue = 0f;
        }
 
        public void Show(ulong _hp, ulong _maxHp)
        {
            var percentage = Mathf.Clamp(_hp, 0, _maxHp) / (float)_maxHp;
            var tempValue = totalSegments * percentage - 0.00001f;
            pattern = tempValue > targetValue ? Pattern.Add : tempValue < targetValue ? Pattern.Reduce : Pattern.None;
 
            behaviourStartValue = currentBehaviourValue;
            targetValue = tempValue;
 
            timer = 0f;
            refValue = 0f;
        }
 
        private void LateUpdate()
        {
            if (Mathf.Abs(currentBehaviourValue - targetValue) > 0.00001f)
            {
                var newValue = Mathf.SmoothDamp(currentBehaviourValue, targetValue, ref refValue, smoothTime);
                currentBehaviourValue = newValue;
            }
        }
 
        static StringBuilder stringBuild = new StringBuilder();
        private void UpdateSurplusSegments(float _targetValue)
        {
            var currentSurplus = Mathf.CeilToInt(_targetValue);
            if (currentSurplus != surplusSegments)
            {
                surplusSegments = currentSurplus;
                var colorSetLength = m_LifeBarSprites.Length;
                var index = surplusSegments % colorSetLength;
                var nextIndex = index == 0 ? colorSetLength - 1 : index - 1;
 
                m_PrefaceGround.overrideSprite = m_LifeBarSprites[index];
                m_MiddleGround.overrideSprite = m_LifeBarSprites[index];
                m_BackGround.overrideSprite = m_LifeBarSprites[nextIndex];
 
                m_BackGround.SetActive(surplusSegments > 1);
 
                var chars = surplusSegments.ToString();
                stringBuild.Remove(0, stringBuild.Length);
                for (var i = 0; i < chars.Length; i++)
                {
                    var numChar = GetNumKey(chars[i]);
                    if (numChar > 0)
                    {
                        stringBuild.Append((char)numChar);
                    }
                }
 
                m_Surplus.text = stringBuild.ToString();
            }
        }
 
        int GetNumKey(int _num)
        {
            var config = DamageNumConfig.Get("BossLifeBarSurplusNum");
            return config.nums[_num - 48];
        }
 
 
        enum Pattern
        {
            None,
            Add,
            Reduce,
        }
 
    }
 
}