IN_game.ts 37 KB

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