yyl
7 天以前 3bd7f56906e31e8fe0072108c9d4652707b51de8
Main/Component/UI/Common/BossLifeBar.cs
@@ -44,8 +44,10 @@
        set {
            m_CurrentBehaviourValue = value;
            UpdateSurplusSegments(currentBehaviourValue);
            var behaviourDecimalValue = m_CurrentBehaviourValue - (int)m_CurrentBehaviourValue;
            var trueDecimalValue = targetValue - (int)targetValue;
            float behaviourDecimalValue = GetSegmentDecimal(m_CurrentBehaviourValue);
            float trueDecimalValue = GetSegmentDecimal(targetValue);
            switch (pattern)
            {
                case Pattern.Add:
@@ -54,7 +56,7 @@
                    break;
                case Pattern.Reduce:
                    m_SliderMiddleground.value = behaviourDecimalValue;
                    m_SliderForeground.value = behaviourDecimalValue < trueDecimalValue ? 0 : trueDecimalValue;
                    m_SliderForeground.value = behaviourDecimalValue < trueDecimalValue ? 0f : trueDecimalValue;
                    break;
                case Pattern.None:
                    m_SliderMiddleground.value = behaviourDecimalValue;
@@ -64,24 +66,29 @@
        }
    }
    public void SetBaseInfo(int _npcId, ulong _hp, ulong _maxHp, int _level)
    public void SetBaseInfo(int _lifeBarCount, ulong _hp, ulong _maxHp)
    {
        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;
        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 - 0.00001f;
        var tempValue = totalSegments * percentage; // 不再减小
        pattern = tempValue > targetValue ? Pattern.Add : tempValue < targetValue ? Pattern.Reduce : Pattern.None;
        behaviourStartValue = currentBehaviourValue;
@@ -89,6 +96,9 @@
        timer = 0f;
        refValue = 0f;
        // 立即更新百分比显示(直接使用 percentage)
        m_SurplusPercent.text = (percentage * 100f).ToString("F2") + "%";
    }
    private void LateUpdate()
@@ -117,19 +127,11 @@
            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() + "%";
            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") + "%";
        }
    }
@@ -147,6 +149,23 @@
        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);
    }
}