IN_game.ts 36 KB

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