| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019 |
- import { _decorator, Component, Node, find, director, UITransform, Button, Label, ProgressBar, } from 'cc';
- import { LevelManager } from './LevelManager';
- import { LevelConfigManager } from './LevelConfigManager';
- import { SaveDataManager } from './SaveDataManager';
- import { ConfigManager } from '../Core/ConfigManager';
- import { PhysicsManager } from '../Core/PhysicsManager';
- // EnemyController已通过事件系统解耦,不再需要直接导入
- import EventBus, { GameEvents } from '../Core/EventBus';
- import { LevelSessionManager } from '../Core/LevelSessionManager';
- import { GameBlockSelection } from '../CombatSystem/BlockSelection/GameBlockSelection';
- // GamePause已通过事件系统解耦,不再需要直接导入
- import { Wall } from '../CombatSystem/Wall';
- import { GameStartMove } from '../Animations/GameStartMove';
- import { StartGame } from './StartGame';
- import { InGameManager, GameState } from './IN_game';
- const { ccclass, property } = _decorator;
- /**
- * 全局应用状态枚举
- * 区分游戏外状态和游戏内状态
- */
- export enum AppState {
- // 应用状态枚举
- MAIN_MENU = 'main_menu', // 主界面
- UPGRADE = 'upgrade', // 升级界面
- SKILLS = 'skills', // 技能界面
- SETTINGS = 'settings', // 设置界面
- // 游戏状态
- IN_GAME = 'in_game' // 游戏进行中(包含所有游戏内子状态)
- }
- // GameState 枚举已迁移到 IN_game.ts
- /**
- * 增强版游戏管理器
- * 整合了游戏启动、状态管理、UI控制等功能
- */
- @ccclass('GameManager')
- export class GameManager extends Component {
-
- // === 原GameManager属性 ===
- @property({
- type: Node,
- tooltip: '拖拽BallController节点到这里'
- })
- public ballController: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽GameBlockSelection节点到这里'
- })
- public gameBlockSelection: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽diban面板动画节点到这里 (Canvas/GameLevelUI/BlockSelectionUI/diban)'
- })
- public dibanAnimationNode: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽GameArea节点到这里'
- })
- public gameArea: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽EnemyController节点到这里'
- })
- public enemyManager: Node = null;
- // === 游戏状态管理属性 ===
- @property({
- type: Node,
- tooltip: '游戏结束UI节点 (GameEnd)'
- })
- public gameEndUI: Node = null;
- // === 游戏内管理器 ===
- @property({
- type: Node,
- tooltip: '游戏内状态管理器节点'
- })
- public inGameManagerNode: Node = null;
- // === UI节点引用 ===
- @property({
- type: Node,
- tooltip: '主界面UI节点 (Canvas/MainUI)'
- })
- public mainUI: Node = null;
- // === 动画组件引用 ===
- @property({
- type: Node,
- tooltip: '摄像机节点,用于获取GameStartMove组件'
- })
- public cameraNode: Node = null;
- // === 游戏配置属性 ===
- @property({
- tooltip: '状态检查间隔(秒)'
- })
- public checkInterval: number = 1.0;
- // === 私有属性 ===
- private gameStarted: boolean = false;
- private currentAppState: AppState = AppState.MAIN_MENU; // 全局应用状态
- private levelManager: LevelManager = null;
- private levelConfigManager: LevelConfigManager = null;
- private saveDataManager: SaveDataManager = null;
- private configManager: ConfigManager = null;
- // enemyController已通过事件系统解耦,不再需要直接引用
-
- // 游戏内管理器引用
- private inGameManager: InGameManager = null;
-
-
- // 游戏区域的边界
- private gameBounds = {
- left: 0,
- right: 0,
- top: 0,
- bottom: 0
- };
- // === 波次相关属性(已迁移到 InGameManager,保留用于兼容性) ===
- private currentWave: number = 1;
- private currentWaveEnemyCount: number = 0;
- private currentWaveTotalEnemies: number = 0;
- private totalEnemiesSpawned: number = 0;
- // levelWaves 和 levelTotalEnemies 已迁移到 InGameManager
- // === 能量系统属性已迁移到 InGameManager ===
- // === UI状态属性 ===
- private pendingSkillSelection: boolean = false;
- private shouldShowNextWavePrompt: boolean = false;
- // === 游戏计时器 ===
- private gameStartTime: number = 0;
- private gameEndTime: number = 0;
- private checkTimer: number = 0;
- // === 组件引用 ===
- private blockSelectionComponent: GameBlockSelection = null;
- private wallComponent: Wall = null;
- private gameStartMoveComponent: GameStartMove = null;
- // 游戏内状态相关方法已迁移到 InGameManager
- // === 游戏状态检查方法 ===
- private isGameOver(): boolean {
- // 通过事件系统检查游戏是否结束
- let isGameOver = false;
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_CHECK_OVER, (result: boolean) => {
- isGameOver = result;
- });
- return isGameOver;
- }
- async start() {
- // 初始化StartGame的静态事件监听器
- StartGame.initializeEventListeners();
-
- // 初始化管理器
- this.initializeManagers();
- // 先初始化UI节点,确保inGameManager可用
- this.initUINodes();
-
- // 提前初始化本局数据,确保 BlockManager 在 start 时能拿到正确的局内金币
- if (!LevelSessionManager.inst.runtime) {
- await LevelSessionManager.inst.initialize(
- this.saveDataManager?.getCurrentLevel() || 1,
- this.getWallHealth()
- );
- }
-
- // 计算游戏区域边界
- this.calculateGameBounds();
-
- // 初始化游戏状态
- this.initializeGameState();
-
- // 保持在主菜单状态,等待用户点击战斗按钮
- console.log('[GameManager] 初始化完成,当前状态为MAIN_MENU,等待用户操作');
-
- // 敌人控制器已通过事件系统解耦,不再需要直接查找和设置
-
- // 设置UI按钮
- this.setupUIButtons();
-
- // 初始化GameStartMove组件(不包含GameBlockSelection,避免过早设置确认回调)
- this.initGameStartMove();
-
- // GameBlockSelection的确认回调将在游戏真正开始时设置,避免场景加载时的意外触发
-
- // 关卡配置加载已移至StartGame.initializeGameData()中,确保正确的时序
-
- // 监听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(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
-
- // 监听游戏重启事件
- eventBus.on(GameEvents.GAME_RESTART, this.onGameRestartEvent, this);
-
- // 监听重置游戏管理器事件
- eventBus.on(GameEvents.RESET_GAME_MANAGER, this.onResetGameManagerEvent, this);
-
- // 监听主菜单按钮点击事件(由UIStateManager转发)
- eventBus.on('CONTINUE_CLICK', this.onMainMenuClick, this);
-
- // 敌人击杀事件监听已迁移到 InGameManager
- }
- /**
- * 处理游戏成功事件
- */
- private onGameSuccessEvent() {
- console.log('[GameManager] 接收到游戏成功事件,执行成功处理');
-
- // 只处理数据记录,UI显示和奖励处理已迁移到GameEnd.ts
- this.onGameSuccess();
- }
- /**
- * 处理游戏失败事件
- */
- private onGameDefeatEvent() {
- console.log('[GameManager] 接收到游戏失败事件,执行失败处理');
-
- // 只处理数据记录,UI显示和奖励处理已迁移到GameEnd.ts
- this.onGameDefeat().catch(error => {
- console.error('[GameManager] 游戏失败处理出错:', error);
- });
- }
- /**
- * 处理游戏恢复事件
- */
- private onGameResumeEvent() {
- console.log('[GameManager] 接收到游戏恢复事件');
- // GameManager在这里可以处理恢复相关的逻辑
- // 但不直接调用EnemyController的方法,避免重复调用
- }
-
- /**
- * 处理游戏重启事件
- */
- private onGameRestartEvent() {
- console.log('[GameManager] 接收到游戏重启事件,重置GameManager状态');
-
- // 设置应用状态为游戏中
- this.currentAppState = AppState.IN_GAME;
- this.gameStarted = false;
- this.gameStartTime = 0;
- this.gameEndTime = 0;
-
-
- console.log('[GameManager] GameManager状态重置完成');
-
- // 直接调用StartGame的启动方法,统一使用游戏启动流程
- console.log('[GameManager] 直接调用StartGame.startGameFlow');
- StartGame.startGameFlow().catch(error => {
- console.error('[GameManager] 游戏重启流程出错:', error);
- });
- }
-
- /**
- * 处理重置游戏管理器事件
- */
- private onResetGameManagerEvent() {
- console.log('[GameManager] 接收到重置游戏管理器事件');
- // 重置游戏管理器状态
- this.currentAppState = AppState.IN_GAME;
- this.gameStarted = false;
- this.gameStartTime = 0;
- this.gameEndTime = 0;
-
-
- console.log('[GameManager] 游戏管理器状态已重置');
- }
-
- // 敌人击杀事件处理已迁移到 InGameManager
-
- // 游戏状态调试方法已迁移到 InGameManager
- // === 暂停游戏 ===
- private pauseGame() {
- // 通过事件系统触发游戏暂停
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_PAUSE);
- }
- // === 恢复游戏 ===
- public resumeGame() {
- // 通过事件系统触发游戏恢复
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_RESUME);
- // === 新增:恢复时弹出下一波提示Toast ===
- if (this.shouldShowNextWavePrompt) {
- this.shouldShowNextWavePrompt = false;
- // 通过事件系统显示下一波提示
- eventBus.emit(GameEvents.ENEMY_SHOW_START_WAVE_PROMPT);
- }
- }
- update(deltaTime: number) {
- // 只有在游戏中时才进行游戏逻辑更新
- if (this.currentAppState !== AppState.IN_GAME) {
- return;
- }
- // 检查自动保存
- if (this.saveDataManager) {
- this.saveDataManager.checkAutoSave();
- }
- }
- // === 管理器初始化 ===
- private initializeManagers() {
- this.levelManager = LevelManager.getInstance();
- // this.upgradeManager = UpgradeManager.getInstance();
- this.configManager = ConfigManager.getInstance();
- this.levelConfigManager = LevelConfigManager.getInstance();
- // enemyController已通过事件系统解耦,不再需要直接初始化
-
- // 初始化物理系统管理器
- PhysicsManager.getInstance();
- console.log('[GameManager] PhysicsManager初始化完成');
-
- // 存档管理器初始化已迁移到StartGame
- this.saveDataManager = SaveDataManager.getInstance();
- }
- // === 游戏状态初始化 ===
- private initializeGameState() {
- // 默认初始化为主菜单状态,游戏开始时会切换到IN_GAME
- this.currentAppState = AppState.MAIN_MENU;
-
- // 游戏内状态管理已迁移到 InGameManager
- this.pendingSkillSelection = false;
- }
- // === 计算游戏区域边界 ===
- // 已迁移到StartGame,这里保留方法以兼容现有调用
- private calculateGameBounds() {
- const canvas = find('Canvas');
- if (!canvas) {
- return;
- }
- const canvasUI = canvas.getComponent(UITransform);
- if (!canvasUI) {
- return;
- }
- const screenWidth = canvasUI.width;
- const screenHeight = canvasUI.height;
- const worldPos = canvas.worldPosition;
- this.gameBounds.left = worldPos.x - screenWidth / 2;
- this.gameBounds.right = worldPos.x + screenWidth / 2;
- this.gameBounds.bottom = worldPos.y - screenHeight / 2;
- this.gameBounds.top = worldPos.y + screenHeight / 2;
- }
- // === 初始化UI节点 ===
- private initUINodes() {
- // 初始化游戏内管理器
- if (this.inGameManagerNode) {
- this.inGameManager = this.inGameManagerNode.getComponent(InGameManager);
- }
- }
- // === 敌人控制器相关方法已通过事件系统解耦,不再需要 ===
- // === 游戏失败回调 ===
- private async onGameDefeat() {
- this.gameEndTime = Date.now();
-
- // 记录游戏失败到存档(不包含奖励处理,奖励已迁移到GameEnd.ts)
- if (this.saveDataManager) {
- const currentLevel = this.saveDataManager.getCurrentLevel();
- this.saveDataManager.failLevel(currentLevel);
-
- // 更新统计数据
- this.saveDataManager.updateStatistic('totalTimePlayed', this.getGameDuration());
-
- console.log(`[GameManager] 游戏失败记录已保存,关卡: ${currentLevel}`);
- }
- }
- // === 游戏成功回调 ===
- private async onGameSuccess() {
- this.gameEndTime = Date.now();
-
- // 奖励处理已迁移到GameEnd.ts,这里只处理关卡完成记录
- this.onLevelComplete();
- }
- // === 处理关卡完成 ===
- private onLevelComplete() {
- if (!this.saveDataManager) return;
-
- const currentLevel = this.saveDataManager.getCurrentLevel();
- const gameTime = this.getGameDuration();
-
- // 记录关卡完成到存档(不包含奖励处理,奖励已迁移到GameEnd.ts)
- this.saveDataManager.completeLevel(currentLevel, 0, gameTime);
-
- // 更新统计数据
- this.saveDataManager.updateStatistic('totalTimePlayed', gameTime);
- this.saveDataManager.updateStatistic('totalEnemiesDefeated', this.totalEnemiesSpawned);
-
- console.log(`[GameManager] 关卡完成记录已保存,关卡: ${currentLevel}, 游戏时长: ${gameTime}秒`);
- }
-
- // clearPreviousGameRecord方法已被移除,其功能已整合到onMainMenuClick中
- // 避免重复清理游戏数据
-
- // === 计算游戏时长 ===
- private getGameDuration(): number {
- if (this.gameStartTime === 0) return 0;
- const endTime = this.gameEndTime || Date.now();
- return Math.floor((endTime - this.gameStartTime) / 1000);
- }
-
-
- // === 设置UI按钮 ===
- private setupUIButtons() {
- this.setupEndUIButtons();
- }
- // === 设置游戏结束界面按钮 ===
- private setupEndUIButtons() {
- // UI按钮事件处理已迁移到 UIStateManager
- // 通过事件系统处理按钮点击事件
- }
- // === 按钮点击事件处理 ===
-
- private onMainMenuClick() {
- console.log('[GameManager] 返回主菜单');
-
- // 1. 获取当前游戏状态,判断是否为获胜状态
- const inGameManager = this.getInGameManager();
- const currentGameState = inGameManager?.getCurrentState();
- const isGameSuccess = currentGameState === GameState.SUCCESS;
-
- console.log(`[GameManager] 当前游戏状态:${currentGameState},开始清理游戏数据`);
- console.log(`[GameManager] GameState.SUCCESS值:${GameState.SUCCESS}`);
- console.log(`[GameManager] 状态比较结果 isGameSuccess:${isGameSuccess}`);
- console.log(`[GameManager] 严格比较:${currentGameState} === ${GameState.SUCCESS} = ${currentGameState === GameState.SUCCESS}`);
-
- // 2. 统一进行游戏数据清理(只调用一次,避免重复清理)
- if (inGameManager) {
- console.log('[GameManager] 触发游戏数据清理');
- inGameManager.triggerGameDataCleanup();
- } else {
- console.warn('[GameManager] 未找到InGameManager,跳过游戏数据清理');
- }
-
- // 3. 发送重置UI状态事件,让UIStateManager统一关闭所有相关面板
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.RESET_UI_STATES);
-
- // 4. 重置GameManager自身的记录(不调用InGameManager的重复清理)
- this.gameStartTime = 0;
- this.gameEndTime = 0;
- this.totalEnemiesSpawned = 0;
- console.log('[GameManager] GameManager记录已重置');
-
- // 5. 根据游戏结果决定关卡处理:获胜时进入下一关,失败时保持当前关
- if (this.saveDataManager) {
- if (isGameSuccess) {
- // 获胜后进入下一关
- const currentLevel = this.saveDataManager.getCurrentLevel();
- const nextLevel = currentLevel + 1;
- this.saveDataManager.setCurrentLevel(nextLevel);
- console.log(`[GameManager] 游戏获胜,从第${currentLevel}关进入第${nextLevel}关`);
- } else {
- // 失败时保持当前关卡,不重置
- const currentLevel = this.saveDataManager.getCurrentLevel();
- console.log(`[GameManager] 游戏失败,保持当前关卡: ${currentLevel}`);
- }
- }
-
- // 6. 重置应用状态为主菜单
- this.currentAppState = AppState.MAIN_MENU;
- console.log('[GameManager] 应用状态已重置为MAIN_MENU');
-
- // 6. 触发返回主菜单事件
- eventBus.emit(GameEvents.RETURN_TO_MAIN_MENU);
-
- // 使用装饰器属性获取MainUI,避免使用find
- if (!this.mainUI) {
- console.error('[GameManager] MainUI节点未在编辑器中设置,请拖拽Canvas/MainUI到GameManager的mainUI属性');
- return;
- }
-
- const mainUIController = this.mainUI.getComponent('MainUIController' as any);
-
- if (mainUIController) {
- // 奖励动画已迁移到GameEnd.ts中,这里只需要普通返回主界面
- if (typeof (mainUIController as any).onReturnToMainUI === 'function') {
- console.log('[GameManager] 调用普通返回主界面方法');
- (mainUIController as any).onReturnToMainUI();
- } else {
- console.warn('[GameManager] 未找到返回主界面方法,使用兜底逻辑');
- this.fallbackMainMenuLogic(mainUIController);
- }
- } else {
- console.error('[GameManager] 未找到MainUIController组件');
- this.fallbackMainMenuLogic(null);
- }
- }
-
- /**
- * 兜底逻辑:当找不到MainUIController或相关方法时使用
- */
- private fallbackMainMenuLogic(mainUIController: any) {
- console.warn('[GameManager] 使用兜底逻辑返回主界面');
- if (this.mainUI) this.mainUI.active = true;
-
- if (mainUIController && typeof (mainUIController as any).updateUI === 'function') {
- (mainUIController as any).updateUI();
- }
- }
-
-
- private onUpgradeClick() {
- console.log('[GameManager] 升级按钮被点击');
- // TODO: 实现升级逻辑
- }
-
- private onReviveClick() {
- const reviveCost = 10; // 复活消耗的钻石数量
-
- if (this.saveDataManager && this.saveDataManager.spendDiamonds(reviveCost)) {
- this.revivePlayer();
- }
- }
- // === 复活玩家 ===
- private revivePlayer() {
- if (this.wallComponent) {
- this.wallComponent.setHealth(50);
- }
- // 通过事件系统进行完整重置
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_RESTART);
- }
- // === 重新开始当前关卡 ===
- private restartCurrentLevel() {
- // 通过事件系统进行完整重置
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_RESTART);
- }
- public startGame() {
- if (this.gameStarted) return;
-
- this.gameStarted = true;
- this.gameStartTime = Date.now();
- // 游戏状态管理已迁移到 InGameManager
-
- // 发送游戏开始事件,通知其他组件
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_START);
- console.log('[GameManager] 发送游戏开始事件');
-
- // 开始生成球
- this.spawnBall();
-
- // 启动状态检查
- this.checkTimer = 0;
-
- // 设置UI按钮事件
- this.setupUIButtons();
- // 第一波提示UI后再开始生成敌人
- // 通过事件系统显示开始波次提示
- eventBus.emit(GameEvents.ENEMY_SHOW_START_WAVE_PROMPT);
-
- // 通过事件系统开始敌人生成
- eventBus.emit(GameEvents.ENEMY_START_GAME);
- // 注意:LevelSessionManager已在StartGame.startGameFlow()中正确初始化,无需重复初始化
- }
-
- private spawnBall() {
- // 通过事件系统启动球的移动
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.BALL_START);
- console.log('[GameManager] 发送BALL_START事件,球已启动');
- }
- public gameOver() {
- this.triggerGameDefeat();
- }
- // === 公共方法 ===
- public setHealth(health: number) {
- this.wallComponent?.setHealth(health);
- }
- public takeDamage(damage: number) {
- this.wallComponent?.takeDamage(damage);
-
- if (this.wallComponent?.getCurrentHealth() <= 0) {
- this.triggerGameDefeat();
- }
- }
- /**
- * 获取当前全局应用状态
- */
- public getCurrentAppState(): AppState {
- return this.currentAppState;
- }
- /**
- * 设置全局应用状态
- */
- public setAppState(state: AppState): void {
- console.log(`[GameManager] 应用状态切换: ${this.currentAppState} -> ${state}`);
- this.currentAppState = state;
-
- // 根据状态控制UI栏显示
- this.updateUIBarsVisibility(state);
-
- // 游戏内状态管理已迁移到 InGameManager
- }
- /**
- * 根据应用状态更新UI栏显示
- */
- private updateUIBarsVisibility(state: AppState): void {
- const topBarNode = find('Canvas/TopBar');
- const navBarNode = find('Canvas/NavBar');
-
- if (state === AppState.IN_GAME) {
- // 游戏内状态:隐藏TopBar和NavBar
- if (topBarNode) topBarNode.active = false;
- if (navBarNode) navBarNode.active = false;
- console.log('[GameManager] 游戏内状态:隐藏TopBar和NavBar');
- } else {
- // 游戏外状态:显示TopBar和NavBar
- if (topBarNode) topBarNode.active = true;
- if (navBarNode) navBarNode.active = true;
- console.log('[GameManager] 游戏外状态:显示TopBar和NavBar');
- }
- }
- /**
- * 获取当前游戏内状态已迁移到 InGameManager
- * 请使用 InGameManager.getInstance().getCurrentState()
- */
- /**
- * 获取InGameManager实例
- * 用于访问游戏内状态和逻辑
- */
- public getInGameManager(): InGameManager | null {
- return this.inGameManager;
- }
- /**
- * 获取当前游戏内状态
- * 通过InGameManager获取
- */
- public getCurrentGameState(): GameState | null {
- return this.inGameManager ? this.inGameManager.getCurrentState() : null;
- }
- /**
- * 检查是否在游戏中
- */
- public isInGame(): boolean {
- return this.currentAppState === AppState.IN_GAME;
- }
- /**
- * 从方块选择状态切换到游戏进行状态
- * 当玩家完成方块选择后调用
- */
- public startGameFromBlockSelection(): void {
- if (this.currentAppState !== AppState.IN_GAME) {
- console.warn('[GameManager] 不在游戏中,无法开始游戏');
- return;
- }
-
- // 游戏状态检查已迁移到 InGameManager
-
- console.log('[GameManager] 从方块选择状态切换到游戏进行状态,播放退出动画');
-
- // 播放退出BLOCK_SELECTION状态的动画
- if (this.gameStartMoveComponent) {
- console.log('[GameManager] 执行退出BLOCK_SELECTION状态的动画');
- // 调用GameStartMove的退出方块选择模式方法
- if (this.gameStartMoveComponent && typeof this.gameStartMoveComponent['exitBlockSelectionMode'] === 'function') {
- (this.gameStartMoveComponent as any).exitBlockSelectionMode(300, 0.3);
- } else {
- console.warn('[GameManager] GameStartMove组件未实现exitBlockSelectionMode方法');
- }
- }
-
- // 游戏状态管理已迁移到 InGameManager
-
- // 发送游戏开始事件,通知GamePause等组件
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_START);
- console.log('[GameManager] 从方块选择切换到游戏时发送游戏开始事件');
- }
- public restartGame() {
- console.log('[GameManager] 重新开始游戏');
-
- // 设置应用状态为游戏中
- this.currentAppState = AppState.IN_GAME;
- this.gameStarted = false;
- this.gameStartTime = 0;
- this.gameEndTime = 0;
- // 通过事件系统触发游戏重启,让StartGame组件处理完整的重置流程
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_RESTART);
-
- console.log('[GameManager] 游戏重启事件已发送');
- }
- public forceGameSuccess() {
- this.triggerGameSuccess();
- }
- public forceGameDefeat() {
- this.triggerGameDefeat();
- }
- // === 触发游戏成功 ===
- private triggerGameSuccess() {
- console.log('[GameManager] 触发游戏成功');
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_SUCCESS);
- }
- // === 触发游戏失败 ===
- private triggerGameDefeat() {
- console.log('[GameManager] 触发游戏失败');
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.GAME_DEFEAT);
- }
- // === EnemyController相关方法已通过事件系统解耦,不再需要直接访问 ===
-
- public setTotalEnemiesSpawned(count: number) {
- this.totalEnemiesSpawned = count;
- }
- onDestroy() {
- // 清理GamePause事件监听
- const eventBus = EventBus.getInstance();
- eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
- eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
- eventBus.off(GameEvents.GAME_RESTART, this.onGameRestartEvent, this);
- eventBus.off(GameEvents.RESET_GAME_MANAGER, this.onResetGameManagerEvent, this);
- eventBus.off('CONTINUE_CLICK', this.onMainMenuClick, this);
- // ENEMY_KILLED事件监听已迁移到 InGameManager
- // 按钮事件监听已迁移到 UIStateManager
- // 清理单例实例
- if (GameManager._instance === this) {
- GameManager._instance = null;
- }
- }
- // === 加载当前关卡配置 ===
- public async loadCurrentLevelConfig() {
- if (!this.saveDataManager || !this.levelConfigManager) return;
-
- const currentLevel = this.saveDataManager.getCurrentLevel();
-
- try {
- const levelConfig = await this.levelConfigManager.getLevelConfig(currentLevel);
- if (levelConfig) {
- this.applyLevelConfig(levelConfig);
- } else {
- console.warn(`关卡 ${currentLevel} 配置加载失败`);
- }
- } catch (error) {
- console.error(`关卡 ${currentLevel} 配置加载错误:`, error);
- }
- }
-
- private applyLevelConfig(levelConfig: any) {
- console.log('[GameManager] 委托关卡配置应用给InGameManager');
-
- // 委托给InGameManager处理关卡配置
- if (this.inGameManager) {
- this.inGameManager.applyLevelConfig(levelConfig);
- } else {
- console.warn('[GameManager] InGameManager未初始化,无法应用关卡配置');
-
- // 备用方案:基本的波次配置处理(已简化)
- if (levelConfig.waves && Array.isArray(levelConfig.waves)) {
- this.currentWave = 1;
- console.log('[GameManager] 使用备用方案处理波次配置(功能有限)');
- console.warn('[GameManager] 建议确保InGameManager正确初始化以获得完整功能');
- }
- }
- }
- // === 获取当前关卡信息 ===
- public async getCurrentLevelInfo() {
- const currentLevel = this.saveDataManager ?
- this.saveDataManager.getCurrentLevel() :
- (this.levelManager ? this.levelManager.getCurrentLevel() : 1);
-
- const levelProgress = this.saveDataManager ?
- this.saveDataManager.getLevelProgress(currentLevel) : null;
-
- const levelData = this.levelManager ?
- this.levelManager.getLevelData(currentLevel) : null;
-
- const levelConfig = await this.loadCurrentLevelConfig();
- return {
- level: currentLevel,
- maxUnlockedLevel: this.saveDataManager ?
- this.saveDataManager.getMaxUnlockedLevel() :
- (this.levelManager ? this.levelManager.getMaxUnlockedLevel() : 1),
- progress: levelProgress,
- data: levelData,
- config: levelConfig,
- playerData: this.saveDataManager ? {
- money: this.saveDataManager.getMoney(),
- diamonds: this.saveDataManager.getDiamonds(),
- wallLevel: this.saveDataManager.getWallLevel()
- } : null
- };
- }
- // === 波次管理和能量系统已迁移到 InGameManager ===
- // 这些方法现在委托给 InGameManager 处理
-
- // === 获取当前波次(委托给InGameManager)===
- public getCurrentWave(): number {
- if (this.inGameManager) {
- return this.inGameManager.getCurrentWave();
- }
- return this.currentWave;
- }
-
- // === 获取当前能量值(委托给InGameManager)===
- public getCurrentEnergy(): number {
- if (this.inGameManager) {
- return this.inGameManager.getCurrentEnergy();
- }
- return 0; // 默认值
- }
-
- // === 获取最大能量值(委托给InGameManager)===
- public getMaxEnergy(): number {
- if (this.inGameManager) {
- return this.inGameManager.getMaxEnergy();
- }
- return 5; // 默认值
- }
- /* ========= 墙体血量 / 等级相关 ========= */
- // === 获取墙体血量(委托给InGameManager)===
- private getWallHealth(): number {
- if (this.inGameManager) {
- return this.inGameManager.getWallHealth();
- }
- // 备用方案:直接访问墙体组件
- return this.wallComponent ? this.wallComponent.getCurrentHealth() : 100;
- }
- // === 墙体血量 / 等级相关方法 - 现在委托给InGameManager ===
- public getWallHealthByLevel(level: number): number {
- if (this.inGameManager) {
- return this.inGameManager.getWallHealthByLevel(level);
- }
- // 备用方案:直接访问墙体组件
- return this.wallComponent ? this.wallComponent.getWallHealthByLevel(level) : 100;
- }
- public getCurrentWallLevel(): number {
- if (this.inGameManager) {
- return this.inGameManager.getCurrentWallLevel();
- }
- // 备用方案:直接访问墙体组件
- return this.wallComponent ? this.wallComponent.getCurrentWallLevel() : 1;
- }
- public getCurrentWallHealth(): number {
- if (this.inGameManager) {
- return this.inGameManager.getCurrentWallHealth();
- }
- // 备用方案:直接访问墙体组件
- return this.wallComponent ? this.wallComponent.getCurrentHealth() : 100;
- }
- // 初始化GameBlockSelection组件
- public initGameBlockSelection() {
- console.log('[GameManager] 初始化GameBlockSelection组件');
- console.log('[GameManager] gameBlockSelection节点:', !!this.gameBlockSelection, this.gameBlockSelection?.name);
-
- if (this.gameBlockSelection) {
- this.blockSelectionComponent = this.gameBlockSelection.getComponent(GameBlockSelection);
- console.log('[GameManager] GameBlockSelection组件获取结果:', !!this.blockSelectionComponent);
-
- if (this.blockSelectionComponent) {
- // 设置确认回调
- this.blockSelectionComponent.setConfirmCallback(() => {
- this.handleConfirmAction();
- });
- console.log('[GameManager] GameBlockSelection组件初始化成功,确认回调已设置');
- } else {
- console.error('[GameManager] 无法获取GameBlockSelection组件,请检查节点是否正确挂载了该组件');
- }
- } else {
- console.error('[GameManager] gameBlockSelection节点未设置,请在Inspector中拖拽正确的节点');
- }
- }
- // 初始化GameStartMove组件
- private initGameStartMove() {
- if (this.cameraNode) {
- this.gameStartMoveComponent = this.cameraNode.getComponent(GameStartMove);
- if (this.gameStartMoveComponent) {
- console.log('[GameManager] GameStartMove组件初始化成功');
- } else {
- console.warn('[GameManager] 未找到GameStartMove组件');
- }
- } else {
- console.warn('[GameManager] 摄像机节点未设置,无法初始化GameStartMove组件');
- }
- }
- // 处理确认操作(委托给InGameManager)
- private handleConfirmAction() {
- console.log('[GameManager] 方块选择确认,委托给InGameManager处理');
-
- // 委托给InGameManager处理确认操作
- if (this.inGameManager) {
- this.inGameManager.handleConfirmAction();
- } else {
- console.warn('[GameManager] InGameManager未初始化,无法处理确认操作');
- }
- }
- // === 单例模式支持 ===
- private static _instance: GameManager = null;
- /**
- * 获取GameManager单例实例
- */
- public static getInstance(): GameManager {
- return GameManager._instance;
- }
- /**
- * 设置GameManager单例实例
- */
- public static setInstance(instance: GameManager): void {
- GameManager._instance = instance;
- }
- onLoad() {
- // 设置单例实例
- GameManager.setInstance(this);
- }
- }
|