BattleFloatTextComponent.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. //普攻 技能伤害、白字;暴击伤害、黄字+红底;我方、红字;我方治疗、绿字【直接往上效果】
  5. //多段总伤害【直接往上、慢】这个技能的累积伤害
  6. public class BattleFloatTextComponent : MonoBehaviour
  7. {
  8. public Animator anim;
  9. public Transform iconTrans;
  10. public float space = 0.3f;
  11. //Sprite3D critBg = null;
  12. //Sprite3D[] numArray = null;
  13. Image critBg = null;
  14. Text hurtNum = null;
  15. float beginShowTime = 0;
  16. Fighter caster = null;
  17. Fighter target = null;
  18. bool bDisposed = false;
  19. bool bInited = false;
  20. int hurtVal = 0;
  21. int buffId = 0;
  22. DIGIT_TYPE dightType = DIGIT_TYPE.AttackHurt;
  23. FONT_TYPE fontType = FONT_TYPE.WHITE_FONT;
  24. float duration = 1.0f;
  25. bool enableTweenAlpha = false;
  26. RectTransform rt = null;
  27. private void Start()
  28. {
  29. rt = GetComponent<RectTransform>();
  30. UpdatePos();
  31. }
  32. private void Update()
  33. {
  34. if (bDisposed || !bInited) return;
  35. UpdatePos();
  36. }
  37. private void OnDestroy()
  38. {
  39. Dispose();
  40. }
  41. /// <summary>
  42. /// 显示伤害数值
  43. /// </summary>
  44. /// <param name="hurtVal">伤害数值</param>
  45. /// <param name="fontType">字体类型</param>
  46. public void ShowHurt(Fighter caster,Fighter target,int buffId,int hurtVal, DIGIT_TYPE dightType,FONT_TYPE fontType,float duration,bool enableTweenAplha=true)
  47. {
  48. this.caster = caster;
  49. this.target = target;
  50. this.buffId = buffId;
  51. bDisposed = false;
  52. this.hurtVal = hurtVal;
  53. this.fontType = fontType;
  54. this.dightType = dightType;
  55. this.duration = duration;
  56. this.enableTweenAlpha = false;
  57. if (!bInited)
  58. {
  59. InitCom();
  60. }
  61. CalcHurt();
  62. }
  63. public bool CanMerge(FloatDigitInfo info)
  64. {
  65. if (caster == null || target == null) return false;
  66. if (info.attacker == caster &&
  67. info.target == target &&
  68. dightType == DIGIT_TYPE.MultiSkillHurtMerge &&
  69. dightType == DIGIT_TYPE.PlayerMultiSkillHurtMerget &&
  70. info.buffId == buffId) return true;
  71. return false;
  72. }
  73. public bool CheckTime()
  74. {
  75. if (target == null) return true;
  76. float passedTime = Time.realtimeSinceStartup - beginShowTime;
  77. if (passedTime >= duration)
  78. return true;
  79. if (enableTweenAlpha)
  80. {
  81. float alpha = 1 - passedTime / duration;
  82. if (critBg.isActiveAndEnabled)
  83. {
  84. Color clr = critBg.color;
  85. clr.a = alpha;
  86. critBg.color = clr;
  87. }
  88. else
  89. {
  90. Color clr = hurtNum.color;
  91. clr.a = alpha;
  92. hurtNum.color = clr;
  93. }
  94. }
  95. return false;
  96. }
  97. public void Dispose()
  98. {
  99. if(critBg!=null)
  100. critBg.gameObject.SetActive(false);
  101. target = null;
  102. bDisposed = true;
  103. caster = null;
  104. hurtVal = 0;
  105. buffId = 0;
  106. beginShowTime = 0;
  107. //hurtNum.gameObject.SetActive(false);
  108. }
  109. void InitCom()
  110. {
  111. if (bInited) return;
  112. hurtNum = iconTrans.Find("hurt").GetComponent<Text>();
  113. critBg = iconTrans.Find("critBg").GetComponent<Image>();
  114. SetCritBgVisible();
  115. bInited = true;
  116. }
  117. void CalcHurt()
  118. {
  119. iconTrans.localScale = Vector3.one;
  120. HandleHurtNum();
  121. UpdatePos();
  122. transform.localRotation = Quaternion.identity;
  123. beginShowTime = Time.realtimeSinceStartup;
  124. }
  125. void HandleHurtNum()
  126. {
  127. if(this.dightType == DIGIT_TYPE.ChangeSP && hurtVal > 0)
  128. {
  129. hurtNum.text = "+"+hurtVal.ToString();
  130. }
  131. else
  132. {
  133. hurtNum.text = hurtVal.ToString();
  134. }
  135. hurtNum.color = GetFonfColor(fontType);
  136. //hurtNum.gameObject.SetActive(true);
  137. SetCritBgVisible();
  138. }
  139. void SetCritBgVisible()
  140. {
  141. if (critBg != null)
  142. {
  143. critBg.gameObject.SetActive(this.dightType == DIGIT_TYPE.CritHurt);
  144. if (this.dightType == DIGIT_TYPE.CritHurt)
  145. {
  146. Color clr = critBg.color;
  147. clr.a = 1.0f;
  148. critBg.color = clr;
  149. }
  150. }
  151. }
  152. void UpdatePos()
  153. {
  154. if (target == null || target.Ctrl == null) return;
  155. Vector3 relativePos = target.Ctrl.GetUIPointPos() + target.Actor.AvatarData.hurtPos;
  156. Vector3 pos = BattleCamera.Instance.RealCamera.WorldToViewportPoint(relativePos);
  157. if (rt!=null)
  158. rt.anchoredPosition3D = new Vector3((pos.x - 0.5f) * UIMgr.SCREEN_WIDTH, (pos.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0);
  159. }
  160. string GetFontPrefix(FONT_TYPE fontType)
  161. {
  162. switch (fontType)
  163. {
  164. case FONT_TYPE.GREEN_FONT:
  165. return BattleFlyWordMgr.green_font_prefix;
  166. case FONT_TYPE.RED_FONT:
  167. return BattleFlyWordMgr.red_font_prefix;
  168. case FONT_TYPE.YELLOW_FONT:
  169. return BattleFlyWordMgr.yellow_font_prefix;
  170. case FONT_TYPE.WHITE_FONT:
  171. return BattleFlyWordMgr.white_font_prefix;
  172. default:
  173. return BattleFlyWordMgr.white_font_prefix;
  174. }
  175. }
  176. Color GetFonfColor(FONT_TYPE fontType)
  177. {
  178. switch (fontType)
  179. {
  180. case FONT_TYPE.GREEN_FONT:
  181. return Constants.green;
  182. case FONT_TYPE.RED_FONT:
  183. return Constants.red;
  184. case FONT_TYPE.YELLOW_FONT:
  185. return Constants.yellow;
  186. case FONT_TYPE.WHITE_FONT:
  187. return Color.white;
  188. case FONT_TYPE.SP_FONT:
  189. return Constants.spClr;
  190. default:
  191. return Color.white;
  192. }
  193. }
  194. }