| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081 |
- 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';
- import { AdManager } from '../Ads/AdManager';
- // 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';
- import { GuideManager } from '../../NewbieGuidePlugin-v1.0.0/NewbieGuidePlugin-v1.0.0/scripts/GuideManager';
- import DragBlockToGridStep from '../Guide/DragBlockToGridStep';
- import { NewbieGuideManager } from '../Core/NewbieGuideManager';
- 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();
-
- // 游戏启动流程将在用户点击战斗按钮时触发,而不是在场景加载时自动触发
- // 初始化并启动新手引导(仅注册基础拖拽到网格步骤)
- try {
- const newbieGuideManager = NewbieGuideManager.getInstance();
- if (newbieGuideManager.isNewUser()) {
- const guideManager = GuideManager.getInstance();
- guideManager.init();
- guideManager.registerStep(new DragBlockToGridStep());
- guideManager.startGuide();
- console.log('[GameManager] 新手引导已初始化并启动');
- } else {
- console.log('[GameManager] 检测到用户已完成新手引导,跳过插件引导初始化');
- const guideMaskAreas = find('Canvas/GuideMaskAreas');
- if (guideMaskAreas) {
- guideMaskAreas.active = false;
- console.log('[GameManager] 已隐藏Canvas/GuideMaskAreas(非新手引导)');
- }
- // 同步隐藏手指动画层(GuideLayer)
- const guideLayer = find('Canvas/GuideLayer');
- if (guideLayer) {
- guideLayer.active = false;
- console.log('[GameManager] 已隐藏Canvas/GuideLayer(非新手引导)');
- }
- }
- } catch (e) {
- console.warn('[GameManager] 新手引导初始化失败:', e);
- }
- }
- /**
- * 设置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_RESUME_SKILL_SELECTION, this.onGameResumeEvent, this);
- eventBus.on(GameEvents.GAME_RESUME_BLOCK_SELECTION, 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初始化完成');
-
- // 初始化广告管理器
- AdManager.initialize();
- console.log('[GameManager] AdManager初始化完成');
-
- // 存档管理器初始化已迁移到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);
-
- // 检查是否需要解锁商店功能(第3关完成后解锁)
- this.checkShopUnlock(currentLevel);
-
- console.log(`[GameManager] 关卡完成记录已保存,关卡: ${currentLevel}, 游戏时长: ${gameTime}秒`);
- }
-
- /**
- * 检查商店解锁条件
- * @param completedLevel 完成的关卡数
- */
- private checkShopUnlock(completedLevel: number) {
- // 第3关完成后解锁商店
- if (completedLevel >= 3) {
- const navBarNode = find('Canvas/NavBar');
- if (navBarNode) {
- const navBarController = navBarNode.getComponent('NavBarController') as any;
- if (navBarController && typeof navBarController.unlockButton === 'function') {
- // 解锁商店按钮(索引3对应商店按钮)
- navBarController.unlockButton(3);
- console.log('[GameManager] 商店功能已解锁');
- }
- }
- }
- }
-
- // 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}关`);
- // 仅当下一关触发新武器解锁时才显示 GainUI
- if (this.saveDataManager && this.saveDataManager.hasNewWeaponUnlockAtLevel(nextLevel)) {
- EventBus.getInstance().emit(GameEvents.SHOW_GAIN_UI);
- }
- } 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) {
- // 根据游戏结果决定是否播放奖励动画
- const inGameManagerForUI = this.getInGameManager();
- const currentStateForUI = inGameManagerForUI?.getCurrentState();
- const isSuccessForUI = currentStateForUI === GameState.SUCCESS;
- if (isSuccessForUI && typeof (mainUIController as any).onReturnToMainUIWithReward === 'function') {
- console.log('[GameManager] 游戏胜利,调用返回主界面并播放奖励动画方法');
- (mainUIController as any).onReturnToMainUIWithReward();
- } else 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);
-
- // 发送应用状态切换事件,通知MenuController等组件
- const eventBus = EventBus.getInstance();
- eventBus.emit(GameEvents.APP_STATE_CHANGED, state);
- console.log(`[GameManager] 发送应用状态切换事件: ${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_RESUME, this.onGameResumeEvent, this);
- eventBus.off(GameEvents.GAME_RESUME_SKILL_SELECTION, this.onGameResumeEvent, this);
- eventBus.off(GameEvents.GAME_RESUME_BLOCK_SELECTION, this.onGameResumeEvent, 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) {
- await this.applyLevelConfig(levelConfig);
- } else {
- console.warn(`关卡 ${currentLevel} 配置加载失败`);
- }
- } catch (error) {
- console.error(`关卡 ${currentLevel} 配置加载错误:`, error);
- }
- }
-
- private async applyLevelConfig(levelConfig: any) {
- console.log('[GameManager] 委托关卡配置应用给InGameManager');
-
- // 委托给InGameManager处理关卡配置
- if (this.inGameManager) {
- await 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);
- }
- }
|