hch
11 小时以前 e95a97e663ba46ed474c89425dd92516a0d9b7dd
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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Monday, September 11, 2017
//--------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
using System.Text;
 
public class BossLifeBar : MonoBehaviour
{
    [SerializeField] Sprite[] m_LifeBarSprites;
 
    [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] Text m_SurplusPercent;
    
 
    [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);
        HeroSkinConfig skinConfig = HeroSkinConfig.Get(npcConfig.SkinID);
 
        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 = surplusSegments.ToString();
            m_SurplusPercent.text = Mathf.CeilToInt((_targetValue / totalSegments) * 100f).ToString() + "%";
        }
    }
 
    // int GetNumKey(int _num)
    // {
    //     var config = DamageNumConfig.Get("BossLifeBarSurplusNum");
    //     return config.nums[_num - 48];
    // }
 
 
    enum Pattern
    {
        None,
        Add,
        Reduce,
    }
 
}