HPBarAnimation.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. private currentTween: any = null; // 当前正在运行的动画
  25. start() {
  26. this.initializeComponents();
  27. }
  28. /**
  29. * 初始化组件
  30. */
  31. private initializeComponents() {
  32. // 获取组件所属的敌人节点信息
  33. const enemyNode = this.node;
  34. const enemyName = enemyNode ? enemyNode.name : 'Unknown';
  35. // 获取红色血条组件
  36. if (this.redBarNode) {
  37. this.redProgressBar = this.redBarNode.getComponent(ProgressBar);
  38. if (this.redProgressBar) {
  39. this.currentProgress = this.redProgressBar.progress;
  40. this.targetProgress = this.currentProgress;
  41. } else {
  42. console.error(`[HPBarAnimation] [${enemyName}] 红色血条节点上未找到ProgressBar组件!`);
  43. }
  44. } else {
  45. console.error(`[HPBarAnimation] [${enemyName}] 红色血条节点未设置!`);
  46. }
  47. // 获取黄色血条组件
  48. if (this.yellowBarNode) {
  49. this.yellowProgressBar = this.yellowBarNode.getComponent(ProgressBar);
  50. if (this.yellowProgressBar) {
  51. // 黄色血条初始进度与红色血条同步
  52. this.yellowProgressBar.progress = this.currentProgress;
  53. } else {
  54. console.error(`[HPBarAnimation] [${enemyName}] 黄色血条节点上未找到ProgressBar组件!`);
  55. }
  56. } else {
  57. console.error(`[HPBarAnimation] [${enemyName}] 黄色血条节点未设置!`);
  58. }
  59. }
  60. /**
  61. * 获取节点路径
  62. */
  63. private getNodePath(node: Node): string {
  64. if (!node) return 'null';
  65. let path = node.name;
  66. let current = node;
  67. while (current.parent) {
  68. current = current.parent;
  69. path = current.name + '/' + path;
  70. }
  71. return path;
  72. }
  73. /**
  74. * 更新血条显示
  75. */
  76. private updateBarDisplay() {
  77. if (!this.redProgressBar || !this.yellowProgressBar) {
  78. return;
  79. }
  80. // 红色血条显示当前血量
  81. if (this.redProgressBar && this.redProgressBar.isValid) {
  82. this.redProgressBar.progress = this.currentProgress;
  83. }
  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. if (this.redProgressBar && this.redProgressBar.isValid) {
  103. this.redProgressBar.progress = newProgress;
  104. }
  105. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  106. this.yellowProgressBar.progress = newProgress;
  107. }
  108. this.updateBarDisplay();
  109. return;
  110. }
  111. // 血量减少时播放黄色血皮滑动动画
  112. this.playDamageAnimation(newProgress);
  113. }
  114. /**
  115. * 播放伤害动画
  116. * @param newProgress 新的血量百分比
  117. */
  118. private playDamageAnimation(newProgress: number) {
  119. console.log(`[HPBarAnimation] 开始播放伤害动画:${this.currentProgress} -> ${newProgress}`);
  120. // 停止当前正在运行的动画,避免动画冲突
  121. if (this.currentTween) {
  122. this.currentTween.stop();
  123. this.currentTween = null;
  124. }
  125. this.targetProgress = newProgress;
  126. // 保存原始血量作为黄色血条起始位置
  127. const originalProgress = this.currentProgress;
  128. // 1. 立即更新红色血条到新的血量值
  129. this.currentProgress = newProgress;
  130. if (this.redProgressBar && this.redProgressBar.isValid) {
  131. this.redProgressBar.progress = newProgress;
  132. }
  133. // 2. 黄色血条从原血量滑动到新血量位置
  134. // 黄色血条保持在原始位置
  135. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  136. this.yellowProgressBar.progress = originalProgress;
  137. }
  138. this.updateBarDisplay();
  139. // 创建滑动动画,先暂停0.4秒再滑动
  140. this.currentTween = tween({ progress: originalProgress })
  141. .delay(0.4) // 暂停0.4秒
  142. .to(0.6, { progress: newProgress }, {
  143. onUpdate: (target) => {
  144. // 动画过程中更新黄色血条进度
  145. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  146. this.yellowProgressBar.progress = target.progress;
  147. this.updateBarDisplay();
  148. }
  149. },
  150. onComplete: () => {
  151. // 动画完成后黄色血条进度等于当前血量
  152. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  153. this.yellowProgressBar.progress = newProgress;
  154. this.updateBarDisplay();
  155. }
  156. // 清除动画引用
  157. this.currentTween = null;
  158. }
  159. })
  160. .start();
  161. }
  162. /**
  163. * 获取当前血量百分比
  164. */
  165. public getCurrentProgress(): number {
  166. return this.currentProgress;
  167. }
  168. /**
  169. * 重置血条到满血状态
  170. */
  171. public resetToFull() {
  172. this.updateProgress(1.0);
  173. }
  174. onDestroy() {
  175. // 清理正在运行的动画,防止内存泄漏
  176. if (this.currentTween) {
  177. this.currentTween.stop();
  178. this.currentTween = null;
  179. }
  180. }
  181. }