| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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;
- 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.endPanel) {
- this.endPanel.active = true;
- this.setEndLabelText('SUCCESS');
- }
- }
- private onGameDefeat() {
- if (this.endPanel) {
- this.endPanel.active = true;
- this.setEndLabelText('DEFEAT');
- }
- }
- /** 对外接口 */
- public closeAllPanels() {
- if (this.endPanel) this.endPanel.active = false;
- }
- }
|