import { _decorator, Component, Node, Button, find, Label, Sprite, Vec3 } from 'cc'; import { GameManager } from '../../LevelSystem/GameManager'; import { SkillButtonAnimator } from './SkillButtonAnimator'; import { GamePause } from '../GamePause'; const { ccclass, property } = _decorator; /** * SkillSelectionController * 放在 Canvas/SelectSkillUI 节点上。 * 负责监听技能按钮点击,并按顺序播放缩小动画,最后关闭整个 SelectSkillUI。 */ @ccclass('SkillSelectionController') export class SkillSelectionController extends Component { @property({ type: [Node], tooltip: '技能按钮节点列表,留空则自动从 SkillsContainer 获取' }) public skillButtons: Node[] = []; @property({ type: Number, tooltip: '收缩动画时长' }) public shrinkDuration: number = 0.3; // 防止重复点击标记 private _clicked = false; start() { this.setupSkillButtons(); } private setupSkillButtons() { if (this.skillButtons.length === 0) { const container = this.node.getChildByName('SkillsContainer'); if (container) { this.skillButtons = container.children.slice(); } } this.skillButtons.forEach(btnNode => { const btn = btnNode.getComponent(Button); if (btn) { btn.node.on(Button.EventType.CLICK, () => this.onSkillSelected(btnNode), this); } }); } /** * 玩家选择某技能按钮 */ private onSkillSelected(selectedBtn: Node) { if (this._clicked) return; // 只允许一次 this._clicked = true; // 禁用所有按钮交互 this.skillButtons.forEach(btn => { const b = btn.getComponent(Button); if (b) b.interactable = false; }); const otherBtns = this.skillButtons.filter(btn => btn !== selectedBtn); let finishedOthers = 0; // 所有其他按钮完成动画后,再收缩选中按钮 const onOtherFinished = () => { finishedOthers++; if (finishedOthers >= otherBtns.length) { // 收缩选中按钮 const selAnim = selectedBtn.getComponent(SkillButtonAnimator); selAnim?.playShrink(this.shrinkDuration, undefined, () => { // 关闭整个 UI this.node.active = false; // 恢复游戏并重置能量 - 使用GamePause const gamePause = GamePause.getInstance(); if (gamePause) { gamePause.resumeGame(); } const gm = this.getGameManager(); if (gm) { gm.resetEnergy(); } // 关闭后立刻重置UI this.resetUI(); }); } }; // 播放其他按钮动画 const targetPos = selectedBtn.position.clone(); otherBtns.forEach(btn => { const anim = btn.getComponent(SkillButtonAnimator); anim?.playShrink(this.shrinkDuration, targetPos, onOtherFinished); }); } /** * 重置 UI 状态,供下次开启时使用 */ private resetUI() { this._clicked = false; // 重新启用所有按钮交互 this.skillButtons.forEach(btn => { const b = btn.getComponent(Button); if (b) b.interactable = true; // 重置按钮动画状态 const anim = btn.getComponent(SkillButtonAnimator); anim?.resetState(); }); } private getGameManager(): GameManager | null { const gmNode = find('Canvas/GameLevelUI/GameManager'); return gmNode?.getComponent(GameManager) || null; } }