IN_game.ts 38 KB

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