HPBarAnimation.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. if (this.redProgressBar && this.redProgressBar.isValid) {
  84. this.redProgressBar.progress = this.currentProgress;
  85. }
  86. }
  87. /**
  88. * 更新血条进度并播放动画
  89. * @param newProgress 新的血量百分比 (0-1)
  90. */
  91. public updateProgress(newProgress: number) {
  92. const enemyName = this.node ? this.node.name : 'Unknown';
  93. if (!this.redProgressBar || !this.yellowProgressBar) {
  94. console.warn(`[HPBarAnimation] [${enemyName}] 红色或黄色血条组件未初始化`);
  95. return;
  96. }
  97. // 限制进度范围
  98. newProgress = Math.max(0, Math.min(1, newProgress));
  99. // 如果血量增加或没有变化,直接更新
  100. if (newProgress >= this.currentProgress) {
  101. this.currentProgress = newProgress;
  102. this.targetProgress = newProgress;
  103. // 同步更新两个血条
  104. if (this.redProgressBar && this.redProgressBar.isValid) {
  105. this.redProgressBar.progress = newProgress;
  106. }
  107. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  108. this.yellowProgressBar.progress = newProgress;
  109. }
  110. this.updateBarDisplay();
  111. return;
  112. }
  113. // 血量减少时播放黄色血皮滑动动画
  114. this.playDamageAnimation(newProgress);
  115. }
  116. /**
  117. * 播放伤害动画
  118. * @param newProgress 新的血量百分比
  119. */
  120. private playDamageAnimation(newProgress: number) {
  121. console.log(`[HPBarAnimation] 开始播放伤害动画:${this.currentProgress} -> ${newProgress}`);
  122. this.targetProgress = newProgress;
  123. // 保存原始血量作为黄色血条起始位置
  124. const originalProgress = this.currentProgress;
  125. // 1. 立即更新红色血条到新的血量值
  126. this.currentProgress = newProgress;
  127. if (this.redProgressBar && this.redProgressBar.isValid) {
  128. this.redProgressBar.progress = newProgress;
  129. }
  130. // 2. 黄色血条从原血量滑动到新血量位置
  131. // 黄色血条保持在原始位置
  132. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  133. this.yellowProgressBar.progress = originalProgress;
  134. }
  135. this.updateBarDisplay();
  136. // 创建滑动动画,先暂停0.4秒再滑动
  137. tween({ progress: originalProgress })
  138. .delay(0.4) // 暂停0.4秒
  139. .to(0.6, { progress: newProgress }, {
  140. onUpdate: (target) => {
  141. // 动画过程中更新黄色血条进度
  142. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  143. this.yellowProgressBar.progress = target.progress;
  144. this.updateBarDisplay();
  145. }
  146. },
  147. onComplete: () => {
  148. // 动画完成后黄色血条进度等于当前血量
  149. if (this.yellowProgressBar && this.yellowProgressBar.isValid) {
  150. this.yellowProgressBar.progress = newProgress;
  151. this.updateBarDisplay();
  152. }
  153. }
  154. })
  155. .start();
  156. }
  157. /**
  158. * 获取当前血量百分比
  159. */
  160. public getCurrentProgress(): number {
  161. return this.currentProgress;
  162. }
  163. /**
  164. * 重置血条到满血状态
  165. */
  166. public resetToFull() {
  167. this.updateProgress(1.0);
  168. }
  169. onDestroy() {
  170. }
  171. }