UIStateManager.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { _decorator, Component, Node, Button, find } from 'cc';
  2. import { BaseSingleton } from '../Core/BaseSingleton';
  3. import EventBus, { GameEvents } from '../Core/EventBus';
  4. import { Audio } from '../AudioManager/AudioManager';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * UIStateManager
  8. * 负责游戏内 UI 按钮事件分发。
  9. * 游戏失败/成功的UI显示逻辑已迁移到GameEnd.ts统一处理。
  10. */
  11. @ccclass('UIStateManager')
  12. export class UIStateManager extends BaseSingleton {
  13. public static _instance: UIStateManager;
  14. @property({ type: Node, tooltip: '游戏结束面板 (Canvas/GameEnd)' })
  15. public endPanel: Node = null;
  16. protected init() {
  17. // 自动查找面板
  18. if (!this.endPanel) {
  19. this.endPanel = find('Canvas/GameEnd');
  20. }
  21. // 绑定按钮事件分发
  22. this.bindPanelButtons();
  23. // 监听UI重置事件
  24. EventBus.getInstance().on(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  25. }
  26. private bindPanelButtons() {
  27. this.bindButtonsInPanel(this.endPanel);
  28. }
  29. private bindButtonsInPanel(panel: Node) {
  30. if (!panel) return;
  31. const buttons = panel.getComponentsInChildren(Button);
  32. buttons.forEach(btn => {
  33. btn.node.on(Button.EventType.CLICK, () => {
  34. // 播放UI点击音效
  35. Audio.playUISound('data/弹球音效/ui play');
  36. EventBus.getInstance().emit(btn.node.name.toUpperCase() + '_CLICK');
  37. });
  38. });
  39. }
  40. /**
  41. * 处理UI重置事件
  42. * 游戏失败/成功的UI显示逻辑已迁移到GameEnd.ts统一处理
  43. */
  44. private onResetUI() {
  45. console.log('[UIStateManager] 处理UI重置事件');
  46. // UI重置逻辑已迁移到GameEnd.ts,这里保留空实现以维持兼容性
  47. }
  48. protected onDestroy() {
  49. // 清理事件监听
  50. const eventBus = EventBus.getInstance();
  51. eventBus.off(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  52. super.onDestroy();
  53. }
  54. }