IN_game.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. // 能量条UI节点
  104. @property({
  105. type: Node,
  106. tooltip: '拖拽能量条节点到这里 (Canvas/GameLevelUI/EnergyBar)'
  107. })
  108. public energyBarNode: Node = null;
  109. // 技能选择UI节点
  110. @property({
  111. type: Node,
  112. tooltip: '拖拽技能选择UI节点到这里 (Canvas/GameLevelUI/SelectSkillUI)'
  113. })
  114. public selectSkillUINode: Node = null;
  115. // GameBlockSelection组件
  116. private blockSelectionComponent: GameBlockSelection = null;
  117. private pendingSkillSelection: boolean = false;
  118. private pendingBlockSelection: boolean = false;
  119. private shouldShowNextWavePrompt: boolean = false;
  120. // 墙体组件引用
  121. private wallComponent: Wall = null;
  122. private topFenceComponent: Wall = null;
  123. private bottomFenceComponent: Wall = null;
  124. // GameStartMove组件引用
  125. private gameStartMoveComponent: GameStartMove = null;
  126. start() {
  127. this.initializeGameState();
  128. this.setupEventListeners();
  129. this.initGameBlockSelection();
  130. this.initGameStartMove();
  131. this.initWallComponent();
  132. this.initUINodes();
  133. }
  134. update(deltaTime: number) {
  135. if (this.currentState !== GameState.PLAYING) {
  136. return;
  137. }
  138. this.checkTimer += deltaTime;
  139. if (this.checkTimer >= this.checkInterval) {
  140. this.checkTimer = 0;
  141. this.checkGameState();
  142. }
  143. }
  144. /**
  145. * 初始化游戏状态
  146. */
  147. private initializeGameState() {
  148. this.currentState = GameState.PLAYING;
  149. this.checkTimer = 0;
  150. this.enemySpawningStarted = false;
  151. this.totalEnemiesSpawned = 0;
  152. this.currentWave = 1;
  153. this.currentWaveEnemyCount = 0;
  154. this.currentWaveTotalEnemies = 0;
  155. this.energyPoints = 0;
  156. this.pendingSkillSelection = false;
  157. }
  158. /**
  159. * 设置事件监听器
  160. */
  161. private setupEventListeners() {
  162. const eventBus = EventBus.getInstance();
  163. eventBus.on(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
  164. eventBus.on(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
  165. eventBus.on(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
  166. eventBus.on('ENEMY_KILLED', this.onEnemyKilledEvent, this);
  167. eventBus.on(GameEvents.RESET_ENERGY_SYSTEM, this.resetEnergySystem, this);
  168. }
  169. /**
  170. * 初始化GameBlockSelection组件
  171. */
  172. private initGameBlockSelection() {
  173. if (this.gameBlockSelection) {
  174. this.blockSelectionComponent = this.gameBlockSelection.getComponent(GameBlockSelection);
  175. }
  176. }
  177. /**
  178. * 初始化GameStartMove组件
  179. */
  180. private initGameStartMove() {
  181. if (this.cameraNode) {
  182. this.gameStartMoveComponent = this.cameraNode.getComponent(GameStartMove);
  183. }
  184. }
  185. /**
  186. * EnemyController现在通过事件系统通信,不再需要直接初始化
  187. */
  188. /**
  189. * 初始化墙体组件
  190. */
  191. private initWallComponent() {
  192. // 初始化主墙体
  193. if (this.wallNode) {
  194. this.wallComponent = this.wallNode.getComponent(Wall);
  195. if (this.wallComponent) {
  196. console.log('[InGameManager] 主墙体组件初始化成功');
  197. } else {
  198. console.warn('[InGameManager] 未找到主墙体Wall组件');
  199. }
  200. } else {
  201. console.warn('[InGameManager] 主墙体节点未通过装饰器挂载');
  202. }
  203. // 初始化上围栏
  204. if (this.topFenceNode) {
  205. this.topFenceComponent = this.topFenceNode.getComponent(Wall);
  206. if (this.topFenceComponent) {
  207. console.log('[InGameManager] 上围栏墙体组件初始化成功');
  208. } else {
  209. console.warn('[InGameManager] 未找到上围栏Wall组件');
  210. }
  211. } else {
  212. console.warn('[InGameManager] 上围栏节点未通过装饰器挂载');
  213. }
  214. // 初始化下围栏
  215. if (this.bottomFenceNode) {
  216. this.bottomFenceComponent = this.bottomFenceNode.getComponent(Wall);
  217. if (this.bottomFenceComponent) {
  218. console.log('[InGameManager] 下围栏墙体组件初始化成功');
  219. } else {
  220. console.warn('[InGameManager] 未找到下围栏Wall组件');
  221. }
  222. } else {
  223. console.warn('[InGameManager] 下围栏节点未通过装饰器挂载');
  224. }
  225. }
  226. /**
  227. * 获取指定波次的敌人配置
  228. */
  229. private getWaveEnemyConfigs(wave: number): any[] {
  230. if (!this.levelWaves || wave < 1 || wave > this.levelWaves.length) {
  231. return [];
  232. }
  233. const waveConfig = this.levelWaves[wave - 1];
  234. return waveConfig?.enemies || [];
  235. }
  236. /**
  237. * 检查是否应该显示方块选择UI
  238. */
  239. public shouldShowBlockSelection(): boolean {
  240. return this.preparingNextWave && this.currentState === GameState.BLOCK_SELECTION;
  241. }
  242. /**
  243. * 在技能选择后显示方块选择UI
  244. */
  245. public showBlockSelectionAfterSkill() {
  246. if (this.pendingBlockSelection) {
  247. console.log('[InGameManager] 技能选择完成,现在显示方块选择UI');
  248. this.pendingBlockSelection = false;
  249. if (this.currentWave < (this.levelWaves?.length || 1)) {
  250. this.showBlockSelectionForNextWave();
  251. } else {
  252. console.log('[InGameManager] 最后一波结束,触发游戏胜利');
  253. this.triggerGameSuccess();
  254. }
  255. } else if (this.shouldShowBlockSelection()) {
  256. this.showBlockSelectionForNextWave();
  257. }
  258. }
  259. /**
  260. * 显示下一波方块选择UI
  261. */
  262. private showBlockSelectionForNextWave() {
  263. // 如果游戏已经结束,不显示方块选择UI
  264. if (this.isGameOver()) {
  265. console.warn('[InGameManager] 游戏已经结束(胜利或失败),不显示下一波方块选择UI!');
  266. return;
  267. }
  268. console.log('[InGameManager] 显示方块选择UI,准备播放进入动画');
  269. if (this.blockSelectionComponent) {
  270. this.blockSelectionComponent.showBlockSelection(true);
  271. // 通过事件系统暂停游戏
  272. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  273. }
  274. // 注意:不在这里直接调用动画,showBlockSelection()内部的playShowAnimation()会处理动画
  275. // 避免重复调用enterBlockSelectionMode导致摄像头偏移两倍
  276. }
  277. /**
  278. * 处理游戏成功事件
  279. */
  280. private onGameSuccessEvent() {
  281. console.log('[InGameManager] 接收到游戏成功事件');
  282. this.currentState = GameState.SUCCESS;
  283. }
  284. /**
  285. * 处理游戏失败事件
  286. */
  287. private onGameDefeatEvent() {
  288. console.log('[InGameManager] 接收到游戏失败事件');
  289. this.currentState = GameState.DEFEAT;
  290. }
  291. /**
  292. * 处理游戏恢复事件
  293. */
  294. private onGameResumeEvent() {
  295. console.log('[InGameManager] 接收到游戏恢复事件');
  296. }
  297. /**
  298. * 处理敌人被击杀事件
  299. */
  300. private onEnemyKilledEvent() {
  301. // 游戏状态检查现在通过事件系统处理
  302. if (this.isGameOver()) {
  303. console.warn('[InGameManager] 游戏已结束状态下onEnemyKilledEvent被调用!');
  304. return;
  305. }
  306. this.enemiesKilled++;
  307. this.currentWaveEnemyCount++;
  308. const remaining = this.currentWaveTotalEnemies - this.currentWaveEnemyCount;
  309. console.log(`[InGameManager] 敌人被消灭,当前波剩余敌人: ${remaining}/${this.currentWaveTotalEnemies}`);
  310. // 每死一个敌人就立即增加能量
  311. const energyBeforeIncrement = this.energyPoints;
  312. this.incrementEnergy();
  313. // 通过事件系统更新敌人计数标签
  314. EventBus.getInstance().emit(GameEvents.ENEMY_UPDATE_COUNT, this.currentWaveEnemyCount);
  315. // 检查能量是否已满,如果满了需要触发技能选择
  316. const energyWillBeFull = energyBeforeIncrement + 1 >= this.energyMax;
  317. // 通过事件系统检查是否有活跃敌人
  318. let hasActiveEnemies = false;
  319. EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_ACTIVE, (active: boolean) => {
  320. hasActiveEnemies = active;
  321. // 在回调中检查波次是否结束
  322. const isWaveEnd = remaining <= 0 && !hasActiveEnemies;
  323. if (isWaveEnd) {
  324. console.log(`[InGameManager] 波次结束检测: remaining=${remaining}, hasActiveEnemies=${hasActiveEnemies}`);
  325. // 如果能量已满,设置等待方块选择状态
  326. if (energyWillBeFull) {
  327. this.pendingBlockSelection = true;
  328. this.preparingNextWave = true;
  329. this.currentState = GameState.BLOCK_SELECTION;
  330. } else {
  331. if (this.currentWave < (this.levelWaves?.length || 1)) {
  332. this.showNextWavePrompt();
  333. } else {
  334. this.triggerGameSuccess();
  335. }
  336. }
  337. } else if (energyWillBeFull) {
  338. // 如果波次未结束但能量已满,也需要触发技能选择
  339. console.log('[InGameManager] 能量已满但波次未结束,触发技能选择');
  340. this.currentState = GameState.BLOCK_SELECTION;
  341. }
  342. });
  343. }
  344. /**
  345. * 增加能量值
  346. */
  347. private incrementEnergy() {
  348. // 获取技能等级
  349. const energyHunterLevel = SkillManager.getInstance() ? SkillManager.getInstance().getSkillLevel('energy_hunter') : 0;
  350. const baseEnergy = 1;
  351. const bonusEnergy = SkillManager.calculateEnergyBonus ? SkillManager.calculateEnergyBonus(baseEnergy, energyHunterLevel) : baseEnergy;
  352. this.energyPoints = Math.min(this.energyPoints + bonusEnergy, this.energyMax);
  353. this.updateEnergyBar();
  354. console.log(`[InGameManager] 能量值增加: +${bonusEnergy}, 当前: ${this.energyPoints}/${this.energyMax}`);
  355. if (this.energyPoints >= this.energyMax) {
  356. this.onEnergyFull();
  357. }
  358. }
  359. /**
  360. * 显示下一波提示
  361. */
  362. private showNextWavePrompt() {
  363. // 设置准备下一波的状态
  364. this.preparingNextWave = true;
  365. this.pendingBlockSelection = true;
  366. this.currentState = GameState.BLOCK_SELECTION;
  367. console.log('[InGameManager] 设置准备下一波状态,准备显示方块选择UI');
  368. // 如果当前没有技能选择UI显示,立即显示方块选择UI
  369. if (!this.selectSkillUI || !this.selectSkillUI.active) {
  370. this.showBlockSelectionForNextWave();
  371. }
  372. // 如果技能选择UI正在显示,则等待技能选择完成后再显示方块选择UI
  373. // 这种情况下,pendingBlockSelection已经在onEnemyKilledEvent中设置为true
  374. }
  375. /**
  376. * 游戏状态检查
  377. */
  378. private checkGameState() {
  379. // 检查所有墙体是否存活
  380. const isAnyWallDestroyed =
  381. (this.wallComponent && !this.wallComponent.isAlive()) ||
  382. (this.topFenceComponent && !this.topFenceComponent.isAlive()) ||
  383. (this.bottomFenceComponent && !this.bottomFenceComponent.isAlive());
  384. if (isAnyWallDestroyed) {
  385. this.triggerGameDefeat();
  386. return;
  387. }
  388. if (this.checkAllEnemiesDefeated()) {
  389. this.triggerGameSuccess();
  390. return;
  391. }
  392. }
  393. /**
  394. * 检查所有敌人是否被击败
  395. */
  396. private checkAllEnemiesDefeated(): boolean {
  397. // 通过事件系统检查游戏是否开始
  398. let gameStarted = false;
  399. EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_GAME_STARTED, (started: boolean) => {
  400. gameStarted = started;
  401. });
  402. if (!this.enemySpawningStarted) {
  403. if (gameStarted) {
  404. this.enemySpawningStarted = true;
  405. } else {
  406. return false;
  407. }
  408. }
  409. // 通过事件系统获取当前敌人数量
  410. let currentEnemyCount = 0;
  411. EventBus.getInstance().emit(GameEvents.ENEMY_GET_COUNT, (count: number) => {
  412. currentEnemyCount = count;
  413. });
  414. if (this.levelTotalEnemies > 0) {
  415. return this.enemiesKilled >= this.levelTotalEnemies && currentEnemyCount === 0;
  416. }
  417. if (currentEnemyCount > this.totalEnemiesSpawned) {
  418. this.totalEnemiesSpawned = currentEnemyCount;
  419. }
  420. const shouldCheckVictory = this.enemySpawningStarted &&
  421. currentEnemyCount === 0 &&
  422. this.totalEnemiesSpawned > 0;
  423. return shouldCheckVictory;
  424. }
  425. /**
  426. * 触发游戏失败
  427. */
  428. private triggerGameDefeat() {
  429. EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
  430. }
  431. /**
  432. * 触发游戏成功
  433. */
  434. private triggerGameSuccess() {
  435. EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
  436. }
  437. /**
  438. * 获取游戏持续时间
  439. */
  440. public getGameDuration(): number {
  441. const endTime = this.gameEndTime || Date.now();
  442. return Math.max(0, endTime - this.gameStartTime);
  443. }
  444. /**
  445. * 获取当前游戏状态
  446. */
  447. public getCurrentState(): GameState {
  448. return this.currentState;
  449. }
  450. /**
  451. * 设置游戏状态
  452. */
  453. public setCurrentState(state: GameState) {
  454. this.currentState = state;
  455. }
  456. /**
  457. * 初始化UI节点
  458. */
  459. private initUINodes() {
  460. // 初始化能量条
  461. if (this.energyBarNode) {
  462. this.energyBar = this.energyBarNode.getComponent(ProgressBar);
  463. if (this.energyBar) {
  464. console.log('[InGameManager] 能量条组件初始化成功');
  465. // 初始化能量条显示
  466. this.updateEnergyBar();
  467. } else {
  468. console.error('[InGameManager] 能量条节点存在但ProgressBar组件未找到');
  469. }
  470. } else {
  471. console.error('[InGameManager] 能量条节点未通过装饰器挂载,请在Inspector中拖拽EnergyBar节点');
  472. }
  473. // 初始化技能选择UI
  474. if (this.selectSkillUINode) {
  475. this.selectSkillUI = this.selectSkillUINode;
  476. console.log('[InGameManager] 技能选择UI节点初始化成功');
  477. } else {
  478. console.error('[InGameManager] 技能选择UI节点未通过装饰器挂载,请在Inspector中拖拽SelectSkillUI节点');
  479. }
  480. }
  481. /**
  482. * 更新能量条显示
  483. */
  484. private updateEnergyBar() {
  485. if (this.energyBar) {
  486. const progress = this.energyPoints / this.energyMax;
  487. this.energyBar.progress = progress;
  488. console.log(`[InGameManager] 能量条更新: ${this.energyPoints}/${this.energyMax} (${Math.round(progress * 100)}%)`);
  489. } else {
  490. console.warn('[InGameManager] 能量条组件未初始化,无法更新显示');
  491. }
  492. }
  493. /**
  494. * 能量满时的处理
  495. */
  496. private onEnergyFull() {
  497. console.log('[InGameManager] 能量已满,显示技能选择UI');
  498. // 直接显示技能选择UI
  499. this.showSkillSelection();
  500. }
  501. /**
  502. * 显示技能选择UI
  503. */
  504. private showSkillSelection() {
  505. if (this.selectSkillUI) {
  506. this.selectSkillUI.active = true;
  507. this.pauseGame();
  508. // 重置能量
  509. ReStartGame.resetEnergy();
  510. }
  511. }
  512. /**
  513. * 暂停游戏
  514. */
  515. private pauseGame() {
  516. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  517. }
  518. /**
  519. * 检查游戏是否结束
  520. */
  521. private isGameOver(): boolean {
  522. let gameOver = false;
  523. EventBus.getInstance().emit(GameEvents.GAME_CHECK_OVER, (isOver: boolean) => {
  524. gameOver = isOver;
  525. });
  526. return gameOver;
  527. }
  528. /**
  529. * 设置当前波次
  530. */
  531. public setCurrentWave(wave: number, enemyCount: number = 0) {
  532. this.currentWave = wave;
  533. this.currentWaveEnemyCount = 0; // 重置当前击杀数
  534. this.currentWaveTotalEnemies = enemyCount; // 设置该波次总敌人数
  535. const totalWaves = this.levelWaves?.length || 1;
  536. // 获取波次敌人配置
  537. const waveEnemyConfigs = this.getWaveEnemyConfigs(wave);
  538. // 通过事件系统启动波次
  539. EventBus.getInstance().emit(GameEvents.ENEMY_START_WAVE, {
  540. wave: wave,
  541. totalWaves: totalWaves,
  542. enemyCount: enemyCount,
  543. waveEnemyConfigs: waveEnemyConfigs
  544. });
  545. }
  546. /**
  547. * 更新当前波次敌人数量
  548. */
  549. public updateCurrentWaveEnemyCount(count: number) {
  550. this.currentWaveEnemyCount = count;
  551. }
  552. /**
  553. * 获取当前波次
  554. */
  555. public getCurrentWave(): number {
  556. return this.currentWave;
  557. }
  558. /**
  559. * 获取当前波次敌人数量
  560. */
  561. public getCurrentWaveEnemyCount(): number {
  562. return this.currentWaveEnemyCount;
  563. }
  564. /**
  565. * 获取当前波次总敌人数量
  566. */
  567. public getCurrentWaveTotalEnemies(): number {
  568. return this.currentWaveTotalEnemies;
  569. }
  570. /**
  571. * 进入下一波
  572. */
  573. public nextWave() {
  574. this.currentWave++;
  575. // 根据关卡配置获取下一波敌人数
  576. let enemyTotal = 0;
  577. if (this.levelWaves && this.levelWaves.length >= this.currentWave) {
  578. const waveCfg = this.levelWaves[this.currentWave - 1];
  579. if (waveCfg && waveCfg.enemies) {
  580. enemyTotal = waveCfg.enemies.reduce((t: number, g: any) => t + (g.count || 0), 0);
  581. }
  582. }
  583. this.setCurrentWave(this.currentWave, enemyTotal);
  584. }
  585. /**
  586. * 获取当前能量值
  587. */
  588. public getCurrentEnergy(): number {
  589. return this.energyPoints;
  590. }
  591. /**
  592. * 获取最大能量值
  593. */
  594. public getMaxEnergy(): number {
  595. return this.energyMax;
  596. }
  597. /**
  598. * 获取墙体健康度(返回主墙体健康度)
  599. */
  600. public getWallHealth(): number {
  601. if (this.wallComponent) {
  602. return this.wallComponent.getCurrentHealth();
  603. }
  604. return 0;
  605. }
  606. /**
  607. * 获取所有墙体的健康度
  608. */
  609. public getAllWallsHealth(): { main: number; topFence: number; bottomFence: number } {
  610. return {
  611. main: this.wallComponent ? this.wallComponent.getCurrentHealth() : 0,
  612. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentHealth() : 0,
  613. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentHealth() : 0
  614. };
  615. }
  616. /**
  617. * 升级墙体等级(升级主墙体)
  618. */
  619. public upgradeWallLevel(): { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null {
  620. if (this.wallComponent) {
  621. return this.wallComponent.upgradeWallLevel();
  622. }
  623. return null;
  624. }
  625. /**
  626. * 升级所有墙体等级
  627. */
  628. public upgradeAllWallsLevel(): {
  629. main: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  630. topFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  631. bottomFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  632. } {
  633. return {
  634. main: this.wallComponent ? this.wallComponent.upgradeWallLevel() : null,
  635. topFence: this.topFenceComponent ? this.topFenceComponent.upgradeWallLevel() : null,
  636. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.upgradeWallLevel() : null
  637. };
  638. }
  639. /**
  640. * 根据等级获取墙体健康度
  641. */
  642. public getWallHealthByLevel(level: number): number {
  643. if (this.wallComponent) {
  644. return this.wallComponent.getWallHealthByLevel(level);
  645. }
  646. return 100;
  647. }
  648. /**
  649. * 获取当前墙体等级(返回主墙体等级)
  650. */
  651. public getCurrentWallLevel(): number {
  652. if (this.wallComponent) {
  653. return this.wallComponent.getCurrentWallLevel();
  654. }
  655. return 1;
  656. }
  657. /**
  658. * 获取当前墙体健康度(返回主墙体健康度)
  659. */
  660. public getCurrentWallHealth(): number {
  661. if (this.wallComponent) {
  662. return this.wallComponent.getCurrentHealth();
  663. }
  664. return 100;
  665. }
  666. /**
  667. * 获取所有墙体的等级
  668. */
  669. public getAllWallsLevel(): { main: number; topFence: number; bottomFence: number } {
  670. return {
  671. main: this.wallComponent ? this.wallComponent.getCurrentWallLevel() : 1,
  672. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentWallLevel() : 1,
  673. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentWallLevel() : 1
  674. };
  675. }
  676. /**
  677. * 处理确认操作(方块选择确认)
  678. */
  679. public handleConfirmAction() {
  680. console.log('[InGameManager] 处理方块选择确认操作');
  681. // 如果是在准备下一波的状态,需要先切换到下一波
  682. if (this.preparingNextWave) {
  683. console.log('[InGameManager] 检测到准备下一波状态,切换到下一波');
  684. this.nextWave();
  685. // 重置状态
  686. this.preparingNextWave = false;
  687. this.pendingBlockSelection = false;
  688. this.currentState = GameState.PLAYING;
  689. }
  690. // 发送游戏开始事件,确保GamePause正确设置状态
  691. EventBus.getInstance().emit(GameEvents.GAME_START);
  692. console.log('[InGameManager] 发送GAME_START事件');
  693. // 通过事件系统启动球的移动
  694. EventBus.getInstance().emit(GameEvents.BALL_START);
  695. console.log('[InGameManager] 发送BALL_START事件,球已启动');
  696. // 通过事件系统开始当前波次的敌人生成
  697. EventBus.getInstance().emit(GameEvents.ENEMY_START_GAME);
  698. EventBus.getInstance().emit(GameEvents.ENEMY_SHOW_START_WAVE_PROMPT);
  699. console.log(`[InGameManager] 波次 ${this.currentWave} 敌人生成已启动`);
  700. }
  701. /**
  702. * 重置能量值(供ReStartGame调用)
  703. */
  704. public resetEnergyValue() {
  705. this.energyPoints = 0;
  706. this.updateEnergyBar();
  707. console.log('[InGameManager] 能量值重置完成');
  708. }
  709. /**
  710. * 重置波次信息(供ReStartGame调用)
  711. */
  712. public resetWaveInfo() {
  713. this.currentWave = 1;
  714. this.currentWaveEnemyCount = 0;
  715. this.currentWaveTotalEnemies = 0;
  716. this.enemiesKilled = 0;
  717. this.totalEnemiesSpawned = 0;
  718. this.levelTotalEnemies = 0;
  719. console.log('[InGameManager] 波次信息重置完成');
  720. }
  721. /**
  722. * 重置能量系统(供ReStartGame调用)
  723. */
  724. public resetEnergySystem() {
  725. this.energyPoints = 0;
  726. this.energyMax = 5;
  727. this.updateEnergyBar();
  728. console.log('[InGameManager] 能量系统重置完成');
  729. }
  730. /**
  731. * 清理游戏数据,为返回主页面做准备
  732. * 当游戏胜利或失败返回主页面时调用
  733. */
  734. private cleanupGameDataForMainMenu() {
  735. console.log('[InGameManager] 开始清理游戏数据,准备返回主页面');
  736. const eventBus = EventBus.getInstance();
  737. // 1. 清空场上的所有游戏对象
  738. console.log('[InGameManager] 清空场上敌人、方块、球');
  739. eventBus.emit(GameEvents.CLEAR_ALL_ENEMIES); // 清空所有敌人
  740. eventBus.emit(GameEvents.CLEAR_ALL_BULLETS); // 清空所有子弹
  741. eventBus.emit(GameEvents.CLEAR_ALL_GAME_OBJECTS); // 清空其他游戏对象
  742. // 2. 重置能量系统
  743. console.log('[InGameManager] 重置能量系统');
  744. this.energyPoints = 0;
  745. this.updateEnergyBar();
  746. // 3. 清空技能选择数据(重置临时技能状态)
  747. console.log('[InGameManager] 清空技能选择数据');
  748. if (SkillManager.getInstance()) {
  749. // 重置技能管理器中的临时技能状态
  750. const skillManager = SkillManager.getInstance();
  751. const allSkills = skillManager.getAllSkillsData();
  752. allSkills.forEach(skill => {
  753. skillManager.setSkillLevel(skill.id, 0); // 重置所有技能等级为0
  754. });
  755. }
  756. // 4. 重置关卡数据和游戏状态
  757. console.log('[InGameManager] 重置关卡数据和游戏状态');
  758. this.currentWave = 1;
  759. this.currentWaveEnemyCount = 0;
  760. this.currentWaveTotalEnemies = 0;
  761. this.enemiesKilled = 0;
  762. this.totalEnemiesSpawned = 0;
  763. this.levelTotalEnemies = 0;
  764. this.levelWaves = [];
  765. this.gameStarted = false;
  766. this.enemySpawningStarted = false;
  767. this.preparingNextWave = false;
  768. this.pendingSkillSelection = false;
  769. this.pendingBlockSelection = false;
  770. this.shouldShowNextWavePrompt = false;
  771. // 5. 重置UI状态
  772. console.log('[InGameManager] 重置UI状态');
  773. if (this.selectSkillUI) {
  774. this.selectSkillUI.active = false;
  775. }
  776. // 6. 清理会话数据(金币等临时数据)
  777. console.log('[InGameManager] 清理会话数据');
  778. if (LevelSessionManager.inst) {
  779. LevelSessionManager.inst.clear(); // 清空局内金币等临时数据
  780. }
  781. // 7. 通过事件系统通知其他组件进行清理
  782. eventBus.emit(GameEvents.RESET_BALL_CONTROLLER); // 重置球控制器
  783. eventBus.emit(GameEvents.RESET_BLOCK_MANAGER); // 重置方块管理器
  784. eventBus.emit(GameEvents.RESET_BLOCK_SELECTION); // 重置方块选择
  785. eventBus.emit(GameEvents.RESET_ENEMY_CONTROLLER); // 重置敌人控制器
  786. eventBus.emit(GameEvents.RESET_WALL_HEALTH); // 重置墙体血量
  787. eventBus.emit(GameEvents.RESET_UI_STATES); // 重置UI状态
  788. console.log('[InGameManager] 游戏数据清理完成,可以安全返回主页面');
  789. }
  790. /**
  791. * 重置游戏状态(供ReStartGame调用)
  792. */
  793. public resetGameStates() {
  794. this.currentState = GameState.PLAYING;
  795. this.gameStarted = false;
  796. this.enemySpawningStarted = false;
  797. this.preparingNextWave = false;
  798. this.pendingSkillSelection = false;
  799. this.pendingBlockSelection = false;
  800. this.shouldShowNextWavePrompt = false;
  801. this.gameStartTime = 0;
  802. this.gameEndTime = 0;
  803. this.checkTimer = 0;
  804. console.log('[InGameManager] 游戏状态重置完成');
  805. }
  806. /**
  807. * 手动触发游戏数据清理(供外部调用)
  808. * 可以在返回主菜单按钮点击时调用
  809. */
  810. public triggerGameDataCleanup() {
  811. this.cleanupGameDataForMainMenu();
  812. }
  813. /**
  814. * 应用关卡配置
  815. */
  816. public applyLevelConfig(levelConfig: any) {
  817. console.log('[InGameManager] 应用关卡配置');
  818. // 应用能量配置
  819. if (levelConfig.levelSettings && levelConfig.levelSettings.energyMax) {
  820. this.energyMax = levelConfig.levelSettings.energyMax;
  821. this.updateEnergyBar();
  822. console.log(`[InGameManager] 应用能量配置: ${this.energyMax}`);
  823. }
  824. // 如果有武器配置,应用武器
  825. if (levelConfig.weapons && Array.isArray(levelConfig.weapons)) {
  826. console.log('[InGameManager] 应用武器配置');
  827. // TODO: 应用武器配置逻辑
  828. }
  829. // 如果有波次配置,设置敌人波次
  830. if (levelConfig.waves && Array.isArray(levelConfig.waves)) {
  831. this.levelWaves = levelConfig.waves;
  832. this.currentWave = 1;
  833. // 计算本关卡总敌人数
  834. this.levelTotalEnemies = 0;
  835. for (const wave of this.levelWaves) {
  836. for (const enemy of wave.enemies || []) {
  837. this.levelTotalEnemies += enemy.count || 0;
  838. }
  839. }
  840. console.log(`[InGameManager] 应用波次配置: ${this.levelWaves.length}波,总敌人数: ${this.levelTotalEnemies}`);
  841. // 通过事件系统通知 EnemyController 初始化第一波数据及 UI
  842. const firstWaveEnemies = this.levelWaves.length > 0 && this.levelWaves[0].enemies ?
  843. this.levelWaves[0].enemies.reduce((t: number, g: any) => t + (g.count || 0), 0) : 0;
  844. // 通过事件系统调用setCurrentWave
  845. this.setCurrentWave(1, firstWaveEnemies);
  846. }
  847. }
  848. onDestroy() {
  849. const eventBus = EventBus.getInstance();
  850. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
  851. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
  852. eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
  853. eventBus.off('ENEMY_KILLED', this.onEnemyKilledEvent, this);
  854. }
  855. }