GamePause.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { _decorator, Component, Node, director } from 'cc';
  2. import EventBus, { GameEvents } from '../Core/EventBus';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 游戏状态枚举
  6. */
  7. export enum GameState {
  8. PLAYING = 'playing',
  9. PAUSED = 'paused',
  10. SUCCESS = 'success',
  11. DEFEAT = 'defeat'
  12. }
  13. /**
  14. * 游戏状态管理器
  15. * 纯粹的状态管理,不直接执行动作
  16. */
  17. @ccclass('GamePause')
  18. export class GamePause extends Component {
  19. public static _instance: GamePause = null;
  20. // 当前游戏状态
  21. private currentState: GameState = GameState.PLAYING;
  22. // 是否允许小球发射子弹(暂停时禁用)
  23. private bulletFireEnabled: boolean = true;
  24. start() {
  25. // 初始化状态
  26. this.resetGameState();
  27. }
  28. /**
  29. * 设置游戏状态为暂停
  30. */
  31. public pauseGame(): void {
  32. if (this.currentState === GameState.PAUSED || this.isGameOver()) {
  33. return;
  34. }
  35. console.log('[GamePause] 设置游戏状态为暂停');
  36. this.currentState = GameState.PAUSED;
  37. this.bulletFireEnabled = false;
  38. // 派发暂停事件,让GameManager处理具体动作
  39. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  40. }
  41. /**
  42. * 设置游戏状态为运行
  43. */
  44. public resumeGame(): void {
  45. if (this.currentState === GameState.PLAYING || this.isGameOver()) {
  46. return;
  47. }
  48. console.log('[GamePause] 设置游戏状态为运行');
  49. this.currentState = GameState.PLAYING;
  50. this.bulletFireEnabled = true;
  51. // 派发恢复事件,让GameManager处理具体动作
  52. EventBus.getInstance().emit(GameEvents.GAME_RESUME);
  53. }
  54. /**
  55. * 设置游戏状态为成功
  56. */
  57. public triggerGameSuccess(): void {
  58. if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) {
  59. return;
  60. }
  61. console.log('[GamePause] 设置游戏状态为成功');
  62. this.currentState = GameState.SUCCESS;
  63. this.bulletFireEnabled = false;
  64. // 派发游戏成功事件,让GameManager处理具体动作
  65. EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
  66. }
  67. /**
  68. * 设置游戏状态为失败
  69. */
  70. public triggerGameDefeat(): void {
  71. if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) {
  72. return;
  73. }
  74. console.log('[GamePause] 设置游戏状态为失败');
  75. this.currentState = GameState.DEFEAT;
  76. this.bulletFireEnabled = false;
  77. // 派发游戏失败事件,让GameManager处理具体动作
  78. EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
  79. }
  80. /**
  81. * 敌人被击杀事件 - 只管理状态,不执行具体动作
  82. */
  83. public onEnemyKilled(): void {
  84. // 如果游戏已经结束,不执行后续逻辑
  85. if (this.isGameOver()) {
  86. console.warn('[GamePause] 游戏已结束状态下onEnemyKilled被调用!当前状态:', this.currentState);
  87. return;
  88. }
  89. // 派发敌人被击杀事件,让GameManager处理具体逻辑
  90. // 使用自定义事件名称,避免依赖GameEvents中可能不存在的事件
  91. EventBus.getInstance().emit('ENEMY_KILLED');
  92. }
  93. /**
  94. * 检查游戏是否结束
  95. */
  96. public isGameOver(): boolean {
  97. return this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT;
  98. }
  99. /**
  100. * 重置游戏状态
  101. */
  102. public resetGameState(): void {
  103. console.log('[GamePause] 重置游戏状态');
  104. this.currentState = GameState.PLAYING;
  105. this.bulletFireEnabled = true;
  106. }
  107. /**
  108. * 检查是否为暂停状态
  109. */
  110. public isPaused(): boolean {
  111. return this.currentState === GameState.PAUSED;
  112. }
  113. /**
  114. * 检查是否允许发射子弹
  115. */
  116. public isBulletFireEnabled(): boolean {
  117. return this.bulletFireEnabled;
  118. }
  119. /**
  120. * 获取当前游戏状态
  121. */
  122. public getCurrentState(): GameState {
  123. return this.currentState;
  124. }
  125. /**
  126. * 强制设置游戏状态(谨慎使用)
  127. */
  128. public forceSetState(state: GameState): void {
  129. this.currentState = state;
  130. this.bulletFireEnabled = (state === GameState.PLAYING);
  131. console.log(`[GamePause] 强制设置状态为: ${state}`);
  132. }
  133. /**
  134. * 获取单例实例
  135. */
  136. public static getInstance(): GamePause {
  137. if (!GamePause._instance) {
  138. // 创建节点和组件
  139. const gamePauseNode = new Node('GamePause');
  140. GamePause._instance = gamePauseNode.addComponent(GamePause);
  141. // 将节点添加到场景中(不销毁)
  142. const scene = director.getScene();
  143. if (scene) {
  144. scene.addChild(gamePauseNode);
  145. }
  146. }
  147. return GamePause._instance;
  148. }
  149. onDestroy() {
  150. GamePause._instance = null;
  151. }
  152. }