SkillButtonAnimator.ts 4.5 KB

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