IN_game.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. import { _decorator, Component, Node, Label, ProgressBar, find } from 'cc';
  2. import EventBus, { GameEvents } from '../Core/EventBus';
  3. import { GameBlockSelection } from '../CombatSystem/BlockSelection/GameBlockSelection';
  4. import { Wall } from '../CombatSystem/Wall';
  5. import { GameStartMove } from '../Animations/GameStartMove';
  6. import { LevelSessionManager } from '../Core/LevelSessionManager';
  7. import { SaveDataManager } from './SaveDataManager';
  8. import { SkillManager } from '../CombatSystem/SkillSelection/SkillManager';
  9. import { ReStartGame } from './ReStartGame';
  10. const { ccclass, property } = _decorator;
  11. /**
  12. * 游戏内状态枚举
  13. * 仅在 AppState.IN_GAME 时使用
  14. */
  15. export enum GameState {
  16. PLAYING = 'playing',
  17. SUCCESS = 'success',
  18. DEFEAT = 'defeat',
  19. PAUSED = 'paused',
  20. BLOCK_SELECTION = 'block_selection'
  21. }
  22. /**
  23. * 游戏内状态管理器
  24. * 负责管理游戏进行中的所有状态和逻辑
  25. */
  26. @ccclass('InGameManager')
  27. export class InGameManager extends Component {
  28. // === 游戏内UI节点引用 ===
  29. @property({
  30. type: Node,
  31. tooltip: '拖拽BallController节点到这里'
  32. })
  33. public ballController: Node = null;
  34. @property({
  35. type: Node,
  36. tooltip: '拖拽GameBlockSelection节点到这里'
  37. })
  38. public gameBlockSelection: Node = null;
  39. @property({
  40. type: Node,
  41. tooltip: '拖拽GameArea节点到这里'
  42. })
  43. public gameArea: Node = null;
  44. @property({
  45. type: Node,
  46. tooltip: '拖拽EnemyController节点到这里'
  47. })
  48. public enemyManager: Node = null;
  49. @property({
  50. type: Node,
  51. tooltip: '摄像机节点,用于获取GameStartMove组件'
  52. })
  53. public cameraNode: Node = null;
  54. @property({
  55. type: Node,
  56. tooltip: '墙体节点,用于获取Wall组件'
  57. })
  58. public wallNode: Node = null;
  59. @property({
  60. type: Node,
  61. tooltip: '上围栏墙体节点 (TopFence)'
  62. })
  63. public topFenceNode: Node = null;
  64. @property({
  65. type: Node,
  66. tooltip: '下围栏墙体节点 (BottomFence)'
  67. })
  68. public bottomFenceNode: Node = null;
  69. // === 游戏配置属性 ===
  70. @property({
  71. tooltip: '状态检查间隔(秒)'
  72. })
  73. public checkInterval: number = 1.0;
  74. // === 私有属性 ===
  75. private gameStarted: boolean = false;
  76. private currentState: GameState = GameState.PLAYING;
  77. private checkTimer: number = 0;
  78. // EnemyController现在通过事件系统通信,不再直接引用
  79. private enemySpawningStarted: boolean = false;
  80. private totalEnemiesSpawned: number = 0;
  81. private currentWave: number = 1;
  82. private currentWaveEnemyCount: number = 0;
  83. private currentWaveTotalEnemies: number = 0;
  84. public levelWaves: any[] = [];
  85. private levelTotalEnemies: number = 0;
  86. private enemiesKilled: number = 0;
  87. // 游戏计时器
  88. private gameStartTime: number = 0;
  89. private gameEndTime: number = 0;
  90. // 游戏区域的边界
  91. private gameBounds = {
  92. left: 0,
  93. right: 0,
  94. top: 0,
  95. bottom: 0
  96. };
  97. private preparingNextWave = false;
  98. // 能量系统
  99. private energyPoints: number = 0;
  100. private energyMax: number = 5;
  101. private energyBar: ProgressBar = null;
  102. private selectSkillUI: Node = null;
  103. // GameBlockSelection组件
  104. private blockSelectionComponent: GameBlockSelection = null;
  105. private pendingSkillSelection: boolean = false;
  106. private pendingBlockSelection: boolean = false;
  107. private shouldShowNextWavePrompt: boolean = false;
  108. // 墙体组件引用
  109. private wallComponent: Wall = null;
  110. private topFenceComponent: Wall = null;
  111. private bottomFenceComponent: Wall = null;
  112. // GameStartMove组件引用
  113. private gameStartMoveComponent: GameStartMove = null;
  114. start() {
  115. this.initializeGameState();
  116. this.setupEventListeners();
  117. this.initGameBlockSelection();
  118. this.initGameStartMove();
  119. this.initWallComponent();
  120. this.initUINodes();
  121. }
  122. update(deltaTime: number) {
  123. if (this.currentState !== GameState.PLAYING) {
  124. return;
  125. }
  126. this.checkTimer += deltaTime;
  127. if (this.checkTimer >= this.checkInterval) {
  128. this.checkTimer = 0;
  129. this.checkGameState();
  130. }
  131. }
  132. /**
  133. * 初始化游戏状态
  134. */
  135. private initializeGameState() {
  136. this.currentState = GameState.PLAYING;
  137. this.checkTimer = 0;
  138. this.enemySpawningStarted = false;
  139. this.totalEnemiesSpawned = 0;
  140. this.currentWave = 1;
  141. this.currentWaveEnemyCount = 0;
  142. this.currentWaveTotalEnemies = 0;
  143. this.energyPoints = 0;
  144. this.pendingSkillSelection = false;
  145. }
  146. /**
  147. * 设置事件监听器
  148. */
  149. private setupEventListeners() {
  150. const eventBus = EventBus.getInstance();
  151. eventBus.on(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
  152. eventBus.on(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
  153. eventBus.on(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
  154. eventBus.on('ENEMY_KILLED', this.onEnemyKilledEvent, this);
  155. }
  156. /**
  157. * 初始化GameBlockSelection组件
  158. */
  159. private initGameBlockSelection() {
  160. if (this.gameBlockSelection) {
  161. this.blockSelectionComponent = this.gameBlockSelection.getComponent(GameBlockSelection);
  162. }
  163. }
  164. /**
  165. * 初始化GameStartMove组件
  166. */
  167. private initGameStartMove() {
  168. if (this.cameraNode) {
  169. this.gameStartMoveComponent = this.cameraNode.getComponent(GameStartMove);
  170. }
  171. }
  172. /**
  173. * EnemyController现在通过事件系统通信,不再需要直接初始化
  174. */
  175. /**
  176. * 初始化墙体组件
  177. */
  178. private initWallComponent() {
  179. // 初始化主墙体
  180. if (this.wallNode) {
  181. this.wallComponent = this.wallNode.getComponent(Wall);
  182. if (this.wallComponent) {
  183. console.log('[InGameManager] 主墙体组件初始化成功');
  184. } else {
  185. console.warn('[InGameManager] 未找到主墙体Wall组件');
  186. }
  187. } else {
  188. console.warn('[InGameManager] 主墙体节点未通过装饰器挂载');
  189. }
  190. // 初始化上围栏
  191. if (this.topFenceNode) {
  192. this.topFenceComponent = this.topFenceNode.getComponent(Wall);
  193. if (this.topFenceComponent) {
  194. console.log('[InGameManager] 上围栏墙体组件初始化成功');
  195. } else {
  196. console.warn('[InGameManager] 未找到上围栏Wall组件');
  197. }
  198. } else {
  199. console.warn('[InGameManager] 上围栏节点未通过装饰器挂载');
  200. }
  201. // 初始化下围栏
  202. if (this.bottomFenceNode) {
  203. this.bottomFenceComponent = this.bottomFenceNode.getComponent(Wall);
  204. if (this.bottomFenceComponent) {
  205. console.log('[InGameManager] 下围栏墙体组件初始化成功');
  206. } else {
  207. console.warn('[InGameManager] 未找到下围栏Wall组件');
  208. }
  209. } else {
  210. console.warn('[InGameManager] 下围栏节点未通过装饰器挂载');
  211. }
  212. }
  213. /**
  214. * 获取指定波次的敌人配置
  215. */
  216. private getWaveEnemyConfigs(wave: number): any[] {
  217. if (!this.levelWaves || wave < 1 || wave > this.levelWaves.length) {
  218. return [];
  219. }
  220. const waveConfig = this.levelWaves[wave - 1];
  221. return waveConfig?.enemies || [];
  222. }
  223. /**
  224. * 检查是否应该显示方块选择UI
  225. */
  226. public shouldShowBlockSelection(): boolean {
  227. return this.preparingNextWave && this.currentState === GameState.BLOCK_SELECTION;
  228. }
  229. /**
  230. * 在技能选择后显示方块选择UI
  231. */
  232. public showBlockSelectionAfterSkill() {
  233. if (this.pendingBlockSelection) {
  234. console.log('[InGameManager] 技能选择完成,现在显示方块选择UI');
  235. this.pendingBlockSelection = false;
  236. if (this.currentWave < (this.levelWaves?.length || 1)) {
  237. this.showBlockSelectionForNextWave();
  238. } else {
  239. console.log('[InGameManager] 最后一波结束,触发游戏胜利');
  240. this.triggerGameSuccess();
  241. }
  242. } else if (this.shouldShowBlockSelection()) {
  243. this.showBlockSelectionForNextWave();
  244. }
  245. }
  246. /**
  247. * 显示下一波方块选择UI
  248. */
  249. private showBlockSelectionForNextWave() {
  250. // 如果游戏已经结束,不显示方块选择UI
  251. if (this.isGameOver()) {
  252. console.warn('[InGameManager] 游戏已经结束(胜利或失败),不显示下一波方块选择UI!');
  253. return;
  254. }
  255. console.log('[InGameManager] 显示方块选择UI,准备播放进入动画');
  256. if (this.blockSelectionComponent) {
  257. this.blockSelectionComponent.showBlockSelection(true);
  258. // 通过事件系统暂停游戏
  259. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  260. }
  261. // 注意:不在这里直接调用动画,showBlockSelection()内部的playShowAnimation()会处理动画
  262. // 避免重复调用enterBlockSelectionMode导致摄像头偏移两倍
  263. }
  264. /**
  265. * 处理游戏成功事件
  266. */
  267. private onGameSuccessEvent() {
  268. console.log('[InGameManager] 接收到游戏成功事件');
  269. this.currentState = GameState.SUCCESS;
  270. }
  271. /**
  272. * 处理游戏失败事件
  273. */
  274. private onGameDefeatEvent() {
  275. console.log('[InGameManager] 接收到游戏失败事件');
  276. this.currentState = GameState.DEFEAT;
  277. }
  278. /**
  279. * 处理游戏恢复事件
  280. */
  281. private onGameResumeEvent() {
  282. console.log('[InGameManager] 接收到游戏恢复事件');
  283. }
  284. /**
  285. * 处理敌人被击杀事件
  286. */
  287. private onEnemyKilledEvent() {
  288. // 游戏状态检查现在通过事件系统处理
  289. if (this.isGameOver()) {
  290. console.warn('[InGameManager] 游戏已结束状态下onEnemyKilledEvent被调用!');
  291. return;
  292. }
  293. this.enemiesKilled++;
  294. this.currentWaveEnemyCount++;
  295. const remaining = this.currentWaveTotalEnemies - this.currentWaveEnemyCount;
  296. console.log(`[InGameManager] 敌人被消灭,当前波剩余敌人: ${remaining}/${this.currentWaveTotalEnemies}`);
  297. // 通过事件系统更新敌人计数标签
  298. EventBus.getInstance().emit(GameEvents.ENEMY_UPDATE_COUNT, this.currentWaveEnemyCount);
  299. // 通过事件系统检查是否有活跃敌人
  300. let hasActiveEnemies = false;
  301. EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_ACTIVE, (active: boolean) => {
  302. hasActiveEnemies = active;
  303. // 在回调中检查波次是否结束
  304. const isWaveEnd = remaining <= 0 && !hasActiveEnemies;
  305. if (isWaveEnd) {
  306. console.log(`[InGameManager] 波次结束检测: remaining=${remaining}, hasActiveEnemies=${hasActiveEnemies}`);
  307. const energyBeforeIncrement = this.energyPoints;
  308. this.incrementEnergy();
  309. const energyWillBeFull = energyBeforeIncrement + 1 >= this.energyMax;
  310. if (energyWillBeFull) {
  311. this.pendingBlockSelection = true;
  312. this.preparingNextWave = true;
  313. this.currentState = GameState.BLOCK_SELECTION;
  314. } else {
  315. if (this.currentWave < (this.levelWaves?.length || 1)) {
  316. this.showNextWavePrompt();
  317. } else {
  318. this.triggerGameSuccess();
  319. }
  320. }
  321. }
  322. });
  323. // 能量增加逻辑移到了回调函数中,这里不再需要重复处理
  324. }
  325. /**
  326. * 增加能量值
  327. */
  328. private incrementEnergy() {
  329. // 获取技能等级
  330. const energyHunterLevel = SkillManager.getInstance() ? SkillManager.getInstance().getSkillLevel('energy_hunter') : 0;
  331. const baseEnergy = 1;
  332. const bonusEnergy = SkillManager.calculateEnergyBonus ? SkillManager.calculateEnergyBonus(baseEnergy, energyHunterLevel) : baseEnergy;
  333. this.energyPoints = Math.min(this.energyPoints + bonusEnergy, this.energyMax);
  334. this.updateEnergyBar();
  335. console.log(`[InGameManager] 能量值增加: +${bonusEnergy}, 当前: ${this.energyPoints}/${this.energyMax}`);
  336. if (this.energyPoints >= this.energyMax) {
  337. this.onEnergyFull();
  338. }
  339. }
  340. /**
  341. * 显示下一波提示
  342. */
  343. private showNextWavePrompt() {
  344. // 设置准备下一波的状态
  345. this.preparingNextWave = true;
  346. this.pendingBlockSelection = true;
  347. this.currentState = GameState.BLOCK_SELECTION;
  348. console.log('[InGameManager] 设置准备下一波状态,准备显示方块选择UI');
  349. // 如果当前没有技能选择UI显示,立即显示方块选择UI
  350. if (!this.selectSkillUI || !this.selectSkillUI.active) {
  351. this.showBlockSelectionForNextWave();
  352. }
  353. // 如果技能选择UI正在显示,则等待技能选择完成后再显示方块选择UI
  354. // 这种情况下,pendingBlockSelection已经在onEnemyKilledEvent中设置为true
  355. }
  356. /**
  357. * 游戏状态检查
  358. */
  359. private checkGameState() {
  360. // 检查所有墙体是否存活
  361. const isAnyWallDestroyed =
  362. (this.wallComponent && !this.wallComponent.isAlive()) ||
  363. (this.topFenceComponent && !this.topFenceComponent.isAlive()) ||
  364. (this.bottomFenceComponent && !this.bottomFenceComponent.isAlive());
  365. if (isAnyWallDestroyed) {
  366. this.triggerGameDefeat();
  367. return;
  368. }
  369. if (this.checkAllEnemiesDefeated()) {
  370. this.triggerGameSuccess();
  371. return;
  372. }
  373. }
  374. /**
  375. * 检查所有敌人是否被击败
  376. */
  377. private checkAllEnemiesDefeated(): boolean {
  378. // 通过事件系统检查游戏是否开始
  379. let gameStarted = false;
  380. EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_GAME_STARTED, (started: boolean) => {
  381. gameStarted = started;
  382. });
  383. if (!this.enemySpawningStarted) {
  384. if (gameStarted) {
  385. this.enemySpawningStarted = true;
  386. } else {
  387. return false;
  388. }
  389. }
  390. // 通过事件系统获取当前敌人数量
  391. let currentEnemyCount = 0;
  392. EventBus.getInstance().emit(GameEvents.ENEMY_GET_COUNT, (count: number) => {
  393. currentEnemyCount = count;
  394. });
  395. if (this.levelTotalEnemies > 0) {
  396. return this.enemiesKilled >= this.levelTotalEnemies && currentEnemyCount === 0;
  397. }
  398. if (currentEnemyCount > this.totalEnemiesSpawned) {
  399. this.totalEnemiesSpawned = currentEnemyCount;
  400. }
  401. const shouldCheckVictory = this.enemySpawningStarted &&
  402. currentEnemyCount === 0 &&
  403. this.totalEnemiesSpawned > 0;
  404. return shouldCheckVictory;
  405. }
  406. /**
  407. * 触发游戏失败
  408. */
  409. private triggerGameDefeat() {
  410. EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
  411. }
  412. /**
  413. * 触发游戏成功
  414. */
  415. private triggerGameSuccess() {
  416. EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
  417. }
  418. /**
  419. * 获取游戏持续时间
  420. */
  421. public getGameDuration(): number {
  422. const endTime = this.gameEndTime || Date.now();
  423. return Math.max(0, endTime - this.gameStartTime);
  424. }
  425. /**
  426. * 获取当前游戏状态
  427. */
  428. public getCurrentState(): GameState {
  429. return this.currentState;
  430. }
  431. /**
  432. * 设置游戏状态
  433. */
  434. public setCurrentState(state: GameState) {
  435. this.currentState = state;
  436. }
  437. /**
  438. * 初始化UI节点
  439. */
  440. private initUINodes() {
  441. // 初始化能量条
  442. const energyBarNode = find('Canvas/GameLevelUI/EnergyBar');
  443. if (energyBarNode) {
  444. this.energyBar = energyBarNode.getComponent(ProgressBar);
  445. }
  446. // 初始化技能选择UI
  447. this.selectSkillUI = find('Canvas/GameLevelUI/SelectSkillUI');
  448. }
  449. /**
  450. * 更新能量条显示
  451. */
  452. private updateEnergyBar() {
  453. if (this.energyBar) {
  454. this.energyBar.progress = this.energyPoints / this.energyMax;
  455. }
  456. }
  457. /**
  458. * 能量满时的处理
  459. */
  460. private onEnergyFull() {
  461. console.log('[InGameManager] 能量已满,显示技能选择UI');
  462. // 直接显示技能选择UI
  463. this.showSkillSelection();
  464. }
  465. /**
  466. * 显示技能选择UI
  467. */
  468. private showSkillSelection() {
  469. if (this.selectSkillUI) {
  470. this.selectSkillUI.active = true;
  471. this.pauseGame();
  472. // 重置能量
  473. ReStartGame.resetEnergy();
  474. }
  475. }
  476. /**
  477. * 暂停游戏
  478. */
  479. private pauseGame() {
  480. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  481. }
  482. /**
  483. * 检查游戏是否结束
  484. */
  485. private isGameOver(): boolean {
  486. let gameOver = false;
  487. EventBus.getInstance().emit(GameEvents.GAME_CHECK_OVER, (isOver: boolean) => {
  488. gameOver = isOver;
  489. });
  490. return gameOver;
  491. }
  492. /**
  493. * 设置当前波次
  494. */
  495. public setCurrentWave(wave: number, enemyCount: number = 0) {
  496. this.currentWave = wave;
  497. this.currentWaveEnemyCount = 0; // 重置当前击杀数
  498. this.currentWaveTotalEnemies = enemyCount; // 设置该波次总敌人数
  499. const totalWaves = this.levelWaves?.length || 1;
  500. // 获取波次敌人配置
  501. const waveEnemyConfigs = this.getWaveEnemyConfigs(wave);
  502. // 通过事件系统启动波次
  503. EventBus.getInstance().emit(GameEvents.ENEMY_START_WAVE, {
  504. wave: wave,
  505. totalWaves: totalWaves,
  506. enemyCount: enemyCount,
  507. waveEnemyConfigs: waveEnemyConfigs
  508. });
  509. }
  510. /**
  511. * 更新当前波次敌人数量
  512. */
  513. public updateCurrentWaveEnemyCount(count: number) {
  514. this.currentWaveEnemyCount = count;
  515. }
  516. /**
  517. * 获取当前波次
  518. */
  519. public getCurrentWave(): number {
  520. return this.currentWave;
  521. }
  522. /**
  523. * 获取当前波次敌人数量
  524. */
  525. public getCurrentWaveEnemyCount(): number {
  526. return this.currentWaveEnemyCount;
  527. }
  528. /**
  529. * 获取当前波次总敌人数量
  530. */
  531. public getCurrentWaveTotalEnemies(): number {
  532. return this.currentWaveTotalEnemies;
  533. }
  534. /**
  535. * 进入下一波
  536. */
  537. public nextWave() {
  538. this.currentWave++;
  539. // 根据关卡配置获取下一波敌人数
  540. let enemyTotal = 0;
  541. if (this.levelWaves && this.levelWaves.length >= this.currentWave) {
  542. const waveCfg = this.levelWaves[this.currentWave - 1];
  543. if (waveCfg && waveCfg.enemies) {
  544. enemyTotal = waveCfg.enemies.reduce((t: number, g: any) => t + (g.count || 0), 0);
  545. }
  546. }
  547. this.setCurrentWave(this.currentWave, enemyTotal);
  548. }
  549. /**
  550. * 获取当前能量值
  551. */
  552. public getCurrentEnergy(): number {
  553. return this.energyPoints;
  554. }
  555. /**
  556. * 获取最大能量值
  557. */
  558. public getMaxEnergy(): number {
  559. return this.energyMax;
  560. }
  561. /**
  562. * 获取墙体健康度(返回主墙体健康度)
  563. */
  564. public getWallHealth(): number {
  565. if (this.wallComponent) {
  566. return this.wallComponent.getCurrentHealth();
  567. }
  568. return 0;
  569. }
  570. /**
  571. * 获取所有墙体的健康度
  572. */
  573. public getAllWallsHealth(): { main: number; topFence: number; bottomFence: number } {
  574. return {
  575. main: this.wallComponent ? this.wallComponent.getCurrentHealth() : 0,
  576. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentHealth() : 0,
  577. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentHealth() : 0
  578. };
  579. }
  580. /**
  581. * 升级墙体等级(升级主墙体)
  582. */
  583. public upgradeWallLevel(): { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null {
  584. if (this.wallComponent) {
  585. return this.wallComponent.upgradeWallLevel();
  586. }
  587. return null;
  588. }
  589. /**
  590. * 升级所有墙体等级
  591. */
  592. public upgradeAllWallsLevel(): {
  593. main: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  594. topFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  595. bottomFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  596. } {
  597. return {
  598. main: this.wallComponent ? this.wallComponent.upgradeWallLevel() : null,
  599. topFence: this.topFenceComponent ? this.topFenceComponent.upgradeWallLevel() : null,
  600. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.upgradeWallLevel() : null
  601. };
  602. }
  603. /**
  604. * 根据等级获取墙体健康度
  605. */
  606. public getWallHealthByLevel(level: number): number {
  607. if (this.wallComponent) {
  608. return this.wallComponent.getWallHealthByLevel(level);
  609. }
  610. return 100;
  611. }
  612. /**
  613. * 获取当前墙体等级(返回主墙体等级)
  614. */
  615. public getCurrentWallLevel(): number {
  616. if (this.wallComponent) {
  617. return this.wallComponent.getCurrentWallLevel();
  618. }
  619. return 1;
  620. }
  621. /**
  622. * 获取当前墙体健康度(返回主墙体健康度)
  623. */
  624. public getCurrentWallHealth(): number {
  625. if (this.wallComponent) {
  626. return this.wallComponent.getCurrentHealth();
  627. }
  628. return 100;
  629. }
  630. /**
  631. * 获取所有墙体的等级
  632. */
  633. public getAllWallsLevel(): { main: number; topFence: number; bottomFence: number } {
  634. return {
  635. main: this.wallComponent ? this.wallComponent.getCurrentWallLevel() : 1,
  636. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentWallLevel() : 1,
  637. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentWallLevel() : 1
  638. };
  639. }
  640. /**
  641. * 处理确认操作(方块选择确认)
  642. */
  643. public handleConfirmAction() {
  644. console.log('[InGameManager] 处理方块选择确认操作');
  645. // 如果是在准备下一波的状态,需要先切换到下一波
  646. if (this.preparingNextWave) {
  647. console.log('[InGameManager] 检测到准备下一波状态,切换到下一波');
  648. this.nextWave();
  649. // 重置状态
  650. this.preparingNextWave = false;
  651. this.pendingBlockSelection = false;
  652. this.currentState = GameState.PLAYING;
  653. }
  654. // 发送游戏开始事件,确保GamePause正确设置状态
  655. EventBus.getInstance().emit(GameEvents.GAME_START);
  656. console.log('[InGameManager] 发送GAME_START事件');
  657. // 通过事件系统启动球的移动
  658. EventBus.getInstance().emit(GameEvents.BALL_START);
  659. console.log('[InGameManager] 发送BALL_START事件,球已启动');
  660. // 通过事件系统开始当前波次的敌人生成
  661. EventBus.getInstance().emit(GameEvents.ENEMY_START_GAME);
  662. EventBus.getInstance().emit(GameEvents.ENEMY_SHOW_START_WAVE_PROMPT);
  663. console.log(`[InGameManager] 波次 ${this.currentWave} 敌人生成已启动`);
  664. }
  665. /**
  666. * 重置能量值(供ReStartGame调用)
  667. */
  668. public resetEnergyValue() {
  669. this.energyPoints = 0;
  670. this.updateEnergyBar();
  671. console.log('[InGameManager] 能量值重置完成');
  672. }
  673. /**
  674. * 重置波次信息(供ReStartGame调用)
  675. */
  676. public resetWaveInfo() {
  677. this.currentWave = 1;
  678. this.currentWaveEnemyCount = 0;
  679. this.currentWaveTotalEnemies = 0;
  680. this.enemiesKilled = 0;
  681. this.totalEnemiesSpawned = 0;
  682. this.levelTotalEnemies = 0;
  683. console.log('[InGameManager] 波次信息重置完成');
  684. }
  685. /**
  686. * 重置能量系统(供ReStartGame调用)
  687. */
  688. public resetEnergySystem() {
  689. this.energyPoints = 0;
  690. this.energyMax = 5;
  691. this.updateEnergyBar();
  692. console.log('[InGameManager] 能量系统重置完成');
  693. }
  694. /**
  695. * 重置游戏状态(供ReStartGame调用)
  696. */
  697. public resetGameStates() {
  698. this.currentState = GameState.PLAYING;
  699. this.gameStarted = false;
  700. this.enemySpawningStarted = false;
  701. this.preparingNextWave = false;
  702. this.pendingSkillSelection = false;
  703. this.pendingBlockSelection = false;
  704. this.shouldShowNextWavePrompt = false;
  705. this.gameStartTime = 0;
  706. this.gameEndTime = 0;
  707. this.checkTimer = 0;
  708. console.log('[InGameManager] 游戏状态重置完成');
  709. }
  710. /**
  711. * 应用关卡配置
  712. */
  713. public applyLevelConfig(levelConfig: any) {
  714. console.log('[InGameManager] 应用关卡配置');
  715. // 应用能量配置
  716. if (levelConfig.levelSettings && levelConfig.levelSettings.energyMax) {
  717. this.energyMax = levelConfig.levelSettings.energyMax;
  718. this.updateEnergyBar();
  719. console.log(`[InGameManager] 应用能量配置: ${this.energyMax}`);
  720. }
  721. // 如果有武器配置,应用武器
  722. if (levelConfig.weapons && Array.isArray(levelConfig.weapons)) {
  723. console.log('[InGameManager] 应用武器配置');
  724. // TODO: 应用武器配置逻辑
  725. }
  726. // 如果有波次配置,设置敌人波次
  727. if (levelConfig.waves && Array.isArray(levelConfig.waves)) {
  728. this.levelWaves = levelConfig.waves;
  729. this.currentWave = 1;
  730. // 计算本关卡总敌人数
  731. this.levelTotalEnemies = 0;
  732. for (const wave of this.levelWaves) {
  733. for (const enemy of wave.enemies || []) {
  734. this.levelTotalEnemies += enemy.count || 0;
  735. }
  736. }
  737. console.log(`[InGameManager] 应用波次配置: ${this.levelWaves.length}波,总敌人数: ${this.levelTotalEnemies}`);
  738. // 通过事件系统通知 EnemyController 初始化第一波数据及 UI
  739. const firstWaveEnemies = this.levelWaves.length > 0 && this.levelWaves[0].enemies ?
  740. this.levelWaves[0].enemies.reduce((t: number, g: any) => t + (g.count || 0), 0) : 0;
  741. // 通过事件系统调用setCurrentWave
  742. this.setCurrentWave(1, firstWaveEnemies);
  743. }
  744. }
  745. onDestroy() {
  746. const eventBus = EventBus.getInstance();
  747. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
  748. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
  749. eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
  750. eventBus.off('ENEMY_KILLED', this.onEnemyKilledEvent, this);
  751. }
  752. }