import { _decorator, Component, Node, Label, ProgressBar, find } from 'cc'; import EventBus, { GameEvents } from '../Core/EventBus'; import { GameBlockSelection } from '../CombatSystem/BlockSelection/GameBlockSelection'; import { Wall } from '../CombatSystem/Wall'; import { GameStartMove } from '../Animations/GameStartMove'; import { LevelSessionManager } from '../Core/LevelSessionManager'; import { SaveDataManager } from './SaveDataManager'; import { SkillManager } from '../CombatSystem/SkillSelection/SkillManager'; import { ReStartGame } from './ReStartGame'; const { ccclass, property } = _decorator; /** * 游戏内状态枚举 * 仅在 AppState.IN_GAME 时使用 */ export enum GameState { PLAYING = 'playing', SUCCESS = 'success', DEFEAT = 'defeat', PAUSED = 'paused', BLOCK_SELECTION = 'block_selection' } /** * 游戏内状态管理器 * 负责管理游戏进行中的所有状态和逻辑 */ @ccclass('InGameManager') export class InGameManager extends Component { // === 游戏内UI节点引用 === @property({ type: Node, tooltip: '拖拽BallController节点到这里' }) public ballController: Node = null; @property({ type: Node, tooltip: '拖拽GameBlockSelection节点到这里' }) public gameBlockSelection: Node = null; @property({ type: Node, tooltip: '拖拽GameArea节点到这里' }) public gameArea: Node = null; @property({ type: Node, tooltip: '拖拽EnemyController节点到这里' }) public enemyManager: Node = null; @property({ type: Node, tooltip: '摄像机节点,用于获取GameStartMove组件' }) public cameraNode: Node = null; @property({ type: Node, tooltip: '墙体节点,用于获取Wall组件' }) public wallNode: Node = null; @property({ type: Node, tooltip: '上围栏墙体节点 (TopFence)' }) public topFenceNode: Node = null; @property({ type: Node, tooltip: '下围栏墙体节点 (BottomFence)' }) public bottomFenceNode: Node = null; // === 游戏配置属性 === @property({ tooltip: '状态检查间隔(秒)' }) public checkInterval: number = 1.0; // === 私有属性 === private gameStarted: boolean = false; private currentState: GameState = GameState.PLAYING; private checkTimer: number = 0; // EnemyController现在通过事件系统通信,不再直接引用 private enemySpawningStarted: boolean = false; private totalEnemiesSpawned: number = 0; private currentWave: number = 1; private currentWaveEnemyCount: number = 0; private currentWaveTotalEnemies: number = 0; public levelWaves: any[] = []; private levelTotalEnemies: number = 0; private enemiesKilled: number = 0; // 游戏计时器 private gameStartTime: number = 0; private gameEndTime: number = 0; // 游戏区域的边界 private gameBounds = { left: 0, right: 0, top: 0, bottom: 0 }; private preparingNextWave = false; // 能量系统 private energyPoints: number = 0; private energyMax: number = 5; private energyBar: ProgressBar = null; private selectSkillUI: Node = null; // 能量条UI节点 @property({ type: Node, tooltip: '拖拽能量条节点到这里 (Canvas/GameLevelUI/EnergyBar)' }) public energyBarNode: Node = null; // 技能选择UI节点 @property({ type: Node, tooltip: '拖拽技能选择UI节点到这里 (Canvas/GameLevelUI/SelectSkillUI)' }) public selectSkillUINode: Node = null; // GameBlockSelection组件 private blockSelectionComponent: GameBlockSelection = null; private pendingSkillSelection: boolean = false; private pendingBlockSelection: boolean = false; private shouldShowNextWavePrompt: boolean = false; // 墙体组件引用 private wallComponent: Wall = null; private topFenceComponent: Wall = null; private bottomFenceComponent: Wall = null; // GameStartMove组件引用 private gameStartMoveComponent: GameStartMove = null; start() { this.initializeGameState(); this.setupEventListeners(); this.initGameBlockSelection(); this.initGameStartMove(); this.initWallComponent(); this.initUINodes(); } update(deltaTime: number) { if (this.currentState !== GameState.PLAYING) { return; } this.checkTimer += deltaTime; if (this.checkTimer >= this.checkInterval) { this.checkTimer = 0; this.checkGameState(); } } /** * 初始化游戏状态 */ private initializeGameState() { this.currentState = GameState.PLAYING; this.checkTimer = 0; this.enemySpawningStarted = false; this.totalEnemiesSpawned = 0; this.currentWave = 1; this.currentWaveEnemyCount = 0; this.currentWaveTotalEnemies = 0; this.energyPoints = 0; this.pendingSkillSelection = false; } /** * 设置事件监听器 */ private setupEventListeners() { const eventBus = EventBus.getInstance(); eventBus.on(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this); eventBus.on(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this); eventBus.on(GameEvents.GAME_RESUME, this.onGameResumeEvent, this); eventBus.on('ENEMY_KILLED', this.onEnemyKilledEvent, this); eventBus.on(GameEvents.RESET_ENERGY_SYSTEM, this.resetEnergySystem, this); } /** * 初始化GameBlockSelection组件 */ private initGameBlockSelection() { if (this.gameBlockSelection) { this.blockSelectionComponent = this.gameBlockSelection.getComponent(GameBlockSelection); } } /** * 初始化GameStartMove组件 */ private initGameStartMove() { if (this.cameraNode) { this.gameStartMoveComponent = this.cameraNode.getComponent(GameStartMove); } } /** * EnemyController现在通过事件系统通信,不再需要直接初始化 */ /** * 初始化墙体组件 */ private initWallComponent() { // 初始化主墙体 if (this.wallNode) { this.wallComponent = this.wallNode.getComponent(Wall); if (this.wallComponent) { console.log('[InGameManager] 主墙体组件初始化成功'); } else { console.warn('[InGameManager] 未找到主墙体Wall组件'); } } else { console.warn('[InGameManager] 主墙体节点未通过装饰器挂载'); } // 初始化上围栏 if (this.topFenceNode) { this.topFenceComponent = this.topFenceNode.getComponent(Wall); if (this.topFenceComponent) { console.log('[InGameManager] 上围栏墙体组件初始化成功'); } else { console.warn('[InGameManager] 未找到上围栏Wall组件'); } } else { console.warn('[InGameManager] 上围栏节点未通过装饰器挂载'); } // 初始化下围栏 if (this.bottomFenceNode) { this.bottomFenceComponent = this.bottomFenceNode.getComponent(Wall); if (this.bottomFenceComponent) { console.log('[InGameManager] 下围栏墙体组件初始化成功'); } else { console.warn('[InGameManager] 未找到下围栏Wall组件'); } } else { console.warn('[InGameManager] 下围栏节点未通过装饰器挂载'); } } /** * 获取指定波次的敌人配置 */ private getWaveEnemyConfigs(wave: number): any[] { if (!this.levelWaves || wave < 1 || wave > this.levelWaves.length) { return []; } const waveConfig = this.levelWaves[wave - 1]; return waveConfig?.enemies || []; } /** * 检查是否应该显示方块选择UI */ public shouldShowBlockSelection(): boolean { return this.preparingNextWave && this.currentState === GameState.BLOCK_SELECTION; } /** * 在技能选择后显示方块选择UI */ public showBlockSelectionAfterSkill() { if (this.pendingBlockSelection) { console.log('[InGameManager] 技能选择完成,现在显示方块选择UI'); this.pendingBlockSelection = false; if (this.currentWave < (this.levelWaves?.length || 1)) { this.showBlockSelectionForNextWave(); } else { console.log('[InGameManager] 最后一波结束,触发游戏胜利'); this.triggerGameSuccess(); } } else if (this.shouldShowBlockSelection()) { this.showBlockSelectionForNextWave(); } } /** * 显示下一波方块选择UI */ private showBlockSelectionForNextWave() { // 如果游戏已经结束,不显示方块选择UI if (this.isGameOver()) { console.warn('[InGameManager] 游戏已经结束(胜利或失败),不显示下一波方块选择UI!'); return; } console.log('[InGameManager] 显示方块选择UI,准备播放进入动画'); if (this.blockSelectionComponent) { this.blockSelectionComponent.showBlockSelection(true); // 通过事件系统暂停游戏 EventBus.getInstance().emit(GameEvents.GAME_PAUSE); } // 注意:不在这里直接调用动画,showBlockSelection()内部的playShowAnimation()会处理动画 // 避免重复调用enterBlockSelectionMode导致摄像头偏移两倍 } /** * 处理游戏成功事件 */ private onGameSuccessEvent() { console.log('[InGameManager] 接收到游戏成功事件'); this.currentState = GameState.SUCCESS; } /** * 处理游戏失败事件 */ private onGameDefeatEvent() { console.log('[InGameManager] 接收到游戏失败事件'); this.currentState = GameState.DEFEAT; } /** * 处理游戏恢复事件 */ private onGameResumeEvent() { console.log('[InGameManager] 接收到游戏恢复事件'); } /** * 处理敌人被击杀事件 */ private onEnemyKilledEvent() { // 游戏状态检查现在通过事件系统处理 if (this.isGameOver()) { console.warn('[InGameManager] 游戏已结束状态下onEnemyKilledEvent被调用!'); return; } // 如果游戏状态已经是成功或失败,不处理敌人击杀事件 if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) { console.warn(`[InGameManager] 游戏已结束(${this.currentState}),跳过敌人击杀事件处理`); return; } this.enemiesKilled++; this.currentWaveEnemyCount++; const remaining = this.currentWaveTotalEnemies - this.currentWaveEnemyCount; console.log(`[InGameManager] 敌人被消灭,当前波剩余敌人: ${remaining}/${this.currentWaveTotalEnemies}`); // 每死一个敌人就立即增加能量 const energyBeforeIncrement = this.energyPoints; this.incrementEnergy(); // 通过事件系统更新敌人计数标签 EventBus.getInstance().emit(GameEvents.ENEMY_UPDATE_COUNT, this.currentWaveEnemyCount); // 检查能量是否已满,如果满了需要触发技能选择 const energyWillBeFull = energyBeforeIncrement + 1 >= this.energyMax; // 通过事件系统检查是否有活跃敌人 let hasActiveEnemies = false; EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_ACTIVE, (active: boolean) => { hasActiveEnemies = active; // 在回调中检查波次是否结束 const isWaveEnd = remaining <= 0 && !hasActiveEnemies; if (isWaveEnd) { console.log(`[InGameManager] 波次结束检测: remaining=${remaining}, hasActiveEnemies=${hasActiveEnemies}`); // 如果能量已满,设置等待方块选择状态 if (energyWillBeFull) { this.pendingBlockSelection = true; this.preparingNextWave = true; this.currentState = GameState.BLOCK_SELECTION; } else { if (this.currentWave < (this.levelWaves?.length || 1)) { this.showNextWavePrompt(); } else { this.triggerGameSuccess(); } } } else if (energyWillBeFull) { // 如果波次未结束但能量已满,也需要触发技能选择 console.log('[InGameManager] 能量已满但波次未结束,触发技能选择'); this.currentState = GameState.BLOCK_SELECTION; } }); } /** * 增加能量值 */ private incrementEnergy() { // 获取技能等级 const energyHunterLevel = SkillManager.getInstance() ? SkillManager.getInstance().getSkillLevel('energy_hunter') : 0; const baseEnergy = 1; const bonusEnergy = SkillManager.calculateEnergyBonus ? SkillManager.calculateEnergyBonus(baseEnergy, energyHunterLevel) : baseEnergy; this.energyPoints = Math.min(this.energyPoints + bonusEnergy, this.energyMax); this.updateEnergyBar(); console.log(`[InGameManager] 能量值增加: +${bonusEnergy}, 当前: ${this.energyPoints}/${this.energyMax}`); if (this.energyPoints >= this.energyMax) { this.onEnergyFull(); } } /** * 显示下一波提示 */ private showNextWavePrompt() { // 设置准备下一波的状态 this.preparingNextWave = true; this.pendingBlockSelection = true; this.currentState = GameState.BLOCK_SELECTION; console.log('[InGameManager] 设置准备下一波状态,准备显示方块选择UI'); // 如果当前没有技能选择UI显示,立即显示方块选择UI if (!this.selectSkillUI || !this.selectSkillUI.active) { this.showBlockSelectionForNextWave(); } // 如果技能选择UI正在显示,则等待技能选择完成后再显示方块选择UI // 这种情况下,pendingBlockSelection已经在onEnemyKilledEvent中设置为true } /** * 游戏状态检查 */ private checkGameState() { // 检查所有墙体是否存活 const isAnyWallDestroyed = (this.wallComponent && !this.wallComponent.isAlive()) || (this.topFenceComponent && !this.topFenceComponent.isAlive()) || (this.bottomFenceComponent && !this.bottomFenceComponent.isAlive()); if (isAnyWallDestroyed) { this.triggerGameDefeat(); return; } if (this.checkAllEnemiesDefeated()) { this.triggerGameSuccess(); return; } } /** * 检查所有敌人是否被击败 */ private checkAllEnemiesDefeated(): boolean { // 通过事件系统检查游戏是否开始 let gameStarted = false; EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_GAME_STARTED, (started: boolean) => { gameStarted = started; }); if (!this.enemySpawningStarted) { if (gameStarted) { this.enemySpawningStarted = true; } else { return false; } } // 通过事件系统获取当前敌人数量 let currentEnemyCount = 0; EventBus.getInstance().emit(GameEvents.ENEMY_GET_COUNT, (count: number) => { currentEnemyCount = count; }); if (this.levelTotalEnemies > 0) { return this.enemiesKilled >= this.levelTotalEnemies && currentEnemyCount === 0; } if (currentEnemyCount > this.totalEnemiesSpawned) { this.totalEnemiesSpawned = currentEnemyCount; } const shouldCheckVictory = this.enemySpawningStarted && currentEnemyCount === 0 && this.totalEnemiesSpawned > 0; return shouldCheckVictory; } /** * 触发游戏失败 */ private triggerGameDefeat() { // 立即设置游戏状态为失败,防止后续敌人击杀事件被处理 this.currentState = GameState.DEFEAT; console.log('[InGameManager] 设置游戏状态为失败,发送GAME_DEFEAT事件'); EventBus.getInstance().emit(GameEvents.GAME_DEFEAT); } /** * 触发游戏成功 */ private triggerGameSuccess() { // 立即设置游戏状态为成功,防止后续敌人击杀事件被处理 this.currentState = GameState.SUCCESS; console.log('[InGameManager] 设置游戏状态为成功,发送GAME_SUCCESS事件'); EventBus.getInstance().emit(GameEvents.GAME_SUCCESS); } /** * 获取游戏持续时间 */ public getGameDuration(): number { const endTime = this.gameEndTime || Date.now(); return Math.max(0, endTime - this.gameStartTime); } /** * 获取当前游戏状态 */ public getCurrentState(): GameState { return this.currentState; } /** * 设置游戏状态 */ public setCurrentState(state: GameState) { this.currentState = state; } /** * 初始化UI节点 */ private initUINodes() { // 初始化能量条 if (this.energyBarNode) { this.energyBar = this.energyBarNode.getComponent(ProgressBar); if (this.energyBar) { console.log('[InGameManager] 能量条组件初始化成功'); // 初始化能量条显示 this.updateEnergyBar(); } else { console.error('[InGameManager] 能量条节点存在但ProgressBar组件未找到'); } } else { console.error('[InGameManager] 能量条节点未通过装饰器挂载,请在Inspector中拖拽EnergyBar节点'); } // 初始化技能选择UI if (this.selectSkillUINode) { this.selectSkillUI = this.selectSkillUINode; console.log('[InGameManager] 技能选择UI节点初始化成功'); } else { console.error('[InGameManager] 技能选择UI节点未通过装饰器挂载,请在Inspector中拖拽SelectSkillUI节点'); } } /** * 更新能量条显示 */ private updateEnergyBar() { if (this.energyBar) { const progress = this.energyPoints / this.energyMax; this.energyBar.progress = progress; console.log(`[InGameManager] 能量条更新: ${this.energyPoints}/${this.energyMax} (${Math.round(progress * 100)}%)`); } else { console.warn('[InGameManager] 能量条组件未初始化,无法更新显示'); } } /** * 能量满时的处理 */ private onEnergyFull() { console.log('[InGameManager] 能量已满,显示技能选择UI'); // 直接显示技能选择UI this.showSkillSelection(); } /** * 显示技能选择UI */ private showSkillSelection() { if (this.selectSkillUI) { this.selectSkillUI.active = true; this.pauseGame(); // 重置能量 ReStartGame.resetEnergy(); } } /** * 暂停游戏 */ private pauseGame() { EventBus.getInstance().emit(GameEvents.GAME_PAUSE); } /** * 检查游戏是否结束 */ private isGameOver(): boolean { let gameOver = false; EventBus.getInstance().emit(GameEvents.GAME_CHECK_OVER, (isOver: boolean) => { gameOver = isOver; }); return gameOver; } /** * 设置当前波次 */ public setCurrentWave(wave: number, enemyCount: number = 0) { this.currentWave = wave; this.currentWaveEnemyCount = 0; // 重置当前击杀数 this.currentWaveTotalEnemies = enemyCount; // 设置该波次总敌人数 const totalWaves = this.levelWaves?.length || 1; // 获取波次敌人配置 const waveEnemyConfigs = this.getWaveEnemyConfigs(wave); // 通过事件系统启动波次 EventBus.getInstance().emit(GameEvents.ENEMY_START_WAVE, { wave: wave, totalWaves: totalWaves, enemyCount: enemyCount, waveEnemyConfigs: waveEnemyConfigs }); } /** * 更新当前波次敌人数量 */ public updateCurrentWaveEnemyCount(count: number) { this.currentWaveEnemyCount = count; } /** * 获取当前波次 */ public getCurrentWave(): number { return this.currentWave; } /** * 获取当前波次敌人数量 */ public getCurrentWaveEnemyCount(): number { return this.currentWaveEnemyCount; } /** * 获取当前波次总敌人数量 */ public getCurrentWaveTotalEnemies(): number { return this.currentWaveTotalEnemies; } /** * 进入下一波 */ public nextWave() { this.currentWave++; // 根据关卡配置获取下一波敌人数 let enemyTotal = 0; if (this.levelWaves && this.levelWaves.length >= this.currentWave) { const waveCfg = this.levelWaves[this.currentWave - 1]; if (waveCfg && waveCfg.enemies) { enemyTotal = waveCfg.enemies.reduce((t: number, g: any) => t + (g.count || 0), 0); } } this.setCurrentWave(this.currentWave, enemyTotal); } /** * 获取当前能量值 */ public getCurrentEnergy(): number { return this.energyPoints; } /** * 获取最大能量值 */ public getMaxEnergy(): number { return this.energyMax; } /** * 获取墙体健康度(返回主墙体健康度) */ public getWallHealth(): number { if (this.wallComponent) { return this.wallComponent.getCurrentHealth(); } return 0; } /** * 获取所有墙体的健康度 */ public getAllWallsHealth(): { main: number; topFence: number; bottomFence: number } { return { main: this.wallComponent ? this.wallComponent.getCurrentHealth() : 0, topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentHealth() : 0, bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentHealth() : 0 }; } /** * 升级墙体等级(升级主墙体) */ public upgradeWallLevel(): { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null { if (this.wallComponent) { return this.wallComponent.upgradeWallLevel(); } return null; } /** * 升级所有墙体等级 */ public upgradeAllWallsLevel(): { main: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null; topFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null; bottomFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null; } { return { main: this.wallComponent ? this.wallComponent.upgradeWallLevel() : null, topFence: this.topFenceComponent ? this.topFenceComponent.upgradeWallLevel() : null, bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.upgradeWallLevel() : null }; } /** * 根据等级获取墙体健康度 */ public getWallHealthByLevel(level: number): number { if (this.wallComponent) { return this.wallComponent.getWallHealthByLevel(level); } return 100; } /** * 获取当前墙体等级(返回主墙体等级) */ public getCurrentWallLevel(): number { if (this.wallComponent) { return this.wallComponent.getCurrentWallLevel(); } return 1; } /** * 获取当前墙体健康度(返回主墙体健康度) */ public getCurrentWallHealth(): number { if (this.wallComponent) { return this.wallComponent.getCurrentHealth(); } return 100; } /** * 获取所有墙体的等级 */ public getAllWallsLevel(): { main: number; topFence: number; bottomFence: number } { return { main: this.wallComponent ? this.wallComponent.getCurrentWallLevel() : 1, topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentWallLevel() : 1, bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentWallLevel() : 1 }; } /** * 处理确认操作(方块选择确认) */ public handleConfirmAction() { console.log('[InGameManager] 处理方块选择确认操作'); // 如果是在准备下一波的状态,需要先切换到下一波 if (this.preparingNextWave) { console.log('[InGameManager] 检测到准备下一波状态,切换到下一波'); this.nextWave(); // 重置状态 this.preparingNextWave = false; this.pendingBlockSelection = false; this.currentState = GameState.PLAYING; } // 发送游戏开始事件,确保GamePause正确设置状态 EventBus.getInstance().emit(GameEvents.GAME_START); console.log('[InGameManager] 发送GAME_START事件'); // 通过事件系统启动球的移动 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] 波次 ${this.currentWave} 敌人生成已启动`); } /** * 重置能量值(供ReStartGame调用) */ public resetEnergyValue() { this.energyPoints = 0; this.updateEnergyBar(); console.log('[InGameManager] 能量值重置完成'); } /** * 重置波次信息(供ReStartGame调用) */ public resetWaveInfo() { this.currentWave = 1; this.currentWaveEnemyCount = 0; this.currentWaveTotalEnemies = 0; this.enemiesKilled = 0; this.totalEnemiesSpawned = 0; this.levelTotalEnemies = 0; console.log('[InGameManager] 波次信息重置完成'); } /** * 重置能量系统(供ReStartGame调用) */ public resetEnergySystem() { this.energyPoints = 0; this.energyMax = 5; this.updateEnergyBar(); console.log('[InGameManager] 能量系统重置完成'); } /** * 清理游戏数据,为返回主页面做准备 * 当游戏胜利或失败返回主页面时调用 */ private cleanupGameDataForMainMenu() { console.log('[InGameManager] 开始清理游戏数据,准备返回主页面'); const eventBus = EventBus.getInstance(); // 1. 清空场上的所有游戏对象 console.log('[InGameManager] 清空场上敌人、方块、球'); eventBus.emit(GameEvents.CLEAR_ALL_ENEMIES); // 清空所有敌人 eventBus.emit(GameEvents.CLEAR_ALL_BULLETS); // 清空所有子弹 eventBus.emit(GameEvents.CLEAR_ALL_GAME_OBJECTS); // 清空其他游戏对象 // 2. 重置能量系统 console.log('[InGameManager] 重置能量系统'); this.energyPoints = 0; this.updateEnergyBar(); // 3. 清空技能选择数据(重置临时技能状态) console.log('[InGameManager] 清空技能选择数据'); if (SkillManager.getInstance()) { // 重置技能管理器中的临时技能状态 const skillManager = SkillManager.getInstance(); const allSkills = skillManager.getAllSkillsData(); allSkills.forEach(skill => { skillManager.setSkillLevel(skill.id, 0); // 重置所有技能等级为0 }); } // 4. 重置关卡数据和游戏状态 console.log('[InGameManager] 重置关卡数据和游戏状态'); this.currentWave = 1; this.currentWaveEnemyCount = 0; this.currentWaveTotalEnemies = 0; this.enemiesKilled = 0; this.totalEnemiesSpawned = 0; this.levelTotalEnemies = 0; this.levelWaves = []; this.gameStarted = false; this.enemySpawningStarted = false; this.preparingNextWave = false; this.pendingSkillSelection = false; this.pendingBlockSelection = false; this.shouldShowNextWavePrompt = false; // 5. 重置UI状态 console.log('[InGameManager] 重置UI状态'); if (this.selectSkillUI) { this.selectSkillUI.active = false; } // 6. 清理会话数据(金币等临时数据) console.log('[InGameManager] 清理会话数据'); if (LevelSessionManager.inst) { LevelSessionManager.inst.clear(); // 清空局内金币等临时数据 } // 7. 通过事件系统通知其他组件进行清理 eventBus.emit(GameEvents.RESET_BALL_CONTROLLER); // 重置球控制器 eventBus.emit(GameEvents.RESET_BLOCK_MANAGER); // 重置方块管理器 eventBus.emit(GameEvents.RESET_BLOCK_SELECTION); // 重置方块选择 eventBus.emit(GameEvents.RESET_ENEMY_CONTROLLER); // 重置敌人控制器 eventBus.emit(GameEvents.RESET_WALL_HEALTH); // 重置墙体血量 eventBus.emit(GameEvents.RESET_UI_STATES); // 重置UI状态 console.log('[InGameManager] 游戏数据清理完成,可以安全返回主页面'); } /** * 重置游戏状态(供ReStartGame调用) */ public resetGameStates() { this.currentState = GameState.PLAYING; this.gameStarted = false; this.enemySpawningStarted = false; this.preparingNextWave = false; this.pendingSkillSelection = false; this.pendingBlockSelection = false; this.shouldShowNextWavePrompt = false; this.gameStartTime = 0; this.gameEndTime = 0; this.checkTimer = 0; console.log('[InGameManager] 游戏状态重置完成'); } /** * 手动触发游戏数据清理(供外部调用) * 可以在返回主菜单按钮点击时调用 */ public triggerGameDataCleanup() { this.cleanupGameDataForMainMenu(); } /** * 应用关卡配置 */ public applyLevelConfig(levelConfig: any) { console.log('[InGameManager] 应用关卡配置'); // 应用能量配置 if (levelConfig.levelSettings && levelConfig.levelSettings.energyMax) { this.energyMax = levelConfig.levelSettings.energyMax; this.updateEnergyBar(); console.log(`[InGameManager] 应用能量配置: ${this.energyMax}`); } // 如果有武器配置,应用武器 if (levelConfig.weapons && Array.isArray(levelConfig.weapons)) { console.log('[InGameManager] 应用武器配置'); // TODO: 应用武器配置逻辑 } // 如果有波次配置,设置敌人波次 if (levelConfig.waves && Array.isArray(levelConfig.waves)) { this.levelWaves = levelConfig.waves; this.currentWave = 1; // 计算本关卡总敌人数 this.levelTotalEnemies = 0; for (const wave of this.levelWaves) { for (const enemy of wave.enemies || []) { this.levelTotalEnemies += enemy.count || 0; } } console.log(`[InGameManager] 应用波次配置: ${this.levelWaves.length}波,总敌人数: ${this.levelTotalEnemies}`); // 通过事件系统通知 EnemyController 初始化第一波数据及 UI const firstWaveEnemies = this.levelWaves.length > 0 && this.levelWaves[0].enemies ? this.levelWaves[0].enemies.reduce((t: number, g: any) => t + (g.count || 0), 0) : 0; // 通过事件系统调用setCurrentWave this.setCurrentWave(1, firstWaveEnemies); } } onDestroy() { const eventBus = EventBus.getInstance(); eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this); eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this); eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this); eventBus.off('ENEMY_KILLED', this.onEnemyKilledEvent, this); } }