Browse Source

解决球的问题

181404010226 4 months ago
parent
commit
6032d5033e

+ 53 - 2
assets/scripts/CombatSystem/BallController.ts

@@ -110,6 +110,15 @@ export class BallController extends Component {
         
         // 监听重置球控制器事件
         eventBus.on(GameEvents.RESET_BALL_CONTROLLER, this.onResetBallControllerEvent, this);
+        
+        // 监听球创建事件
+        eventBus.on(GameEvents.BALL_CREATE, this.onBallCreateEvent, this);
+        
+        // 监听球启动事件
+        eventBus.on(GameEvents.BALL_START, this.onBallStartEvent, this);
+        
+        // 监听创建额外球事件
+        eventBus.on(GameEvents.BALL_CREATE_ADDITIONAL, this.onBallCreateAdditionalEvent, this);
     }
 
     /**
@@ -139,6 +148,30 @@ export class BallController extends Component {
         console.log('[BallController] 接收到重置球控制器事件');
         this.resetBallController();
     }
+    
+    /**
+     * 处理球创建事件
+     */
+    private onBallCreateEvent() {
+        console.log('[BallController] 接收到球创建事件');
+        this.createBall();
+    }
+    
+    /**
+     * 处理球启动事件
+     */
+    private onBallStartEvent() {
+        console.log('[BallController] 接收到球启动事件');
+        this.startBall();
+    }
+    
+    /**
+     * 处理创建额外球事件
+     */
+    private onBallCreateAdditionalEvent() {
+        console.log('[BallController] 接收到创建额外球事件');
+        this.createAdditionalBall();
+    }
 
     // 计算游戏边界(使用GameArea节点)
     calculateGameBounds() {
@@ -171,19 +204,24 @@ export class BallController extends Component {
     // 创建小球
     createBall() {
         if (!this.ballPrefab) {
+            console.error('[BallController] ballPrefab 未设置,无法创建小球');
             return;
         }
 
-    
-
         // 实例化小球
         this.activeBall = instantiate(this.ballPrefab);
         
+        if (!this.activeBall) {
+            console.error('[BallController] 小球实例化失败');
+            return;
+        }
+        
         // 将小球添加到GameArea中
         const gameArea = find('Canvas/GameLevelUI/GameArea');
         if (gameArea) {
             gameArea.addChild(this.activeBall);
         } else {
+            console.warn('[BallController] 未找到GameArea,将小球添加到当前节点');
             this.node.addChild(this.activeBall);
         }
 
@@ -957,6 +995,11 @@ export class BallController extends Component {
     
     // 启动小球 - 公开方法,在确定按钮点击后调用
     public startBall() {
+        // 检查ballPrefab是否设置
+        if (!this.ballPrefab) {
+            console.error('[BallController] ballPrefab 未设置,无法创建小球');
+            return;
+        }
         
         // 如果还没有初始化,先初始化
         if (!this.initialized) {
@@ -968,6 +1011,12 @@ export class BallController extends Component {
             this.createBall();
         }
         
+        // 检查小球是否成功创建
+        if (!this.activeBall) {
+            console.error('[BallController] 小球创建失败');
+            return;
+        }
+        
         // 重新定位小球,避免与方块重叠
         this.positionBallRandomly();
         
@@ -979,6 +1028,8 @@ export class BallController extends Component {
         
         // 设置运动状态
         this.ballStarted = true;
+        
+        console.log('[BallController] 小球启动完成');
     }
 
     /**

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

@@ -231,19 +231,10 @@ export class GameBlockSelection extends Component {
         if (this.session.spendCoins(actualCost)) {
             this.updateCoinDisplay();
             
-            // 创建新的小球
-            if (this.ballControllerNode) {
-                const ballController = this.ballControllerNode.getComponent(BallController);
-                if (ballController) {
-                    // 使用createAdditionalBall方法创建额外的小球,而不是替换现有的小球
-                    ballController.createAdditionalBall();
-                    console.log(`新增小球成功,扣除${actualCost}金币`);
-                } else {
-                    console.error('找不到BallController组件');
-                }
-            } else {
-                console.error('找不到BallController节点,无法创建小球');
-            }
+            // 通过事件系统创建新的小球
+            const eventBus = EventBus.getInstance();
+            eventBus.emit(GameEvents.BALL_CREATE_ADDITIONAL);
+            console.log(`新增小球成功,扣除${actualCost}金币`);
         }
     }
 

+ 9 - 0
assets/scripts/Core/EventBus.ts

@@ -52,6 +52,8 @@ export enum GameEvents {
     
     // 球控制事件
     BALL_CREATE = 'BALL_CREATE',
+    BALL_START = 'BALL_START',
+    BALL_CREATE_ADDITIONAL = 'BALL_CREATE_ADDITIONAL',
     BALL_FIRE_BULLET = 'BALL_FIRE_BULLET',
     
     // 敌人控制事件
@@ -91,4 +93,11 @@ export default class EventBus extends EventTarget {
         }
         return this._instance;
     }
+
+    // 重写emit方法,添加黄色字体的日志输出
+    emit(type: string, ...args: any[]): void {
+        console.log(`%c[EventBus] 发送事件: ${type}`, 'color: #FFD700; font-weight: bold;', args.length > 0 ? args : '');
+        super.emit(type, ...args);
+    }
+
 }

+ 4 - 7
assets/scripts/LevelSystem/GameManager.ts

@@ -6,7 +6,6 @@ import { ConfigManager } from '../Core/ConfigManager';
 // EnemyController已通过事件系统解耦,不再需要直接导入
 import EventBus, { GameEvents } from '../Core/EventBus';
 import { PhysicsManager } from '../Core/PhysicsManager';
-import { BallController } from '../CombatSystem/BallController';
 import { LevelSessionManager } from '../Core/LevelSessionManager';
 import { GameBlockSelection } from '../CombatSystem/BlockSelection/GameBlockSelection';
 // GamePause已通过事件系统解耦,不再需要直接导入
@@ -666,12 +665,10 @@ export class GameManager extends Component {
     }
     
     private spawnBall() {
-        if (!this.ballController) return;
-        
-        const ballControllerComponent = this.ballController.getComponent(BallController);
-        if (ballControllerComponent) {
-            ballControllerComponent.startBall();
-        }
+        // 通过事件系统启动球的移动
+        const eventBus = EventBus.getInstance();
+        eventBus.emit(GameEvents.BALL_START);
+        console.log('[GameManager] 发送BALL_START事件,球已启动');
     }
 
     public gameOver() {

+ 5 - 4
assets/scripts/LevelSystem/IN_game.ts

@@ -740,13 +740,14 @@ export class InGameManager extends Component {
         EventBus.getInstance().emit(GameEvents.GAME_START);
         console.log('[InGameManager] 发送GAME_START事件');
         
-        // 更新波次
-        this.nextWave();
+        // 通过事件系统启动球的移动
+        EventBus.getInstance().emit(GameEvents.BALL_START);
+        console.log('[InGameManager] 发送BALL_START事件,球已启动');
         
-        // 通过事件系统开始新波次的敌人生成
+        // 通过事件系统开始当前波次的敌人生成(不更新波次)
         EventBus.getInstance().emit(GameEvents.ENEMY_START_GAME);
         EventBus.getInstance().emit(GameEvents.ENEMY_SHOW_START_WAVE_PROMPT);
-        console.log('[InGameManager] 波次敌人生成已启动');
+        console.log('[InGameManager] 当前波次敌人生成已启动');
     }
     
     /**