IN_game.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. const energyBeforeIncrement = this.energyPoints;
  306. this.incrementEnergy();
  307. const energyWillBeFull = energyBeforeIncrement + 1 >= this.energyMax;
  308. if (isWaveEnd) {
  309. if (energyWillBeFull) {
  310. this.pendingBlockSelection = true;
  311. this.preparingNextWave = true;
  312. this.currentState = GameState.BLOCK_SELECTION;
  313. } else {
  314. if (this.currentWave < (this.levelWaves?.length || 1)) {
  315. this.showNextWavePrompt();
  316. } else {
  317. this.triggerGameSuccess();
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * 增加能量值
  324. */
  325. private incrementEnergy() {
  326. // 获取技能等级
  327. const energyHunterLevel = SkillManager.getInstance() ? SkillManager.getInstance().getSkillLevel('energy_hunter') : 0;
  328. const baseEnergy = 1;
  329. const bonusEnergy = SkillManager.calculateEnergyBonus ? SkillManager.calculateEnergyBonus(baseEnergy, energyHunterLevel) : baseEnergy;
  330. this.energyPoints = Math.min(this.energyPoints + bonusEnergy, this.energyMax);
  331. this.updateEnergyBar();
  332. console.log(`[InGameManager] 能量值增加: +${bonusEnergy}, 当前: ${this.energyPoints}/${this.energyMax}`);
  333. if (this.energyPoints >= this.energyMax) {
  334. this.onEnergyFull();
  335. }
  336. }
  337. /**
  338. * 显示下一波提示
  339. */
  340. private showNextWavePrompt() {
  341. // 如果当前没有技能选择UI显示,立即显示方块选择UI
  342. if (!this.selectSkillUI || !this.selectSkillUI.active) {
  343. this.showBlockSelectionForNextWave();
  344. }
  345. // 如果技能选择UI正在显示,则等待技能选择完成后再显示方块选择UI
  346. // 这种情况下,pendingBlockSelection已经在onEnemyKilledEvent中设置为true
  347. }
  348. /**
  349. * 游戏状态检查
  350. */
  351. private checkGameState() {
  352. // 检查所有墙体是否存活
  353. const isAnyWallDestroyed =
  354. (this.wallComponent && !this.wallComponent.isAlive()) ||
  355. (this.topFenceComponent && !this.topFenceComponent.isAlive()) ||
  356. (this.bottomFenceComponent && !this.bottomFenceComponent.isAlive());
  357. if (isAnyWallDestroyed) {
  358. this.triggerGameDefeat();
  359. return;
  360. }
  361. if (this.checkAllEnemiesDefeated()) {
  362. this.triggerGameSuccess();
  363. return;
  364. }
  365. }
  366. /**
  367. * 检查所有敌人是否被击败
  368. */
  369. private checkAllEnemiesDefeated(): boolean {
  370. // 通过事件系统检查游戏是否开始
  371. let gameStarted = false;
  372. EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_GAME_STARTED, (started: boolean) => {
  373. gameStarted = started;
  374. });
  375. if (!this.enemySpawningStarted) {
  376. if (gameStarted) {
  377. this.enemySpawningStarted = true;
  378. } else {
  379. return false;
  380. }
  381. }
  382. // 通过事件系统获取当前敌人数量
  383. let currentEnemyCount = 0;
  384. EventBus.getInstance().emit(GameEvents.ENEMY_GET_COUNT, (count: number) => {
  385. currentEnemyCount = count;
  386. });
  387. if (this.levelTotalEnemies > 0) {
  388. return this.enemiesKilled >= this.levelTotalEnemies && currentEnemyCount === 0;
  389. }
  390. if (currentEnemyCount > this.totalEnemiesSpawned) {
  391. this.totalEnemiesSpawned = currentEnemyCount;
  392. }
  393. const shouldCheckVictory = this.enemySpawningStarted &&
  394. currentEnemyCount === 0 &&
  395. this.totalEnemiesSpawned > 0;
  396. return shouldCheckVictory;
  397. }
  398. /**
  399. * 触发游戏失败
  400. */
  401. private triggerGameDefeat() {
  402. EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
  403. }
  404. /**
  405. * 触发游戏成功
  406. */
  407. private triggerGameSuccess() {
  408. EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
  409. }
  410. /**
  411. * 获取游戏持续时间
  412. */
  413. public getGameDuration(): number {
  414. const endTime = this.gameEndTime || Date.now();
  415. return Math.max(0, endTime - this.gameStartTime);
  416. }
  417. /**
  418. * 获取当前游戏状态
  419. */
  420. public getCurrentState(): GameState {
  421. return this.currentState;
  422. }
  423. /**
  424. * 设置游戏状态
  425. */
  426. public setCurrentState(state: GameState) {
  427. this.currentState = state;
  428. }
  429. /**
  430. * 初始化UI节点
  431. */
  432. private initUINodes() {
  433. // 初始化能量条
  434. const energyBarNode = find('Canvas/GameLevelUI/EnergyBar');
  435. if (energyBarNode) {
  436. this.energyBar = energyBarNode.getComponent(ProgressBar);
  437. }
  438. // 初始化技能选择UI
  439. this.selectSkillUI = find('Canvas/GameLevelUI/SelectSkillUI');
  440. }
  441. /**
  442. * 更新能量条显示
  443. */
  444. private updateEnergyBar() {
  445. if (this.energyBar) {
  446. this.energyBar.progress = this.energyPoints / this.energyMax;
  447. }
  448. }
  449. /**
  450. * 能量满时的处理
  451. */
  452. private onEnergyFull() {
  453. console.log('[InGameManager] 能量已满,显示技能选择UI');
  454. // 直接显示技能选择UI
  455. this.showSkillSelection();
  456. }
  457. /**
  458. * 显示技能选择UI
  459. */
  460. private showSkillSelection() {
  461. if (this.selectSkillUI) {
  462. this.selectSkillUI.active = true;
  463. this.pauseGame();
  464. // 重置能量
  465. ReStartGame.resetEnergy();
  466. }
  467. }
  468. /**
  469. * 暂停游戏
  470. */
  471. private pauseGame() {
  472. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  473. }
  474. /**
  475. * 检查游戏是否结束
  476. */
  477. private isGameOver(): boolean {
  478. let gameOver = false;
  479. EventBus.getInstance().emit(GameEvents.GAME_CHECK_OVER, (isOver: boolean) => {
  480. gameOver = isOver;
  481. });
  482. return gameOver;
  483. }
  484. /**
  485. * 设置当前波次
  486. */
  487. public setCurrentWave(wave: number, enemyCount: number = 0) {
  488. this.currentWave = wave;
  489. this.currentWaveEnemyCount = 0; // 重置当前击杀数
  490. this.currentWaveTotalEnemies = enemyCount; // 设置该波次总敌人数
  491. const totalWaves = this.levelWaves?.length || 1;
  492. // 获取波次敌人配置
  493. const waveEnemyConfigs = this.getWaveEnemyConfigs(wave);
  494. // 通过事件系统启动波次
  495. EventBus.getInstance().emit(GameEvents.ENEMY_START_WAVE, {
  496. wave: wave,
  497. totalWaves: totalWaves,
  498. enemyCount: enemyCount,
  499. waveEnemyConfigs: waveEnemyConfigs
  500. });
  501. }
  502. /**
  503. * 更新当前波次敌人数量
  504. */
  505. public updateCurrentWaveEnemyCount(count: number) {
  506. this.currentWaveEnemyCount = count;
  507. }
  508. /**
  509. * 获取当前波次
  510. */
  511. public getCurrentWave(): number {
  512. return this.currentWave;
  513. }
  514. /**
  515. * 获取当前波次敌人数量
  516. */
  517. public getCurrentWaveEnemyCount(): number {
  518. return this.currentWaveEnemyCount;
  519. }
  520. /**
  521. * 获取当前波次总敌人数量
  522. */
  523. public getCurrentWaveTotalEnemies(): number {
  524. return this.currentWaveTotalEnemies;
  525. }
  526. /**
  527. * 进入下一波
  528. */
  529. public nextWave() {
  530. this.currentWave++;
  531. // 根据关卡配置获取下一波敌人数
  532. let enemyTotal = 0;
  533. if (this.levelWaves && this.levelWaves.length >= this.currentWave) {
  534. const waveCfg = this.levelWaves[this.currentWave - 1];
  535. if (waveCfg && waveCfg.enemies) {
  536. enemyTotal = waveCfg.enemies.reduce((t: number, g: any) => t + (g.count || 0), 0);
  537. }
  538. }
  539. this.setCurrentWave(this.currentWave, enemyTotal);
  540. }
  541. /**
  542. * 获取当前能量值
  543. */
  544. public getCurrentEnergy(): number {
  545. return this.energyPoints;
  546. }
  547. /**
  548. * 获取最大能量值
  549. */
  550. public getMaxEnergy(): number {
  551. return this.energyMax;
  552. }
  553. /**
  554. * 获取墙体健康度(返回主墙体健康度)
  555. */
  556. public getWallHealth(): number {
  557. if (this.wallComponent) {
  558. return this.wallComponent.getCurrentHealth();
  559. }
  560. return 0;
  561. }
  562. /**
  563. * 获取所有墙体的健康度
  564. */
  565. public getAllWallsHealth(): { main: number; topFence: number; bottomFence: number } {
  566. return {
  567. main: this.wallComponent ? this.wallComponent.getCurrentHealth() : 0,
  568. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentHealth() : 0,
  569. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentHealth() : 0
  570. };
  571. }
  572. /**
  573. * 升级墙体等级(升级主墙体)
  574. */
  575. public upgradeWallLevel(): { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null {
  576. if (this.wallComponent) {
  577. return this.wallComponent.upgradeWallLevel();
  578. }
  579. return null;
  580. }
  581. /**
  582. * 升级所有墙体等级
  583. */
  584. public upgradeAllWallsLevel(): {
  585. main: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  586. topFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  587. bottomFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  588. } {
  589. return {
  590. main: this.wallComponent ? this.wallComponent.upgradeWallLevel() : null,
  591. topFence: this.topFenceComponent ? this.topFenceComponent.upgradeWallLevel() : null,
  592. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.upgradeWallLevel() : null
  593. };
  594. }
  595. /**
  596. * 根据等级获取墙体健康度
  597. */
  598. public getWallHealthByLevel(level: number): number {
  599. if (this.wallComponent) {
  600. return this.wallComponent.getWallHealthByLevel(level);
  601. }
  602. return 100;
  603. }
  604. /**
  605. * 获取当前墙体等级(返回主墙体等级)
  606. */
  607. public getCurrentWallLevel(): number {
  608. if (this.wallComponent) {
  609. return this.wallComponent.getCurrentWallLevel();
  610. }
  611. return 1;
  612. }
  613. /**
  614. * 获取当前墙体健康度(返回主墙体健康度)
  615. */
  616. public getCurrentWallHealth(): number {
  617. if (this.wallComponent) {
  618. return this.wallComponent.getCurrentHealth();
  619. }
  620. return 100;
  621. }
  622. /**
  623. * 获取所有墙体的等级
  624. */
  625. public getAllWallsLevel(): { main: number; topFence: number; bottomFence: number } {
  626. return {
  627. main: this.wallComponent ? this.wallComponent.getCurrentWallLevel() : 1,
  628. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentWallLevel() : 1,
  629. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentWallLevel() : 1
  630. };
  631. }
  632. /**
  633. * 处理确认操作(方块选择确认)
  634. */
  635. public handleConfirmAction() {
  636. console.log('[InGameManager] 处理方块选择确认操作');
  637. // 发送游戏开始事件,确保GamePause正确设置状态
  638. EventBus.getInstance().emit(GameEvents.GAME_START);
  639. console.log('[InGameManager] 发送GAME_START事件');
  640. // 通过事件系统启动球的移动
  641. EventBus.getInstance().emit(GameEvents.BALL_START);
  642. console.log('[InGameManager] 发送BALL_START事件,球已启动');
  643. // 通过事件系统开始当前波次的敌人生成(不更新波次)
  644. EventBus.getInstance().emit(GameEvents.ENEMY_START_GAME);
  645. EventBus.getInstance().emit(GameEvents.ENEMY_SHOW_START_WAVE_PROMPT);
  646. console.log('[InGameManager] 当前波次敌人生成已启动');
  647. }
  648. /**
  649. * 重置能量值(供ReStartGame调用)
  650. */
  651. public resetEnergyValue() {
  652. this.energyPoints = 0;
  653. this.updateEnergyBar();
  654. console.log('[InGameManager] 能量值重置完成');
  655. }
  656. /**
  657. * 重置波次信息(供ReStartGame调用)
  658. */
  659. public resetWaveInfo() {
  660. this.currentWave = 1;
  661. this.currentWaveEnemyCount = 0;
  662. this.currentWaveTotalEnemies = 0;
  663. this.enemiesKilled = 0;
  664. this.totalEnemiesSpawned = 0;
  665. this.levelTotalEnemies = 0;
  666. console.log('[InGameManager] 波次信息重置完成');
  667. }
  668. /**
  669. * 重置能量系统(供ReStartGame调用)
  670. */
  671. public resetEnergySystem() {
  672. this.energyPoints = 0;
  673. this.energyMax = 5;
  674. this.updateEnergyBar();
  675. console.log('[InGameManager] 能量系统重置完成');
  676. }
  677. /**
  678. * 重置游戏状态(供ReStartGame调用)
  679. */
  680. public resetGameStates() {
  681. this.currentState = GameState.PLAYING;
  682. this.gameStarted = false;
  683. this.enemySpawningStarted = false;
  684. this.preparingNextWave = false;
  685. this.pendingSkillSelection = false;
  686. this.pendingBlockSelection = false;
  687. this.shouldShowNextWavePrompt = false;
  688. this.gameStartTime = 0;
  689. this.gameEndTime = 0;
  690. this.checkTimer = 0;
  691. console.log('[InGameManager] 游戏状态重置完成');
  692. }
  693. /**
  694. * 应用关卡配置
  695. */
  696. public applyLevelConfig(levelConfig: any) {
  697. console.log('[InGameManager] 应用关卡配置');
  698. // 应用能量配置
  699. if (levelConfig.levelSettings && levelConfig.levelSettings.energyMax) {
  700. this.energyMax = levelConfig.levelSettings.energyMax;
  701. this.updateEnergyBar();
  702. console.log(`[InGameManager] 应用能量配置: ${this.energyMax}`);
  703. }
  704. // 如果有武器配置,应用武器
  705. if (levelConfig.weapons && Array.isArray(levelConfig.weapons)) {
  706. console.log('[InGameManager] 应用武器配置');
  707. // TODO: 应用武器配置逻辑
  708. }
  709. // 如果有波次配置,设置敌人波次
  710. if (levelConfig.waves && Array.isArray(levelConfig.waves)) {
  711. this.levelWaves = levelConfig.waves;
  712. this.currentWave = 1;
  713. // 计算本关卡总敌人数
  714. this.levelTotalEnemies = 0;
  715. for (const wave of this.levelWaves) {
  716. for (const enemy of wave.enemies || []) {
  717. this.levelTotalEnemies += enemy.count || 0;
  718. }
  719. }
  720. console.log(`[InGameManager] 应用波次配置: ${this.levelWaves.length}波,总敌人数: ${this.levelTotalEnemies}`);
  721. // 通过事件系统通知 EnemyController 初始化第一波数据及 UI
  722. const firstWaveEnemies = this.levelWaves.length > 0 && this.levelWaves[0].enemies ?
  723. this.levelWaves[0].enemies.reduce((t: number, g: any) => t + (g.count || 0), 0) : 0;
  724. // 通过事件系统调用setCurrentWave
  725. this.setCurrentWave(1, firstWaveEnemies);
  726. }
  727. }
  728. onDestroy() {
  729. const eventBus = EventBus.getInstance();
  730. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
  731. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
  732. eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
  733. eventBus.off('ENEMY_KILLED', this.onEnemyKilledEvent, this);
  734. }
  735. }