GamePause.ts 9.6 KB

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