IN_game.ts 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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(300, 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. /**
  846. * 重置游戏记录(供GameManager调用)
  847. * 清除上一关的成功或失败记录
  848. */
  849. public resetGameRecord() {
  850. console.log('[InGameManager] 重置游戏记录');
  851. // 重置游戏状态为初始状态
  852. this.currentState = GameState.PLAYING;
  853. // 重置时间记录
  854. this.gameStartTime = 0;
  855. this.gameEndTime = 0;
  856. // 重置波次和敌人统计
  857. this.currentWave = 1;
  858. this.currentWaveEnemyCount = 0;
  859. this.currentWaveTotalEnemies = 0;
  860. this.enemiesKilled = 0;
  861. this.totalEnemiesSpawned = 0;
  862. this.levelTotalEnemies = 0;
  863. // 重置游戏标志
  864. this.gameStarted = false;
  865. this.enemySpawningStarted = false;
  866. this.preparingNextWave = false;
  867. this.pendingSkillSelection = false;
  868. this.pendingBlockSelection = false;
  869. this.shouldShowNextWavePrompt = false;
  870. // 重置能量系统
  871. this.energyPoints = 0;
  872. this.updateEnergyBar();
  873. console.log('[InGameManager] 游戏记录重置完成');
  874. }
  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. onDestroy() {
  918. const eventBus = EventBus.getInstance();
  919. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccessEvent, this);
  920. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeatEvent, this);
  921. eventBus.off(GameEvents.GAME_RESUME, this.onGameResumeEvent, this);
  922. eventBus.off('ENEMY_KILLED', this.onEnemyKilledEvent, this);
  923. }
  924. }