HeroHpWidget.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Common.Utility.CombatEvent;
  2. using Core.Language;
  3. using Fort23.Core;
  4. using Fort23.UTool;
  5. using GameLogic.Combat.Buff;
  6. using UnityEngine;
  7. namespace Fort23.Mono
  8. {
  9. [UIBinding(prefab = "HeroHpWidget")]
  10. public partial class HeroHpWidget : UIComponent
  11. {
  12. public CombatHeroEntity combatHeroEntity;
  13. private Transform hpTransform;
  14. public int size = 100;
  15. public int shieldSize = 50;
  16. public bool isFollowTarget = true;
  17. private BetterList<BuffWidget> buBetterList = new BetterList<BuffWidget>();
  18. private void Init()
  19. {
  20. }
  21. public override void AddEvent()
  22. {
  23. }
  24. public override void DelEvent()
  25. {
  26. }
  27. public override void AddButtonEvent()
  28. {
  29. }
  30. public override void DormancyObj()
  31. {
  32. CombatEventManager.Instance.RemoveEventListener(CombatEventType.HeroHpUpdate, HeroHpUpdateEventData);
  33. CombatEventManager.Instance.RemoveEventListener(CombatEventType.ClearHeroHp, ClearHeroHp);
  34. CombatEventManager.Instance.RemoveEventListener(CombatEventType.HeroDie, HeroDie);
  35. CombatEventManager.Instance.RemoveEventListener(CombatEventType.AddBuff,
  36. AddBuff);
  37. CombatEventManager.Instance.RemoveEventListener(CombatEventType.RemoveBuff,
  38. RemoveBuff);
  39. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  40. for (int i = 0; i < buBetterList.Count; i++)
  41. {
  42. UIManager.Instance.DormancyGComponent(buBetterList[i]);
  43. }
  44. buBetterList.Clear();
  45. base.DormancyObj();
  46. }
  47. private async void RemoveBuff(IEventData iEventData)
  48. {
  49. BuffEventData buffEventData = iEventData as BuffEventData;
  50. if (buffEventData.target == combatHeroEntity)
  51. {
  52. for (int i = 0; i < buBetterList.Count; i++)
  53. {
  54. BuffWidget buffWidget = buBetterList[i];
  55. if (buffWidget.buffBasic == buffEventData.BuffBasic)
  56. {
  57. UIManager.Instance.DormancyGComponent(buffWidget);
  58. buBetterList.RemoveAt(i);
  59. }
  60. }
  61. }
  62. }
  63. private async void AddBuff(IEventData iEventData)
  64. {
  65. BuffEventData buffEventData = iEventData as BuffEventData;
  66. if (buffEventData.target == combatHeroEntity)
  67. {
  68. BuffBasic buffBasic = buffEventData.BuffBasic;
  69. BuffWidget buffWidget = await UIManager.Instance.CreateGComponent<BuffWidget>(null, buffRoot);
  70. buffWidget.InitBuff(buffBasic);
  71. buBetterList.Add(buffWidget);
  72. }
  73. }
  74. private void HeroHpUpdateEventData(IEventData iEventData)
  75. {
  76. HeroHpUpdateEventData heroHpUpdateEventData = iEventData as HeroHpUpdateEventData;
  77. if (heroHpUpdateEventData.combatHeroEntity == combatHeroEntity)
  78. {
  79. UpdateHp();
  80. }
  81. }
  82. private void UpdateHp()
  83. {
  84. float v = (combatHeroEntity.CurrCombatHeroInfo.hp.Value * 1f) /
  85. combatHeroEntity.MaxCombatHeroInfo.hp.Value;
  86. v = Mathf.Clamp(v, 0, 1);
  87. // if (v < 0.99f && !transform.gameObject.activeSelf)
  88. // {
  89. // transform.gameObject.SetActive(true);
  90. // }
  91. hp.rectTransform.sizeDelta = new Vector2(v * size, hp.rectTransform.sizeDelta.y);
  92. if (combatHeroEntity.MaxCombatHeroInfo.Shield.Value > 0)
  93. {
  94. float v2 = (combatHeroEntity.CurrCombatHeroInfo.Shield.Value * 1f) /
  95. combatHeroEntity.MaxCombatHeroInfo.Shield.Value;
  96. v2 = Mathf.Clamp(v2, 0, 1);
  97. shield.rectTransform.sizeDelta = new Vector2(v2 * shieldSize, shield.rectTransform.sizeDelta.y);
  98. }
  99. else
  100. {
  101. shield.rectTransform.sizeDelta = new Vector2(0, shield.rectTransform.sizeDelta.y);
  102. }
  103. }
  104. private void ClearHeroHp(IEventData iEventData)
  105. {
  106. HeroHpUpdateEventData heroHpUpdateEventData = iEventData as HeroHpUpdateEventData;
  107. if (heroHpUpdateEventData.combatHeroEntity == combatHeroEntity)
  108. {
  109. if (string.IsNullOrEmpty(poolObjName))
  110. {
  111. return;
  112. }
  113. GObjectPool.Instance.Recycle(this);
  114. }
  115. }
  116. private void HeroDie(IEventData iEventData)
  117. {
  118. HeroDieEventData heroHpUpdateEventData = iEventData as HeroDieEventData;
  119. if (heroHpUpdateEventData.combatHeroEntity == combatHeroEntity)
  120. {
  121. for (int i = 0; i < buBetterList.Count; i++)
  122. {
  123. UIManager.Instance.DormancyGComponent(buBetterList[i]);
  124. }
  125. buBetterList.Clear();
  126. if (string.IsNullOrEmpty(poolObjName))
  127. {
  128. return;
  129. }
  130. GObjectPool.Instance.Recycle(this);
  131. }
  132. }
  133. public void Init(CombatHeroEntity combatHeroEntity)
  134. {
  135. this.combatHeroEntity = combatHeroEntity;
  136. CombatEventManager.Instance.AddEventListener(CombatEventType.HeroHpUpdate, HeroHpUpdateEventData);
  137. CombatEventManager.Instance.AddEventListener(CombatEventType.ClearHeroHp, ClearHeroHp);
  138. CombatEventManager.Instance.AddEventListener(CombatEventType.HeroDie, HeroDie);
  139. CombatEventManager.Instance.AddEventListener(CombatEventType.AddBuff,
  140. AddBuff);
  141. CombatEventManager.Instance.AddEventListener(CombatEventType.RemoveBuff,
  142. RemoveBuff);
  143. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  144. hpTransform = combatHeroEntity.combatHeroGameObject.hpTransform;
  145. // transform.gameObject.SetActive(false);
  146. jy.SetActive(false);
  147. if (playerLevel != null)
  148. {
  149. if (combatHeroEntity.IsEnemy)
  150. {
  151. playerLevel.text =
  152. LanguageManager.Instance.Text(combatHeroEntity.CurrCombatHeroInfo.MonsterPowerUpConfig.jingjie1)
  153. + LanguageManager.Instance.Text(combatHeroEntity.CurrCombatHeroInfo.MonsterPowerUpConfig
  154. .jingjie2)
  155. + LanguageManager.Instance.Text(combatHeroEntity.CurrCombatHeroInfo.MonsterPowerUpConfig
  156. .jingjie3);
  157. }
  158. else
  159. {
  160. playerLevel.text =
  161. LanguageManager.Instance.Text(combatHeroEntity.CurrCombatHeroInfo.powerUpConfig.jingjie1)
  162. + LanguageManager.Instance.Text(combatHeroEntity.CurrCombatHeroInfo.powerUpConfig.jingjie2)
  163. + LanguageManager.Instance.Text(combatHeroEntity.CurrCombatHeroInfo.powerUpConfig.jingjie3);
  164. }
  165. }
  166. if (combatHeroEntity.CurrCombatHeroInfo.Shield > 0)
  167. {
  168. ShieldsRoot.SetActive(true);
  169. }
  170. else
  171. {
  172. ShieldsRoot.SetActive(false);
  173. }
  174. UpdateHp();
  175. Update();
  176. }
  177. private void Update()
  178. {
  179. for (int i = 0; i < buBetterList.Count; i++)
  180. {
  181. buBetterList[i].Update();
  182. }
  183. if (!isFollowTarget)
  184. {
  185. return;
  186. }
  187. Vector3 worldPos = hpTransform.position;
  188. Vector3 p = UIManager.Instance.CurrCustomCameraStack.camera.WorldToScreenPoint(worldPos);
  189. Vector3 p2 = UIManager.Instance.UICamera.ScreenToWorldPoint(p);
  190. transform.position = p2;
  191. }
  192. }
  193. }