DamageNumberAni.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { _decorator, Component, Node, Label, Vec3, tween, Tween, UITransform, UIOpacity, instantiate, resources, Prefab, Color, find } from 'cc';
  2. import { EnemyController } from '../CombatSystem/EnemyController';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 伤害数字动画组件
  6. * 实现伤害数字从小变大再缩小消失的动画效果
  7. * 可挂载到EnemyController节点上,通过装饰器获取EnemyController组件
  8. */
  9. @ccclass('DamageNumberAni')
  10. export class DamageNumberAni extends Component {
  11. @property({ type: EnemyController, tooltip: 'EnemyController组件引用' })
  12. private enemyController: EnemyController = null;
  13. @property({ type: Node, tooltip: 'GameArea节点引用' })
  14. private gameArea: Node = null;
  15. @property({ type: Prefab, tooltip: 'DamageNumber预制体引用' })
  16. private damageNumberPrefab: Prefab = null;
  17. @property({ type: Prefab, tooltip: 'EnemyLabel预制体引用' })
  18. private enemyLabelPrefab: Prefab = null;
  19. // 为频繁触发的伤害数字提供左右交替的偏移,避免重叠
  20. @property({ tooltip: '伤害数字左右偏移距离(像素)' })
  21. private spawnOffsetX: number = 24;
  22. private alternatingSide: number = 1; // 1: 向右, -1: 向左
  23. onLoad() {
  24. // 自动获取同节点上的EnemyController组件
  25. if (!this.enemyController) {
  26. this.enemyController = this.getComponent(EnemyController);
  27. }
  28. // 只有在需要使用EnemyController功能时才报错
  29. // 动态创建的伤害数字节点不需要EnemyController组件
  30. if (!this.enemyController && this.damageNumberPrefab) {
  31. console.warn('[DamageNumberAni] 未找到EnemyController组件,某些功能可能不可用');
  32. }
  33. }
  34. /**
  35. * 创建并显示伤害数字
  36. * @param damage 伤害值
  37. * @param worldPosition 世界坐标位置(敌人头顶)
  38. * @param isCritical 是否暴击
  39. */
  40. public showDamageNumber(damage: number, worldPosition: Vec3, isCritical: boolean = false) {
  41. // 如果伤害为0,显示格挡文字而不是数字0
  42. if (damage === 0) {
  43. this.showBlockText(worldPosition);
  44. return;
  45. }
  46. // 检查预制体引用
  47. if (!this.damageNumberPrefab) {
  48. console.error('[DamageNumberAni] DamageNumber预制体引用未设置');
  49. return;
  50. }
  51. // 实例化伤害数字节点
  52. const damageNode = instantiate(this.damageNumberPrefab);
  53. // 使用装饰器引用的GameArea节点
  54. if (!this.gameArea) {
  55. console.error('[DamageNumberAni] GameArea节点引用未设置');
  56. damageNode.destroy();
  57. return;
  58. }
  59. // 添加到GameArea
  60. this.gameArea.addChild(damageNode);
  61. // 转换世界坐标到GameArea的本地坐标
  62. const gameAreaTransform = this.gameArea.getComponent(UITransform);
  63. if (gameAreaTransform) {
  64. const localPos = gameAreaTransform.convertToNodeSpaceAR(worldPosition);
  65. // 稍微向上偏移,显示在敌人头顶
  66. localPos.y += 30;
  67. // 为频繁伤害提供左右交替偏移,避免重叠在同一点
  68. const offset = isCritical ? this.spawnOffsetX * 1.2 : this.spawnOffsetX;
  69. localPos.x += this.alternatingSide * offset;
  70. // 切换下次生成方向
  71. this.alternatingSide = -this.alternatingSide;
  72. damageNode.position = localPos;
  73. }
  74. // 设置伤害数字文本
  75. const label = damageNode.getComponent(Label);
  76. if (label) {
  77. // 显示精确的伤害值,保留一位小数(如果有小数部分)
  78. const damageText = damage % 1 === 0 ? damage.toString() : damage.toFixed(1);
  79. label.string = damageText;
  80. // 暴击时使用不同颜色
  81. if (isCritical) {
  82. label.color = Color.YELLOW; // 暴击显示黄色
  83. label.fontSize = 30; // 暴击字体更大
  84. } else {
  85. label.color = Color.WHITE; // 普通伤害白色
  86. label.fontSize = 25;
  87. }
  88. }
  89. // 将DamageNumberAni组件添加到伤害数字节点
  90. const aniComponent = damageNode.addComponent(DamageNumberAni);
  91. aniComponent.playAnimation(isCritical);
  92. }
  93. /**
  94. * 静态方法保持兼容性,但推荐使用实例方法
  95. * @param damage 伤害值
  96. * @param worldPosition 世界坐标位置(敌人头顶)
  97. * @param isCritical 是否暴击
  98. */
  99. public static showDamageNumber(damage: number, worldPosition: Vec3, isCritical: boolean = false) {
  100. // 尝试找到EnemyController节点上的DamageNumberAni组件
  101. const enemyControllerNode = find('Canvas/GameLevelUI/EnemyController');
  102. if (enemyControllerNode) {
  103. const damageAni = enemyControllerNode.getComponent(DamageNumberAni);
  104. if (damageAni) {
  105. damageAni.showDamageNumber(damage, worldPosition, isCritical);
  106. return;
  107. }
  108. }
  109. console.warn('[DamageNumberAni] 未找到挂载的DamageNumberAni组件,请确保组件已挂载到EnemyController节点');
  110. }
  111. /**
  112. * 播放伤害数字动画
  113. * @param isCritical 是否暴击
  114. */
  115. private playAnimation(isCritical: boolean = false) {
  116. // 初始状态:缩放为0,透明度为1
  117. this.node.setScale(0, 0, 1);
  118. const uiTransform = this.node.getComponent(UITransform);
  119. const uiOpacity = this.node.getComponent(UIOpacity);
  120. if (uiOpacity) {
  121. uiOpacity.opacity = 255;
  122. }
  123. // 动画参数
  124. const maxScale = isCritical ? 1.3 : 1.0; // 暴击时放大更多
  125. const growDuration = 0.2; // 放大阶段持续时间
  126. const stayDuration = 0.3; // 停留时间
  127. const shrinkDuration = 0.4; // 缩小消失时间
  128. const floatDistance = 50; // 向上漂浮距离
  129. // 第一阶段:从0放大到最大尺寸(从底部开始放大)
  130. tween(this.node)
  131. .to(growDuration, {
  132. scale: new Vec3(maxScale, maxScale, 1)
  133. }, {
  134. easing: 'backOut' // 弹性效果
  135. })
  136. .call(() => {
  137. // 第二阶段:保持大小,开始向上漂浮并逐渐缩小透明
  138. const startPos = this.node.position.clone();
  139. const endPos = startPos.clone();
  140. endPos.y += floatDistance;
  141. // 位置动画
  142. tween(this.node)
  143. .to(stayDuration + shrinkDuration, {
  144. position: endPos
  145. }, {
  146. easing: 'sineOut'
  147. })
  148. .start();
  149. // 延迟后开始缩小和淡出
  150. tween(this.node)
  151. .delay(stayDuration)
  152. .to(shrinkDuration, {
  153. scale: new Vec3(0.3, 0.3, 1)
  154. }, {
  155. easing: 'sineIn'
  156. })
  157. .start();
  158. // 透明度动画
  159. if (uiOpacity) {
  160. tween(uiOpacity)
  161. .delay(stayDuration)
  162. .to(shrinkDuration, {
  163. opacity: 0
  164. }, {
  165. easing: 'sineIn'
  166. })
  167. .call(() => {
  168. // 动画完成,销毁节点
  169. if (this.node && this.node.isValid) {
  170. this.node.destroy();
  171. }
  172. })
  173. .start();
  174. }
  175. })
  176. .start();
  177. }
  178. /**
  179. * 显示格挡文字
  180. * @param worldPosition 世界坐标位置(敌人头顶)
  181. */
  182. public showBlockText(worldPosition: Vec3) {
  183. // 检查预制体引用
  184. if (!this.enemyLabelPrefab) {
  185. console.error('[DamageNumberAni] EnemyLabel预制体引用未设置');
  186. return;
  187. }
  188. // 实例化格挡文字节点
  189. const blockTextNode = instantiate(this.enemyLabelPrefab);
  190. // 使用装饰器引用的GameArea节点
  191. if (!this.gameArea) {
  192. console.error('[DamageNumberAni] GameArea节点引用未设置');
  193. blockTextNode.destroy();
  194. return;
  195. }
  196. // 添加到GameArea
  197. this.gameArea.addChild(blockTextNode);
  198. // 转换世界坐标到GameArea的本地坐标
  199. const gameAreaTransform = this.gameArea.getComponent(UITransform);
  200. if (gameAreaTransform) {
  201. const localPos = gameAreaTransform.convertToNodeSpaceAR(worldPosition);
  202. // 稍微向上偏移,显示在敌人头顶
  203. localPos.y += 30;
  204. blockTextNode.position = localPos;
  205. }
  206. // 设置文字内容
  207. const label = blockTextNode.getComponent(Label);
  208. if (label) {
  209. label.string = "格挡";
  210. label.color = Color.CYAN; // 使用青色显示格挡文字
  211. label.fontSize = 22;
  212. }
  213. // 将DamageNumberAni组件添加到格挡文字节点
  214. const aniComponent = blockTextNode.addComponent(DamageNumberAni);
  215. console.log("格挡",aniComponent);
  216. aniComponent.playBlockAnimation();
  217. }
  218. /**
  219. * 播放格挡文字动画
  220. */
  221. private playBlockAnimation(): void {
  222. // 初始状态:缩放为0,透明度为1
  223. this.node.setScale(0, 0, 1);
  224. const uiOpacity = this.node.getComponent(UIOpacity);
  225. if (uiOpacity) {
  226. uiOpacity.opacity = 255;
  227. }
  228. // 创建动画序列
  229. tween(this.node)
  230. .to(0.2, { scale: new Vec3(1.2, 1.2, 1) }) // 放大
  231. .to(0.1, { scale: new Vec3(1, 1, 1) }) // 回到正常大小
  232. .delay(0.5) // 停留0.5秒
  233. .parallel( // 同时执行淡出和向上移动
  234. tween().to(0.3, { scale: new Vec3(0.8, 0.8, 1) }),
  235. tween().by(0.3, { position: new Vec3(0, 20, 0) }),
  236. uiOpacity ? tween(uiOpacity).to(0.3, { opacity: 0 }) : tween()
  237. )
  238. .call(() => {
  239. if (this.node && this.node.isValid) {
  240. this.node.destroy(); // 动画结束后销毁节点
  241. }
  242. })
  243. .start();
  244. }
  245. onDestroy() {
  246. // 清理所有tween动画
  247. Tween.stopAllByTarget(this.node);
  248. const uiTransform = this.node.getComponent(UITransform);
  249. const uiOpacity = this.node.getComponent(UIOpacity);
  250. if (uiTransform) {
  251. Tween.stopAllByTarget(uiTransform);
  252. }
  253. if (uiOpacity) {
  254. Tween.stopAllByTarget(uiOpacity);
  255. }
  256. }
  257. }