| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import { _decorator, Component, Node, director } from 'cc';
- import EventBus, { GameEvents } from '../Core/EventBus';
- const { ccclass, property } = _decorator;
- /**
- * 游戏状态枚举
- */
- export enum GameState {
- PLAYING = 'playing',
- PAUSED = 'paused',
- SUCCESS = 'success',
- DEFEAT = 'defeat'
- }
- /**
- * 游戏状态管理器
- * 纯粹的状态管理,不直接执行动作
- */
- @ccclass('GamePause')
- export class GamePause extends Component {
- public static _instance: GamePause = null;
- // 当前游戏状态
- private currentState: GameState = GameState.PLAYING;
- // 是否允许小球发射子弹(暂停时禁用)
- private bulletFireEnabled: boolean = true;
- start() {
- // 初始化状态
- this.resetGameState();
- }
- /**
- * 设置游戏状态为暂停
- */
- public pauseGame(): void {
- if (this.currentState === GameState.PAUSED || this.isGameOver()) {
- return;
- }
- console.log('[GamePause] 设置游戏状态为暂停');
- this.currentState = GameState.PAUSED;
- this.bulletFireEnabled = false;
- // 派发暂停事件,让GameManager处理具体动作
- EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
- }
- /**
- * 设置游戏状态为运行
- */
- public resumeGame(): void {
- if (this.currentState === GameState.PLAYING || this.isGameOver()) {
- return;
- }
- console.log('[GamePause] 设置游戏状态为运行');
- this.currentState = GameState.PLAYING;
- this.bulletFireEnabled = true;
- // 派发恢复事件,让GameManager处理具体动作
- EventBus.getInstance().emit(GameEvents.GAME_RESUME);
- }
- /**
- * 设置游戏状态为成功
- */
- public triggerGameSuccess(): void {
- 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.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;
- }
- /**
- * 检查是否为暂停状态
- */
- 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() {
- GamePause._instance = null;
- }
- }
|