GamePause.ts 9.4 KB

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