| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { _decorator, Component, Node, Button, find } 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/GameSuccess)' })
- public successPanel: Node = null;
- @property({ type: Node, tooltip: '失败面板 (Canvas/GameDefeat)' })
- public defeatPanel: Node = null;
- protected init() {
- // 自动查找面板
- if (!this.successPanel) {
- this.successPanel = find('Canvas/GameSuccess');
- }
- if (!this.defeatPanel) {
- this.defeatPanel = find('Canvas/GameDefeat');
- }
- // 默认隐藏
- if (this.successPanel) this.successPanel.active = false;
- if (this.defeatPanel) this.defeatPanel.active = false;
- // 绑定按钮
- this.bindPanelButtons();
- // 监听事件
- EventBus.getInstance().on(GameEvents.GAME_SUCCESS, this.onGameSuccess, this);
- EventBus.getInstance().on(GameEvents.GAME_DEFEAT, this.onGameDefeat, this);
- }
- private bindPanelButtons() {
- this.bindButtonsInPanel(this.successPanel);
- this.bindButtonsInPanel(this.defeatPanel);
- }
- 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');
- });
- });
- }
- private onGameSuccess() {
- if (this.successPanel) this.successPanel.active = true;
- }
- private onGameDefeat() {
- if (this.defeatPanel) this.defeatPanel.active = true;
- }
- /** 对外接口 */
- public closeAllPanels() {
- if (this.successPanel) this.successPanel.active = false;
- if (this.defeatPanel) this.defeatPanel.active = false;
- }
- }
|