import { _decorator, Component, Node, Button, find, Label } from 'cc'; import { BaseSingleton } from '../Core/BaseSingleton'; import EventBus, { GameEvents } from '../Core/EventBus'; const { ccclass, property } = _decorator; /** * UIStateManager * 负责游戏内 UI 面板的显示/隐藏,以及按钮事件分发。 * 注意:仅处理 Canvas/GameLevelUI 下与战斗胜败相关的 UI。 */ @ccclass('UIStateManager') export class UIStateManager extends BaseSingleton { public static _instance: UIStateManager; @property({ type: Node, tooltip: '游戏结束面板 (Canvas/GameEnd)' }) public endPanel: Node = null; // 控制是否响应游戏结束事件的标志 private shouldRespondToGameEvents: boolean = true; protected init() { // 自动查找面板 if (!this.endPanel) { this.endPanel = find('Canvas/GameEnd'); } // 默认隐藏 if (this.endPanel) this.endPanel.active = false; // 绑定按钮 this.bindPanelButtons(); // 监听事件 EventBus.getInstance().on(GameEvents.GAME_SUCCESS, this.onGameSuccess, this); EventBus.getInstance().on(GameEvents.GAME_DEFEAT, this.onGameDefeat, this); EventBus.getInstance().on(GameEvents.RESET_UI_STATES, this.closeAllPanels, this); } private bindPanelButtons() { this.bindButtonsInPanel(this.endPanel); } private bindButtonsInPanel(panel: Node) { if (!panel) return; const buttons = panel.getComponentsInChildren(Button); buttons.forEach(btn => { btn.node.on(Button.EventType.CLICK, () => { EventBus.getInstance().emit(btn.node.name.toUpperCase() + '_CLICK'); }); }); } /** * 设置游戏结束UI的EndLabel文本 * @param text 要显示的文本 ('SUCCESS' 或 'DEFEAT') */ private setEndLabelText(text: string) { if (!this.endPanel) return; const endLabel = this.endPanel.getChildByPath('Sprite/EndLabel'); if (endLabel) { const labelComponent = endLabel.getComponent(Label); if (labelComponent) { labelComponent.string = text; console.log(`[UIStateManager] 设置EndLabel文本为: ${text}`); } else { console.warn('[UIStateManager] 未找到EndLabel的Label组件'); } } else { console.warn('[UIStateManager] 未找到EndLabel节点路径: Sprite/EndLabel'); } } private onGameSuccess() { // 如果被禁用响应游戏事件,则跳过 if (!this.shouldRespondToGameEvents) { console.log('[UIStateManager] 已禁用游戏事件响应,跳过GAME_SUCCESS处理'); return; } if (this.endPanel) { this.endPanel.active = true; this.setEndLabelText('SUCCESS'); } } private onGameDefeat() { // 如果被禁用响应游戏事件,则跳过 if (!this.shouldRespondToGameEvents) { console.log('[UIStateManager] 已禁用游戏事件响应,跳过GAME_DEFEAT处理'); return; } if (this.endPanel) { this.endPanel.active = true; this.setEndLabelText('DEFEAT'); } } /** 对外接口 */ public closeAllPanels() { console.log('[UIStateManager] 重置UI状态,关闭所有面板'); // 关闭游戏结束面板 if (this.endPanel) { this.endPanel.active = false; console.log('[UIStateManager] 已关闭GameEnd面板'); } // 暂时禁用游戏事件响应,避免与GameManager冲突 this.shouldRespondToGameEvents = false; // 延迟重新启用游戏事件响应 this.scheduleOnce(() => { this.shouldRespondToGameEvents = true; console.log('[UIStateManager] 游戏事件响应已重新启用'); }, 1.0); } protected onDestroy() { // 清理事件监听 const eventBus = EventBus.getInstance(); eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccess, this); eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeat, this); eventBus.off(GameEvents.RESET_UI_STATES, this.closeAllPanels, this); super.onDestroy(); } }