FighterHPBar.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class FighterHPBar
  5. {
  6. public const string HUD_HERO_PREFAB = "Battle/HPBar";
  7. private GameObject mHud;
  8. private RectTransform mItemRt = null;
  9. private GameObject mBloodGo;
  10. private Image mBloodImage;
  11. private Vector3 m_bloodPos;
  12. private Fighter mFighter = null;
  13. private bool m_bloodDirty = false;
  14. private bool m_hudLogicVisible = false;
  15. public void Born(Fighter owner)
  16. {
  17. mFighter = owner;
  18. mHud = null;
  19. mBloodImage = null;
  20. mItemRt = null;
  21. m_bloodPos = Vector3.zero;
  22. }
  23. public void Dispose()
  24. {
  25. if (mHud != null)
  26. {
  27. ResourceMgr.Instance.RecycleGO(Constants.UIPath, HUD_HERO_PREFAB, mHud);
  28. }
  29. mItemRt = null;
  30. mHud = null;
  31. mBloodImage = null;
  32. m_bloodPos = Vector3.zero;
  33. }
  34. public void Destroy()
  35. {
  36. Dispose();
  37. }
  38. public void Prepare()
  39. {
  40. InitHudUI();
  41. mHud.SetActive(false);
  42. }
  43. public void Fight()
  44. {
  45. #if UNITY_EDITOR
  46. if (mHud)
  47. {
  48. mHud.name = mFighter.Ctrl.name + "_hpbar";
  49. }
  50. #endif
  51. ResetHudUI();
  52. }
  53. public void Deactive()
  54. {
  55. setHudLogicVisible(false);
  56. }
  57. public void OnActorDead()
  58. {
  59. if (null == mHud)
  60. return;
  61. mHud.SetActive(false);
  62. setHudLogicVisible(false);
  63. }
  64. public void OnActorRevive()
  65. {
  66. if (mFighter == null) return;
  67. setHudLogicVisible(false);
  68. }
  69. public void LateUpdate(float deltaTime)
  70. {
  71. if (mHud == null) return;
  72. if (mFighter == null)
  73. {
  74. ResourceMgr.Instance.RecycleGO(Constants.UIPath, HUD_HERO_PREFAB, mHud);
  75. mHud = null;
  76. return;
  77. }
  78. // 这几种情况不需要更新下面的东西,太费了
  79. if (!mFighter.IsVisible)
  80. {
  81. if (m_bloodDirty)
  82. {
  83. m_bloodDirty = false;
  84. mHud.SetActive(false);
  85. }
  86. return;
  87. }
  88. if (!BattleCamera.Instance.BattleCameraEnabled) return;
  89. Vector3 curUIPointPos = mFighter.Ctrl.GetUIPointPos();
  90. // 血条可见性需要修改
  91. if (m_bloodDirty)
  92. {
  93. m_bloodDirty = false;
  94. bool result = m_hudLogicVisible && mFighter.IsVisible && mFighter.IsAlive && mFighter.IsFighting;
  95. mHud.SetActive(result);
  96. }
  97. // Update Hud
  98. //if (mHud.activeSelf)
  99. {
  100. Vector3 hudPosition = curUIPointPos + mFighter.Actor.AvatarData.bloodBarPos;
  101. Vector3 curBloodPos = Camera.main.WorldToViewportPoint(hudPosition);
  102. bool isNeedUpdateUIHud = (m_bloodPos != curBloodPos);
  103. m_bloodPos = curBloodPos;
  104. if (isNeedUpdateUIHud)
  105. {
  106. UpdateUIHud(ref curBloodPos);
  107. }
  108. }
  109. }
  110. public void OnLifeChanged(long life, long maxLife)
  111. {
  112. if (life < maxLife)
  113. setHudLogicVisible(true);
  114. UpdateBloodBar(life, maxLife);
  115. }
  116. private void setHudLogicVisible(bool isVisible)
  117. {
  118. m_hudLogicVisible = isVisible;
  119. refreshHudVisible();
  120. }
  121. private void refreshHudVisible()
  122. {
  123. if (null != mHud)
  124. {
  125. bool result = m_hudLogicVisible && mFighter.IsVisible && mFighter.IsFighting;
  126. if (result != mHud.activeSelf)
  127. {
  128. m_bloodDirty = true;
  129. }
  130. }
  131. }
  132. ///-------------------------------------------------
  133. /// 初始化Hud UI
  134. ///-------------------------------------------------
  135. private void InitHudUI()
  136. {
  137. if (mFighter == null)
  138. return;
  139. if (mHud == null)
  140. {
  141. mHud = ResourceMgr.Instance.GetGoFromPool(Constants.UIPath, HUD_HERO_PREFAB);
  142. DebugHelper.Assert(mHud != null, "wtf?");
  143. if (mHud == null)
  144. {
  145. return;
  146. }
  147. Transform hudPanel = BattleFlyWordMgr.Instance.HudRootTrans;
  148. if (hudPanel != null)
  149. {
  150. mHud.transform.SetParent(hudPanel, true);
  151. mHud.transform.localScale = Vector3.one;
  152. mHud.transform.localRotation = Quaternion.identity;
  153. }
  154. RectTransform rt = mHud.GetComponent<RectTransform>();
  155. rt.anchorMin = Vector2.zero;
  156. rt.anchorMax = Vector2.one;
  157. rt.anchoredPosition3D = Vector3.zero;
  158. rt.offsetMin = rt.offsetMax = Vector2.zero;
  159. Transform item = mHud.transform.Find("Item");
  160. mItemRt = item.GetComponent<RectTransform>();
  161. mBloodGo = item.Find("HP").gameObject;
  162. mBloodImage = item.Find("HP/bloodImg").GetComponent<Image>();
  163. DebugHelper.Assert(mBloodImage != null);
  164. if (mBloodImage != null)
  165. {
  166. if (mFighter.Life > 0)
  167. {
  168. mBloodImage.fillAmount = ((float)mFighter.Life / (float)mFighter.MaxLife);
  169. }
  170. else
  171. {
  172. mBloodImage.fillAmount = 0;
  173. }
  174. }
  175. }
  176. setHudLogicVisible(false);
  177. }
  178. private void ResetHudUI()
  179. {
  180. if (mHud == null || mFighter == null || mBloodImage == null)
  181. {
  182. return;
  183. }
  184. if (mFighter.Life > 0)
  185. {
  186. mBloodImage.fillAmount = ((float)mFighter.Life / (float)mFighter.MaxLife);
  187. }
  188. else
  189. {
  190. mBloodImage.fillAmount = 0;
  191. }
  192. mBloodGo.SetActive(true);
  193. //添加到对应的Panel
  194. Transform hudPanel = BattleFlyWordMgr.Instance.HudRootTrans;
  195. if (hudPanel != null)
  196. {
  197. mHud.transform.SetParent(hudPanel, true);
  198. mHud.transform.localScale = Vector3.one;
  199. mHud.transform.localRotation = Quaternion.identity;
  200. }
  201. setHudLogicVisible(true);
  202. }
  203. ///-------------------------------------------------
  204. /// 更新UI Hud
  205. ///-------------------------------------------------
  206. private void UpdateUIHud(ref Vector3 bloodPosition)
  207. {
  208. if (mItemRt != null)
  209. {
  210. bloodPosition.Set(bloodPosition.x, bloodPosition.y, bloodPosition.z);
  211. if (CameraMgr.Instance.UICamera == null)
  212. {
  213. return;
  214. }
  215. mItemRt.anchoredPosition3D = new Vector3((bloodPosition.x - 0.5f) * UIMgr.SCREEN_WIDTH, (bloodPosition.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0);
  216. }
  217. }
  218. ///-------------------------------------------------
  219. /// 更新血条值
  220. ///-------------------------------------------------
  221. private void UpdateBloodBar(long curValue, long maxValue)
  222. {
  223. if (mHud != null && maxValue != 0)
  224. {
  225. if ((curValue == 0 || curValue >= maxValue) && mFighter.IsDisposed)
  226. {
  227. setHudLogicVisible(false);
  228. }
  229. else
  230. {
  231. setHudLogicVisible(true);
  232. if (mBloodImage != null)
  233. {
  234. float lastVal = mBloodImage.fillAmount;
  235. mBloodImage.fillAmount = ((float)curValue / (float)maxValue);
  236. }
  237. }
  238. }
  239. }
  240. }