HPBarAnimation.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { _decorator, Component, Node, ProgressBar, tween } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 血条动画组件
  5. * 使用两个重叠的血条实现黄色血皮滑动动画效果
  6. * 红色血条显示当前血量,黄色血条用于滑动动画
  7. */
  8. @ccclass('HPBarAnimation')
  9. export class HPBarAnimation extends Component {
  10. @property({
  11. type: Node,
  12. tooltip: '红色血条节点'
  13. })
  14. public redBarNode: Node = null;
  15. @property({
  16. type: Node,
  17. tooltip: '黄色血条节点'
  18. })
  19. public yellowBarNode: Node = null;
  20. private redProgressBar: ProgressBar = null;
  21. private yellowProgressBar: ProgressBar = null;
  22. private currentProgress: number = 1.0;
  23. private targetProgress: number = 1.0;
  24. start() {
  25. this.initializeComponents();
  26. }
  27. /**
  28. * 初始化组件
  29. */
  30. private initializeComponents() {
  31. // 获取组件所属的敌人节点信息
  32. const enemyNode = this.node;
  33. const enemyName = enemyNode ? enemyNode.name : 'Unknown';
  34. // 获取红色血条组件
  35. if (this.redBarNode) {
  36. console.log(`[HPBarAnimation] [${enemyName}] 红色血条节点已设置:`, this.redBarNode.name);
  37. this.redProgressBar = this.redBarNode.getComponent(ProgressBar);
  38. if (this.redProgressBar) {
  39. this.currentProgress = this.redProgressBar.progress;
  40. this.targetProgress = this.currentProgress;
  41. console.log(`[HPBarAnimation] [${enemyName}] 红色血条组件初始化成功,当前进度:`, this.currentProgress);
  42. } else {
  43. console.error(`[HPBarAnimation] [${enemyName}] 红色血条节点上未找到ProgressBar组件!`);
  44. }
  45. } else {
  46. console.error(`[HPBarAnimation] [${enemyName}] 红色血条节点未设置!`);
  47. }
  48. // 获取黄色血条组件
  49. if (this.yellowBarNode) {
  50. console.log(`[HPBarAnimation] [${enemyName}] 黄色血条节点已设置:`, this.yellowBarNode.name);
  51. this.yellowProgressBar = this.yellowBarNode.getComponent(ProgressBar);
  52. if (this.yellowProgressBar) {
  53. // 黄色血条初始进度与红色血条同步
  54. this.yellowProgressBar.progress = this.currentProgress;
  55. } else {
  56. console.error(`[HPBarAnimation] [${enemyName}] 黄色血条节点上未找到ProgressBar组件!`);
  57. }
  58. } else {
  59. console.error(`[HPBarAnimation] [${enemyName}] 黄色血条节点未设置!`);
  60. }
  61. }
  62. /**
  63. * 获取节点路径
  64. */
  65. private getNodePath(node: Node): string {
  66. if (!node) return 'null';
  67. let path = node.name;
  68. let current = node;
  69. while (current.parent) {
  70. current = current.parent;
  71. path = current.name + '/' + path;
  72. }
  73. return path;
  74. }
  75. /**
  76. * 更新血条显示
  77. */
  78. private updateBarDisplay() {
  79. if (!this.redProgressBar || !this.yellowProgressBar) {
  80. return;
  81. }
  82. // 红色血条显示当前血量
  83. this.redProgressBar.progress = this.currentProgress;
  84. }
  85. /**
  86. * 更新血条进度并播放动画
  87. * @param newProgress 新的血量百分比 (0-1)
  88. */
  89. public updateProgress(newProgress: number) {
  90. const enemyName = this.node ? this.node.name : 'Unknown';
  91. if (!this.redProgressBar || !this.yellowProgressBar) {
  92. console.warn(`[HPBarAnimation] [${enemyName}] 红色或黄色血条组件未初始化`);
  93. return;
  94. }
  95. // 限制进度范围
  96. newProgress = Math.max(0, Math.min(1, newProgress));
  97. // 如果血量增加或没有变化,直接更新
  98. if (newProgress >= this.currentProgress) {
  99. this.currentProgress = newProgress;
  100. this.targetProgress = newProgress;
  101. // 同步更新两个血条
  102. this.redProgressBar.progress = newProgress;
  103. this.yellowProgressBar.progress = newProgress;
  104. this.updateBarDisplay();
  105. return;
  106. }
  107. // 血量减少时播放黄色血皮滑动动画
  108. this.playDamageAnimation(newProgress);
  109. }
  110. /**
  111. * 播放伤害动画
  112. * @param newProgress 新的血量百分比
  113. */
  114. private playDamageAnimation(newProgress: number) {
  115. console.log(`[HPBarAnimation] 开始播放伤害动画:${this.currentProgress} -> ${newProgress}`);
  116. this.targetProgress = newProgress;
  117. // 保存原始血量作为黄色血条起始位置
  118. const originalProgress = this.currentProgress;
  119. // 1. 立即更新红色血条到新的血量值
  120. this.currentProgress = newProgress;
  121. this.redProgressBar.progress = newProgress;
  122. // 2. 黄色血条从原血量滑动到新血量位置
  123. // 黄色血条保持在原始位置
  124. this.yellowProgressBar.progress = originalProgress;
  125. this.updateBarDisplay();
  126. // 创建滑动动画,先暂停0.4秒再滑动
  127. tween({ progress: originalProgress })
  128. .delay(0.4) // 暂停0.4秒
  129. .to(0.6, { progress: newProgress }, {
  130. onUpdate: (target) => {
  131. // 动画过程中更新黄色血条进度
  132. this.yellowProgressBar.progress = target.progress;
  133. this.updateBarDisplay();
  134. },
  135. onComplete: () => {
  136. // 动画完成后黄色血条进度等于当前血量
  137. this.yellowProgressBar.progress = newProgress;
  138. this.updateBarDisplay();
  139. }
  140. })
  141. .start();
  142. }
  143. /**
  144. * 获取当前血量百分比
  145. */
  146. public getCurrentProgress(): number {
  147. return this.currentProgress;
  148. }
  149. /**
  150. * 重置血条到满血状态
  151. */
  152. public resetToFull() {
  153. this.updateProgress(1.0);
  154. }
  155. onDestroy() {
  156. }
  157. }