From 8e413f4371d0ab1fcb6f2e52b8ca54074d9e1ae5 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期三, 17 十二月 2025 18:41:12 +0800
Subject: [PATCH] 125 战斗 修复战斗血量偶尔不是最新的问题

---
 Main/System/Battle/BattleField/BattleField.cs  |   52 +++++++++-
 Main/System/Battle/BattleUtility.cs            |    2 
 Main/System/Battle/UIComp/BattleHeroInfoBar.cs |  177 ++++++++++++++++++++++++++++++-----
 Main/System/Battle/Define/BattleDmgInfo.cs     |   16 +++
 4 files changed, 214 insertions(+), 33 deletions(-)

diff --git a/Main/System/Battle/BattleField/BattleField.cs b/Main/System/Battle/BattleField/BattleField.cs
index dcc0fda..0c7e101 100644
--- a/Main/System/Battle/BattleField/BattleField.cs
+++ b/Main/System/Battle/BattleField/BattleField.cs
@@ -131,6 +131,20 @@
 
         LoadMap(MapID);
 
+        // 娓呯┖褰撳墠鎴樺満鐨勮閲忚褰曪紙闃叉涓嶅悓鎴樻枟涔嬮棿鐨勬暟鎹贩涔憋級
+        if (BattleHeroInfoBar.largestPackUID.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUID.Remove(guid);
+        }
+        if (BattleHeroInfoBar.largestPackUIDAllObjectsToHp.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUIDAllObjectsToHp.Remove(guid);
+        }
+        if (BattleHeroInfoBar.largestPackUIDAllObjectsMaxHp.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUIDAllObjectsMaxHp.Remove(guid);
+        }
+
         // battleRootNode.SetBackground(ResManager.Instance.LoadAsset<Texture>("Texture/FullScreenBg", "mainui_img_277"));
 
         SetBattleStartState();
@@ -259,6 +273,20 @@
 
         // 娓呯悊姝讳骸澶勭悊璁板綍
         processingDeathObjIds.Clear();
+        
+        // 娓呯┖褰撳墠鎴樺満鐨勮閲忚褰�
+        if (BattleHeroInfoBar.largestPackUID.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUID.Remove(guid);
+        }
+        if (BattleHeroInfoBar.largestPackUIDAllObjectsToHp.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUIDAllObjectsToHp.Remove(guid);
+        }
+        if (BattleHeroInfoBar.largestPackUIDAllObjectsMaxHp.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUIDAllObjectsMaxHp.Remove(guid);
+        }
     }
 
     public virtual void Run()
@@ -394,7 +422,7 @@
         if (State == 4)
         {
             //宸茬粡缁撴潫骞剁粨绠�
-            Debug.Log("鎴樻枟缁撴潫");
+            // Debug.Log("鎴樻枟缁撴潫");
             rejectNewPackage = true;
             OnBattleEnd(turnFightStateData);
             return;
@@ -405,12 +433,12 @@
         {
             if (State == 2)
             {
-                Debug.Log("鎴樻枟寮�濮�");
+                // Debug.Log("鎴樻枟寮�濮�");
             }
         }
         else
         {
-            Debug.Log("鎴樻枟鍥炲悎 : " + turnNum + "锛岀姸鎬� " + State);
+            // Debug.Log("鎴樻枟鍥炲悎 : " + turnNum + "锛岀姸鎬� " + State);
         }
 
         DistributeNextPackage();
@@ -454,14 +482,14 @@
                 // 妫�鏌ユ槸鍚﹀凡缁忓湪澶勭悊姝讳骸
                 if (processingDeathObjIds.Contains(objID))
                 {
-                    Debug.LogWarning($"OnObjsDead: 瑙掕壊姝e湪澶勭悊姝讳骸锛屽拷鐣ラ噸澶嶆浜℃秷鎭� ObjID={objID}");
+                    // Debug.LogWarning($"OnObjsDead: 瑙掕壊姝e湪澶勭悊姝讳骸锛屽拷鐣ラ噸澶嶆浜℃秷鎭� ObjID={objID}");
                     continue;
                 }
                 
                 BattleObject battleObj = battleObjMgr.GetBattleObject((int)objID);
                 if (battleObj == null)
                 {
-                    Debug.LogWarning($"OnObjsDead: 鎵句笉鍒拌鑹� ObjID={objID}");
+                    Debug.LogError($"OnObjsDead: 鎵句笉鍒拌鑹� ObjID={objID}");
                     continue;
                 }
                 
@@ -673,6 +701,20 @@
         
         // 娓呯悊姝讳骸澶勭悊璁板綍
         processingDeathObjIds.Clear();
+        
+        // 娓呯┖褰撳墠鎴樺満鐨勮閲忚褰�
+        if (BattleHeroInfoBar.largestPackUID.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUID.Remove(guid);
+        }
+        if (BattleHeroInfoBar.largestPackUIDAllObjectsToHp.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUIDAllObjectsToHp.Remove(guid);
+        }
+        if (BattleHeroInfoBar.largestPackUIDAllObjectsMaxHp.ContainsKey(guid))
+        {
+            BattleHeroInfoBar.largestPackUIDAllObjectsMaxHp.Remove(guid);
+        }
 
         // ===== 鏂板锛氬嵏杞借摑闃熻祫婧� =====
         BattleResManager.Instance.UnloadBattleResources(guid);
diff --git a/Main/System/Battle/BattleUtility.cs b/Main/System/Battle/BattleUtility.cs
index 6e338dd..11b0e78 100644
--- a/Main/System/Battle/BattleUtility.cs
+++ b/Main/System/Battle/BattleUtility.cs
@@ -369,7 +369,7 @@
         battleHurtParam.hitIndex = hitIndex;
         battleHurtParam.deadPack = deadPack;
         battleHurtParam.skillConfig = skillConfig;
-
+        battleHurtParam.packUID = skillBase.tagUseSkillAttack.packUID;
         return battleHurtParam;
     }
 
diff --git a/Main/System/Battle/Define/BattleDmgInfo.cs b/Main/System/Battle/Define/BattleDmgInfo.cs
index 35faf25..a56c2fa 100644
--- a/Main/System/Battle/Define/BattleDmgInfo.cs
+++ b/Main/System/Battle/Define/BattleDmgInfo.cs
@@ -472,6 +472,13 @@
         {
             return hurtObj != null ? hurtObj.teamHero.maxHp : 0;
         }
+        set
+        {
+            if (hurtObj != null)
+            {
+                hurtObj.teamHero.maxHp = value;
+            }
+        }
     }
 
     public long fromShieldValue;
@@ -512,6 +519,13 @@
         {
             return casterObj != null ? casterObj.teamHero.maxHp : 0;
         }
+        set
+        {
+            if (casterObj != null)
+            {
+                casterObj.teamHero.maxHp = value;
+            }
+        }
     }
 
     public long fromShieldValue;
@@ -531,5 +545,5 @@
     public BattleDeadPack deadPack;
     public SkillConfig skillConfig;
 
-
+    public ulong packUID;
 }
\ No newline at end of file
diff --git a/Main/System/Battle/UIComp/BattleHeroInfoBar.cs b/Main/System/Battle/UIComp/BattleHeroInfoBar.cs
index 8387259..5ccd517 100644
--- a/Main/System/Battle/UIComp/BattleHeroInfoBar.cs
+++ b/Main/System/Battle/UIComp/BattleHeroInfoBar.cs
@@ -76,6 +76,11 @@
     // 椋樺瓧GCD鐩稿叧
     private float tipsGCDTimer = 0f;
     private const int TIPS_GCD_FRAMES = 5;
+
+    // 鍏ㄥ眬琛�閲忚褰曪紙鎸夋垬鍦篻uid缁勭粐锛屼互鏈�澶ackUID涓哄噯锛岃褰曟墍鏈夊璞★細鏂芥硶鑰呭拰鍙楀嚮鑰咃級
+    public static Dictionary<string, ulong> largestPackUID = new Dictionary<string, ulong>();
+    public static Dictionary<string, Dictionary<long, long>> largestPackUIDAllObjectsToHp = new Dictionary<string, Dictionary<long, long>>();
+    public static Dictionary<string, Dictionary<long, long>> largestPackUIDAllObjectsMaxHp = new Dictionary<string, Dictionary<long, long>>();
     
     protected void OnDisable()
     {
@@ -108,9 +113,11 @@
         // 鎶ょ浘2鐨勫�� = max(褰撳墠琛�閲� + 鎶ょ浘鍊� - maxHp, 0) / maxHp
         float shield2Value = maxHp > 0 ? Mathf.Max((float)(curHp + shieldValue - maxHp), 0f) / (float)maxHp : 0;
         
-        sliderShield1.value = shieldValue > 0 ? shield1Value : 0;
-        sliderShield2.value = shieldValue > maxHp ? shield2Value : 0;
+        sliderShield1.value = shield1Value;
+        sliderShield2.value = shield2Value;
         
+
+
         // 鎵撳嵃璁剧疆鎶ょ浘鏃剁殑鐘舵��
         // Debug.LogError($"[BattleHeroInfoBar.SetBattleObject] 璁剧疆鎶ょ浘 - curHp: {curHp}, shieldValue: {shieldValue}, maxHp: {maxHp}, shield1鍓�: {oldShield1Value}, shield1鍚�: {shield1Value}, shield2鍓�: {oldShield2Value}, shield2鍚�: {shield2Value}");
     }
@@ -152,8 +159,8 @@
         // 鎶ょ浘2鐨勫�� = max(褰撳墠琛�閲� + 鎶ょ浘鍊� - maxHp, 0) / maxHp
         float shield2Value = maxHp > 0 ? Mathf.Max((float)(curHp + shieldValue - maxHp), 0f) / (float)maxHp : 0;
         
-        sliderShield1.value = shieldValue > 0 ? shield1Value : 0;
-        sliderShield2.value = shieldValue > maxHp ? shield2Value : 0;
+        sliderShield1.value = shield1Value;
+        sliderShield2.value = shield2Value;
 
         if (!battleObject.IsTianziBoss())
         {
@@ -293,40 +300,61 @@
     {
         KillTween(ref damageSequence);
 
-        // 鍒ゆ柇鏄洰鏍囪瑙掕繕鏄柦娉曡�呰瑙掞紝浣跨敤瀵瑰簲鐨勬暟鎹�
-        bool isCasterView = dmgInfo.hurtObj == dmgInfo.casterObj;
+        string guid = battleObject.battleField.guid;
+        long objID = battleObject.ObjID;
+        ulong currentPackUID = dmgInfo.battleHurtParam.packUID;
+        
+        // 妫�鏌ユ槸鍚︽槸鏈�鏂扮殑 PackUID锛屽鏋滀笉鏄垯蹇界暐
+        if (!largestPackUID.ContainsKey(guid) || currentPackUID < largestPackUID[guid])
+        {
+            // Debug.LogWarning($"[ExecuteDamageUpdate] 蹇界暐鏃у寘 - ObjID:{objID}, 褰撳墠PackUID:{currentPackUID} < 鏈�澶ackUID:{largestPackUID[guid]}");
+            return;
+        }
         
         long maxHp, fromHp, toHp, fromShield, toShield;
         
-        if (isCasterView)
+        // 浼樺厛鍒ゆ柇褰撳墠InfoBar鏄惁涓哄彈鍑昏�咃紙琛�閲忓彉鍖栨�绘槸浣撶幇鍦╤urter閲岋級
+        BattleHurtObj hurter = dmgInfo.battleHurtParam.hurter;
+        if (hurter?.hurtObj != null && hurter.hurtObj.ObjID == objID)
         {
-            // 鏂芥硶鑰呰瑙掞細浣跨敤 caster 鏁版嵁
-            BattleCastObj caster = dmgInfo.battleHurtParam.caster;
-            if (caster.casterObj != null && caster.casterObj.IsTianziBoss())
+            // 褰撳墠InfoBar鏄彈鍑昏�咃紙鍖呮嫭缁欒嚜宸辨不鐤椼�佺粰鑷繁閫犳垚浼ゅ鐨勬儏鍐碉級
+            if (hurter.hurtObj.IsTianziBoss())
             {
-                // 澶╁瓙瑙嗚涓嶅鐞嗘姢鐩惧彉鍖�
                 return;
             }
-            maxHp = caster.maxHp;
-            fromHp = caster.fromHp;
-            toHp = caster.toHp;
-            fromShield = caster.fromShieldValue;
-            toShield = caster.toShieldValue;
-        }
-        else
-        {
-            // 鐩爣瑙嗚锛氫娇鐢� hurter 鏁版嵁
-            BattleHurtObj hurter = dmgInfo.battleHurtParam.hurter;
-            if (hurter.hurtObj != null && hurter.hurtObj.IsTianziBoss())
-            {
-                // 澶╁瓙瑙嗚涓嶅鐞嗘姢鐩惧彉鍖�
-                return;
-            }
+            
+            // 鐩存帴浣跨敤 dmgInfo 涓殑鏁版嵁锛堝凡缁忚 CompareAndExchangeLargestPackUIDHp 楠岃瘉杩囷級
             maxHp = hurter.maxHp;
             fromHp = hurter.fromHp;
             toHp = hurter.toHp;
             fromShield = hurter.fromShieldValue;
             toShield = hurter.toShieldValue;
+            
+            // Debug.LogError($"[ExecuteDamageUpdate] 鍙楀嚮鑰� - ObjID:{objID}, fromHp:{fromHp}, toHp:{toHp}, maxHp:{maxHp} (PackUID:{currentPackUID})");
+        }
+        // 鍏舵鍒ゆ柇鏄惁涓烘柦娉曡�咃紙鏂芥硶娑堣�楃敓鍛界瓑鎯呭喌锛�
+        else
+        {
+            BattleCastObj caster = dmgInfo.battleHurtParam.caster;
+            if (caster?.casterObj == null || caster.casterObj.ObjID != objID)
+            {
+                // Debug.LogWarning($"[ExecuteDamageUpdate] 褰撳墠瀵硅薄 {objID} 鏃笉鏄柦娉曡�呬篃涓嶆槸鍙楀嚮鑰�");
+                return;
+            }
+            
+            if (caster.casterObj.IsTianziBoss())
+            {
+                return;
+            }
+            
+            // 鐩存帴浣跨敤 dmgInfo 涓殑鏁版嵁锛堝凡缁忚 CompareAndExchangeLargestPackUIDHp 楠岃瘉杩囷級
+            maxHp = caster.maxHp;
+            fromHp = caster.fromHp;
+            toHp = caster.toHp;
+            fromShield = caster.fromShieldValue;
+            toShield = caster.toShieldValue;
+            
+            // Debug.LogError($"[ExecuteDamageUpdate] 鏂芥硶鑰� - ObjID:{objID}, fromHp:{fromHp}, toHp:{toHp}, maxHp:{maxHp} (PackUID:{currentPackUID})");
         }
 
         if (maxHp <= 0)
@@ -418,6 +446,102 @@
         }
     }
     
+    private void CompareAndExchangeLargestPackUIDHp(BattleDmgInfo dmgInfo)
+    {
+        string guid = battleObject.battleField.guid;
+        ulong currentPackUID = dmgInfo.battleHurtParam.packUID;
+        
+        // 鑾峰彇鎴栧垵濮嬪寲褰撳墠鎴樺満鐨勬暟鎹�
+        if (!largestPackUID.ContainsKey(guid))
+        {
+            largestPackUID[guid] = 0ul;
+        }
+        if (!largestPackUIDAllObjectsToHp.ContainsKey(guid))
+        {
+            largestPackUIDAllObjectsToHp[guid] = new Dictionary<long, long>();
+        }
+        if (!largestPackUIDAllObjectsMaxHp.ContainsKey(guid))
+        {
+            largestPackUIDAllObjectsMaxHp[guid] = new Dictionary<long, long>();
+        }
+        
+        ulong currentLargestPackUID = largestPackUID[guid];
+        Dictionary<long, long> hpDict = largestPackUIDAllObjectsToHp[guid];
+        Dictionary<long, long> maxHpDict = largestPackUIDAllObjectsMaxHp[guid];
+        
+        // 濡傛灉閬囧埌鏇村ぇ鐨刾ackUID锛屾洿鏂版爣璁帮紙涓嶆竻绌烘暟鎹紝淇濈暀鎵�鏈夎瘉鎹級
+        if (currentPackUID > currentLargestPackUID)
+        {
+            // Debug.LogError($"[琛�閲忚褰昡 妫�娴嬪埌鏂版壒娆ackUID: {currentPackUID} > {currentLargestPackUID}锛屼繚鐣欐墍鏈夊巻鍙叉暟鎹�");
+            largestPackUID[guid] = currentPackUID;
+            currentLargestPackUID = currentPackUID;
+        }
+        
+        // 璁板綍鎵�鏈塸ackUID鐨勬暟鎹紙鍖呮嫭鏃╄Е鍙戠殑鍖咃級锛屼絾鍙噰鐢ㄦ渶澶ackUID鐨勬暟鎹�
+        // 璁板綍鏂芥硶鑰呯殑琛�閲忓彉鍖�
+        if (dmgInfo.battleHurtParam.caster?.casterObj != null)
+        {
+            BattleCastObj caster = dmgInfo.battleHurtParam.caster;
+            long casterID = caster.casterObj.ObjID;
+            
+            // 鑾峰彇鏃ц閲忕敤浜庤绠楀彉鍖�
+            long oldHp = hpDict.ContainsKey(casterID) ? hpDict[casterID] : caster.fromHp;
+            long newHp = caster.toHp;
+            long maxHp = caster.maxHp;
+            long hpChange = newHp - oldHp;
+            
+            // 鍙湁褰撳墠packUID绛変簬鏈�澶ackUID鏃舵墠鏇存柊璁板綍
+            if (currentPackUID == currentLargestPackUID)
+            {
+                hpDict[casterID] = newHp;
+                maxHpDict[casterID] = maxHp;
+                
+                // 鎵撳嵃琛�閲忓彉鍖栨棩蹇楋紙鏂芥硶鑰呴�氬父鏄仮澶嶇敓鍛斤級
+                // string casterName = caster.casterObj.teamHero?.heroConfig.Name ?? "鏈煡姝﹀皢";
+                if (hpChange != 0)
+                {
+                    // string changeType = hpChange > 0 ? "鎭㈠" : "鎹熷け";
+                    // Debug.LogError($"[琛�閲忓彉鍖朷 {casterName}(ID:{casterID}) {changeType} {Math.Abs(hpChange)} 鐢熷懡锛岃閲忎粠 {oldHp}/{maxHp} 鍙樹负 {newHp}/{maxHp} (PackUID:{currentPackUID})");
+                }
+            }
+            else
+            {
+                // Debug.LogWarning($"[琛�閲忚褰昡 蹇界暐鏃у寘鏁版嵁 - 鏂芥硶鑰厈casterID}, 褰撳墠PackUID:{currentPackUID} < 鏈�澶ackUID:{currentLargestPackUID}");
+            }
+        }
+        
+        // 璁板綍鍙楀嚮鑰呯殑琛�閲忓彉鍖�
+        if (dmgInfo.battleHurtParam.hurter?.hurtObj != null)
+        {
+            BattleHurtObj hurter = dmgInfo.battleHurtParam.hurter;
+            long hurterID = hurter.hurtObj.ObjID;
+            
+            // 鑾峰彇鏃ц閲忕敤浜庤绠椾激瀹�
+            long oldHp = hpDict.ContainsKey(hurterID) ? hpDict[hurterID] : hurter.fromHp;
+            long newHp = hurter.toHp;
+            long maxHp = hurter.maxHp;
+            long damage = oldHp - newHp;
+            
+            // 鍙湁褰撳墠packUID绛変簬鏈�澶ackUID鏃舵墠鏇存柊璁板綍
+            if (currentPackUID == currentLargestPackUID)
+            {
+                hpDict[hurterID] = newHp;
+                maxHpDict[hurterID] = maxHp;
+                
+                // 鎵撳嵃琛�閲忓彉鍖栨棩蹇�
+                // string hurterName = hurter.hurtObj.teamHero?.heroConfig.Name ?? "鏈煡姝﹀皢";
+                if (damage != 0)
+                {
+                    // Debug.LogError($"[琛�閲忓彉鍖朷 {hurterName}(ID:{hurterID}) 鍙楀埌 {damage} 浼ゅ锛岃閲忎粠 {oldHp}/{maxHp} 鍙樹负 {newHp}/{maxHp} (PackUID:{currentPackUID})");
+                }
+            }
+            else
+            {
+                // Debug.LogWarning($"[琛�閲忚褰昡 蹇界暐鏃у寘鏁版嵁 - 鍙楀嚮鑰厈hurterID}, 褰撳墠PackUID:{currentPackUID} < 鏈�澶ackUID:{currentLargestPackUID}");
+            }
+        }
+    }
+
     /// <summary>
     /// 澶勭悊琛�鏉″拰浼ゅ鏇存柊闃熷垪
     /// </summary>
@@ -427,6 +551,7 @@
         if (damageUpdateQueue.Count > 0)
         {
             BattleDmgInfo dmgInfo = damageUpdateQueue.Dequeue();
+            CompareAndExchangeLargestPackUIDHp(dmgInfo);
             ExecuteDamageUpdate(dmgInfo);
             return;
         }

--
Gitblit v1.8.0