yyl
6 天以前 3bd7f56906e31e8fe0072108c9d4652707b51de8
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
//--------------------------------------------------------
//    [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);
 
            float behaviourDecimalValue = GetSegmentDecimal(m_CurrentBehaviourValue);
            float trueDecimalValue = GetSegmentDecimal(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 ? 0f : trueDecimalValue;
                    break;
                case Pattern.None:
                    m_SliderMiddleground.value = behaviourDecimalValue;
                    m_SliderForeground.value = behaviourDecimalValue;
                    break;
            }
        }
    }
 
    public void SetBaseInfo(int _lifeBarCount, ulong _hp, ulong _maxHp)
    {
        surplusSegments = -1;
        totalSegments = _lifeBarCount;
 
        // 使用精确值,不再人为减小
        float percentage = (_maxHp > 0) ? (float)_hp / (float)_maxHp : 0f;
        targetValue = currentBehaviourValue = percentage * totalSegments;
 
        // 使用统一的 GetSegmentDecimal,避免小数精度导致进度条为0
        var behaviourDecimalValue = GetSegmentDecimal(currentBehaviourValue);
        m_SliderForeground.value = m_SliderMiddleground.value = behaviourDecimalValue;
 
        refValue = 0f;
 
        // 立刻显示基准百分比(使用 percentage)
        m_SurplusPercent.text = (percentage * 100f).ToString("F2") + "%";
    }
 
    public void Show(ulong _hp, ulong _maxHp)
    {
        var percentage = Mathf.Clamp(_hp, 0, _maxHp) / (float)_maxHp;
        var tempValue = totalSegments * percentage; // 不再减小
        pattern = tempValue > targetValue ? Pattern.Add : tempValue < targetValue ? Pattern.Reduce : Pattern.None;
 
        behaviourStartValue = currentBehaviourValue;
        targetValue = tempValue;
 
        timer = 0f;
        refValue = 0f;
 
        // 立即更新百分比显示(直接使用 percentage)
        m_SurplusPercent.text = (percentage * 100f).ToString("F2") + "%";
    }
 
    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);
 
            m_Surplus.text = "x" + surplusSegments.ToString();
            float pct = totalSegments > 0 ? Mathf.Clamp01(_targetValue / totalSegments) : 0f;
            // 修复格式并处理接近 100% 的情况
            if (1f - pct < 0.00005f) pct = 1f;
            m_SurplusPercent.text = (pct * 100f).ToString("F2") + "%";
        }
    }
 
    // int GetNumKey(int _num)
    // {
    //     var config = DamageNumConfig.Get("BossLifeBarSurplusNum");
    //     return config.nums[_num - 48];
    // }
 
 
    enum Pattern
    {
        None,
        Add,
        Reduce,
    }
 
    // helper: 返回段内小数部分;当恰好为整数且大于0时,返回1以表示满格(避免0导致进度条变空)
    private float GetSegmentDecimal(float value)
    {
        if (value <= 0f) return 0f;
 
        // 使用一个小的容忍值来处理浮点误差,避免 near-integer 导致 0
        const float eps = 1e-5f;
 
        // 先做一个向下稳定的 floor,避免 2.999999 变成 2 的问题
        float stableFloor = Mathf.Floor(value + eps);
        float frac = value - stableFloor;
 
        if (frac <= eps)
            return 1f; // 视为整段,显示满格
        return Mathf.Clamp01(frac);
    }
 
}