HeroHpWidget.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Common.Utility.CombatEvent;
  2. using Fort23.Core;
  3. using Fort23.UTool;
  4. using UnityEngine;
  5. namespace Fort23.Mono
  6. {
  7. [UIBinding(prefab = "HeroHpWidget")]
  8. public partial class HeroHpWidget : UIComponent
  9. {
  10. public CombatHeroEntity combatHeroEntity;
  11. private Transform hpTransform;
  12. public int size=100;
  13. public int shieldSize=50;
  14. public bool isFollowTarget=true;
  15. private void Init()
  16. {
  17. }
  18. public override void AddEvent()
  19. {
  20. }
  21. public override void DelEvent()
  22. {
  23. }
  24. public override void AddButtonEvent()
  25. {
  26. }
  27. public override void DormancyObj()
  28. {
  29. CombatEventManager.Instance.RemoveEventListener(CombatEventType.HeroHpUpdate, HeroHpUpdateEventData);
  30. CombatEventManager.Instance.RemoveEventListener(CombatEventType.ClearHeroHp, ClearHeroHp);
  31. CombatEventManager.Instance.RemoveEventListener(CombatEventType.HeroDie, HeroDie);
  32. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  33. base.DormancyObj();
  34. }
  35. private void HeroHpUpdateEventData(IEventData iEventData)
  36. {
  37. HeroHpUpdateEventData heroHpUpdateEventData = iEventData as HeroHpUpdateEventData;
  38. if (heroHpUpdateEventData.combatHeroEntity == combatHeroEntity)
  39. {
  40. UpdateHp();
  41. }
  42. }
  43. private void UpdateHp()
  44. {
  45. float v = (combatHeroEntity.CurrCombatHeroInfo.hp.Value * 1f) /
  46. combatHeroEntity.MaxCombatHeroInfo.hp.Value;
  47. v = Mathf.Clamp(v, 0, 1);
  48. // if (v < 0.99f && !transform.gameObject.activeSelf)
  49. // {
  50. // transform.gameObject.SetActive(true);
  51. // }
  52. hp.rectTransform.sizeDelta = new Vector2(v * size, hp.rectTransform.sizeDelta.y);
  53. float v2 = (combatHeroEntity.CurrCombatHeroInfo.Shield.Value * 1f) /
  54. combatHeroEntity.MaxCombatHeroInfo.Shield.Value;
  55. v2 = Mathf.Clamp(v2, 0, 1);
  56. shield.rectTransform.sizeDelta = new Vector2(v2 * shieldSize, shield.rectTransform.sizeDelta.y);
  57. }
  58. private void ClearHeroHp(IEventData iEventData)
  59. {
  60. HeroHpUpdateEventData heroHpUpdateEventData = iEventData as HeroHpUpdateEventData;
  61. if (heroHpUpdateEventData.combatHeroEntity == combatHeroEntity)
  62. {
  63. GObjectPool.Instance.Recycle(this);
  64. }
  65. }
  66. private void HeroDie(IEventData iEventData)
  67. {
  68. HeroDieEventData heroHpUpdateEventData = iEventData as HeroDieEventData;
  69. if (heroHpUpdateEventData.combatHeroEntity == combatHeroEntity && combatHeroEntity.IsEnemy)
  70. {
  71. GObjectPool.Instance.Recycle(this);
  72. }
  73. }
  74. public void Init(CombatHeroEntity combatHeroEntity)
  75. {
  76. this.combatHeroEntity = combatHeroEntity;
  77. CombatEventManager.Instance.AddEventListener(CombatEventType.HeroHpUpdate, HeroHpUpdateEventData);
  78. CombatEventManager.Instance.AddEventListener(CombatEventType.ClearHeroHp, ClearHeroHp);
  79. CombatEventManager.Instance.AddEventListener(CombatEventType.HeroDie, HeroDie);
  80. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  81. hpTransform = combatHeroEntity.combatHeroGameObject.hpTransform;
  82. // transform.gameObject.SetActive(false);
  83. jy.SetActive(false);
  84. // size = 100;
  85. //
  86. // bg.sizeDelta = new Vector2(size, hp.rectTransform.sizeDelta.y);
  87. // hp.rectTransform.sizeDelta = new Vector2(size, hp.rectTransform.sizeDelta.y);
  88. // bg.anchoredPosition = new Vector2(-size / 2, 0);
  89. // hp.rectTransform.anchoredPosition = new Vector2(-size / 2, 0);
  90. if (combatHeroEntity.CurrCombatHeroInfo.Shield > 0)
  91. {
  92. ShieldsRoot.SetActive(true);
  93. }
  94. else
  95. {
  96. ShieldsRoot.SetActive(false);
  97. }
  98. UpdateHp();
  99. Update();
  100. }
  101. private void Update()
  102. {
  103. if (!isFollowTarget)
  104. {
  105. return;
  106. }
  107. Vector3 worldPos = hpTransform.position;
  108. Vector3 p = UIManager.Instance.CurrCustomCameraStack.camera.WorldToScreenPoint(worldPos);
  109. Vector3 p2 = UIManager.Instance.UICamera.ScreenToWorldPoint(p);
  110. transform.position = p2;
  111. }
  112. }
  113. }