IN_game.ts 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. // 如果游戏状态已经是成功或失败,不处理敌人击杀事件
  307. if (this.currentState === GameState.SUCCESS || this.currentState === GameState.DEFEAT) {
  308. console.warn(`[InGameManager] 游戏已结束(${this.currentState}),跳过敌人击杀事件处理`);
  309. return;
  310. }
  311. this.enemiesKilled++;
  312. this.currentWaveEnemyCount++;
  313. const remaining = this.currentWaveTotalEnemies - this.currentWaveEnemyCount;
  314. console.log(`[InGameManager] 敌人被消灭,当前波剩余敌人: ${remaining}/${this.currentWaveTotalEnemies}`);
  315. // 每死一个敌人就立即增加能量
  316. const energyBeforeIncrement = this.energyPoints;
  317. this.incrementEnergy();
  318. // 通过事件系统更新敌人计数标签
  319. EventBus.getInstance().emit(GameEvents.ENEMY_UPDATE_COUNT, this.currentWaveEnemyCount);
  320. // 检查能量是否已满,如果满了需要触发技能选择
  321. const energyWillBeFull = energyBeforeIncrement + 1 >= this.energyMax;
  322. // 通过事件系统检查是否有活跃敌人
  323. let hasActiveEnemies = false;
  324. EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_ACTIVE, (active: boolean) => {
  325. hasActiveEnemies = active;
  326. // 在回调中检查波次是否结束
  327. const isWaveEnd = remaining <= 0 && !hasActiveEnemies;
  328. if (isWaveEnd) {
  329. console.log(`[InGameManager] 波次结束检测: remaining=${remaining}, hasActiveEnemies=${hasActiveEnemies}`);
  330. // 如果能量已满,设置等待方块选择状态
  331. if (energyWillBeFull) {
  332. this.pendingBlockSelection = true;
  333. this.preparingNextWave = true;
  334. this.currentState = GameState.BLOCK_SELECTION;
  335. } else {
  336. if (this.currentWave < (this.levelWaves?.length || 1)) {
  337. this.showNextWavePrompt();
  338. } else {
  339. this.triggerGameSuccess();
  340. }
  341. }
  342. } else if (energyWillBeFull) {
  343. // 如果波次未结束但能量已满,也需要触发技能选择
  344. console.log('[InGameManager] 能量已满但波次未结束,触发技能选择');
  345. this.currentState = GameState.BLOCK_SELECTION;
  346. }
  347. });
  348. }
  349. /**
  350. * 增加能量值
  351. */
  352. private incrementEnergy() {
  353. // 获取技能等级
  354. const energyHunterLevel = SkillManager.getInstance() ? SkillManager.getInstance().getSkillLevel('energy_hunter') : 0;
  355. const baseEnergy = 1;
  356. const bonusEnergy = SkillManager.calculateEnergyBonus ? SkillManager.calculateEnergyBonus(baseEnergy, energyHunterLevel) : baseEnergy;
  357. this.energyPoints = Math.min(this.energyPoints + bonusEnergy, this.energyMax);
  358. this.updateEnergyBar();
  359. console.log(`[InGameManager] 能量值增加: +${bonusEnergy}, 当前: ${this.energyPoints}/${this.energyMax}`);
  360. if (this.energyPoints >= this.energyMax) {
  361. this.onEnergyFull();
  362. }
  363. }
  364. /**
  365. * 显示下一波提示
  366. */
  367. private showNextWavePrompt() {
  368. // 设置准备下一波的状态
  369. this.preparingNextWave = true;
  370. this.pendingBlockSelection = true;
  371. this.currentState = GameState.BLOCK_SELECTION;
  372. console.log('[InGameManager] 设置准备下一波状态,准备显示方块选择UI');
  373. // 如果当前没有技能选择UI显示,立即显示方块选择UI
  374. if (!this.selectSkillUI || !this.selectSkillUI.active) {
  375. this.showBlockSelectionForNextWave();
  376. }
  377. // 如果技能选择UI正在显示,则等待技能选择完成后再显示方块选择UI
  378. // 这种情况下,pendingBlockSelection已经在onEnemyKilledEvent中设置为true
  379. }
  380. /**
  381. * 游戏状态检查
  382. */
  383. private checkGameState() {
  384. // 检查所有墙体是否存活
  385. const isAnyWallDestroyed =
  386. (this.wallComponent && !this.wallComponent.isAlive()) ||
  387. (this.topFenceComponent && !this.topFenceComponent.isAlive()) ||
  388. (this.bottomFenceComponent && !this.bottomFenceComponent.isAlive());
  389. if (isAnyWallDestroyed) {
  390. this.triggerGameDefeat();
  391. return;
  392. }
  393. if (this.checkAllEnemiesDefeated()) {
  394. this.triggerGameSuccess();
  395. return;
  396. }
  397. }
  398. /**
  399. * 检查所有敌人是否被击败
  400. */
  401. private checkAllEnemiesDefeated(): boolean {
  402. // 通过事件系统检查游戏是否开始
  403. let gameStarted = false;
  404. EventBus.getInstance().emit(GameEvents.ENEMY_CHECK_GAME_STARTED, (started: boolean) => {
  405. gameStarted = started;
  406. });
  407. if (!this.enemySpawningStarted) {
  408. if (gameStarted) {
  409. this.enemySpawningStarted = true;
  410. } else {
  411. return false;
  412. }
  413. }
  414. // 通过事件系统获取当前敌人数量
  415. let currentEnemyCount = 0;
  416. EventBus.getInstance().emit(GameEvents.ENEMY_GET_COUNT, (count: number) => {
  417. currentEnemyCount = count;
  418. });
  419. if (this.levelTotalEnemies > 0) {
  420. return this.enemiesKilled >= this.levelTotalEnemies && currentEnemyCount === 0;
  421. }
  422. if (currentEnemyCount > this.totalEnemiesSpawned) {
  423. this.totalEnemiesSpawned = currentEnemyCount;
  424. }
  425. const shouldCheckVictory = this.enemySpawningStarted &&
  426. currentEnemyCount === 0 &&
  427. this.totalEnemiesSpawned > 0;
  428. return shouldCheckVictory;
  429. }
  430. /**
  431. * 触发游戏失败
  432. */
  433. private triggerGameDefeat() {
  434. // 立即设置游戏状态为失败,防止后续敌人击杀事件被处理
  435. this.currentState = GameState.DEFEAT;
  436. console.log('[InGameManager] 设置游戏状态为失败,发送GAME_DEFEAT事件');
  437. EventBus.getInstance().emit(GameEvents.GAME_DEFEAT);
  438. }
  439. /**
  440. * 触发游戏成功
  441. */
  442. private triggerGameSuccess() {
  443. // 立即设置游戏状态为成功,防止后续敌人击杀事件被处理
  444. this.currentState = GameState.SUCCESS;
  445. console.log('[InGameManager] 设置游戏状态为成功,发送GAME_SUCCESS事件');
  446. EventBus.getInstance().emit(GameEvents.GAME_SUCCESS);
  447. }
  448. /**
  449. * 获取游戏持续时间
  450. */
  451. public getGameDuration(): number {
  452. const endTime = this.gameEndTime || Date.now();
  453. return Math.max(0, endTime - this.gameStartTime);
  454. }
  455. /**
  456. * 获取当前游戏状态
  457. */
  458. public getCurrentState(): GameState {
  459. return this.currentState;
  460. }
  461. /**
  462. * 设置游戏状态
  463. */
  464. public setCurrentState(state: GameState) {
  465. this.currentState = state;
  466. }
  467. /**
  468. * 初始化UI节点
  469. */
  470. private initUINodes() {
  471. // 初始化能量条
  472. if (this.energyBarNode) {
  473. this.energyBar = this.energyBarNode.getComponent(ProgressBar);
  474. if (this.energyBar) {
  475. console.log('[InGameManager] 能量条组件初始化成功');
  476. // 初始化能量条显示
  477. this.updateEnergyBar();
  478. } else {
  479. console.error('[InGameManager] 能量条节点存在但ProgressBar组件未找到');
  480. }
  481. } else {
  482. console.error('[InGameManager] 能量条节点未通过装饰器挂载,请在Inspector中拖拽EnergyBar节点');
  483. }
  484. // 初始化技能选择UI
  485. if (this.selectSkillUINode) {
  486. this.selectSkillUI = this.selectSkillUINode;
  487. console.log('[InGameManager] 技能选择UI节点初始化成功');
  488. } else {
  489. console.error('[InGameManager] 技能选择UI节点未通过装饰器挂载,请在Inspector中拖拽SelectSkillUI节点');
  490. }
  491. }
  492. /**
  493. * 更新能量条显示
  494. */
  495. private updateEnergyBar() {
  496. if (this.energyBar) {
  497. const progress = this.energyPoints / this.energyMax;
  498. this.energyBar.progress = progress;
  499. console.log(`[InGameManager] 能量条更新: ${this.energyPoints}/${this.energyMax} (${Math.round(progress * 100)}%)`);
  500. } else {
  501. console.warn('[InGameManager] 能量条组件未初始化,无法更新显示');
  502. }
  503. }
  504. /**
  505. * 能量满时的处理
  506. */
  507. private onEnergyFull() {
  508. console.log('[InGameManager] 能量已满,显示技能选择UI');
  509. // 直接显示技能选择UI
  510. this.showSkillSelection();
  511. }
  512. /**
  513. * 显示技能选择UI
  514. */
  515. private showSkillSelection() {
  516. if (this.selectSkillUI) {
  517. this.selectSkillUI.active = true;
  518. this.pauseGame();
  519. // 重置能量
  520. ReStartGame.resetEnergy();
  521. }
  522. }
  523. /**
  524. * 暂停游戏
  525. */
  526. private pauseGame() {
  527. EventBus.getInstance().emit(GameEvents.GAME_PAUSE);
  528. }
  529. /**
  530. * 检查游戏是否结束
  531. */
  532. private isGameOver(): boolean {
  533. let gameOver = false;
  534. EventBus.getInstance().emit(GameEvents.GAME_CHECK_OVER, (isOver: boolean) => {
  535. gameOver = isOver;
  536. });
  537. return gameOver;
  538. }
  539. /**
  540. * 设置当前波次
  541. */
  542. public setCurrentWave(wave: number, enemyCount: number = 0) {
  543. this.currentWave = wave;
  544. this.currentWaveEnemyCount = 0; // 重置当前击杀数
  545. this.currentWaveTotalEnemies = enemyCount; // 设置该波次总敌人数
  546. const totalWaves = this.levelWaves?.length || 1;
  547. // 获取波次敌人配置
  548. const waveEnemyConfigs = this.getWaveEnemyConfigs(wave);
  549. // 通过事件系统启动波次
  550. EventBus.getInstance().emit(GameEvents.ENEMY_START_WAVE, {
  551. wave: wave,
  552. totalWaves: totalWaves,
  553. enemyCount: enemyCount,
  554. waveEnemyConfigs: waveEnemyConfigs
  555. });
  556. }
  557. /**
  558. * 更新当前波次敌人数量
  559. */
  560. public updateCurrentWaveEnemyCount(count: number) {
  561. this.currentWaveEnemyCount = count;
  562. }
  563. /**
  564. * 获取当前波次
  565. */
  566. public getCurrentWave(): number {
  567. return this.currentWave;
  568. }
  569. /**
  570. * 获取当前波次敌人数量
  571. */
  572. public getCurrentWaveEnemyCount(): number {
  573. return this.currentWaveEnemyCount;
  574. }
  575. /**
  576. * 获取当前波次总敌人数量
  577. */
  578. public getCurrentWaveTotalEnemies(): number {
  579. return this.currentWaveTotalEnemies;
  580. }
  581. /**
  582. * 进入下一波
  583. */
  584. public nextWave() {
  585. this.currentWave++;
  586. // 根据关卡配置获取下一波敌人数
  587. let enemyTotal = 0;
  588. if (this.levelWaves && this.levelWaves.length >= this.currentWave) {
  589. const waveCfg = this.levelWaves[this.currentWave - 1];
  590. if (waveCfg && waveCfg.enemies) {
  591. enemyTotal = waveCfg.enemies.reduce((t: number, g: any) => t + (g.count || 0), 0);
  592. }
  593. }
  594. this.setCurrentWave(this.currentWave, enemyTotal);
  595. }
  596. /**
  597. * 获取当前能量值
  598. */
  599. public getCurrentEnergy(): number {
  600. return this.energyPoints;
  601. }
  602. /**
  603. * 获取最大能量值
  604. */
  605. public getMaxEnergy(): number {
  606. return this.energyMax;
  607. }
  608. /**
  609. * 获取墙体健康度(返回主墙体健康度)
  610. */
  611. public getWallHealth(): number {
  612. if (this.wallComponent) {
  613. return this.wallComponent.getCurrentHealth();
  614. }
  615. return 0;
  616. }
  617. /**
  618. * 获取所有墙体的健康度
  619. */
  620. public getAllWallsHealth(): { main: number; topFence: number; bottomFence: number } {
  621. return {
  622. main: this.wallComponent ? this.wallComponent.getCurrentHealth() : 0,
  623. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentHealth() : 0,
  624. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentHealth() : 0
  625. };
  626. }
  627. /**
  628. * 升级墙体等级(升级主墙体)
  629. */
  630. public upgradeWallLevel(): { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null {
  631. if (this.wallComponent) {
  632. return this.wallComponent.upgradeWallLevel();
  633. }
  634. return null;
  635. }
  636. /**
  637. * 升级所有墙体等级
  638. */
  639. public upgradeAllWallsLevel(): {
  640. main: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  641. topFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  642. bottomFence: { currentLevel: number; currentHp: number; nextLevel: number; nextHp: number } | null;
  643. } {
  644. return {
  645. main: this.wallComponent ? this.wallComponent.upgradeWallLevel() : null,
  646. topFence: this.topFenceComponent ? this.topFenceComponent.upgradeWallLevel() : null,
  647. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.upgradeWallLevel() : null
  648. };
  649. }
  650. /**
  651. * 根据等级获取墙体健康度
  652. */
  653. public getWallHealthByLevel(level: number): number {
  654. if (this.wallComponent) {
  655. return this.wallComponent.getWallHealthByLevel(level);
  656. }
  657. return 100;
  658. }
  659. /**
  660. * 获取当前墙体等级(返回主墙体等级)
  661. */
  662. public getCurrentWallLevel(): number {
  663. if (this.wallComponent) {
  664. return this.wallComponent.getCurrentWallLevel();
  665. }
  666. return 1;
  667. }
  668. /**
  669. * 获取当前墙体健康度(返回主墙体健康度)
  670. */
  671. public getCurrentWallHealth(): number {
  672. if (this.wallComponent) {
  673. return this.wallComponent.getCurrentHealth();
  674. }
  675. return 100;
  676. }
  677. /**
  678. * 获取所有墙体的等级
  679. */
  680. public getAllWallsLevel(): { main: number; topFence: number; bottomFence: number } {
  681. return {
  682. main: this.wallComponent ? this.wallComponent.getCurrentWallLevel() : 1,
  683. topFence: this.topFenceComponent ? this.topFenceComponent.getCurrentWallLevel() : 1,
  684. bottomFence: this.bottomFenceComponent ? this.bottomFenceComponent.getCurrentWallLevel() : 1
  685. };
  686. }
  687. /**
  688. * 处理确认操作(方块选择确认)
  689. */
  690. public handleConfirmAction() {
  691. console.log('[InGameManager] 处理方块选择确认操作');
  692. // 如果是在准备下一波的状态,需要先切换到下一波
  693. if (this.preparingNextWave) {
  694. console.log('[InGameManager] 检测到准备下一波状态,切换到下一波');
  695. this.nextWave();
  696. // 重置状态
  697. this.preparingNextWave = false;
  698. this.pendingBlockSelection = false;
  699. this.currentState = GameState.PLAYING;
  700. }
  701. // 发送游戏开始事件,确保GamePause正确设置状态
  702. EventBus.getInstance().emit(GameEvents.GAME_START);
  703. console.log('[InGameManager] 发送GAME_START事件');
  704. // 通过事件系统启动球的移动
  705. EventBus.getInstance().emit(GameEvents.BALL_START);
  706. console.log('[InGameManager] 发送BALL_START事件,球已启动');
  707. // 通过事件系统开始当前波次的敌人生成
  708. EventBus.getInstance().emit(GameEvents.ENEMY_START_GAME);
  709. EventBus.getInstance().emit(GameEvents.ENEMY_SHOW_START_WAVE_PROMPT);
  710. console.log(`[InGameManager] 波次 ${this.currentWave} 敌人生成已启动`);
  711. }
  712. /**
  713. * 重置能量值(供ReStartGame调用)
  714. */
  715. public resetEnergyValue() {
  716. this.energyPoints = 0;
  717. this.updateEnergyBar();
  718. console.log('[InGameManager] 能量值重置完成');
  719. }
  720. /**
  721. * 重置波次信息(供ReStartGame调用)
  722. */
  723. public resetWaveInfo() {
  724. this.currentWave = 1;
  725. this.currentWaveEnemyCount = 0;
  726. this.currentWaveTotalEnemies = 0;
  727. this.enemiesKilled = 0;
  728. this.totalEnemiesSpawned = 0;
  729. this.levelTotalEnemies = 0;
  730. console.log('[InGameManager] 波次信息重置完成');
  731. }
  732. /**
  733. * 重置能量系统(供ReStartGame调用)
  734. */
  735. public resetEnergySystem() {
  736. this.energyPoints = 0;
  737. this.energyMax = 5;
  738. this.updateEnergyBar();
  739. console.log('[InGameManager] 能量系统重置完成');
  740. }
  741. /**
  742. * 清理游戏数据,为返回主页面做准备
  743. * 当游戏胜利或失败返回主页面时调用
  744. */
  745. private cleanupGameDataForMainMenu() {
  746. console.log('[InGameManager] 开始清理游戏数据,准备返回主页面');
  747. const eventBus = EventBus.getInstance();
  748. // 1. 清空场上的所有游戏对象
  749. console.log('[InGameManager] 清空场上敌人、方块、球');
  750. eventBus.emit(GameEvents.CLEAR_ALL_ENEMIES); // 清空所有敌人
  751. eventBus.emit(GameEvents.CLEAR_ALL_BULLETS); // 清空所有子弹
  752. eventBus.emit(GameEvents.CLEAR_ALL_GAME_OBJECTS); // 清空其他游戏对象
  753. // 2. 重置能量系统
  754. console.log('[InGameManager] 重置能量系统');
  755. this.energyPoints = 0;
  756. this.updateEnergyBar();
  757. // 3. 清空技能选择数据(重置临时技能状态)
  758. console.log('[InGameManager] 清空技能选择数据');
  759. if (SkillManager.getInstance()) {
  760. // 重置技能管理器中的临时技能状态
  761. const skillManager = SkillManager.getInstance();
  762. const allSkills = skillManager.getAllSkillsData();
  763. allSkills.forEach(skill => {
  764. skillManager.setSkillLevel(skill.id, 0); // 重置所有技能等级为0
  765. });
  766. }
  767. // 4. 重置关卡数据和游戏状态
  768. console.log('[InGameManager] 重置关卡数据和游戏状态');
  769. this.currentWave = 1;
  770. this.currentWaveEnemyCount = 0;
  771. this.currentWaveTotalEnemies = 0;
  772. this.enemiesKilled = 0;
  773. this.totalEnemiesSpawned = 0;
  774. this.levelTotalEnemies = 0;
  775. this.levelWaves = [];
  776. this.gameStarted = false;
  777. this.enemySpawningStarted = false;
  778. this.preparingNextWave = false;
  779. this.pendingSkillSelection = false;
  780. this.pendingBlockSelection = false;
  781. this.shouldShowNextWavePrompt = false;
  782. // 5. 重置UI状态
  783. console.log('[InGameManager] 重置UI状态');
  784. if (this.selectSkillUI) {
  785. this.selectSkillUI.active = false;
  786. }
  787. // 6. 清理会话数据(金币等临时数据)
  788. console.log('[InGameManager] 清理会话数据');
  789. if (LevelSessionManager.inst) {
  790. LevelSessionManager.inst.clear(); // 清空局内金币等临时数据
  791. }
  792. // 7. 通过事件系统通知其他组件进行清理
  793. eventBus.emit(GameEvents.RESET_BALL_CONTROLLER); // 重置球控制器
  794. eventBus.emit(GameEvents.RESET_BLOCK_MANAGER); // 重置方块管理器
  795. eventBus.emit(GameEvents.RESET_BLOCK_SELECTION); // 重置方块选择
  796. eventBus.emit(GameEvents.RESET_ENEMY_CONTROLLER); // 重置敌人控制器
  797. eventBus.emit(GameEvents.RESET_WALL_HEALTH); // 重置墙体血量
  798. eventBus.emit(GameEvents.RESET_UI_STATES); // 重置UI状态
  799. console.log('[InGameManager] 游戏数据清理完成,可以安全返回主页面');
  800. }
  801. /**
  802. * 重置游戏状态(供ReStartGame调用)
  803. */
  804. public resetGameStates() {
  805. this.currentState = GameState.PLAYING;
  806. this.gameStarted = false;
  807. this.enemySpawningStarted = false;
  808. this.preparingNextWave = false;
  809. this.pendingSkillSelection = false;
  810. this.pendingBlockSelection = false;
  811. this.shouldShowNextWavePrompt = false;
  812. this.gameStartTime = 0;
  813. this.gameEndTime = 0;
  814. this.checkTimer = 0;
  815. console.log('[InGameManager] 游戏状态重置完成');
  816. }
  817. /**
  818. * 手动触发游戏数据清理(供外部调用)
  819. * 可以在返回主菜单按钮点击时调用
  820. */
  821. public triggerGameDataCleanup() {
  822. this.cleanupGameDataForMainMenu();
  823. }
  824. /**
  825. * 应用关卡配置
  826. */
  827. public applyLevelConfig(levelConfig: any) {
  828. console.log('[InGameManager] 应用关卡配置');
  829. // 应用能量配置
  830. if (levelConfig.levelSettings && levelConfig.levelSettings.energyMax) {
  831. this.energyMax = levelConfig.levelSettings.energyMax;
  832. this.updateEnergyBar();
  833. console.log(`[InGameManager] 应用能量配置: ${this.energyMax}`);
  834. }
  835. // 如果有武器配置,应用武器
  836. if (levelConfig.weapons && Array.isArray(levelConfig.weapons)) {
  837. console.log('[InGameManager] 应用武器配置');
  838. // TODO: 应用武器配置逻辑
  839. }
  840. // 如果有波次配置,设置敌人波次
  841. if (levelConfig.waves && Array.isArray(levelConfig.waves)) {
  842. this.levelWaves = levelConfig.waves;
  843. this.currentWave = 1;
  844. // 计算本关卡总敌人数
  845. this.levelTotalEnemies = 0;
  846. for (const wave of this.levelWaves) {
  847. for (const enemy of wave.enemies || []) {
  848. this.levelTotalEnemies += enemy.count || 0;
  849. }
  850. }
  851. console.log(`[InGameManager] 应用波次配置: ${this.levelWaves.length}波,总敌人数: ${this.levelTotalEnemies}`);
  852. // 通过事件系统通知 EnemyController 初始化第一波数据及 UI
  853. const firstWaveEnemies = this.levelWaves.length > 0 && this.levelWaves[0].enemies ?
  854. this.levelWaves[0].enemies.reduce((t: number, g: any) => t + (g.count || 0), 0) : 0;
  855. // 通过事件系统调用setCurrentWave
  856. this.setCurrentWave(1, firstWaveEnemies);
  857. }
  858. }
  859. onDestroy() {
  860. const eventBus = EventBus.getInstance();
  861. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
  862. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
  863. eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
  864. eventBus.off('ENEMY_KILLED', this.onEnemyKilledEvent, this);
  865. }
  866. }