GameEnd.ts 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. import { _decorator, Component, Node, Label, Button, tween, Tween, Vec3, UIOpacity, find } from 'cc';
  2. import { SaveDataManager } from '../LevelSystem/SaveDataManager';
  3. import { InGameManager, GameState } from '../LevelSystem/IN_game';
  4. import EventBus, { GameEvents } from '../Core/EventBus';
  5. import { Audio } from '../AudioManager/AudioManager';
  6. const { ccclass, property } = _decorator;
  7. /**
  8. * 游戏结算界面管理器
  9. * 负责处理游戏结束后的奖励显示和双倍奖励功能
  10. */
  11. @ccclass('GameEnd')
  12. export class GameEnd extends Component {
  13. // === UI节点引用 ===
  14. @property({
  15. type: Button,
  16. tooltip: '双倍奖励按钮 (Canvas/GameEnd/double)'
  17. })
  18. public doubleButton: Button = null;
  19. @property({
  20. type: Label,
  21. tooltip: '钞票数量显示 '
  22. })
  23. public moneyLabel: Label = null;
  24. @property({
  25. type: Label,
  26. tooltip: '钻石数量显示 '
  27. })
  28. public diamondLabel: Label = null;
  29. @property({
  30. type: Button,
  31. tooltip: '继续按钮 (Canvas/GameEnd/Continue)'
  32. })
  33. public continueButton: Button = null;
  34. @property({
  35. type: InGameManager,
  36. tooltip: '游戏管理器组件'
  37. })
  38. public inGameManager: InGameManager = null;
  39. @property({
  40. type: Label,
  41. tooltip: 'EndLabel文本显示 (Canvas/GameEnd/Sprite/EndLabel)'
  42. })
  43. public endLabel: Label = null;
  44. // === 动画相关属性 ===
  45. @property({ tooltip: '动画持续时间(秒)' })
  46. public animationDuration: number = 0.3;
  47. // 淡入淡出和缩放动画都直接作用于当前节点(Canvas/GameEnd)
  48. // 不需要额外的目标节点属性
  49. private _originalScale: Vec3 = new Vec3();
  50. // === 私有属性 ===
  51. private saveDataManager: SaveDataManager = null;
  52. public currentRewards: {money: number, diamonds: number} = {money: -1, diamonds: -1};
  53. private hasDoubledReward: boolean = false;
  54. private isGameSuccess: boolean = false;
  55. private hasProcessedGameEnd: boolean = false; // 添加处理状态标志
  56. onLoad() {
  57. console.log('[GameEnd] onLoad方法被调用');
  58. this.setupEventListeners();
  59. }
  60. start() {
  61. console.log('[GameEnd] start方法被调用');
  62. // 保存原始缩放值
  63. if (this.node) {
  64. this._originalScale.set(this.node.scale);
  65. }
  66. // 设置事件监听器
  67. this.setupEventListeners();
  68. // 初始化为隐藏状态(但保持节点激活以便接收事件)
  69. this.initializeHiddenStateKeepActive();
  70. console.log('[GameEnd] start方法完成,面板已初始化为隐藏状态');
  71. }
  72. onEnable() {
  73. console.log('[GameEnd] onEnable方法被调用,节点已激活');
  74. // 初始化管理器
  75. this.initializeManagers();
  76. // UI节点已通过装饰器挂载,无需自动查找
  77. // 绑定按钮事件
  78. this.bindButtonEvents();
  79. // 初始化UI状态
  80. this.initializeUI();
  81. // 检查当前游戏状态并处理奖励(解决时序问题)
  82. this.checkAndHandleGameState();
  83. }
  84. /**
  85. * 初始化管理器
  86. */
  87. private initializeManagers() {
  88. this.saveDataManager = SaveDataManager.getInstance();
  89. // InGameManager已通过装饰器挂载,无需查找
  90. // 如果UI组件未在编辑器中绑定,尝试自动查找
  91. this.autoFindUIComponents();
  92. }
  93. /**
  94. * 自动查找UI组件(如果编辑器中未绑定)
  95. */
  96. private autoFindUIComponents() {
  97. // 自动查找moneyLabel
  98. if (!this.moneyLabel) {
  99. // 尝试多个可能的路径
  100. const moneyLabelPaths = [
  101. 'ResourceNode/MoneyResourceNode/MoneyLabel',
  102. 'Sprite/ResourceNode/MoneyResourceNode/MoneyLabel',
  103. 'ResourceNode/MoneyLabel',
  104. 'MoneyResourceNode/MoneyLabel'
  105. ];
  106. for (const path of moneyLabelPaths) {
  107. const moneyLabelNode = this.node.getChildByPath(path);
  108. if (moneyLabelNode) {
  109. this.moneyLabel = moneyLabelNode.getComponent(Label);
  110. if (this.moneyLabel) {
  111. console.log(`[GameEnd] 自动找到moneyLabel,路径: ${path}`);
  112. break;
  113. }
  114. }
  115. }
  116. // 如果还是没找到,尝试递归查找
  117. if (!this.moneyLabel) {
  118. this.moneyLabel = this.findLabelByName(this.node, 'MoneyLabel');
  119. if (this.moneyLabel) {
  120. console.log('[GameEnd] 通过递归查找找到moneyLabel');
  121. }
  122. }
  123. }
  124. // 自动查找diamondLabel
  125. if (!this.diamondLabel) {
  126. // 尝试多个可能的路径
  127. const diamondLabelPaths = [
  128. 'ResourceNode/DiamondResourceNode/DiamondLabel',
  129. 'Sprite/ResourceNode/DiamondResourceNode/DiamondLabel',
  130. 'ResourceNode/DiamondLabel',
  131. 'DiamondResourceNode/DiamondLabel'
  132. ];
  133. for (const path of diamondLabelPaths) {
  134. const diamondLabelNode = this.node.getChildByPath(path);
  135. if (diamondLabelNode) {
  136. this.diamondLabel = diamondLabelNode.getComponent(Label);
  137. if (this.diamondLabel) {
  138. console.log(`[GameEnd] 自动找到diamondLabel,路径: ${path}`);
  139. break;
  140. }
  141. }
  142. }
  143. // 如果还是没找到,尝试递归查找
  144. if (!this.diamondLabel) {
  145. this.diamondLabel = this.findLabelByName(this.node, 'DiamondLabel');
  146. if (this.diamondLabel) {
  147. console.log('[GameEnd] 通过递归查找找到diamondLabel');
  148. }
  149. }
  150. }
  151. console.log('[GameEnd] UI组件自动查找完成 - moneyLabel:', !!this.moneyLabel, 'diamondLabel:', !!this.diamondLabel);
  152. // 如果仍然没有找到组件,打印节点结构用于调试
  153. if (!this.moneyLabel || !this.diamondLabel) {
  154. console.warn('[GameEnd] 部分UI组件未找到,打印节点结构用于调试:');
  155. this.printNodeStructure(this.node, 0, 3); // 最多打印3层
  156. }
  157. }
  158. /**
  159. * 递归查找指定名称的Label组件
  160. */
  161. private findLabelByName(node: Node, targetName: string): Label | null {
  162. // 检查当前节点
  163. if (node.name === targetName) {
  164. const label = node.getComponent(Label);
  165. if (label) return label;
  166. }
  167. // 递归检查子节点
  168. for (const child of node.children) {
  169. const result = this.findLabelByName(child, targetName);
  170. if (result) return result;
  171. }
  172. return null;
  173. }
  174. /**
  175. * 打印节点结构用于调试
  176. */
  177. private printNodeStructure(node: Node, depth: number, maxDepth: number) {
  178. if (depth > maxDepth) return;
  179. const indent = ' '.repeat(depth);
  180. const components = node.getComponents(Component).map(c => c.constructor.name).join(', ');
  181. console.log(`${indent}${node.name} [${components || 'No Components'}]`);
  182. for (const child of node.children) {
  183. this.printNodeStructure(child, depth + 1, maxDepth);
  184. }
  185. }
  186. /**
  187. * 绑定按钮事件
  188. */
  189. private bindButtonEvents() {
  190. if (this.doubleButton) {
  191. this.doubleButton.node.on(Button.EventType.CLICK, this.onDoubleButtonClick, this);
  192. }
  193. if (this.continueButton) {
  194. this.continueButton.node.on(Button.EventType.CLICK, this.onContinueButtonClick, this);
  195. }
  196. }
  197. /**
  198. * 设置事件监听器
  199. */
  200. private setupEventListeners() {
  201. console.log('[GameEnd] 开始设置事件监听器');
  202. const eventBus = EventBus.getInstance();
  203. // 监听游戏成功事件
  204. eventBus.on(GameEvents.GAME_SUCCESS, this.onGameSuccess, this);
  205. console.log('[GameEnd] 已注册GAME_SUCCESS事件监听器');
  206. // 监听游戏失败事件
  207. eventBus.on(GameEvents.GAME_DEFEAT, this.onGameDefeat, this);
  208. console.log('[GameEnd] 已注册GAME_DEFEAT事件监听器');
  209. // 监听游戏开始事件
  210. eventBus.on(GameEvents.GAME_START, this.onGameStart, this);
  211. console.log('[GameEnd] 已注册GAME_START事件监听器');
  212. // 监听UI重置事件
  213. eventBus.on(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  214. console.log('[GameEnd] 事件监听器设置完成');
  215. }
  216. /**
  217. * 初始化UI状态
  218. */
  219. private initializeUI() {
  220. // 重置状态
  221. this.hasDoubledReward = false;
  222. this.currentRewards = {money: 0, diamonds: 0};
  223. console.log('[GameEnd] UI状态初始化完成');
  224. }
  225. /**
  226. * 检查当前游戏状态并处理奖励(解决时序问题)
  227. */
  228. private checkAndHandleGameState() {
  229. console.log('[GameEnd] 检查当前游戏状态');
  230. if (!this.inGameManager) {
  231. console.log('[GameEnd] InGameManager未初始化,无法检查游戏状态');
  232. return;
  233. }
  234. const currentState = this.inGameManager.getCurrentState();
  235. console.log('[GameEnd] 当前游戏状态:', currentState);
  236. // 如果游戏已经结束,主动处理奖励
  237. if (currentState === GameState.SUCCESS) {
  238. console.log('[GameEnd] 检测到游戏成功状态,主动处理奖励');
  239. this.isGameSuccess = true;
  240. this.calculateAndShowRewards();
  241. } else if (currentState === GameState.DEFEAT) {
  242. console.log('[GameEnd] 检测到游戏失败状态,主动处理奖励');
  243. this.isGameSuccess = false;
  244. this.calculateAndShowRewards();
  245. }
  246. }
  247. /**
  248. * 处理游戏成功事件
  249. * 统一处理游戏成功逻辑:状态切换 + UI显示 + 奖励计算
  250. */
  251. private onGameSuccess() {
  252. console.log('[GameEnd] 接收到GAME_SUCCESS事件');
  253. console.log('[GameEnd] 游戏成功事件处理,开始统一处理流程');
  254. // 1. 清理敌人(阶段一:UI弹出阶段的职责)
  255. this.clearAllEnemies();
  256. // 2. 设置游戏状态为成功(如果还未设置)
  257. if (this.inGameManager && this.inGameManager.getCurrentState() !== GameState.SUCCESS) {
  258. this.inGameManager.setCurrentState(GameState.SUCCESS);
  259. console.log('[GameEnd] 已将游戏状态切换为SUCCESS');
  260. }
  261. // 3. 播放游戏成功音效
  262. Audio.playUISound('data/弹球音效/win');
  263. console.log('[GameEnd] 已播放游戏成功音效');
  264. // 4. 设置EndLabel文本
  265. this.setEndLabelText('SUCCESS');
  266. // 5. 设置成功标志
  267. this.isGameSuccess = true;
  268. // 6. 计算和显示奖励(包含面板动画显示)
  269. this.calculateAndShowRewards();
  270. }
  271. /**
  272. * 处理游戏失败事件
  273. * 统一处理游戏失败逻辑:状态切换 + UI显示 + 奖励计算
  274. */
  275. private onGameDefeat() {
  276. console.log('[GameEnd] 接收到GAME_DEFEAT事件');
  277. console.log('[GameEnd] 游戏失败事件处理,开始统一处理流程');
  278. // 1. 清理敌人(阶段一:UI弹出阶段的职责)
  279. this.clearAllEnemies();
  280. // 2. 设置游戏状态为失败(如果还未设置)
  281. if (this.inGameManager && this.inGameManager.getCurrentState() !== GameState.DEFEAT) {
  282. this.inGameManager.setCurrentState(GameState.DEFEAT);
  283. console.log('[GameEnd] 已将游戏状态切换为DEFEAT');
  284. }
  285. // 3. 播放游戏失败音效
  286. Audio.playUISound('data/弹球音效/lose');
  287. console.log('[GameEnd] 已播放游戏失败音效');
  288. // 4. 设置EndLabel文本
  289. this.setEndLabelText('DEFEAT');
  290. // 5. 设置失败标志
  291. this.isGameSuccess = false;
  292. // 6. 计算和显示奖励(包含面板动画显示)
  293. this.calculateAndShowRewards();
  294. }
  295. /**
  296. * 游戏开始处理
  297. */
  298. private onGameStart() {
  299. console.log('[GameEnd] 收到游戏开始事件,重置奖励显示');
  300. // 重置奖励显示,为新游戏做准备
  301. this.currentRewards = {money: 0, diamonds: 0};
  302. this.hasProcessedGameEnd = false;
  303. this.hasDoubledReward = false;
  304. this.isGameSuccess = false;
  305. }
  306. /**
  307. * 计算并显示奖励
  308. */
  309. private async calculateAndShowRewards() {
  310. console.log('[GameEnd] 开始计算并显示奖励');
  311. // 防止重复计算奖励(使用专用标志判断)
  312. if (this.hasProcessedGameEnd) {
  313. console.log('[GameEnd] 游戏结束已处理过,跳过重复计算,直接显示面板');
  314. this.updateRewardDisplay();
  315. // 确保面板显示
  316. this.showEndPanelWithAnimation();
  317. return;
  318. }
  319. // 标记为已处理,防止重复执行
  320. this.hasProcessedGameEnd = true;
  321. if (!this.saveDataManager) {
  322. console.error('[GameEnd] SaveDataManager未初始化');
  323. return;
  324. }
  325. const currentLevel = this.saveDataManager.getCurrentLevel();
  326. console.log(`[GameEnd] 当前关卡: ${currentLevel}, 游戏成功: ${this.isGameSuccess}`);
  327. try {
  328. if (this.isGameSuccess) {
  329. // 游戏成功,先完成关卡(更新maxUnlockedLevel),再给予奖励
  330. console.log('[GameEnd] 准备完成关卡并给予成功奖励');
  331. // 获取游戏时长
  332. const gameTime = this.inGameManager ? Math.floor(this.inGameManager.getGameDuration() / 1000) : 0;
  333. console.log(`[GameEnd] 游戏时长: ${gameTime}秒`);
  334. // 先调用completeLevel更新maxUnlockedLevel,这样武器解锁机制才能正常工作
  335. this.saveDataManager.completeLevel(currentLevel, 0, gameTime);
  336. console.log('[GameEnd] 已完成关卡,maxUnlockedLevel已更新');
  337. // 然后给予奖励
  338. await this.saveDataManager.giveCompletionRewards(currentLevel);
  339. console.log('[GameEnd] 已给予成功奖励');
  340. } else {
  341. // 游戏失败,给予按比例奖励
  342. const totalWaves = this.inGameManager?.levelWaves?.length || 1;
  343. const completedWaves = this.inGameManager ? Math.max(0, this.inGameManager.getCurrentWave() - 1) : 0;
  344. console.log(`[GameEnd] 准备给予失败奖励,完成波数: ${completedWaves}/${totalWaves}`);
  345. await this.saveDataManager.giveFailureRewards(currentLevel, completedWaves, totalWaves);
  346. console.log(`[GameEnd] 已给予失败奖励,完成波数: ${completedWaves}/${totalWaves}`);
  347. }
  348. // 获取奖励数据并显示
  349. this.currentRewards = this.saveDataManager.getLastRewards();
  350. console.log('[GameEnd] 获取到的奖励数据:', this.currentRewards);
  351. console.log('[GameEnd] 奖励计算完成');
  352. this.updateRewardDisplay();
  353. // 显示结算面板(通过动画)
  354. this.showEndPanelWithAnimation();
  355. } catch (error) {
  356. console.error('[GameEnd] 计算奖励时出错:', error);
  357. }
  358. }
  359. /**
  360. * 更新奖励显示
  361. */
  362. private updateRewardDisplay() {
  363. console.log(`[GameEnd] 开始更新奖励显示 - 钞票: ${this.currentRewards.money}, 钻石: ${this.currentRewards.diamonds}`);
  364. // 检查moneyLabel绑定状态
  365. if (this.moneyLabel) {
  366. this.moneyLabel.string = this.currentRewards.money.toString();
  367. console.log(`[GameEnd] 钞票标签已更新: ${this.currentRewards.money}`);
  368. } else {
  369. console.error('[GameEnd] moneyLabel未绑定!请在编辑器中将Canvas/GameEnd/ResourceNode/MoneyResourceNode/MoneyLabel拖拽到GameEnd组件的moneyLabel属性');
  370. }
  371. // 检查diamondLabel绑定状态
  372. if (this.diamondLabel) {
  373. this.diamondLabel.string = this.currentRewards.diamonds.toString();
  374. console.log(`[GameEnd] 钻石标签已更新: ${this.currentRewards.diamonds}`);
  375. } else {
  376. console.error('[GameEnd] diamondLabel未绑定!请在编辑器中将Canvas/GameEnd/ResourceNode/DiamondResourceNode/DiamondLabel拖拽到GameEnd组件的diamondLabel属性');
  377. }
  378. console.log(`[GameEnd] 奖励显示更新完成`);
  379. }
  380. /**
  381. * 显示结算面板(通过动画)
  382. */
  383. private showEndPanelWithAnimation() {
  384. console.log('[GameEnd] 开始显示结算面板动画');
  385. // 确保节点处于激活状态
  386. if (this.node && !this.node.active) {
  387. this.node.active = true;
  388. console.log('[GameEnd] 激活GameEnd节点');
  389. }
  390. // 重置双倍奖励状态
  391. this.hasDoubledReward = false;
  392. if (this.doubleButton) {
  393. this.doubleButton.interactable = true;
  394. }
  395. // 确保有UIOpacity组件
  396. if (this.node) {
  397. let uiOpacity = this.node.getComponent(UIOpacity);
  398. if (!uiOpacity) {
  399. uiOpacity = this.node.addComponent(UIOpacity);
  400. console.log('[GameEnd] 在showEndPanelWithAnimation中添加UIOpacity组件');
  401. }
  402. }
  403. // 播放显示动画(如果有的话)
  404. if (this.animationDuration > 0) {
  405. console.log(`[GameEnd] 动画持续时间: ${this.animationDuration}秒,开始播放GameEnd面板弹出动画`);
  406. this.playShowAnimation();
  407. } else {
  408. // 如果没有动画,直接显示
  409. if (this.node) {
  410. this.node.setScale(1, 1, 1);
  411. const uiOpacity = this.node.getComponent(UIOpacity);
  412. if (uiOpacity) {
  413. uiOpacity.opacity = 255;
  414. }
  415. }
  416. console.log('[GameEnd] 无动画配置,直接显示面板');
  417. }
  418. console.log('[GameEnd] GameEnd面板显示流程完成');
  419. }
  420. /**
  421. * 播放显示动画
  422. */
  423. private playShowAnimation() {
  424. if (!this.node) {
  425. console.error('[GameEnd] playShowAnimation: 节点不存在');
  426. return;
  427. }
  428. console.log('[GameEnd] 开始播放GameEnd面板弹出动画');
  429. // 停止所有正在进行的动画
  430. this.stopAllAnimations();
  431. // 设置节点位置到屏幕中心
  432. this.centerNodeOnScreen();
  433. // 确保有UIOpacity组件
  434. let uiOpacity = this.node.getComponent(UIOpacity);
  435. if (!uiOpacity) {
  436. uiOpacity = this.node.addComponent(UIOpacity);
  437. console.log('[GameEnd] 添加UIOpacity组件');
  438. }
  439. // 设置初始状态
  440. this.node.setScale(0, 0, 1);
  441. uiOpacity.opacity = 0;
  442. console.log('[GameEnd] 设置动画初始状态 - 缩放: 0, 透明度: 0');
  443. // 播放缩放和淡入动画
  444. tween(this.node)
  445. .to(this.animationDuration, {
  446. scale: new Vec3(1, 1, 1)
  447. }, {
  448. easing: 'backOut'
  449. })
  450. .call(() => {
  451. console.log('[GameEnd] GameEnd面板缩放动画完成');
  452. })
  453. .start();
  454. tween(uiOpacity)
  455. .to(this.animationDuration, {
  456. opacity: 255
  457. })
  458. .call(() => {
  459. console.log('[GameEnd] GameEnd面板淡入动画完成');
  460. })
  461. .start();
  462. console.log('[GameEnd] GameEnd面板弹出动画开始执行');
  463. }
  464. /**
  465. * 将节点居中到屏幕中央
  466. */
  467. private centerNodeOnScreen() {
  468. if (!this.node) return;
  469. // 获取Canvas节点
  470. const canvas = find('Canvas');
  471. if (!canvas) {
  472. console.warn('[GameEnd] 未找到Canvas节点');
  473. return;
  474. }
  475. // 设置位置为(0, 0),这在Canvas坐标系中是屏幕中心
  476. this.node.setPosition(0, 0, 0);
  477. console.log('[GameEnd] 已将面板居中到屏幕中央');
  478. }
  479. /**
  480. * 双倍按钮点击事件
  481. */
  482. private onDoubleButtonClick() {
  483. // 播放UI点击音效
  484. Audio.playUISound('data/弹球音效/ui play');
  485. if (this.hasDoubledReward) {
  486. console.log('[GameEnd] 已经获得过双倍奖励');
  487. return;
  488. }
  489. console.log('[GameEnd] 点击双倍奖励按钮');
  490. // 这里可以添加观看广告的逻辑
  491. // 暂时直接给予双倍奖励
  492. this.giveDoubleReward();
  493. }
  494. /**
  495. * 给予双倍奖励
  496. */
  497. private giveDoubleReward() {
  498. if (!this.saveDataManager || this.hasDoubledReward) {
  499. return;
  500. }
  501. // 计算双倍奖励
  502. const doubleMoney = this.currentRewards.money;
  503. const doubleDiamonds = this.currentRewards.diamonds;
  504. // 添加额外奖励到玩家账户
  505. if (doubleMoney > 0) {
  506. this.saveDataManager.addMoney(doubleMoney, 'double_reward');
  507. }
  508. if (doubleDiamonds > 0) {
  509. this.saveDataManager.addDiamonds(doubleDiamonds, 'double_reward');
  510. }
  511. // 更新当前奖励显示(显示双倍后的数值)
  512. this.currentRewards.money += doubleMoney;
  513. this.currentRewards.diamonds += doubleDiamonds;
  514. this.updateRewardDisplay();
  515. // 标记已获得双倍奖励
  516. this.hasDoubledReward = true;
  517. // 禁用双倍按钮
  518. if (this.doubleButton) {
  519. this.doubleButton.interactable = false;
  520. }
  521. console.log(`[GameEnd] 双倍奖励已给予 - 额外钞票: ${doubleMoney}, 额外钻石: ${doubleDiamonds}`);
  522. // 触发货币变化事件
  523. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  524. // 派发事件给MoneyAni播放奖励动画
  525. const rewards = this.saveDataManager.getLastRewards();
  526. console.log('[GameEnd] 派发奖励动画事件,奖励数据:', rewards);
  527. EventBus.getInstance().emit('PLAY_REWARD_ANIMATION', {
  528. money: rewards.money,
  529. diamonds: rewards.diamonds
  530. });
  531. // 触发返回主菜单事件
  532. EventBus.getInstance().emit('CONTINUE_CLICK');
  533. // 隐藏结算面板(通过动画)
  534. this.hideEndPanelWithAnimation();
  535. }
  536. /**
  537. * 继续按钮点击事件
  538. */
  539. private onContinueButtonClick() {
  540. // 播放UI点击音效
  541. Audio.playUISound('data/弹球音效/ui play');
  542. console.log('[GameEnd] 点击继续按钮');
  543. // 派发事件给MoneyAni播放奖励动画
  544. const rewards = this.saveDataManager.getLastRewards();
  545. console.log('[GameEnd] 派发奖励动画事件,奖励数据:', rewards);
  546. EventBus.getInstance().emit('PLAY_REWARD_ANIMATION', {
  547. money: rewards.money,
  548. diamonds: rewards.diamonds
  549. });
  550. // 触发返回主菜单事件
  551. EventBus.getInstance().emit('CONTINUE_CLICK');
  552. // 隐藏结算面板(通过动画)
  553. this.hideEndPanelWithAnimation();
  554. }
  555. /**
  556. * 隐藏结算面板(通过动画)
  557. */
  558. private async hideEndPanelWithAnimation() {
  559. // 播放隐藏动画
  560. await this.fadeOutWithScale();
  561. console.log('[GameEnd] 结算面板已通过动画隐藏');
  562. }
  563. /**
  564. * 重置UI状态
  565. */
  566. private onResetUI() {
  567. console.log('[GameEnd] 重置UI状态');
  568. // 停止所有动画
  569. this.stopAllAnimations();
  570. // 直接重置到隐藏状态,不使用动画
  571. this.initializeHiddenState();
  572. // 重置所有状态,为下一局游戏做准备
  573. this.hasDoubledReward = false;
  574. // 注意:不重置currentRewards,保持奖励显示直到下次游戏开始
  575. // this.currentRewards = {money: -1, diamonds: -1}; // 移除这行,避免重置奖励显示
  576. this.isGameSuccess = false;
  577. this.hasProcessedGameEnd = false; // 重置处理状态标志
  578. // 重置按钮状态
  579. if (this.doubleButton) {
  580. this.doubleButton.interactable = true;
  581. }
  582. console.log('[GameEnd] UI状态重置完成');
  583. }
  584. /**
  585. * 设置EndLabel文本
  586. * @param text 要显示的文本内容
  587. */
  588. private setEndLabelText(text: string) {
  589. if (this.endLabel) {
  590. this.endLabel.string = text;
  591. console.log(`[GameEnd] 已设置EndLabel文本为: ${text}`);
  592. } else {
  593. // 如果endLabel未绑定,尝试通过路径查找
  594. const endLabelNode = find('Canvas/GameEnd/Sprite/EndLabel');
  595. if (endLabelNode) {
  596. const labelComponent = endLabelNode.getComponent(Label);
  597. if (labelComponent) {
  598. labelComponent.string = text;
  599. console.log(`[GameEnd] 通过路径查找设置EndLabel文本为: ${text}`);
  600. } else {
  601. console.warn('[GameEnd] 找到EndLabel节点但无Label组件');
  602. }
  603. } else {
  604. console.warn('[GameEnd] 未找到EndLabel节点,请检查路径: Canvas/GameEnd/Sprite/EndLabel');
  605. }
  606. }
  607. }
  608. /**
  609. * 获取当前奖励信息(用于外部查询)
  610. */
  611. public getCurrentRewards(): {money: number, diamonds: number} {
  612. return {...this.currentRewards};
  613. }
  614. /**
  615. * 检查是否已获得双倍奖励
  616. */
  617. public hasGotDoubleReward(): boolean {
  618. return this.hasDoubledReward;
  619. }
  620. // === 动画方法 ===
  621. /**
  622. * 初始化为隐藏状态
  623. */
  624. private initializeHiddenState() {
  625. // 设置初始透明度为0
  626. if (this.node) {
  627. let uiOpacity = this.node.getComponent(UIOpacity);
  628. if (!uiOpacity) {
  629. uiOpacity = this.node.addComponent(UIOpacity);
  630. console.log('[GameEnd] 添加UIOpacity组件到节点');
  631. }
  632. uiOpacity.opacity = 0;
  633. }
  634. // 设置初始缩放为0(完全隐藏,恢复到历史版本实现)
  635. if (this.node) {
  636. this.node.setScale(0, 0, 1);
  637. }
  638. console.log('[GameEnd] 初始化为隐藏状态 - 缩放: 0, 透明度: 0');
  639. }
  640. /**
  641. * 初始化为隐藏状态但保持节点激活(用于游戏开始时)
  642. */
  643. private initializeHiddenStateKeepActive() {
  644. // 设置初始透明度为0
  645. if (this.node) {
  646. let uiOpacity = this.node.getComponent(UIOpacity);
  647. if (!uiOpacity) {
  648. uiOpacity = this.node.addComponent(UIOpacity);
  649. console.log('[GameEnd] 添加UIOpacity组件到节点');
  650. }
  651. uiOpacity.opacity = 0;
  652. }
  653. // 设置初始缩放为0(完全隐藏,恢复到历史版本实现)
  654. if (this.node) {
  655. this.node.setScale(0, 0, 1);
  656. }
  657. console.log('[GameEnd] 初始化为隐藏状态(保持激活) - 缩放: 0, 透明度: 0');
  658. }
  659. /**
  660. * 淡入动画
  661. */
  662. public fadeIn(target?: Node, duration?: number): Promise<void> {
  663. const animTarget = target || this.node;
  664. const animDuration = duration !== undefined ? duration : this.animationDuration;
  665. if (!animTarget) {
  666. console.warn('[GameEnd] fadeIn: 未指定目标节点');
  667. return Promise.resolve();
  668. }
  669. return new Promise<void>((resolve) => {
  670. let uiOpacity = animTarget.getComponent(UIOpacity);
  671. if (!uiOpacity) {
  672. uiOpacity = animTarget.addComponent(UIOpacity);
  673. }
  674. Tween.stopAllByTarget(uiOpacity);
  675. uiOpacity.opacity = 0;
  676. tween(uiOpacity)
  677. .to(animDuration, { opacity: 255 }, { easing: 'quadOut' })
  678. .call(() => {
  679. resolve();
  680. })
  681. .start();
  682. });
  683. }
  684. /**
  685. * 淡出动画
  686. */
  687. public fadeOut(target?: Node, duration?: number): Promise<void> {
  688. const animTarget = target || this.node;
  689. const animDuration = duration !== undefined ? duration : this.animationDuration;
  690. if (!animTarget) {
  691. console.warn('[GameEnd] fadeOut: 未指定目标节点');
  692. return Promise.resolve();
  693. }
  694. return new Promise<void>((resolve) => {
  695. let uiOpacity = animTarget.getComponent(UIOpacity);
  696. if (!uiOpacity) {
  697. uiOpacity = animTarget.addComponent(UIOpacity);
  698. }
  699. Tween.stopAllByTarget(uiOpacity);
  700. tween(uiOpacity)
  701. .to(animDuration, { opacity: 0 }, { easing: 'quadIn' })
  702. .call(() => {
  703. resolve();
  704. })
  705. .start();
  706. });
  707. }
  708. /**
  709. * 缩放弹出动画
  710. */
  711. public scalePopIn(target?: Node, duration?: number): Promise<void> {
  712. const animTarget = target || this.node;
  713. const animDuration = duration !== undefined ? duration : this.animationDuration;
  714. if (!animTarget) {
  715. console.warn('[GameEnd] scalePopIn: 未指定目标节点');
  716. return Promise.resolve();
  717. }
  718. return new Promise<void>((resolve) => {
  719. Tween.stopAllByTarget(animTarget);
  720. animTarget.setScale(0, 0, 1);
  721. tween(animTarget)
  722. .to(animDuration, { scale: this._originalScale }, { easing: 'backOut' })
  723. .call(() => {
  724. resolve();
  725. })
  726. .start();
  727. });
  728. }
  729. /**
  730. * 缩放收缩动画
  731. */
  732. public scalePopOut(target?: Node, duration?: number): Promise<void> {
  733. const animTarget = target || this.node;
  734. const animDuration = duration !== undefined ? duration : this.animationDuration;
  735. if (!animTarget) {
  736. console.warn('[GameEnd] scalePopOut: 未指定目标节点');
  737. return Promise.resolve();
  738. }
  739. return new Promise<void>((resolve) => {
  740. Tween.stopAllByTarget(animTarget);
  741. tween(animTarget)
  742. .to(animDuration, { scale: new Vec3(0, 0, 1) }, { easing: 'backIn' })
  743. .call(() => {
  744. resolve();
  745. })
  746. .start();
  747. });
  748. }
  749. /**
  750. * 组合动画:淡入 + 缩放弹出
  751. */
  752. public async fadeInWithScale(fadeTarget?: Node, scaleTarget?: Node, duration?: number): Promise<void> {
  753. const promises: Promise<void>[] = [];
  754. if (fadeTarget || this.node) {
  755. promises.push(this.fadeIn(fadeTarget, duration));
  756. }
  757. if (scaleTarget || this.node) {
  758. promises.push(this.scalePopIn(scaleTarget, duration));
  759. }
  760. await Promise.all(promises);
  761. }
  762. /**
  763. * 组合动画:淡出 + 缩放收缩
  764. */
  765. public async fadeOutWithScale(fadeTarget?: Node, scaleTarget?: Node, duration?: number): Promise<void> {
  766. const promises: Promise<void>[] = [];
  767. if (fadeTarget || this.node) {
  768. promises.push(this.fadeOut(fadeTarget, duration));
  769. }
  770. if (scaleTarget || this.node) {
  771. promises.push(this.scalePopOut(scaleTarget, duration));
  772. }
  773. await Promise.all(promises);
  774. }
  775. /**
  776. * 停止所有动画
  777. */
  778. public stopAllAnimations() {
  779. if (this.node) {
  780. // 停止所有Tween动画
  781. Tween.stopAllByTarget(this.node);
  782. // 停止UIOpacity组件的动画
  783. const uiOpacity = this.node.getComponent(UIOpacity);
  784. if (uiOpacity) {
  785. Tween.stopAllByTarget(uiOpacity);
  786. }
  787. console.log('[GameEnd] 已停止所有动画');
  788. } else {
  789. console.warn('[GameEnd] stopAllAnimations: 节点不存在');
  790. }
  791. }
  792. /**
  793. * 清理所有敌人
  794. * 在游戏结束时调用,符合阶段一UI弹出阶段的职责
  795. */
  796. /**
  797. * 通过事件系统清理所有敌人
  798. * 使用CLEAR_ALL_ENEMIES事件,避免直接引用EnemyController
  799. */
  800. private clearAllEnemies() {
  801. console.log('[GameEnd] 游戏结束,派发CLEAR_ALL_ENEMIES事件清理所有敌人');
  802. try {
  803. // 通过事件总线派发清理敌人事件
  804. EventBus.getInstance().emit(GameEvents.CLEAR_ALL_ENEMIES);
  805. console.log('[GameEnd] 已派发CLEAR_ALL_ENEMIES事件,统一清理敌人');
  806. } catch (error) {
  807. console.error('[GameEnd] 派发清理敌人事件时发生错误:', error);
  808. }
  809. }
  810. /**
  811. * 重置所有目标节点到初始状态
  812. */
  813. public resetToInitialState() {
  814. this.stopAllAnimations();
  815. if (this.node) {
  816. let uiOpacity = this.node.getComponent(UIOpacity);
  817. if (uiOpacity) {
  818. uiOpacity.opacity = 255;
  819. }
  820. this.node.setScale(this._originalScale);
  821. }
  822. }
  823. onDisable() {
  824. console.log('[GameEnd] onDisable方法被调用,节点已禁用');
  825. // 停止所有动画
  826. this.stopAllAnimations();
  827. // 清理事件监听
  828. const eventBus = EventBus.getInstance();
  829. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccess, this);
  830. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeat, this);
  831. eventBus.off(GameEvents.GAME_START, this.onGameStart, this);
  832. eventBus.off(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  833. console.log('[GameEnd] 事件监听器已清理');
  834. }
  835. protected onDestroy() {
  836. // 停止所有动画
  837. this.stopAllAnimations();
  838. // 清理事件监听
  839. const eventBus = EventBus.getInstance();
  840. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccess, this);
  841. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeat, this);
  842. eventBus.off(GameEvents.GAME_START, this.onGameStart, this);
  843. eventBus.off(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  844. // 清理按钮事件
  845. if (this.doubleButton) {
  846. this.doubleButton.node.off(Button.EventType.CLICK, this.onDoubleButtonClick, this);
  847. }
  848. if (this.continueButton) {
  849. this.continueButton.node.off(Button.EventType.CLICK, this.onContinueButtonClick, this);
  850. }
  851. }
  852. }