HeroHpWidget.cs 9.4 KB

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