| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973 |
- import { _decorator, Component, Node, Prefab, instantiate, Vec3, find, director, Canvas, UITransform, Button, Label, PhysicsSystem2D, EPhysics2DDrawFlags, Vec2, sys } from 'cc';
- import { LevelManager } from './LevelManager';
- import { LevelConfigManager } from './LevelConfigManager';
- import { ShopManager } from '../ShopSystem/ShopManager';
- import { ConfigManager } from '../Core/ConfigManager';
- import { EnemyController } from '../CombatSystem/EnemyController';
- const { ccclass, property } = _decorator;
- /**
- * 游戏状态枚举
- */
- enum GameState {
- PLAYING = 'playing',
- SUCCESS = 'success',
- DEFEAT = 'defeat',
- PAUSED = 'paused'
- }
- /**
- * 增强版游戏管理器
- * 整合了游戏启动、状态管理、UI控制等功能
- */
- @ccclass('GameManager')
- export class GameManager extends Component {
-
- // === 原GameManager属性 ===
- @property({
- type: Node,
- tooltip: '拖拽BallController节点到这里'
- })
- public ballController: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽BlockSelectionUI节点到这里'
- })
- public blockSelectionUI: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽GameArea节点到这里'
- })
- public gameArea: Node = null;
- @property({
- type: Node,
- tooltip: '拖拽EnemyController节点到这里'
- })
- public enemyManager: Node = null;
- // === 游戏状态管理属性 ===
- @property({
- type: Node,
- tooltip: '血量显示节点 (HeartLabeld)'
- })
- public heartLabelNode: Node = null;
- @property({
- type: Node,
- tooltip: '游戏成功UI节点 (GameSuccess)'
- })
- public gameSuccessUI: Node = null;
- @property({
- type: Node,
- tooltip: '游戏失败UI节点 (GameDefeat)'
- })
- public gameDefeatUI: Node = null;
- @property({
- type: Node,
- tooltip: '波次显示节点 (WaveNumber)'
- })
- public waveNumberNode: Node = null;
- @property({
- type: Node,
- tooltip: '敌人数量显示节点 (EnemyNumber)'
- })
- public enemyNumberNode: Node = null;
- // === 游戏配置属性 ===
- @property({
- tooltip: '墙体初始血量'
- })
- public wallHealth: number = 1200;
- @property({
- tooltip: '初始血量'
- })
- public initialHealth: number = 100;
- @property({
- tooltip: '状态检查间隔(秒)'
- })
- public checkInterval: number = 1.0;
- // === 私有属性 ===
- private gameStarted: boolean = false;
- private currentHealth: number = 100;
- private currentState: GameState = GameState.PLAYING;
- private checkTimer: number = 0;
- private heartLabel: Label = null;
- private waveNumberLabel: Label = null;
- private enemyNumberLabel: Label = null;
- private enemyController: EnemyController = null;
- private levelManager: LevelManager = null;
- private levelConfigManager: LevelConfigManager = null;
- private shopManager: ShopManager = null;
- private configManager: ConfigManager = null;
- private enemySpawningStarted: boolean = false;
- private totalEnemiesSpawned: number = 0;
- private currentWave: number = 1;
- private currentWaveEnemyCount: number = 0;
- private currentWaveTotalEnemies: number = 0; // 当前波次总敌人数
- // 游戏区域的边界
- private gameBounds = {
- left: 0,
- right: 0,
- top: 0,
- bottom: 0
- };
- start() {
- // 初始化物理系统
- this.initPhysicsSystem();
-
- // 初始化管理器
- this.initializeManagers();
-
- // 计算游戏区域边界
- this.calculateGameBounds();
-
- // 初始化游戏状态
- this.initializeGameState();
-
- // 查找UI节点
- this.findUINodes();
-
- // 查找敌人控制器
- this.findEnemyController();
-
- // 初始化墙体血量显示
- this.initWallHealthDisplay();
-
- // 设置敌人控制器
- this.setupEnemyController();
-
- // 设置UI按钮
- this.setupUIButtons();
-
- // 加载当前关卡配置
- this.loadCurrentLevelConfig();
- }
- update(deltaTime: number) {
- if (this.currentState !== GameState.PLAYING) {
- return;
- }
- // 更新检查计时器
- this.checkTimer += deltaTime;
-
- if (this.checkTimer >= this.checkInterval) {
- this.checkTimer = 0;
- this.checkGameState();
- }
- }
- // === 物理系统初始化 ===
- private initPhysicsSystem() {
- // 启用物理系统
- PhysicsSystem2D.instance.enable = true;
-
- // 设置物理系统参数
- PhysicsSystem2D.instance.gravity = new Vec2(0, 0); // 无重力
-
- // 调试模式下显示碰撞框
- PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
- EPhysics2DDrawFlags.Pair |
- EPhysics2DDrawFlags.CenterOfMass |
- EPhysics2DDrawFlags.Joint |
- EPhysics2DDrawFlags.Shape;
- }
- // === 管理器初始化 ===
- private initializeManagers() {
- this.levelManager = LevelManager.getInstance();
- this.shopManager = ShopManager.getInstance();
- this.configManager = ConfigManager.getInstance();
- this.levelConfigManager = LevelConfigManager.getInstance();
- }
- // === 游戏状态初始化 ===
- private initializeGameState() {
- this.currentHealth = this.initialHealth;
- this.currentState = GameState.PLAYING;
- this.checkTimer = 0;
- this.enemySpawningStarted = false;
- this.totalEnemiesSpawned = 0;
- this.currentWave = 1;
- this.currentWaveEnemyCount = 0;
- this.currentWaveTotalEnemies = 0; // 当前波次总敌人数
-
- // 初始化UI显示
- this.updateWaveDisplay();
- this.updateEnemyCountDisplay();
- }
- // === 计算游戏区域边界 ===
- 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 findUINodes() {
- // 查找血量显示节点
- if (!this.heartLabelNode) {
- this.heartLabelNode = find('Canvas/GameLevelUI/HeartNode/HeartLabeld');
- }
-
- if (this.heartLabelNode) {
- this.heartLabel = this.heartLabelNode.getComponent(Label);
- }
-
- // 查找波次显示节点
- if (!this.waveNumberNode) {
- this.waveNumberNode = find('Canvas/GameLevelUI/WaveInfo/WaveNumber');
- }
-
- if (this.waveNumberNode) {
- this.waveNumberLabel = this.waveNumberNode.getComponent(Label);
- }
-
- // 查找敌人数量显示节点
- if (!this.enemyNumberNode) {
- this.enemyNumberNode = find('Canvas/GameLevelUI/EnemyNode/EnemyNumber');
- }
-
- if (this.enemyNumberNode) {
- this.enemyNumberLabel = this.enemyNumberNode.getComponent(Label);
- }
-
- // 查找游戏成功UI
- if (!this.gameSuccessUI) {
- this.gameSuccessUI = find('Canvas/GameSuccess');
- }
-
- if (this.gameSuccessUI) {
- this.gameSuccessUI.active = false;
- }
-
- // 查找游戏失败UI
- if (!this.gameDefeatUI) {
- this.gameDefeatUI = find('Canvas/GameDefeat');
- }
-
- if (this.gameDefeatUI) {
- this.gameDefeatUI.active = false;
- }
- }
- // === 查找敌人控制器 ===
- private findEnemyController() {
- if (this.enemyManager) {
- this.enemyController = this.enemyManager.getComponent(EnemyController);
- }
-
- if (!this.enemyController) {
- const enemyNode = find('Canvas/GameLevelUI/EnemyController');
- if (enemyNode) {
- this.enemyController = enemyNode.getComponent(EnemyController);
- }
- }
- }
- // === 初始化墙体血量显示 ===
- private initWallHealthDisplay() {
- if (this.heartLabelNode && this.heartLabel) {
- this.heartLabel.string = this.wallHealth.toString();
- }
-
- // 通知敌人控制器墙体血量节点
- if (this.enemyController && this.heartLabelNode) {
- this.updateEnemyControllerWallHealth(this.heartLabelNode);
- }
- }
- // === 更新EnemyController的墙体血量 ===
- private updateEnemyControllerWallHealth(wallHealthNode: Node) {
- if (this.enemyController) {
- // 同步血量值
- this.enemyController.wallHealth = this.wallHealth;
-
- // 设置血量显示节点
- const heartLabelNode = find('Canvas/GameLevelUI/HeartNode/HeartLabeld');
- if (heartLabelNode) {
- this.enemyController.wallHealthNode = heartLabelNode;
- }
-
- // 让EnemyController更新显示
- this.enemyController.updateWallHealthDisplay();
- }
- }
- // === 设置敌人控制器 ===
- private setupEnemyController() {
- if (!this.enemyManager) {
- const gameLevelUI = find('Canvas/GameLevelUI');
- if (!gameLevelUI) {
- console.error('找不到GameLevelUI节点,无法创建EnemyController');
- return;
- }
-
- this.enemyManager = new Node('EnemyController');
- gameLevelUI.addChild(this.enemyManager);
- }
-
- if (!this.enemyController) {
- this.enemyController = this.enemyManager.addComponent(EnemyController);
- this.enemyController.wallHealth = this.wallHealth;
- }
- }
- // === 游戏状态检查 ===
- private checkGameState() {
- // 检查血量
- this.updateHealthFromUI();
-
- if (this.currentHealth <= 0) {
- this.triggerGameDefeat();
- return;
- }
- // 更新当前敌人数量(用于胜利检测,不影响UI显示)
- if (this.enemyController) {
- const currentEnemyCount = this.enemyController.getCurrentEnemyCount ?
- this.enemyController.getCurrentEnemyCount() : 0;
- this.updateCurrentWaveEnemyCount(currentEnemyCount);
- }
- // 检查敌人状态
- if (this.checkAllEnemiesDefeated()) {
- this.triggerGameSuccess();
- return;
- }
- }
- // === 从UI更新血量 ===
- private updateHealthFromUI() {
- if (this.heartLabel) {
- const healthText = this.heartLabel.string;
- const healthMatch = healthText.match(/\d+/);
- if (healthMatch) {
- const newHealth = parseInt(healthMatch[0]);
- if (newHealth !== this.currentHealth) {
- this.currentHealth = newHealth;
- }
- }
- }
- }
- // === 检查所有敌人是否被击败 ===
- private checkAllEnemiesDefeated(): boolean {
- if (!this.enemyController) {
- return false;
- }
- // 检查敌人是否已开始生成
- if (!this.enemySpawningStarted) {
- // 检查游戏是否已开始(EnemyController的gameStarted属性)
- if (this.enemyController.isGameStarted()) {
- this.enemySpawningStarted = true;
- } else {
- // 敌人还未开始生成,不算胜利
- return false;
- }
- }
- // 获取当前敌人数量
- const currentEnemyCount = this.enemyController.getCurrentEnemyCount ?
- this.enemyController.getCurrentEnemyCount() : 0;
- // 更新已生成敌人总数(记录曾经达到的最大值)
- if (currentEnemyCount > this.totalEnemiesSpawned) {
- this.totalEnemiesSpawned = currentEnemyCount;
- }
- // 只有在以下情况下才算胜利:
- // 1. 敌人生成已开始
- // 2. 当前敌人数量为0
- // 3. 曾经生成过敌人(避免游戏刚开始就判定胜利)
- const shouldCheckVictory = this.enemySpawningStarted &&
- currentEnemyCount === 0 &&
- this.totalEnemiesSpawned > 0;
- return shouldCheckVictory;
- }
- // === 触发游戏失败 ===
- private triggerGameDefeat() {
- if (this.currentState === GameState.DEFEAT) {
- return;
- }
- this.currentState = GameState.DEFEAT;
- this.pauseGame();
- if (this.gameDefeatUI) {
- this.gameDefeatUI.active = true;
- }
- this.onGameDefeat();
- }
- // === 触发游戏成功 ===
- private triggerGameSuccess() {
- if (this.currentState === GameState.SUCCESS) {
- return;
- }
- this.currentState = GameState.SUCCESS;
- this.pauseGame();
- if (this.gameSuccessUI) {
- this.gameSuccessUI.active = true;
- }
- this.onGameSuccess();
- }
- // === 暂停游戏 ===
- private pauseGame() {
- this.gameStarted = false;
-
- if (this.enemyController && this.enemyController.pauseSpawning) {
- this.enemyController.pauseSpawning();
- }
- }
- // === 恢复游戏 ===
- private resumeGame() {
- this.currentState = GameState.PLAYING;
- this.gameStarted = true;
-
- if (this.enemyController && this.enemyController.resumeSpawning) {
- this.enemyController.resumeSpawning();
- }
- if (this.gameSuccessUI) {
- this.gameSuccessUI.active = false;
- }
- if (this.gameDefeatUI) {
- this.gameDefeatUI.active = false;
- }
- }
- // === 游戏失败回调 ===
- private onGameDefeat() {
- console.log('处理游戏失败逻辑');
- }
- // === 游戏成功回调 ===
- private onGameSuccess() {
- console.log('处理游戏成功逻辑');
- this.giveReward();
- this.onLevelComplete();
- }
- // === 给予奖励 ===
- private giveReward() {
- if (!this.shopManager) return;
- const baseReward = 100;
- const healthBonus = Math.floor(this.currentHealth * 0.1);
- const totalReward = baseReward + healthBonus;
-
- this.shopManager.addCoins(totalReward);
- }
- // === 处理关卡完成 ===
- private onLevelComplete(score: number = 0, stars: number = 1) {
- if (this.levelManager) {
- const currentLevel = this.levelManager.getCurrentLevel();
- this.levelManager.completeLevel(currentLevel, score, stars);
- }
- }
- // === 设置UI按钮 ===
- private setupUIButtons() {
- this.setupSuccessUIButtons();
- this.setupDefeatUIButtons();
- }
- // === 设置成功界面按钮 ===
- private setupSuccessUIButtons() {
- if (this.gameSuccessUI) {
- const nextLevelBtn = this.gameSuccessUI.getChildByName('NextLevelBtn');
- const restartBtn = this.gameSuccessUI.getChildByName('RestartBtn');
- const mainMenuBtn = this.gameSuccessUI.getChildByName('MainMenuBtn');
- const shopBtn = this.gameSuccessUI.getChildByName('ShopBtn');
- if (nextLevelBtn) {
- const button = nextLevelBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onNextLevelClick, this);
- }
- }
- if (restartBtn) {
- const button = restartBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onRestartClick, this);
- }
- }
- if (mainMenuBtn) {
- const button = mainMenuBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onMainMenuClick, this);
- }
- }
- if (shopBtn) {
- const button = shopBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onShopClick, this);
- }
- }
- }
- }
- // === 设置失败界面按钮 ===
- private setupDefeatUIButtons() {
- if (this.gameDefeatUI) {
- const restartBtn = this.gameDefeatUI.getChildByName('RestartBtn');
- const mainMenuBtn = this.gameDefeatUI.getChildByName('MainMenuBtn');
- const shopBtn = this.gameDefeatUI.getChildByName('ShopBtn');
- const reviveBtn = this.gameDefeatUI.getChildByName('ReviveBtn');
- if (restartBtn) {
- const button = restartBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onRestartClick, this);
- }
- }
- if (mainMenuBtn) {
- const button = mainMenuBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onMainMenuClick, this);
- }
- }
- if (shopBtn) {
- const button = shopBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onShopClick, this);
- }
- }
- if (reviveBtn) {
- const button = reviveBtn.getComponent(Button);
- if (button) {
- button.node.on(Button.EventType.CLICK, this.onReviveClick, this);
- }
- }
- }
- }
- // === 按钮点击事件处理 ===
- private onNextLevelClick() {
- if (this.levelManager) {
- const currentLevel = this.levelManager.getCurrentLevel();
- const nextLevel = currentLevel + 1;
-
- if (this.levelManager.isLevelUnlocked(nextLevel)) {
- this.levelManager.setCurrentLevel(nextLevel);
- this.restartCurrentLevel();
- }
- }
- }
- private onRestartClick() {
- this.restartCurrentLevel();
- }
- private onMainMenuClick() {
- this.restartGame();
- }
- private onShopClick() {
- // 可以在这里实现商店界面逻辑
- }
- private onReviveClick() {
- const reviveCost = 5;
-
- if (this.shopManager && this.shopManager.getGems() >= reviveCost) {
- if (this.shopManager.spendGems(reviveCost)) {
- this.revivePlayer();
- }
- }
- }
- // === 复活玩家 ===
- private revivePlayer() {
- this.setHealth(50);
- this.restartGame();
- }
- // === 重新开始当前关卡 ===
- private restartCurrentLevel() {
- this.restartGame();
- }
- // === 原GameManager方法 ===
- public onConfirmButtonClicked() {
- if (this.blockSelectionUI) {
- this.blockSelectionUI.active = false;
-
- const gridContainer = find('Canvas/GameLevelUI/GameArea/GridContainer');
- if (gridContainer) {
- gridContainer.active = true;
- }
-
- this.preservePlacedBlocks();
- }
- this.startGame();
- }
-
- private preservePlacedBlocks() {
- const blockController = find('Canvas/GameLevelUI/BlockController');
-
- if (blockController) {
- const blockManager = blockController.getComponent('BlockManager') as any;
-
- if (blockManager) {
- blockManager.onGameStart();
- }
- }
- }
- public startGame() {
- if (this.gameStarted) return;
-
- this.gameStarted = true;
- this.currentState = GameState.PLAYING;
- this.spawnBall();
- if (this.enemyController) {
- this.enemyController.startGame();
- }
- }
- private spawnBall() {
- if (!this.ballController) {
- return;
- }
- // 注意:BallController可能需要导入,这里先使用any类型
- const ballControllerComp = this.ballController.getComponent('BallController') as any;
- if (ballControllerComp) {
- ballControllerComp.initialize();
- }
- }
- public gameOver() {
- this.triggerGameDefeat();
- }
- // === 公共方法 ===
- public setHealth(health: number) {
- this.currentHealth = Math.max(0, health);
- }
- public takeDamage(damage: number) {
- this.currentHealth = Math.max(0, this.currentHealth - damage);
-
- if (this.currentHealth <= 0) {
- this.triggerGameDefeat();
- }
- }
- public getCurrentState(): GameState {
- return this.currentState;
- }
- public restartGame() {
- this.initializeGameState();
- this.resumeGame();
- }
- public isGameOver(): boolean {
- return this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT;
- }
- public forceGameSuccess() {
- this.triggerGameSuccess();
- }
- public forceGameDefeat() {
- this.triggerGameDefeat();
- }
- // === 获取EnemyController组件 ===
- public getEnemyController() {
- return this.enemyController;
- }
- // === 调试方法 ===
- public getEnemyStatus() {
- if (!this.enemyController) {
- return;
- }
- const currentCount = this.enemyController.getCurrentEnemyCount ?
- this.enemyController.getCurrentEnemyCount() : 0;
- const gameStarted = this.enemyController.isGameStarted();
- console.log('=== 敌人状态调试信息 ===');
- console.log(`敌人生成已开始: ${this.enemySpawningStarted}`);
- console.log(`当前敌人数量: ${currentCount}`);
- console.log(`曾生成最大敌人数: ${this.totalEnemiesSpawned}`);
- console.log(`EnemyController游戏已开始: ${gameStarted}`);
- console.log(`GameManager游戏状态: ${this.currentState}`);
- console.log(`GameManager游戏已开始: ${this.gameStarted}`);
- console.log(`当前血量: ${this.currentHealth}`);
-
- // 显示活跃敌人的详细信息
- if (this.enemyController.getActiveEnemies) {
- const activeEnemies = this.enemyController.getActiveEnemies();
- console.log(`活跃敌人列表 (${activeEnemies.length}个):`);
- activeEnemies.forEach((enemy, index) => {
- if (enemy && enemy.isValid) {
- console.log(` ${index + 1}. ${enemy.name} - 位置: (${enemy.position.x.toFixed(1)}, ${enemy.position.y.toFixed(1)})`);
- } else {
- console.log(` ${index + 1}. 无效敌人节点`);
- }
- });
- }
- }
- public forceStartEnemySpawning() {
- this.enemySpawningStarted = true;
- }
- public setTotalEnemiesSpawned(count: number) {
- this.totalEnemiesSpawned = count;
- }
- // === 测试方法 ===
- public testEnemyDetection() {
- const result = this.checkAllEnemiesDefeated();
- this.getEnemyStatus();
- return result;
- }
- // 测试组件获取是否正常
- public testComponentAccess() {
- console.log('=== 测试组件访问 ===');
-
- if (this.enemyController) {
- console.log('✅ EnemyController 访问正常');
- console.log(`当前敌人数量: ${this.enemyController.getCurrentEnemyCount()}`);
- console.log(`游戏是否开始: ${this.enemyController.isGameStarted()}`);
- } else {
- console.log('❌ EnemyController 未找到');
- }
-
- console.log('组件测试完成');
- }
- // === 测试敌人攻击 ===
- public testEnemyAttackWall() {
- console.log('=== GameManager: 测试敌人攻击墙体 ===');
-
- if (this.enemyController) {
- console.log(`攻击前墙体血量: ${this.enemyController.getCurrentWallHealth()}`);
-
- // 测试EnemyController的攻击功能
- const newHealth = this.enemyController.testEnemyAttack();
- console.log(`攻击后墙体血量: ${newHealth}`);
-
- // 强制所有敌人攻击
- this.enemyController.forceEnemyAttack();
-
- return newHealth;
- } else {
- console.error('EnemyController未找到');
- return -1;
- }
- }
- onDestroy() {
- // 清理按钮事件监听
- if (this.gameSuccessUI) {
- const buttons = this.gameSuccessUI.getComponentsInChildren(Button);
- buttons.forEach(button => {
- button.node.off(Button.EventType.CLICK);
- });
- }
- if (this.gameDefeatUI) {
- const buttons = this.gameDefeatUI.getComponentsInChildren(Button);
- buttons.forEach(button => {
- button.node.off(Button.EventType.CLICK);
- });
- }
- }
- // === 加载当前关卡配置 ===
- public async loadCurrentLevelConfig() {
- if (!this.levelManager || !this.levelConfigManager) {
- return null;
- }
- try {
- const currentLevel = this.levelManager.getCurrentLevel();
- console.log(`加载关卡 ${currentLevel} 配置`);
-
- const levelConfig = await this.levelConfigManager.getLevelConfig(currentLevel);
-
- if (levelConfig) {
- console.log(`关卡 ${currentLevel} 配置加载成功:`, levelConfig);
- this.applyLevelConfig(levelConfig);
- return levelConfig;
- } else {
- console.warn(`关卡 ${currentLevel} 的配置文件不存在`);
- return null;
- }
- } catch (error) {
- console.error('加载关卡配置失败:', error);
- return null;
- }
- }
- // === 应用关卡配置 ===
- private applyLevelConfig(levelConfig: any) {
- if (!levelConfig) return;
- // 应用关卡设置
- if (levelConfig.settings) {
- const settings = levelConfig.settings;
-
- if (settings.initialHealth !== undefined) {
- this.wallHealth = settings.initialHealth;
- this.initialHealth = settings.initialHealth;
- this.currentHealth = settings.initialHealth;
- }
-
- if (settings.checkInterval !== undefined) {
- this.checkInterval = settings.checkInterval;
- }
- }
- // 记录武器配置信息
- if (levelConfig.weapons) {
- console.log('关卡武器配置:', levelConfig.weapons);
- }
- // 记录敌人波次配置信息并初始化第一波
- if (levelConfig.waves && levelConfig.waves.length > 0) {
- console.log('关卡敌人波次配置:', levelConfig.waves);
-
- // 设置第一波的信息
- const firstWave = levelConfig.waves[0];
- if (firstWave && firstWave.enemies) {
- const totalEnemiesInFirstWave = firstWave.enemies.reduce((total, enemyGroup) => {
- return total + (enemyGroup.count || 0);
- }, 0);
-
- this.setCurrentWave(1, totalEnemiesInFirstWave);
- }
- }
- }
- // === 获取当前关卡信息 ===
- public async getCurrentLevelInfo() {
- if (!this.levelManager) {
- return null;
- }
- const currentLevel = this.levelManager.getCurrentLevel();
- const levelData = this.levelManager.getLevelData(currentLevel);
- const levelConfig = await this.loadCurrentLevelConfig();
- return {
- level: currentLevel,
- data: levelData,
- config: levelConfig
- };
- }
- // === 更新波次显示 ===
- private updateWaveDisplay() {
- if (this.waveNumberLabel) {
- this.waveNumberLabel.string = this.currentWave.toString();
- }
- }
- // === 更新敌人数量显示 ===
- private updateEnemyCountDisplay() {
- if (this.enemyNumberLabel) {
- this.enemyNumberLabel.string = this.currentWaveTotalEnemies.toString();
- }
- }
- // === 设置当前波次 ===
- public setCurrentWave(wave: number, enemyCount: number = 0) {
- this.currentWave = wave;
- this.currentWaveEnemyCount = 0; // 重置当前敌人数量为0
- this.currentWaveTotalEnemies = enemyCount; // 设置该波次总敌人数
- this.updateWaveDisplay();
- this.updateEnemyCountDisplay();
- }
- // === 更新当前波次敌人数量 ===
- public updateCurrentWaveEnemyCount(count: number) {
- this.currentWaveEnemyCount = count;
- // 不应该修改总敌人数,总敌人数只在设置波次时确定
- // this.currentWaveTotalEnemies = count; // 移除这行
- // 敌人数量显示的是总数,不需要在这里更新
- // this.updateEnemyCountDisplay(); // 移除这行
- }
- // === 获取当前波次 ===
- public getCurrentWave(): number {
- return this.currentWave;
- }
- // === 获取当前波次敌人数量 ===
- public getCurrentWaveEnemyCount(): number {
- return this.currentWaveEnemyCount;
- }
- // === 获取当前波次总敌人数量 ===
- public getCurrentWaveTotalEnemies(): number {
- return this.currentWaveTotalEnemies;
- }
- // === 进入下一波 ===
- public nextWave() {
- this.currentWave++;
- this.updateWaveDisplay();
-
- // 这里可以根据关卡配置设置下一波的敌人数量
- // 暂时设置为0,等待具体的波次系统实现
- this.setCurrentWave(this.currentWave, 0);
- }
- }
|