IN_game.ts 45 KB

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