GamePause.ts 10 KB

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