GamePause.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { _decorator, Component, Node, director } from 'cc';
  2. import EventBus, { GameEvents } from '../Core/EventBus';
  3. import { GameState } from '../LevelSystem/IN_game';
  4. const { ccclass } = _decorator;
  5. /**
  6. * 游戏状态管理器
  7. * 纯粹的状态管理,不直接执行动作
  8. */
  9. @ccclass('GamePause')
  10. export class GamePause extends Component {
  11. public static _instance: GamePause = null;
  12. // 当前游戏状态
  13. private currentState: GameState = GameState.PLAYING;
  14. // 是否允许小球发射子弹(暂停时禁用)
  15. private bulletFireEnabled: boolean = true;
  16. // 游戏是否在进行中(用于替代对GameManager的直接依赖)
  17. private isInGameMode: boolean = false;
  18. start() {
  19. // 初始化状态
  20. this.resetGameState();
  21. // 设置事件监听器
  22. this.setupEventListeners();
  23. }
  24. /**
  25. * 设置事件监听器
  26. */
  27. private setupEventListeners() {
  28. const eventBus = EventBus.getInstance();
  29. // 监听游戏暂停重置事件
  30. eventBus.on(GameEvents.RESET_GAME_PAUSE, this.onResetGamePauseEvent, this);
  31. // 监听游戏开始和结束事件来更新游戏模式状态
  32. eventBus.on(GameEvents.GAME_START, this.onGameStartEvent, this);
  33. eventBus.on(GameEvents.SHOW_BLOCK_SELECTION, this.onGameEndEvent, this);
  34. // 监听子弹发射检查事件
  35. eventBus.on(GameEvents.BALL_FIRE_BULLET, this.onBallFireBulletEvent, this);
  36. // 监听游戏结束状态检查事件
  37. eventBus.on(GameEvents.GAME_CHECK_OVER, this.onGameCheckOverEvent, this);
  38. }
  39. /**
  40. * 处理重置游戏暂停事件
  41. */
  42. private onResetGamePauseEvent() {
  43. console.log('[GamePause] 接收到重置游戏暂停事件,重置游戏状态');
  44. this.resetGameState();
  45. // 重置游戏模式状态
  46. this.isInGameMode = false;
  47. console.log('[GamePause] 游戏模式状态已重置为false');
  48. }
  49. /**
  50. * 处理游戏开始事件
  51. */
  52. private onGameStartEvent() {
  53. console.log('[GamePause] 接收到游戏开始事件,设置为游戏模式');
  54. this.isInGameMode = true;
  55. }
  56. /**
  57. * 处理游戏结束事件
  58. */
  59. private onGameEndEvent() {
  60. console.log('[GamePause] 接收到游戏结束事件,退出游戏模式');
  61. this.isInGameMode = false;
  62. }
  63. /**
  64. * 处理子弹发射检查事件
  65. */
  66. private onBallFireBulletEvent(data: { canFire: (value: boolean) => void }) {
  67. // 检查是否允许发射子弹
  68. const canFire = this.bulletFireEnabled && !this.isGameOver() && this.isInGameMode;
  69. data.canFire(canFire);
  70. }
  71. /**
  72. * 处理游戏结束状态检查事件
  73. */
  74. private onGameCheckOverEvent(callback: (isOver: boolean) => void) {
  75. callback(this.isGameOver());
  76. }
  77. /**
  78. * 设置游戏状态为暂停
  79. */
  80. public pauseGame(): void {
  81. // 检查是否在游戏中
  82. if (!this.isInGameMode) {
  83. console.log('[GamePause] 不在游戏中,无法暂停');
  84. return;
  85. }
  86. if (this.currentState === GameState.PAUSED) {
  87. console.log('[GamePause] 游戏已经是暂停状态,跳过重复暂停');
  88. return;
  89. }
  90. if (this.isGameOver()) {
  91. console.log('[GamePause] 游戏已结束,无法暂停,当前状态:', this.currentState);
  92. return;
  93. }
  94. console.log('[GamePause] 设置游戏状态为暂停,之前状态:', this.currentState);
  95. this.currentState = GameState.PAUSED;
  96. this.bulletFireEnabled = false;
  97. // 派发暂停事件,让GameManager处理具体动作
  98. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  99. }
  100. /**
  101. * 设置游戏状态为运行
  102. */
  103. public resumeGame(): void {
  104. // 检查是否在游戏中
  105. if (!this.isInGameMode) {
  106. console.log('[GamePause] 不在游戏中,无法恢复');
  107. return;
  108. }
  109. if (this.currentState === GameState.PLAYING) {
  110. console.log('[GamePause] 游戏已经是运行状态,跳过重复恢复');
  111. return;
  112. }
  113. if (this.isGameOver()) {
  114. console.log('[GamePause] 游戏已结束,无法恢复,当前状态:', this.currentState);
  115. return;
  116. }
  117. console.log('[GamePause] 设置游戏状态为运行,之前状态:', this.currentState);
  118. this.currentState = GameState.PLAYING;
  119. this.bulletFireEnabled = true;
  120. // 派发恢复事件,让GameManager处理具体动作
  121. EventBus.getInstance().emit(GameEvents.GAME_RESUME);
  122. }
  123. /**
  124. * 设置游戏状态为成功
  125. */
  126. public triggerGameSuccess(): void {
  127. // 检查是否在游戏中
  128. if (!this.isInGameMode) {
  129. console.log('[GamePause] 不在游戏中,无法触发游戏成功');
  130. return;
  131. }
  132. if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) {
  133. return;
  134. }
  135. console.log('[GamePause] 设置游戏状态为成功');
  136. this.currentState = GameState.SUCCESS;
  137. this.bulletFireEnabled = false;
  138. // 派发游戏成功事件,让GameManager处理具体动作
  139. EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
  140. }
  141. /**
  142. * 设置游戏状态为失败
  143. */
  144. public triggerGameDefeat(): void {
  145. // 检查是否在游戏中
  146. if (!this.isInGameMode) {
  147. console.log('[GamePause] 不在游戏中,无法触发游戏失败');
  148. return;
  149. }
  150. if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) {
  151. return;
  152. }
  153. console.log('[GamePause] 设置游戏状态为失败');
  154. this.currentState = GameState.DEFEAT;
  155. this.bulletFireEnabled = false;
  156. // 派发游戏失败事件,让GameManager处理具体动作
  157. EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
  158. }
  159. /**
  160. * 敌人被击杀事件 - 只管理状态,不执行具体动作
  161. */
  162. public onEnemyKilled(): void {
  163. // 如果游戏已经结束,不执行后续逻辑
  164. if (this.isGameOver()) {
  165. console.warn('[GamePause] 游戏已结束状态下onEnemyKilled被调用!当前状态:', this.currentState);
  166. return;
  167. }
  168. // 派发敌人被击杀事件,让GameManager处理具体逻辑
  169. // 使用自定义事件名称,避免依赖GameEvents中可能不存在的事件
  170. EventBus.getInstance().emit('ENEMY_KILLED');
  171. }
  172. /**
  173. * 检查游戏是否结束
  174. */
  175. public isGameOver(): boolean {
  176. return this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT;
  177. }
  178. /**
  179. * 重置游戏状态
  180. */
  181. public resetGameState(): void {
  182. console.log('[GamePause] 重置游戏状态');
  183. this.currentState = GameState.PLAYING;
  184. this.bulletFireEnabled = true;
  185. console.log('[GamePause] 游戏状态重置完成');
  186. }
  187. /**
  188. * 检查是否为暂停状态
  189. */
  190. public isPaused(): boolean {
  191. return this.currentState === GameState.PAUSED;
  192. }
  193. /**
  194. * 检查是否允许发射子弹
  195. */
  196. public isBulletFireEnabled(): boolean {
  197. return this.bulletFireEnabled;
  198. }
  199. /**
  200. * 获取当前游戏状态
  201. */
  202. public getCurrentState(): GameState {
  203. return this.currentState;
  204. }
  205. /**
  206. * 强制设置游戏状态(谨慎使用)
  207. */
  208. public forceSetState(state: GameState): void {
  209. this.currentState = state;
  210. this.bulletFireEnabled = (state === GameState.PLAYING);
  211. console.log(`[GamePause] 强制设置状态为: ${state}`);
  212. }
  213. /**
  214. * 获取单例实例
  215. */
  216. public static getInstance(): GamePause {
  217. if (!GamePause._instance) {
  218. // 创建节点和组件
  219. const gamePauseNode = new Node('GamePause');
  220. GamePause._instance = gamePauseNode.addComponent(GamePause);
  221. // 将节点添加到场景中(不销毁)
  222. const scene = director.getScene();
  223. if (scene) {
  224. scene.addChild(gamePauseNode);
  225. }
  226. }
  227. return GamePause._instance;
  228. }
  229. onDestroy() {
  230. // 清理事件监听
  231. const eventBus = EventBus.getInstance();
  232. eventBus.off(GameEvents.RESET_GAME_PAUSE, this.onResetGamePauseEvent, this);
  233. eventBus.off(GameEvents.GAME_START, this.onGameStartEvent, this);
  234. eventBus.off(GameEvents.GAME_CHECK_OVER, this.onGameCheckOverEvent, this);
  235. eventBus.off(GameEvents.SHOW_BLOCK_SELECTION, this.onGameEndEvent, this);
  236. GamePause._instance = null;
  237. }
  238. }