import { _decorator, Component, Node, director } from 'cc'; import EventBus, { GameEvents } from '../Core/EventBus'; import { GameState } from '../LevelSystem/IN_game'; const { ccclass } = _decorator; /** * 游戏状态管理器 * 纯粹的状态管理,不直接执行动作 */ @ccclass('GamePause') export class GamePause extends Component { public static _instance: GamePause = null; // 当前游戏状态 private currentState: GameState = GameState.PLAYING; // 是否允许小球发射子弹(暂停时禁用) private bulletFireEnabled: boolean = true; // 游戏是否在进行中(用于替代对GameManager的直接依赖) private isInGameMode: boolean = false; start() { // 初始化状态 this.resetGameState(); // 设置事件监听器 this.setupEventListeners(); } /** * 设置事件监听器 */ private setupEventListeners() { const eventBus = EventBus.getInstance(); // 监听游戏暂停重置事件 eventBus.on(GameEvents.RESET_GAME_PAUSE, this.onResetGamePauseEvent, this); // 监听游戏开始和结束事件来更新游戏模式状态 eventBus.on(GameEvents.GAME_START, this.onGameStartEvent, this); // 监听游戏暂停事件 eventBus.on(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this); eventBus.on(GameEvents.GAME_PAUSE_SKILL_SELECTION, this.onGamePauseEvent, this); eventBus.on(GameEvents.GAME_PAUSE_BLOCK_SELECTION, this.onGamePauseEvent, this); // 监听游戏恢复事件 eventBus.on(GameEvents.GAME_RESUME, this.onGameResumeEvent, this); eventBus.on(GameEvents.GAME_RESUME_SKILL_SELECTION, this.onGameResumeEvent, this); eventBus.on(GameEvents.GAME_RESUME_BLOCK_SELECTION, this.onGameResumeEvent, this); // 监听子弹发射检查事件 eventBus.on(GameEvents.BALL_FIRE_BULLET, this.onBallFireBulletEvent, this); // 监听游戏结束状态检查事件 eventBus.on(GameEvents.GAME_CHECK_OVER, this.onGameCheckOverEvent, this); // 监听游戏结束事件来重置游戏模式状态 eventBus.on('GAME_END', this.onGameEndEvent, this); } /** * 处理重置游戏暂停事件 */ private onResetGamePauseEvent() { console.log('[GamePause] 接收到重置游戏暂停事件,重置游戏状态'); this.resetGameState(); // 重置游戏模式状态 this.isInGameMode = false; console.log('[GamePause] 游戏模式状态已重置为false'); } /** * 处理游戏开始事件 */ private onGameStartEvent() { console.log('[GamePause] 接收到游戏开始事件,设置为游戏模式'); this.isInGameMode = true; } /** * 处理游戏暂停事件 */ private onGamePauseEvent() { console.log('[GamePause] 接收到游戏暂停事件'); this.pauseGame(); } /** * 处理游戏恢复事件 */ private onGameResumeEvent() { console.log('[GamePause] 接收到游戏恢复事件'); this.resumeGame(); } /** * 处理游戏结束事件 */ private onGameEndEvent() { console.log('[GamePause] 接收到游戏结束事件,退出游戏模式'); this.isInGameMode = false; } /** * 处理子弹发射检查事件 */ private onBallFireBulletEvent(data: { canFire: (value: boolean) => void }) { // 检查是否允许发射子弹 const canFire = this.bulletFireEnabled && !this.isGameOver() && this.isInGameMode; data.canFire(canFire); } /** * 处理游戏结束状态检查事件 */ private onGameCheckOverEvent(callback: (isOver: boolean) => void) { callback(this.isGameOver()); } /** * 设置游戏状态为暂停 */ public pauseGame(): void { // 检查是否在游戏中 if (!this.isInGameMode) { console.log('[GamePause] 不在游戏中,无法暂停'); return; } if (this.currentState === GameState.PAUSED) { console.log('[GamePause] 游戏已经是暂停状态,跳过重复暂停'); return; } // 移除游戏结束检查,允许游戏结束时也能暂停 // 这样可以在成功/失败时暂停所有游戏对象的行为 console.log('[GamePause] 设置游戏状态为暂停,之前状态:', this.currentState); this.currentState = GameState.PAUSED; this.bulletFireEnabled = false; // 派发暂停事件,让GameManager处理具体动作 EventBus.getInstance().emit(GameEvents.GAME_PAUSE); } /** * 设置游戏状态为运行 */ public resumeGame(): void { // 检查是否在游戏中 if (!this.isInGameMode) { console.log('[GamePause] 不在游戏中,无法恢复'); return; } if (this.currentState === GameState.PLAYING) { console.log('[GamePause] 游戏已经是运行状态,跳过重复恢复'); return; } if (this.isGameOver()) { console.log('[GamePause] 游戏已结束,无法恢复,当前状态:', this.currentState); return; } console.log('[GamePause] 设置游戏状态为运行,之前状态:', this.currentState); this.currentState = GameState.PLAYING; this.bulletFireEnabled = true; // 派发恢复事件,让GameManager处理具体动作 EventBus.getInstance().emit(GameEvents.GAME_RESUME); } /** * 设置游戏状态为成功 */ public triggerGameSuccess(): void { // 检查是否在游戏中 if (!this.isInGameMode) { console.log('[GamePause] 不在游戏中,无法触发游戏成功'); return; } if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) { return; } console.log('[GamePause] 设置游戏状态为成功'); this.currentState = GameState.SUCCESS; this.bulletFireEnabled = false; // 派发游戏成功事件,让GameManager处理具体动作 EventBus.getInstance().emit(GameEvents.GAME_SUCCESS); } /** * 设置游戏状态为失败 */ public triggerGameDefeat(): void { // 检查是否在游戏中 if (!this.isInGameMode) { console.log('[GamePause] 不在游戏中,无法触发游戏失败'); return; } if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) { return; } console.log('[GamePause] 设置游戏状态为失败'); this.currentState = GameState.DEFEAT; this.bulletFireEnabled = false; // 派发游戏失败事件,让GameManager处理具体动作 EventBus.getInstance().emit(GameEvents.GAME_DEFEAT); } /** * 敌人被击杀事件 - 只管理状态,不执行具体动作 */ public onEnemyKilled(): void { // 如果游戏已经结束,不执行后续逻辑 if (this.isGameOver()) { console.warn('[GamePause] 游戏已结束状态下onEnemyKilled被调用!当前状态:', this.currentState); return; } // 派发敌人被击杀事件,让GameManager处理具体逻辑 // 使用自定义事件名称,避免依赖GameEvents中可能不存在的事件 EventBus.getInstance().emit('ENEMY_KILLED'); } /** * 检查游戏是否结束 */ public isGameOver(): boolean { return this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT; } /** * 重置游戏状态 */ public resetGameState(): void { console.log('[GamePause] 重置游戏状态'); this.currentState = GameState.PLAYING; this.bulletFireEnabled = true; console.log('[GamePause] 游戏状态重置完成'); } /** * 检查是否为暂停状态 */ public isPaused(): boolean { return this.currentState === GameState.PAUSED; } /** * 检查是否允许发射子弹 */ public isBulletFireEnabled(): boolean { return this.bulletFireEnabled; } /** * 获取当前游戏状态 */ public getCurrentState(): GameState { return this.currentState; } /** * 强制设置游戏状态(谨慎使用) */ public forceSetState(state: GameState): void { this.currentState = state; this.bulletFireEnabled = (state === GameState.PLAYING); console.log(`[GamePause] 强制设置状态为: ${state}`); } /** * 获取单例实例 */ public static getInstance(): GamePause { if (!GamePause._instance) { // 创建节点和组件 const gamePauseNode = new Node('GamePause'); GamePause._instance = gamePauseNode.addComponent(GamePause); // 将节点添加到场景中(不销毁) const scene = director.getScene(); if (scene) { scene.addChild(gamePauseNode); } } return GamePause._instance; } onDestroy() { // 清理事件监听 const eventBus = EventBus.getInstance(); eventBus.off(GameEvents.RESET_GAME_PAUSE, this.onResetGamePauseEvent, this); eventBus.off(GameEvents.GAME_START, this.onGameStartEvent, this); eventBus.off(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this); eventBus.off(GameEvents.GAME_PAUSE_SKILL_SELECTION, this.onGamePauseEvent, this); eventBus.off(GameEvents.GAME_PAUSE_BLOCK_SELECTION, this.onGamePauseEvent, this); eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this); eventBus.off(GameEvents.GAME_RESUME_SKILL_SELECTION, this.onGameResumeEvent, this); eventBus.off(GameEvents.GAME_RESUME_BLOCK_SELECTION, this.onGameResumeEvent, this); eventBus.off(GameEvents.BALL_FIRE_BULLET, this.onBallFireBulletEvent, this); eventBus.off(GameEvents.GAME_CHECK_OVER, this.onGameCheckOverEvent, this); eventBus.off('GAME_END', this.onGameEndEvent, this); GamePause._instance = null; } }