IN_game.ts 35 KB

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