SkillButtonAnimator.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { _decorator, Component, Node, Vec3, tween, find, Sprite, Color } from 'cc';
  2. import BlinkScaleAnimator from '../../Animations/BlinkScaleAnimator';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * SkillButtonAnimator
  6. * 负责技能按钮的缩放 / 移动动画和星星闪烁效果。
  7. * 提供 playShrink 接口供外部调用。
  8. */
  9. @ccclass('SkillButtonAnimator')
  10. export class SkillButtonAnimator extends Component {
  11. @property({ type: [Node], tooltip: '星星节点数组 (xx-1 到 xx-5)' })
  12. starNodes: Node[] = [];
  13. private _origScale: Vec3 = new Vec3();
  14. private _origPos: Vec3 = new Vec3();
  15. private _blinkTween: any = null;
  16. private _starBlinkComp: BlinkScaleAnimator | null = null;
  17. // 星星颜色配置
  18. private readonly STAR_ACTIVE_COLOR = new Color(255, 255, 255, 255); // 亮起的星星
  19. private readonly STAR_INACTIVE_COLOR = new Color(58, 37, 37, 255); // 未亮起的星星 (3A2525)
  20. onLoad() {
  21. // 记录初始位置、缩放,用于恢复
  22. this._origScale.set(this.node.scale);
  23. this._origPos.set(this.node.position);
  24. }
  25. /**
  26. * 设置技能等级并更新星星显示
  27. * @param currentLevel 当前等级 (0-5)
  28. */
  29. public setSkillLevel(currentLevel: number) {
  30. // 停止之前的闪烁动画
  31. this.stopBlinkAnimation();
  32. // 更新星星显示
  33. this.starNodes.forEach((starNode, index) => {
  34. const sprite = starNode.getComponent(Sprite);
  35. if (sprite) {
  36. if (index < currentLevel) {
  37. // 已获得等级的星星 - 常亮
  38. sprite.color = this.STAR_ACTIVE_COLOR;
  39. } else if (index === currentLevel && currentLevel < 5) {
  40. // 下一等级的星星 - 设置为未亮起状态,稍后开始闪烁
  41. sprite.color = this.STAR_INACTIVE_COLOR;
  42. } else {
  43. // 剩余星星 - 不亮
  44. sprite.color = this.STAR_INACTIVE_COLOR;
  45. }
  46. }
  47. });
  48. // 如果当前等级小于最大等级,开始闪烁下一颗星星(即将获得的等级)
  49. if (currentLevel < 5) {
  50. this.startBlinkAnimation(currentLevel);
  51. }
  52. }
  53. /**
  54. * 开始星星闪烁动画
  55. * @param nextStarIndex 下一颗要闪烁的星星索引 (0-4)
  56. */
  57. private startBlinkAnimation(nextStarIndex: number) {
  58. if (nextStarIndex >= this.starNodes.length) return;
  59. const nextStar = this.starNodes[nextStarIndex];
  60. const sprite = nextStar.getComponent(Sprite);
  61. if (!sprite) return;
  62. // 使用通用组件在该星星上播放闪烁缩放(不再使用颜色闪烁)
  63. if (this._starBlinkComp) {
  64. this._starBlinkComp.stop();
  65. this._starBlinkComp = null;
  66. }
  67. this._starBlinkComp = BlinkScaleAnimator.ensure(nextStar, {
  68. scaleFactor: 1.3,
  69. upDuration: 0.15,
  70. downDuration: 0.15,
  71. easingUp: 'sineOut',
  72. easingDown: 'sineIn'
  73. });
  74. }
  75. /**
  76. * 停止星星闪烁动画
  77. */
  78. private stopBlinkAnimation() {
  79. if (this._starBlinkComp) {
  80. this._starBlinkComp.stop();
  81. this._starBlinkComp = null;
  82. }
  83. if (this._blinkTween) {
  84. this._blinkTween.stop();
  85. this._blinkTween = null;
  86. }
  87. }
  88. /**
  89. * 播放缩小并(可选)移动到指定目标位置的动画。
  90. * @param duration 动画时长(秒)
  91. * @param targetPos 目标位置(节点本地坐标系),不传则保持原位置
  92. * @param onComplete 完成回调
  93. */
  94. public playShrink(duration: number = 0.3, targetPos?: Vec3, onComplete?: () => void) {
  95. // 停止闪烁动画
  96. this.stopBlinkAnimation();
  97. // 若目标位置存在,则先 tween 到该位置;否则只缩放
  98. const props: any = { scale: new Vec3(0, 0, 0) };
  99. if (targetPos) {
  100. props.position = targetPos;
  101. }
  102. tween(this.node)
  103. .to(duration, props, { easing: 'quadIn' })
  104. .call(() => {
  105. // 动画结束后隐藏节点,但允许后续 resetState 恢复
  106. this.node.active = false;
  107. if (onComplete) onComplete();
  108. })
  109. .start();
  110. this.node.setPosition(this._origPos);
  111. }
  112. public resetState() {
  113. // 停止闪烁动画
  114. this.stopBlinkAnimation();
  115. // Reset scale, position, opacity, etc. as needed
  116. this.node.setScale(Vec3.ONE);
  117. this.node.active = true;
  118. this.node.setPosition(this._origPos);
  119. // 重置星星显示为0级状态
  120. this.setSkillLevel(0);
  121. }
  122. onDestroy() {
  123. // 清理闪烁动画
  124. this.stopBlinkAnimation();
  125. }
  126. }
  127. export default SkillButtonAnimator;