IN_game.ts 38 KB

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