HeroHpWidget.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. private void Init()
  13. {
  14. }
  15. public override void AddEvent()
  16. {
  17. }
  18. public override void DelEvent()
  19. {
  20. }
  21. public override void AddButtonEvent()
  22. {
  23. }
  24. public override void DormancyObj()
  25. {
  26. CombatEventManager.Instance.RemoveEventListener(CombatEventType.HeroHpUpdate, HeroHpUpdateEventData);
  27. CombatEventManager.Instance.RemoveEventListener(CombatEventType.HeroDie, HeroDie);
  28. StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update);
  29. base.DormancyObj();
  30. }
  31. private void HeroHpUpdateEventData(IEventData iEventData)
  32. {
  33. HeroHpUpdateEventData heroHpUpdateEventData = iEventData as HeroHpUpdateEventData;
  34. if (heroHpUpdateEventData.combatHeroEntity == combatHeroEntity)
  35. {
  36. UpdateHp();
  37. }
  38. }
  39. private void UpdateHp()
  40. {
  41. float v = (combatHeroEntity.CurrCombatHeroInfo.hp.Value * 1f) /
  42. combatHeroEntity.MaxCombatHeroInfo.hp.Value;
  43. v = Mathf.Clamp(v, 0, 1);
  44. hp.rectTransform.sizeDelta = new Vector2(v * 60, hp.rectTransform.sizeDelta.y);
  45. }
  46. private void HeroDie(IEventData iEventData)
  47. {
  48. HeroDieEventData heroDieEventData = iEventData as HeroDieEventData;
  49. if (heroDieEventData.combatHeroEntity == combatHeroEntity)
  50. {
  51. GObjectPool.Instance.Recycle(this);
  52. }
  53. }
  54. public void Init(CombatHeroEntity combatHeroEntity)
  55. {
  56. this.combatHeroEntity = combatHeroEntity;
  57. CombatEventManager.Instance.AddEventListener(CombatEventType.HeroHpUpdate, HeroHpUpdateEventData);
  58. CombatEventManager.Instance.AddEventListener(CombatEventType.HeroDie, HeroDie);
  59. StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
  60. hpTransform = combatHeroEntity.combatHeroGameObject.GameObjectPool.own.transform.Find("hp");
  61. if (hpTransform == null)
  62. {
  63. hpTransform = combatHeroEntity.combatHeroGameObject.GameObjectPool.own.transform;
  64. }
  65. if (combatHeroEntity.IsEnemy)
  66. {
  67. hp.color = new Color(0.56f, 0f, 0f);
  68. }
  69. else
  70. {
  71. hp.color = new Color(0.06f, 0.56f, 0.06f);
  72. }
  73. UpdateHp();
  74. }
  75. private void Update()
  76. {
  77. Vector3 worldPos = hpTransform.position;
  78. Vector3 p = UIManager.Instance.CurrCustomCameraStack.camera.WorldToScreenPoint(worldPos);
  79. Vector3 p2 = UIManager.Instance.UICamera.ScreenToWorldPoint(p);
  80. transform.position = p2;
  81. }
  82. }
  83. }