| using UnityEngine; | 
| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine.UI; | 
| using DG.Tweening; | 
| using Cysharp.Threading.Tasks; | 
| using System; | 
| using LitJson; | 
|   | 
|   | 
| //  这个界面是 persistent的界面 | 
| public class BattleHUDWin : UIBase | 
| { | 
|     // 组件引用 | 
|     // private List<HUDContent> damageList = new List<HUDContent>(); | 
|   | 
|     // private List<BuffContent> buffList = new List<BuffContent>(); | 
|   | 
|     private GameObjectPoolManager.GameObjectPool damagePrefabPool; | 
|   | 
|     private GameObjectPoolManager.GameObjectPool buffIconPrefabPool; | 
|   | 
|     private GameObjectPoolManager.GameObjectPool buffLabelPrefabPool; | 
|   | 
|     public Transform damageNode; | 
|   | 
|     public Transform buffIconNode; | 
|   | 
|     public Transform buffLabelNode; | 
|   | 
|     private BattleField battleField; | 
|   | 
|     private List<DamageContent> damageContentList = new List<DamageContent>(); | 
|   | 
|     // 生命周期 | 
|     protected override void InitComponent() | 
|     { | 
|         base.InitComponent(); | 
|         // 初始化组件引用 绑定按钮等UI组件事件 | 
|     } | 
|   | 
|     protected override void OnPreOpen() | 
|     { | 
|         base.OnPreOpen(); | 
|         EventBroadcast.Instance.AddListener<BattleDmgInfo>(EventName.BATTLE_DAMAGE_TAKEN, OnDamageTaken); | 
|         EventBroadcast.Instance.AddListener<string, JsonData>(EventName.BATTLE_END, OnBattleEnd); | 
|         damagePrefabPool = GameObjectPoolManager.Instance.RequestPool(UILoader.LoadPrefab("DamageContent")); | 
|         // buffIconPrefabPool = GameObjectPoolManager.Instance.RequestPool(); | 
|         // buffLabelPrefabPool = GameObjectPoolManager.Instance.RequestPool(ResManager.Instance.LoadAsset<GameObject>("UIComp", "BuffContent")); | 
|     } | 
|   | 
|     private void OnBattleEnd(string guid, JsonData data) | 
|     { | 
|         ClearContent(guid); | 
|     } | 
|   | 
|     private void ClearContent(string guid, bool force = false) | 
|     { | 
|         if ((battleField != null && battleField.guid == guid) || force) | 
|         { | 
|             for (int i = damageContentList.Count - 1; i >= 0; i--) | 
|             { | 
|                 var content = damageContentList[i]; | 
|                 content.Stop(); | 
|                 RemoveDamageContent(content); | 
|             } | 
|             damageContentList.Clear(); | 
|         } | 
|     } | 
|   | 
|     protected override void OnPreClose() | 
|     { | 
|         base.OnPreClose(); | 
|         EventBroadcast.Instance.RemoveListener<BattleDmgInfo>(EventName.BATTLE_DAMAGE_TAKEN, OnDamageTaken); | 
|         EventBroadcast.Instance.RemoveListener<string, JsonData>(EventName.BATTLE_END, OnBattleEnd); | 
|     } | 
|   | 
|     protected override void OnOpen() | 
|     { | 
|         base.OnOpen(); | 
|     } | 
|   | 
|     protected override void OnClose() | 
|     { | 
|         base.OnClose(); | 
|         battleField.OnBattlePause -= OnBattlePause; | 
|         battleField = null; | 
|     } | 
|   | 
|     protected override void NextFrameAfterOpen() | 
|     { | 
|         base.NextFrameAfterOpen(); | 
|     } | 
|   | 
|     protected override void CompleteClose() | 
|     { | 
|         base.CompleteClose(); | 
|     } | 
|   | 
|     private void RemoveDamageContent(DamageContent content) | 
|     { | 
|         damageContentList.Remove(content); | 
|         damagePrefabPool.Release(content.gameObject); | 
|     } | 
|   | 
|     private void OnDamageTaken(BattleDmgInfo damageInfo) | 
|     { | 
|         GameObject damageContent = damagePrefabPool.Request(); | 
|         DamageContent content = damageContent.GetComponent<DamageContent>(); | 
|         damageContent.transform.SetParent(damageNode, false); | 
|         damageContent.transform.localPosition = new Vector3(damageContent.transform.localPosition.x, damageContent.transform.localPosition.y, 0); | 
|         content.SetDamage(damageInfo, () => RemoveDamageContent(content)); | 
|         damageContentList.Add(content); | 
|   | 
|         var heroRect = damageInfo.hurtObj.heroRectTrans; | 
|         if (heroRect == null) | 
|             return; | 
|   | 
|         var contentRect = content.GetComponent<RectTransform>(); | 
|         var contentParentRect = contentRect.parent as RectTransform; | 
|   | 
|         // 获取 heroRect 的世界坐标(锚点为中心) | 
|         Vector3 worldTargetPos = heroRect.transform.TransformPoint(heroRect.rect.center); | 
|   | 
|         // 转换到 content 父节点下的 anchoredPosition | 
|         Vector2 anchoredPos; | 
|         RectTransformUtility.ScreenPointToLocalPointInRectangle( | 
|             contentParentRect, | 
|             RectTransformUtility.WorldToScreenPoint(null, worldTargetPos), | 
|             null, | 
|             out anchoredPos); | 
|   | 
|         contentRect.anchoredPosition = anchoredPos; | 
|     } | 
|   | 
|     public void SetBattleField(BattleField _battleField) | 
|     { | 
|         if (battleField != null) | 
|         { | 
|             battleField.OnBattlePause -= OnBattlePause; | 
|         } | 
|         ClearContent(string.Empty, true); | 
|         battleField = _battleField; | 
|         battleField.OnBattlePause += OnBattlePause; | 
|     } | 
|   | 
|     private void OnBattlePause(bool isPause) | 
|     { | 
|         //  游戏暂停 | 
|         if (isPause) | 
|         { | 
|             foreach (var content in damageContentList) | 
|             { | 
|                 content.Stop(); | 
|             } | 
|         } | 
|         //  游戏恢复 | 
|         else | 
|         { | 
|             foreach (var content in damageContentList) | 
|             { | 
|                 content.Resume(); | 
|             } | 
|         } | 
|     } | 
| } |