SkillName2D.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class SkillName2D
  5. {
  6. public const string HUD_SKILLNAME_PREFAB = "Battle/SkillNameText";
  7. private Text mSkillName;
  8. private GameObject mHud;
  9. private RectTransform mItemRt = null;
  10. //引导条
  11. private GameObject mCastingGo;
  12. private Image mCastingAdd;
  13. private Image mCastingImage;
  14. private GameObject mBreakGo;
  15. private GameObject mSingGo;
  16. private Image mSingImage;
  17. private Animator mAnimator = null;
  18. private Fighter mFighter = null;
  19. private int mHideBreakTimer = 0;
  20. private int mAutoTimer = 0;
  21. private Vector3 m_skillNamePos;
  22. const float c_miniShowTime = 1.0f;
  23. public void Born(Fighter owner)
  24. {
  25. mFighter = owner;
  26. mHud = null;
  27. mItemRt = null;
  28. }
  29. bool registered = false;
  30. void RegisterEvents()
  31. {
  32. if (registered) return;
  33. registered = true;
  34. //EventMgr.AddEventListener<Fighter>(ECoreEventType.EID_FIGHTER_DO_SKILL, OnShowSkillName);
  35. }
  36. void UnRegisterEvents()
  37. {
  38. registered = false;
  39. //EventMgr.RemoveEventListener<Fighter>(ECoreEventType.EID_FIGHTER_DO_SKILL, OnShowSkillName);
  40. }
  41. public void Dispose()
  42. {
  43. if (mHud != null)
  44. {
  45. ResourceMgr.Instance.RecycleGO(Constants.UIPath, HUD_SKILLNAME_PREFAB, mHud);
  46. }
  47. m_skillNamePos = Vector3.zero;
  48. mHud = null;
  49. mItemRt = null;
  50. if (mHideBreakTimer > 0)
  51. {
  52. TimerManager.Instance.RemoveTimer(mHideBreakTimer);
  53. mHideBreakTimer = 0;
  54. }
  55. RemoveAutoTimer();
  56. mItemRt = null;
  57. mHud = null;
  58. mSingImage = null;
  59. mCastingGo = null;
  60. mCastingAdd = null;
  61. mCastingImage = null;
  62. mAnimator = null;
  63. }
  64. public void Destroy()
  65. {
  66. UnRegisterEvents();
  67. Dispose();
  68. }
  69. public void Prepare()
  70. {
  71. InitHudUI();
  72. }
  73. public void Fight()
  74. {
  75. #if UNITY_EDITOR
  76. if (mHud)
  77. {
  78. mHud.name = mFighter.Ctrl.name + "_skillName";
  79. }
  80. #endif
  81. ResetHudUI();
  82. RegisterEvents();
  83. }
  84. public void Deactive()
  85. {
  86. SetVisible(false);
  87. }
  88. public void OnActorDead()
  89. {
  90. if (mFighter == null) return;
  91. UnRegisterEvents();
  92. SetVisible(false);
  93. }
  94. public void OnActorRevive()
  95. {
  96. if (mFighter == null) return;
  97. RegisterEvents();
  98. SetVisible(false);
  99. }
  100. public void SetVisible(bool vis)
  101. {
  102. if (mHud == null) return;
  103. mHud.SetActive(vis);
  104. }
  105. public void LateUpdate(float deltaTime)
  106. {
  107. if (mHud == null) return;
  108. if (mFighter == null)
  109. {
  110. Dispose();
  111. return;
  112. }
  113. // 这几种情况不需要更新下面的东西,太费了
  114. if (!mFighter.IsVisible)
  115. {
  116. return;
  117. }
  118. // Update Hud
  119. if (mHud.activeSelf && Camera.main != null && Camera.main.enabled)
  120. {
  121. Vector3 curSkillNamePos = mFighter.Ctrl.GetUIPointPos() + mFighter.Actor.AvatarData.skillNamePos;
  122. Vector3 pos = Camera.main.WorldToViewportPoint(curSkillNamePos);
  123. bool isNeedUpdateUIHud = (m_skillNamePos != pos);
  124. if (isNeedUpdateUIHud && mItemRt != null)
  125. {
  126. mItemRt.anchoredPosition3D = new Vector3((pos.x - 0.5f) * UIMgr.SCREEN_WIDTH, (pos.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0); ;
  127. m_skillNamePos = pos;
  128. }
  129. if (curentCastingTime > 0 && mCastingImage != null && BattleMgr.Instance.Battle.IsFighting)
  130. {
  131. mCastingImage.fillAmount = curentCastingTime / totalCastingTime;
  132. if (mCastingAdd != null)
  133. mCastingAdd.fillAmount = mCastingImage.fillAmount;
  134. curentCastingTime -= deltaTime;
  135. }
  136. }
  137. }
  138. ///-------------------------------------------------
  139. /// 初始化Hud UI
  140. ///-------------------------------------------------
  141. private void InitHudUI()
  142. {
  143. if (mFighter == null)
  144. return;
  145. if (mHud == null)
  146. {
  147. mHud = ResourceMgr.Instance.GetGoFromPool(Constants.UIPath, HUD_SKILLNAME_PREFAB);
  148. DebugHelper.Assert(mHud != null, "wtf?");
  149. if (mHud == null)
  150. {
  151. return;
  152. }
  153. Transform hudPanel = BattleFlyWordMgr.Instance.HudRootTrans;
  154. if (hudPanel != null)
  155. {
  156. mHud.transform.SetParent(hudPanel, true);
  157. mHud.transform.localScale = Vector3.one;
  158. mHud.transform.localRotation = Quaternion.identity;
  159. }
  160. RectTransform rt = mHud.GetComponent<RectTransform>();
  161. rt.anchorMin = Vector2.zero;
  162. rt.anchorMax = Vector2.one;
  163. rt.anchoredPosition3D = Vector3.zero;
  164. rt.offsetMin = rt.offsetMax = Vector2.zero;
  165. Transform item = mHud.transform.Find("Item");
  166. mAnimator = item.GetComponent<Animator>();
  167. mAnimator.logWarnings = false;
  168. mItemRt = item.GetComponent<RectTransform>();
  169. mSkillName = item.Find("name").GetComponent<Text>();
  170. mSingGo = item.Find("Sing").gameObject;
  171. mSingImage = item.getGameObject("Sing/singImg").GetComponent<Image>();
  172. mSingGo.SetActive(false);
  173. mCastingGo = item.Find("Working").gameObject;
  174. mCastingImage = item.getGameObject("Working/workingImg").GetComponent<Image>();
  175. mCastingAdd = item.getGameObject("Working/workingAdd").GetComponent<Image>();
  176. mCastingGo.SetActive(false);
  177. mBreakGo = item.Find("Broken").gameObject;
  178. mBreakGo.SetActive(false);
  179. }
  180. SetVisible(false);
  181. }
  182. private void ResetHudUI()
  183. {
  184. if (mHud == null || mFighter == null)
  185. {
  186. return;
  187. }
  188. Transform hudPanel = BattleFlyWordMgr.Instance.HudRootTrans;
  189. if (hudPanel != null)
  190. {
  191. mHud.transform.SetParent(hudPanel, true);
  192. mHud.transform.localScale = Vector3.one;
  193. mHud.transform.localRotation = Quaternion.identity;
  194. }
  195. SetVisible(false);
  196. }
  197. private void OnShowSkillName(CoreEvent<Fighter> ce)
  198. {
  199. Fighter fighter = ce.Data;
  200. if (fighter != mFighter) return;
  201. OnShowSkillName(ce.Param.ToString());
  202. }
  203. public void OnShowSkillName(string skillName)
  204. {
  205. RemoveAutoTimer();
  206. mSkillName.text = skillName;
  207. SetVisible(true);
  208. mAutoTimer = TimerManager.Instance.AddTimer((int)(c_miniShowTime * 1000), 1, OnHideSkillNameTimer);
  209. LateUpdate(0);
  210. }
  211. private void OnHideSkillNameTimer(int seq)
  212. {
  213. SetVisible(false);
  214. mAutoTimer = 0;
  215. }
  216. public void OnBeginSing(float totalTime)
  217. {
  218. if (totalTime > c_miniShowTime )
  219. RemoveAutoTimer();
  220. mSingGo.SetActive(true);
  221. mCastingGo.SetActive(false);
  222. mBreakGo.SetActive(false);
  223. }
  224. public void OnUpdateSing(float totalTime, float leftTime)
  225. {
  226. if (mSingImage != null)
  227. {
  228. mSingImage.fillAmount = (totalTime - leftTime) / totalTime;
  229. }
  230. }
  231. public void OnEndSing()
  232. {
  233. mSingGo.SetActive(false);
  234. if (mAutoTimer == 0)
  235. SetVisible(false);
  236. }
  237. float totalCastingTime = 0;
  238. float curentCastingTime = 0;
  239. public void OnCasting(float totalTime, bool vis)
  240. {
  241. if(totalTime > c_miniShowTime)
  242. RemoveAutoTimer();
  243. if (mAnimator != null)
  244. {
  245. if (vis)
  246. {
  247. mCastingImage.fillAmount = 1;
  248. mCastingAdd.fillAmount = 1;
  249. totalCastingTime = totalTime;
  250. curentCastingTime = totalCastingTime;
  251. mAnimator.Play("Working");
  252. }
  253. else
  254. {
  255. if(mAutoTimer == 0)
  256. SetVisible(false);
  257. }
  258. }
  259. mCastingGo.SetActive(vis);
  260. }
  261. public void OnSkillBreak()
  262. {
  263. if (mAnimator == null) return;
  264. if (mHideBreakTimer == 0) return;//已经打断
  265. if (mCastingGo.activeSelf || mSingGo.activeSelf)
  266. {
  267. mBreakGo.SetActive(true);
  268. mAnimator.Play("Broken");
  269. mHideBreakTimer = TimerManager.Instance.AddTimer(1000, 1, OnHideBreakGo);
  270. }
  271. }
  272. private void OnHideBreakGo(int timerSequence)
  273. {
  274. mHideBreakTimer = 0;
  275. if (null != mBreakGo)
  276. mBreakGo.SetActive(false);
  277. }
  278. private void RemoveAutoTimer()
  279. {
  280. if (mAutoTimer > 0) {
  281. TimerManager.Instance.RemoveTimer(mAutoTimer);
  282. mAutoTimer = 0;
  283. }
  284. }
  285. }