Browse Source

重构完成

181404010226 5 months ago
parent
commit
bdcb57554c

+ 9 - 0
assets/Weapons.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "651974e3-5c4b-432a-9cc3-fd7a03270fa4",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 9 - 0
assets/resources/images/SkillImages.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "118ba183-fad6-4fb2-882e-f75fe8668525",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 9 - 0
assets/resources/prefabs.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "48b78e11-cbc9-47ef-b14c-d8c2eb51c733",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 9 - 0
assets/resources/prefabs/UI.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "5b9a5e9e-2460-4c4e-b901-bac60223b00e",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 9 - 0
assets/scripts/CombatSystem/Ball.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "71cb3f21-b75f-4c90-a546-16b26d9d9014",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 49 - 0
assets/scripts/CombatSystem/BallController.ts

@@ -3,6 +3,7 @@ import { PhysicsManager } from '../Core/PhysicsManager';
 import { WeaponBullet, BulletInitData, WeaponConfig } from './WeaponBullet';
 import { EnemyController } from './EnemyController';
 import { GamePause } from './GamePause';
+import EventBus, { GameEvents } from '../Core/EventBus';
 const { ccclass, property } = _decorator;
 
 @ccclass('BallController')
@@ -87,6 +88,42 @@ export class BallController extends Component {
         
         // 只进行初始设置,不创建小球
         this.calculateGameBounds();
+
+        // 监听游戏事件
+        this.setupEventListeners();
+    }
+
+    /**
+     * 设置事件监听器
+     */
+    private setupEventListeners() {
+        const eventBus = EventBus.getInstance();
+        
+        // 监听暂停事件
+        eventBus.on(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this);
+        
+        // 监听恢复事件
+        eventBus.on(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
+    }
+
+    /**
+     * 处理游戏暂停事件
+     */
+    private onGamePauseEvent() {
+        console.log('[BallController] 接收到游戏暂停事件');
+        // 根据需求,暂停时小球继续运动但不发射子弹
+        // 子弹发射的控制已经通过GamePause.isBulletFireEnabled()实现
+        // 这里可以添加其他暂停相关的逻辑
+    }
+
+    /**
+     * 处理游戏恢复事件
+     */
+    private onGameResumeEvent() {
+        console.log('[BallController] 接收到游戏恢复事件');
+        // 恢复时重新启用子弹发射
+        // 子弹发射的控制已经通过GamePause.isBulletFireEnabled()实现
+        // 这里可以添加其他恢复相关的逻辑
     }
 
     // 计算游戏边界(使用GameArea节点)
@@ -523,6 +560,11 @@ export class BallController extends Component {
             return;
         }
         
+        // 检查游戏是否已经结束,结束时不发射子弹
+        if (gamePause && gamePause.isGameOver()) {
+            return;
+        }
+        
         // 判断哪个是小球,哪个是方块
         let ballNode: Node = null;
         let blockNode: Node = null;
@@ -985,4 +1027,11 @@ export class BallController extends Component {
         const weaponConfig: WeaponConfig | null = (blockNode as any)['weaponConfig'] || null;
         this.createAndFireBullet(fireWorldPos, weaponConfig);
     }
+
+    onDestroy() {
+        // 清理事件监听
+        const eventBus = EventBus.getInstance();
+        eventBus.off(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this);
+        eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
+    }
 } 

+ 4 - 11
assets/scripts/CombatSystem/BlockSelection/GameBlockSelection.ts

@@ -3,7 +3,6 @@ import { LevelSessionManager } from '../../Core/LevelSessionManager';
 import { BallController } from '../BallController';
 import { BlockManager } from '../BlockManager';
 import { GameStartMove } from '../../Animations/GameStartMove';
-import { GameManager } from '../../LevelSystem/GameManager';
 import { GamePause } from '../GamePause';
 import { BlockTag } from './BlockTag';
 const { ccclass, property } = _decorator;
@@ -92,7 +91,6 @@ export class GameBlockSelection extends Component {
     private ballController: BallController = null;
     private blockManager: BlockManager = null;
     private gameStartMove: GameStartMove = null;
-    private gameManager: GameManager = null;
 
     // 回调函数,用于通知GameManager
     public onConfirmCallback: () => void = null;
@@ -150,12 +148,6 @@ export class GameBlockSelection extends Component {
             this.coinLabelNode = find('Canvas-001/TopArea/CoinNode/CoinLabel');
         }
 
-        // 获取GameManager实例
-        const gameManagerNode = find('Canvas/GameManager');
-        if (gameManagerNode) {
-            this.gameManager = gameManagerNode.getComponent(GameManager);
-        }
-
         // 绑定按钮事件
         this.bindButtonEvents();
         
@@ -334,9 +326,10 @@ export class GameBlockSelection extends Component {
 
     // 显示方块选择UI(用于游戏开始或下一波)
     public showBlockSelection(isNextWave: boolean = false) {
-        // 检查游戏是否已结束(胜利或失败),如果是则不显示方块选择UI
-        if (this.gameManager && this.gameManager.isGameOver()) {
-            console.warn('[GameBlockSelection] 游戏已经结束(胜利或失败),不显示方块选择UI');
+        // 检查游戏是否已结束,使用GamePause来检查状态
+        const gamePause = GamePause.getInstance();
+        if (gamePause && gamePause.isGameOver()) {
+            console.warn('[GameBlockSelection] 游戏已经结束,不显示方块选择UI');
             return;
         }
         

+ 170 - 57
assets/scripts/CombatSystem/EnemyController.ts

@@ -1,11 +1,12 @@
-import { _decorator, Node, Label, Vec3, Prefab, instantiate, find, UITransform, resources } from 'cc';
+import { _decorator, Node, Label, Vec3, Prefab, instantiate, find, UITransform, resources, RigidBody2D } from 'cc';
 import { sp } from 'cc';
 import { ConfigManager, EnemyConfig } from '../Core/ConfigManager';
 import { EnemyComponent } from '../CombatSystem/EnemyComponent';
 import { EnemyInstance } from './EnemyInstance';
 import { BaseSingleton } from '../Core/BaseSingleton';
 import { SaveDataManager } from '../LevelSystem/SaveDataManager';
-import { GameManager } from '../LevelSystem/GameManager';
+import { GamePause } from './GamePause';
+import EventBus, { GameEvents } from '../Core/EventBus';
 const { ccclass, property } = _decorator;
 
 // 前向声明EnemyInstance类型,避免循环引用
@@ -79,9 +80,6 @@ export class EnemyController extends BaseSingleton {
     // 墙体节点
     private wallNodes: Node[] = [];
 
-    // 私有属性
-    private gameManager: any = null;
-
     // 配置管理器
     private configManager: ConfigManager = null;
 
@@ -108,6 +106,14 @@ export class EnemyController extends BaseSingleton {
     private currentWaveTotalEnemies: number = 0;
     private currentWaveEnemiesKilled: number = 0;
 
+    // 暂停前保存的敌人状态
+    private pausedEnemyStates: Map<Node, {
+        velocity: any,
+        angularVelocity: number,
+        isMoving: boolean,
+        attackTimer: number
+    }> = new Map();
+
     /**
      * BaseSingleton 首次实例化回调
      */
@@ -139,9 +145,6 @@ export class EnemyController extends BaseSingleton {
         
         // 确保enemyContainer节点存在
         this.ensureEnemyContainer();
-        
-        // 查找GameManager
-        this.findGameManager();
 
         // 如果没有指定enemyCountLabelNode,尝试找到它
         if (!this.enemyCountLabelNode) {
@@ -155,6 +158,133 @@ export class EnemyController extends BaseSingleton {
         }
         // 初始化敌人数量显示
         this.updateEnemyCountLabel();
+
+        // 监听游戏事件
+        this.setupEventListeners();
+    }
+
+    /**
+     * 设置事件监听器
+     */
+    private setupEventListeners() {
+        const eventBus = EventBus.getInstance();
+        
+        // 监听暂停事件
+        eventBus.on(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this);
+        
+        // 监听恢复事件
+        eventBus.on(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
+        
+        // 监听游戏成功/失败事件
+        eventBus.on(GameEvents.GAME_SUCCESS, this.onGameEndEvent, this);
+        eventBus.on(GameEvents.GAME_DEFEAT, this.onGameEndEvent, this);
+    }
+
+    /**
+     * 处理游戏暂停事件
+     */
+    private onGamePauseEvent() {
+        console.log('[EnemyController] 接收到游戏暂停事件,暂停所有敌人');
+        this.pauseAllEnemies();
+        this.pauseSpawning();
+    }
+
+    /**
+     * 处理游戏恢复事件
+     */
+    private onGameResumeEvent() {
+        console.log('[EnemyController] 接收到游戏恢复事件,恢复所有敌人');
+        this.resumeAllEnemies();
+        
+        // 检查游戏是否已经结束,如果结束则不恢复敌人生成
+        const gamePause = GamePause.getInstance();
+        if (!gamePause.isGameOver()) {
+            this.resumeSpawning();
+        }
+    }
+
+    /**
+     * 处理游戏结束事件
+     */
+    private onGameEndEvent() {
+        console.log('[EnemyController] 接收到游戏结束事件,停止敌人生成');
+        this.stopGame(false); // 停止游戏但不清除敌人
+    }
+
+    /**
+     * 暂停所有敌人
+     */
+    private pauseAllEnemies(): void {
+        const activeEnemies = this.getActiveEnemies();
+        
+        for (const enemy of activeEnemies) {
+            if (!enemy || !enemy.isValid) continue;
+
+            // 保存敌人状态
+            const rigidBody = enemy.getComponent(RigidBody2D);
+            if (rigidBody) {
+                this.pausedEnemyStates.set(enemy, {
+                    velocity: rigidBody.linearVelocity.clone(),
+                    angularVelocity: rigidBody.angularVelocity,
+                    isMoving: true,
+                    attackTimer: 0 // 可以扩展保存攻击计时器
+                });
+
+                // 停止敌人运动
+                rigidBody.linearVelocity.set(0, 0);
+                rigidBody.angularVelocity = 0;
+                rigidBody.sleep();
+            }
+
+            // 暂停敌人AI组件(如果有的话)
+            const enemyAI = enemy.getComponent('EnemyAI');
+            if (enemyAI && typeof (enemyAI as any).pause === 'function') {
+                (enemyAI as any).pause();
+            }
+
+            // 暂停敌人动画(如果有的话)
+            const animation = enemy.getComponent('Animation');
+            if (animation && typeof (animation as any).pause === 'function') {
+                (animation as any).pause();
+            }
+        }
+    }
+
+    /**
+     * 恢复所有敌人
+     */
+    private resumeAllEnemies(): void {
+        const activeEnemies = this.getActiveEnemies();
+        
+        for (const enemy of activeEnemies) {
+            if (!enemy || !enemy.isValid) continue;
+
+            const savedState = this.pausedEnemyStates.get(enemy);
+            if (savedState) {
+                const rigidBody = enemy.getComponent(RigidBody2D);
+                if (rigidBody) {
+                    // 恢复敌人运动
+                    rigidBody.wakeUp();
+                    rigidBody.linearVelocity = savedState.velocity;
+                    rigidBody.angularVelocity = savedState.angularVelocity;
+                }
+            }
+
+            // 恢复敌人AI组件
+            const enemyAI = enemy.getComponent('EnemyAI');
+            if (enemyAI && typeof (enemyAI as any).resume === 'function') {
+                (enemyAI as any).resume();
+            }
+
+            // 恢复敌人动画
+            const animation = enemy.getComponent('Animation');
+            if (animation && typeof (animation as any).resume === 'function') {
+                (animation as any).resume();
+            }
+        }
+
+        // 清空保存的状态
+        this.pausedEnemyStates.clear();
     }
 
     // 计算游戏区域边界
@@ -251,7 +381,8 @@ export class EnemyController extends BaseSingleton {
     // 游戏开始
     startGame() {
         // 检查游戏是否已经结束,如果结束则不开始生成敌人
-        if (this.gameManager && typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver()) {
+        const gamePause = GamePause.getInstance();
+        if (gamePause.isGameOver()) {
             console.log('[EnemyController] 游戏已经结束,不启动敌人生成');
             return;
         }
@@ -277,7 +408,8 @@ export class EnemyController extends BaseSingleton {
         // 只有在指定时才清除所有敌人
         if (clearEnemies) {
             // 当游戏结束时清除敌人,不触发敌人死亡事件
-            const isGameOver = this.gameManager && typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver();
+            const gamePause = GamePause.getInstance();
+            const isGameOver = gamePause.isGameOver();
             this.clearAllEnemies(!isGameOver); // 只有在游戏没有结束时才触发事件
         }
         
@@ -289,7 +421,8 @@ export class EnemyController extends BaseSingleton {
         if (!this.gameStarted || !this.enemyPrefab) return;
         
         // 检查游戏是否已经结束,如果结束则不生成敌人
-        if (this.gameManager && typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver()) {
+        const gamePause = GamePause.getInstance();
+        if (gamePause.isGameOver()) {
             console.log('[EnemyController] 游戏已经结束,不再生成敌人');
             this.unschedule(this.spawnEnemy); // 取消定时生成
             return;
@@ -458,7 +591,8 @@ export class EnemyController extends BaseSingleton {
     // 恢复生成敌人
     public resumeSpawning(): void {
         // 检查游戏是否已经结束,如果结束则不恢复生成敌人
-        if (this.gameManager && typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver()) {
+        const gamePause = GamePause.getInstance();
+        if (gamePause.isGameOver()) {
             console.log('[EnemyController] 游戏已经结束,不恢复敌人生成');
             return;
         }
@@ -473,7 +607,8 @@ export class EnemyController extends BaseSingleton {
         if (!enemy || !enemy.isValid) return;
         
         // 检查游戏是否已经结束
-        if (this.gameManager && typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver()) {
+        const gamePause = GamePause.getInstance();
+        if (gamePause.isGameOver()) {
             console.warn('[EnemyController] 游戏已经结束,不再处理敌人伤害');
             return;
         }
@@ -519,19 +654,9 @@ export class EnemyController extends BaseSingleton {
         // 停止游戏,但不清除敌人
         this.stopGame(false);
         
-        // 通知GameManager游戏结束
-        if (this.gameManager) {
-            this.gameManager.gameOver();
-        } else {
-            // 如果没有引用,尝试查找GameManager
-            const gameManagerNode = find('Canvas/GameManager');
-        if (gameManagerNode) {
-                const gameManager = gameManagerNode.getComponent(GameManager);
-            if (gameManager) {
-                gameManager.gameOver();
-                }
-            }
-        }
+        // 通知GamePause触发游戏失败
+        const gamePause = GamePause.getInstance();
+        gamePause.triggerGameDefeat();
     }
 
     update(dt: number) {
@@ -583,24 +708,6 @@ export class EnemyController extends BaseSingleton {
         }
     }
 
-    // === 查找GameManager ===
-    private findGameManager() {
-        // 尝试在GameLevelUI下查找GameManager
-        let gameManagerNode = find('Canvas/GameLevelUI/GameManager');
-        if (!gameManagerNode) {
-            // 如果没找到,尝试在Canvas下直接查找
-            gameManagerNode = find('Canvas/GameManager');
-        }
-        
-        if (gameManagerNode) {
-            // 使用正确的类型获取组件
-            this.gameManager = gameManagerNode.getComponent(GameManager);
-            console.log('[EnemyController] GameManager引用已更新:', !!this.gameManager);
-        } else {
-            console.warn('[EnemyController] 找不到GameManager节点');
-        }
-    }
-
     /** 供 EnemyInstance 在 onDestroy 中调用 */
     public notifyEnemyDead(enemyNode?: Node) {
         if (enemyNode) {
@@ -610,17 +717,9 @@ export class EnemyController extends BaseSingleton {
             this.updateEnemyCountLabel();
         }
         
-        if (this.gameManager) {
-            // 检查游戏是否已经结束
-            if (typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver()) {
-                console.warn('[EnemyController] 游戏已经结束,不再通知GameManager敌人被击杀');
-                return;
-            }
-            
-            if (this.gameManager.onEnemyKilled) {
-            this.gameManager.onEnemyKilled();
-            }
-        }
+        // 通知GamePause处理敌人被击杀
+        const gamePause = GamePause.getInstance();
+        gamePause.onEnemyKilled();
     }
 
     /**
@@ -693,7 +792,8 @@ export class EnemyController extends BaseSingleton {
     /** 显示每波开始提示,随后开启敌人生成 */
     public showStartWavePromptUI(duration: number = 2) {
         // 检查游戏是否已经结束,如果结束则不显示波次提示
-        if (this.gameManager && typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver()) {
+        const gamePause = GamePause.getInstance();
+        if (gamePause.isGameOver()) {
             console.log('[EnemyController] 游戏已经结束,不显示波次提示');
             return;
         }
@@ -707,7 +807,8 @@ export class EnemyController extends BaseSingleton {
                 if (this.startWaveUI) this.startWaveUI.active = false;
                 
                 // 再次检查游戏是否已经结束,如果结束则不开始生成敌人
-                if (this.gameManager && typeof this.gameManager.isGameOver === 'function' && this.gameManager.isGameOver()) {
+                const gamePauseCheck = GamePause.getInstance();
+                if (gamePauseCheck.isGameOver()) {
                     console.log('[EnemyController] 游戏已经结束,不启动敌人生成(延时检查)');
                     return;
                 }
@@ -719,4 +820,16 @@ export class EnemyController extends BaseSingleton {
             this.startGame();
         }
     }
+
+    onDestroy() {
+        // 清理事件监听
+        const eventBus = EventBus.getInstance();
+        eventBus.off(GameEvents.GAME_PAUSE, this.onGamePauseEvent, this);
+        eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
+        eventBus.off(GameEvents.GAME_SUCCESS, this.onGameEndEvent, this);
+        eventBus.off(GameEvents.GAME_DEFEAT, this.onGameEndEvent, this);
+        
+        // 清空状态
+        this.pausedEnemyStates.clear();
+    }
 } 

+ 66 - 227
assets/scripts/CombatSystem/GamePause.ts

@@ -1,290 +1,137 @@
-import { _decorator, Component, Node, RigidBody2D, find } from 'cc';
-import { EnemyController } from './EnemyController';
-import { BallController } from './BallController';
+import { _decorator, Component, Node, director } from 'cc';
 import EventBus, { GameEvents } from '../Core/EventBus';
 
 const { ccclass, property } = _decorator;
 
 /**
- * 游戏暂停状态枚举
+ * 游戏状态枚举
  */
-export enum PauseState {
+export enum GameState {
     PLAYING = 'playing',
-    PAUSED = 'paused'
+    PAUSED = 'paused',
+    SUCCESS = 'success',
+    DEFEAT = 'defeat'
 }
 
 /**
- * 游戏暂停管理器
- * 统一管理游戏的暂停和恢复逻辑
+ * 游戏状态管理器
+ * 纯粹的状态管理,不直接执行动作
  */
 @ccclass('GamePause')
 export class GamePause extends Component {
     public static _instance: GamePause = null;
 
-    // 当前暂停状态
-    private currentState: PauseState = PauseState.PLAYING;
-
-    // 暂停前保存的敌人状态
-    private pausedEnemyStates: Map<Node, {
-        velocity: any,
-        angularVelocity: number,
-        isMoving: boolean,
-        attackTimer: number
-    }> = new Map();
-
-    // 暂停前保存的小球状态
-    private pausedBallStates: Map<Node, {
-        velocity: any,
-        angularVelocity: number
-    }> = new Map();
+    // 当前游戏状态
+    private currentState: GameState = GameState.PLAYING;
 
     // 是否允许小球发射子弹(暂停时禁用)
     private bulletFireEnabled: boolean = true;
 
     start() {
-        // 监听游戏事件
-        const eventBus = EventBus.getInstance();
-        eventBus.on(GameEvents.GAME_SUCCESS, this.onGameEnd, this);
-        eventBus.on(GameEvents.GAME_DEFEAT, this.onGameEnd, this);
+        // 初始化状态
+        this.resetGameState();
     }
 
     /**
-     * 暂停游戏
+     * 设置游戏状态为暂停
      */
     public pauseGame(): void {
-        if (this.currentState === PauseState.PAUSED) {
+        if (this.currentState === GameState.PAUSED || this.isGameOver()) {
             return;
         }
 
-        console.log('GamePause: 暂停游戏');
-        this.currentState = PauseState.PAUSED;
-
-        // 禁用小球发射子弹
+        console.log('[GamePause] 设置游戏状态为暂停');
+        this.currentState = GameState.PAUSED;
         this.bulletFireEnabled = false;
 
-        // 暂停敌人
-        this.pauseAllEnemies();
-
-        // 暂停敌人生成
-        this.pauseEnemySpawning();
-
-        // 小球继续运动但不发射子弹(根据新需求)
-        // 不调用 pauseBalls() 方法
-
-        // 派发暂停事件
+        // 派发暂停事件,让GameManager处理具体动作
         EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
     }
 
     /**
-     * 恢复游戏
+     * 设置游戏状态为运行
      */
     public resumeGame(): void {
-        if (this.currentState === PauseState.PLAYING) {
+        if (this.currentState === GameState.PLAYING || this.isGameOver()) {
             return;
         }
 
-        console.log('GamePause: 恢复游戏');
-        this.currentState = PauseState.PLAYING;
-
-        // 启用小球发射子弹
+        console.log('[GamePause] 设置游戏状态为运行');
+        this.currentState = GameState.PLAYING;
         this.bulletFireEnabled = true;
 
-        // 恢复敌人
-        this.resumeAllEnemies();
-
-        // 检查游戏是否已经结束,如果结束则不恢复敌人生成
-        const gameManager = find('Canvas/GameManager')?.getComponent('GameManager');
-        if (gameManager && typeof (gameManager as any).isGameOver === 'function' && (gameManager as any).isGameOver()) {
-            console.log('[GamePause] 游戏已经结束,不恢复敌人生成');
-        } else {
-            // 恢复敌人生成
-            this.resumeEnemySpawning();
-        }
-
-        // 恢复小球(如果之前被暂停的话)
-        this.resumeAllBalls();
-
-        // 派发恢复事件
+        // 派发恢复事件,让GameManager处理具体动作
         EventBus.getInstance().emit(GameEvents.GAME_RESUME);
     }
 
     /**
-     * 暂停所有敌人
+     * 设置游戏状态为成功
      */
-    private pauseAllEnemies(): void {
-        const enemyController = EnemyController.getInstance();
-        if (!enemyController) return;
-
-        const activeEnemies = enemyController.getActiveEnemies?.() || [];
-        
-        for (const enemy of activeEnemies) {
-            if (!enemy || !enemy.isValid) continue;
-
-            // 保存敌人状态
-            const rigidBody = enemy.getComponent(RigidBody2D);
-            if (rigidBody) {
-                this.pausedEnemyStates.set(enemy, {
-                    velocity: rigidBody.linearVelocity.clone(),
-                    angularVelocity: rigidBody.angularVelocity,
-                    isMoving: true,
-                    attackTimer: 0 // 可以扩展保存攻击计时器
-                });
-
-                // 停止敌人运动
-                rigidBody.linearVelocity.set(0, 0);
-                rigidBody.angularVelocity = 0;
-                rigidBody.sleep();
-            }
-
-            // 暂停敌人AI组件(如果有的话)
-            const enemyAI = enemy.getComponent('EnemyAI');
-            if (enemyAI && typeof (enemyAI as any).pause === 'function') {
-                (enemyAI as any).pause();
-            }
-
-            // 暂停敌人动画(如果有的话)
-            const animation = enemy.getComponent('Animation');
-            if (animation && typeof (animation as any).pause === 'function') {
-                (animation as any).pause();
-            }
+    public triggerGameSuccess(): void {
+        if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) {
+            return;
         }
-    }
-
-    /**
-     * 恢复所有敌人
-     */
-    private resumeAllEnemies(): void {
-        const enemyController = EnemyController.getInstance();
-        if (!enemyController) return;
-
-        const activeEnemies = enemyController.getActiveEnemies?.() || [];
-        
-        for (const enemy of activeEnemies) {
-            if (!enemy || !enemy.isValid) continue;
-
-            const savedState = this.pausedEnemyStates.get(enemy);
-            if (savedState) {
-                const rigidBody = enemy.getComponent(RigidBody2D);
-                if (rigidBody) {
-                    // 恢复敌人运动
-                    rigidBody.wakeUp();
-                    rigidBody.linearVelocity = savedState.velocity;
-                    rigidBody.angularVelocity = savedState.angularVelocity;
-                }
-            }
 
-            // 恢复敌人AI组件
-            const enemyAI = enemy.getComponent('EnemyAI');
-            if (enemyAI && typeof (enemyAI as any).resume === 'function') {
-                (enemyAI as any).resume();
-            }
-
-            // 恢复敌人动画
-            const animation = enemy.getComponent('Animation');
-            if (animation && typeof (animation as any).resume === 'function') {
-                (animation as any).resume();
-            }
-        }
+        console.log('[GamePause] 设置游戏状态为成功');
+        this.currentState = GameState.SUCCESS;
+        this.bulletFireEnabled = false;
 
-        // 清空保存的状态
-        this.pausedEnemyStates.clear();
+        // 派发游戏成功事件,让GameManager处理具体动作
+        EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
     }
 
     /**
-     * 暂停敌人生成
+     * 设置游戏状态为失败
      */
-    private pauseEnemySpawning(): void {
-        const enemyController = EnemyController.getInstance();
-        if (enemyController && enemyController.pauseSpawning) {
-            enemyController.pauseSpawning();
+    public triggerGameDefeat(): void {
+        if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) {
+            return;
         }
-    }
 
-    /**
-     * 恢复敌人生成
-     */
-    private resumeEnemySpawning(): void {
-        const enemyController = EnemyController.getInstance();
-        if (enemyController && enemyController.resumeSpawning) {
-            enemyController.resumeSpawning();
-        }
-    }
+        console.log('[GamePause] 设置游戏状态为失败');
+        this.currentState = GameState.DEFEAT;
+        this.bulletFireEnabled = false;
 
-    /**
-     * 暂停所有小球
-     */
-    private pauseAllBalls(): void {
-        // 查找所有小球控制器
-        const ballControllers = this.findAllBallControllers();
-        
-        for (const ballController of ballControllers) {
-            if (ballController && ballController.pauseBall) {
-                ballController.pauseBall();
-            }
-        }
+        // 派发游戏失败事件,让GameManager处理具体动作
+        EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
     }
 
     /**
-     * 恢复所有小球
+     * 敌人被击杀事件 - 只管理状态,不执行具体动作
      */
-    private resumeAllBalls(): void {
-        // 查找所有小球控制器
-        const ballControllers = this.findAllBallControllers();
-        
-        for (const ballController of ballControllers) {
-            if (ballController && ballController.resumeBall) {
-                ballController.resumeBall();
-            }
+    public onEnemyKilled(): void {
+        // 如果游戏已经结束,不执行后续逻辑
+        if (this.isGameOver()) {
+            console.warn('[GamePause] 游戏已结束状态下onEnemyKilled被调用!当前状态:', this.currentState);
+            return;
         }
+
+        // 派发敌人被击杀事件,让GameManager处理具体逻辑
+        // 使用自定义事件名称,避免依赖GameEvents中可能不存在的事件
+        EventBus.getInstance().emit('ENEMY_KILLED');
     }
 
     /**
-     * 查找所有小球控制器
+     * 检查游戏是否结束
      */
-    private findAllBallControllers(): BallController[] {
-        const controllers: BallController[] = [];
-        
-        // 主要的小球控制器
-        const mainBallControllerNode = find('Canvas/GameLevelUI/BallController');
-        if (mainBallControllerNode) {
-            const controller = mainBallControllerNode.getComponent(BallController);
-            if (controller) {
-                controllers.push(controller);
-            }
-        }
-
-        // 可以扩展查找其他小球控制器
-        // 例如:多球模式下的额外控制器
-
-        return controllers;
+    public isGameOver(): boolean {
+        return this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT;
     }
 
     /**
-     * 游戏结束时的处理
+     * 重置游戏状态
      */
-    private onGameEnd(): void {
-        console.log('GamePause: 游戏结束,确保停止敌人生成');
-        
-        // 停止敌人生成
-        const enemyController = EnemyController.getInstance();
-        if (enemyController) {
-            // 确保停止生成敌人
-            enemyController.unschedule(enemyController.spawnEnemy);
-            console.log('GamePause: 游戏结束,已取消敌人生成定时器');
-        }
-        
-        // 设置为暂停状态
-        this.currentState = PauseState.PAUSED;
-        
-        // 禁用子弹发射
-        this.bulletFireEnabled = false;
+    public resetGameState(): void {
+        console.log('[GamePause] 重置游戏状态');
+        this.currentState = GameState.PLAYING;
+        this.bulletFireEnabled = true;
     }
 
     /**
      * 检查是否为暂停状态
      */
     public isPaused(): boolean {
-        return this.currentState === PauseState.PAUSED;
+        return this.currentState === GameState.PAUSED;
     }
 
     /**
@@ -295,18 +142,19 @@ export class GamePause extends Component {
     }
 
     /**
-     * 获取当前暂停状态
+     * 获取当前游戏状态
      */
-    public getCurrentState(): PauseState {
+    public getCurrentState(): GameState {
         return this.currentState;
     }
 
     /**
-     * 强制设置暂停状态(谨慎使用)
+     * 强制设置游戏状态(谨慎使用)
      */
-    public forceSetState(state: PauseState): void {
+    public forceSetState(state: GameState): void {
         this.currentState = state;
-        this.bulletFireEnabled = (state === PauseState.PLAYING);
+        this.bulletFireEnabled = (state === GameState.PLAYING);
+        console.log(`[GamePause] 强制设置状态为: ${state}`);
     }
 
     /**
@@ -319,7 +167,7 @@ export class GamePause extends Component {
             GamePause._instance = gamePauseNode.addComponent(GamePause);
             
             // 将节点添加到场景中(不销毁)
-            const scene = find('Canvas');
+            const scene = director.getScene();
             if (scene) {
                 scene.addChild(gamePauseNode);
             }
@@ -328,15 +176,6 @@ export class GamePause extends Component {
     }
 
     onDestroy() {
-        // 清理事件监听
-        const eventBus = EventBus.getInstance();
-        eventBus.off(GameEvents.GAME_SUCCESS, this.onGameEnd, this);
-        eventBus.off(GameEvents.GAME_DEFEAT, this.onGameEnd, this);
-        
-        // 清空状态
-        this.pausedEnemyStates.clear();
-        this.pausedBallStates.clear();
-        
         GamePause._instance = null;
     }
 }

+ 9 - 0
assets/scripts/LevelSystem/GameManager.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "1.2.0",
+  "importer": "directory",
+  "imported": true,
+  "uuid": "d9952d73-25f7-4427-b4ee-16580482295a",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 132 - 99
assets/scripts/LevelSystem/GameManager.ts

@@ -188,6 +188,115 @@ export class GameManager extends Component {
         
         // 加载当前关卡配置
         this.loadCurrentLevelConfig();
+
+        // 监听GamePause状态变化事件
+        this.setupGamePauseEventListeners();
+    }
+
+    /**
+     * 设置GamePause事件监听器
+     */
+    private setupGamePauseEventListeners() {
+        const eventBus = EventBus.getInstance();
+        
+        // 监听游戏成功事件
+        eventBus.on(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
+        
+        // 监听游戏失败事件
+        eventBus.on(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
+        
+        // 监听敌人被击杀事件
+        eventBus.on('ENEMY_KILLED', this.onEnemyKilledEvent, this);
+    }
+
+    /**
+     * 处理游戏成功事件
+     */
+    private onGameSuccessEvent() {
+        console.log('[GameManager] 接收到游戏成功事件,执行成功处理');
+        
+        // 显示游戏成功UI
+        if (this.gameSuccessUI) {
+            this.gameSuccessUI.active = true;
+        }
+
+        // 执行游戏成功逻辑
+        this.onGameSuccess();
+    }
+
+    /**
+     * 处理游戏失败事件
+     */
+    private onGameDefeatEvent() {
+        console.log('[GameManager] 接收到游戏失败事件,执行失败处理');
+        
+        // 显示游戏失败UI
+        if (this.gameDefeatUI) {
+            this.gameDefeatUI.active = true;
+        }
+
+        // 执行游戏失败逻辑
+        this.onGameDefeat();
+    }
+
+    /**
+     * 处理敌人被击杀事件
+     */
+    private onEnemyKilledEvent() {
+        // 如果游戏已经结束,不执行后续逻辑
+        const gamePause = GamePause.getInstance();
+        if (gamePause.isGameOver()) {
+            console.warn('[GameManager] 游戏已结束状态下onEnemyKilledEvent被调用!当前状态:', gamePause.getCurrentState());
+            return;
+        }
+        
+        this.enemiesKilled++;
+        // 当前波击杀 +1
+        this.currentWaveEnemyCount++;
+
+        // 增加能量点
+        this.incrementEnergy();
+        
+        const remaining = this.currentWaveTotalEnemies - this.currentWaveEnemyCount;
+        console.log(`[GameManager] 敌人被消灭,当前波剩余敌人: ${remaining}/${this.currentWaveTotalEnemies}, 总击杀: ${this.enemiesKilled}`);
+        
+        if (remaining <= 0) {
+            // 当前波结束
+            if (this.currentWave < (this.levelWaves?.length || 1)) {
+                // 还有下一波,显示提示
+                console.log(`[GameManager] 当前波结束,准备显示下一波(${this.currentWave + 1}/${this.levelWaves?.length || 1})选择UI`);
+                this.showNextWavePrompt();
+            } else {
+                // 最后一波也结束
+                console.log('[GameManager] 最后一波结束,触发游戏胜利');
+                this.triggerGameSuccess();
+            }
+        }
+    }
+
+    // === 暂停游戏 ===
+    private pauseGame() {
+        // 使用GamePause统一管理暂停逻辑
+        const gamePause = GamePause.getInstance();
+        if (gamePause) {
+            gamePause.pauseGame();
+        }
+    }
+
+    // === 恢复游戏 ===
+    public resumeGame() {
+        // 使用GamePause统一管理恢复逻辑
+        const gamePause = GamePause.getInstance();
+        if (gamePause) {
+            gamePause.resumeGame();
+        }
+
+        if (this.gameSuccessUI) {
+            this.gameSuccessUI.active = false;
+        }
+        if (this.gameDefeatUI) {
+            this.gameDefeatUI.active = false;
+        }
     }
 
     update(deltaTime: number) {
@@ -447,80 +556,16 @@ export class GameManager extends Component {
 
     // === 触发游戏失败 ===
     private triggerGameDefeat() {
-        if (this.currentState === GameState.DEFEAT) {
-            return;
-        }
-
-        this.currentState = GameState.DEFEAT;
-
-        this.pauseGame();
-
-        if (this.gameDefeatUI) {
-            this.gameDefeatUI.active = true;
-        }
-
-        this.onGameDefeat();
-
-        // 派发游戏失败事件
-        EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
+        // 使用GamePause统一管理游戏状态
+        const gamePause = GamePause.getInstance();
+        gamePause.triggerGameDefeat();
     }
 
     // === 触发游戏成功 ===
     private triggerGameSuccess() {
-        if (this.currentState === GameState.SUCCESS) {
-            return;
-        }
-
-        this.currentState = GameState.SUCCESS;
-
-        // 确保停止敌人生成
-        if (this.enemyController) {
-            this.enemyController.stopGame(false); // 停止游戏但不清除敌人
-            console.log('[GameManager] 游戏成功,已停止敌人生成');
-        }
-
-        this.pauseGame();
-
-        if (this.gameSuccessUI) {
-            this.gameSuccessUI.active = true;
-        }
-
-        this.onGameSuccess();
-
-        // 派发游戏成功事件
-        EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
-    }
-
-    // === 暂停游戏 ===
-    private pauseGame() {
-        // 设置状态为暂停
-        this.currentState = GameState.PAUSED;
-        this.gameStarted = false;
-        
-        // 使用GamePause统一管理暂停逻辑
-        const gamePause = GamePause.getInstance();
-        if (gamePause) {
-            gamePause.pauseGame();
-        }
-    }
-
-    // === 恢复游戏 ===
-    public resumeGame() {
-        this.currentState = GameState.PLAYING;
-        this.gameStarted = true;
-        
-        // 使用GamePause统一管理恢复逻辑
+        // 使用GamePause统一管理游戏状态
         const gamePause = GamePause.getInstance();
-        if (gamePause) {
-            gamePause.resumeGame();
-        }
-
-        if (this.gameSuccessUI) {
-            this.gameSuccessUI.active = false;
-        }
-        if (this.gameDefeatUI) {
-            this.gameDefeatUI.active = false;
-        }
+        gamePause.triggerGameSuccess();
     }
 
     // === 游戏失败回调 ===
@@ -852,6 +897,10 @@ export class GameManager extends Component {
         this.currentWave = 1;
         this.currentWaveEnemyCount = 0;
 
+        // 重置GamePause状态
+        const gamePause = GamePause.getInstance();
+        gamePause.resetGameState();
+
         // 重置能量条
         this.energyPoints = 0;
         if (this.energyBar) {
@@ -898,7 +947,9 @@ export class GameManager extends Component {
     }
 
     public isGameOver(): boolean {
-        return this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT;
+        // 统一使用GamePause来判断游戏是否结束
+        const gamePause = GamePause.getInstance();
+        return gamePause.isGameOver();
     }
 
     public forceGameSuccess() {
@@ -969,6 +1020,12 @@ export class GameManager extends Component {
     }
 
     onDestroy() {
+        // 清理GamePause事件监听
+        const eventBus = EventBus.getInstance();
+        eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
+        eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
+        eventBus.off('ENEMY_KILLED', this.onEnemyKilledEvent, this);
+
         // 清理按钮事件监听
         if (this.gameSuccessUI) {
             const buttons = this.gameSuccessUI.getComponentsInChildren(Button);
@@ -1134,7 +1191,8 @@ export class GameManager extends Component {
     private showBlockSelectionForNextWave() {
         // 如果游戏已经结束,不显示方块选择UI
         if (this.isGameOver()) {
-            console.warn('[GameManager] 游戏已经结束(胜利或失败),不显示下一波方块选择UI!当前状态:', this.currentState);
+            const gamePause = GamePause.getInstance();
+            console.warn('[GameManager] 游戏已经结束(胜利或失败),不显示下一波方块选择UI!当前状态:', gamePause.getCurrentState());
             return;
         }
         
@@ -1152,34 +1210,9 @@ export class GameManager extends Component {
 
     /** 敌人被消灭时由 EnemyController 调用 */
     public onEnemyKilled() {
-        // 如果游戏已经结束,不执行后续逻辑
-        if (this.isGameOver()) {
-            console.warn('[GameManager] 游戏已结束状态下onEnemyKilled被调用!当前状态:', this.currentState);
-            return;
-        }
-        
-        this.enemiesKilled++;
-        // 当前波击杀 +1
-        this.currentWaveEnemyCount++;
-
-        // 增加能量点
-        this.incrementEnergy();
-        
-        const remaining = this.currentWaveTotalEnemies - this.currentWaveEnemyCount;
-        console.log(`[GameManager] 敌人被消灭,当前波剩余敌人: ${remaining}/${this.currentWaveTotalEnemies}, 总击杀: ${this.enemiesKilled}`);
-        
-        if (remaining <= 0) {
-            // 当前波结束
-            if (this.currentWave < (this.levelWaves?.length || 1)) {
-                // 还有下一波,显示提示
-                console.log(`[GameManager] 当前波结束,准备显示下一波(${this.currentWave + 1}/${this.levelWaves?.length || 1})选择UI`);
-                this.showNextWavePrompt();
-            } else {
-                // 最后一波也结束
-                console.log('[GameManager] 最后一波结束,触发游戏胜利');
-                this.triggerGameSuccess();
-            }
-        }
+        // 直接调用GamePause来处理
+        const gamePause = GamePause.getInstance();
+        gamePause.onEnemyKilled();
     }
 
     /** 每击杀敌人调用,能量 +1,并更新进度条。满值时弹出技能选择界面 */