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