IN_game.ts 35 KB

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