GameEnd.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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};
  53. private hasDoubledReward: boolean = false;
  54. private isGameSuccess: boolean = false;
  55. onLoad() {
  56. console.log('[GameEnd] onLoad方法被调用');
  57. this.setupEventListeners();
  58. }
  59. start() {
  60. console.log('[GameEnd] start方法被调用');
  61. // 保存原始缩放值
  62. if (this.node) {
  63. this._originalScale.set(this.node.scale);
  64. }
  65. // 初始化为隐藏状态(通过动画实现)
  66. this.initializeHiddenState();
  67. // 监听游戏事件
  68. // start方法只在节点首次激活时调用一次
  69. // 如果节点初始状态为false,start不会被调用
  70. }
  71. onEnable() {
  72. console.log('[GameEnd] onEnable方法被调用,节点已激活');
  73. // 初始化管理器
  74. this.initializeManagers();
  75. // UI节点已通过装饰器挂载,无需自动查找
  76. // 绑定按钮事件
  77. this.bindButtonEvents();
  78. // 初始化UI状态
  79. this.initializeUI();
  80. // 检查当前游戏状态并处理奖励(解决时序问题)
  81. this.checkAndHandleGameState();
  82. }
  83. /**
  84. * 初始化管理器
  85. */
  86. private initializeManagers() {
  87. this.saveDataManager = SaveDataManager.getInstance();
  88. // InGameManager已通过装饰器挂载,无需查找
  89. // 如果UI组件未在编辑器中绑定,尝试自动查找
  90. this.autoFindUIComponents();
  91. }
  92. /**
  93. * 自动查找UI组件(如果编辑器中未绑定)
  94. */
  95. private autoFindUIComponents() {
  96. // 自动查找moneyLabel
  97. if (!this.moneyLabel) {
  98. // 尝试多个可能的路径
  99. const moneyLabelPaths = [
  100. 'ResourceNode/MoneyResourceNode/MoneyLabel',
  101. 'Sprite/ResourceNode/MoneyResourceNode/MoneyLabel',
  102. 'ResourceNode/MoneyLabel',
  103. 'MoneyResourceNode/MoneyLabel'
  104. ];
  105. for (const path of moneyLabelPaths) {
  106. const moneyLabelNode = this.node.getChildByPath(path);
  107. if (moneyLabelNode) {
  108. this.moneyLabel = moneyLabelNode.getComponent(Label);
  109. if (this.moneyLabel) {
  110. console.log(`[GameEnd] 自动找到moneyLabel,路径: ${path}`);
  111. break;
  112. }
  113. }
  114. }
  115. // 如果还是没找到,尝试递归查找
  116. if (!this.moneyLabel) {
  117. this.moneyLabel = this.findLabelByName(this.node, 'MoneyLabel');
  118. if (this.moneyLabel) {
  119. console.log('[GameEnd] 通过递归查找找到moneyLabel');
  120. }
  121. }
  122. }
  123. // 自动查找diamondLabel
  124. if (!this.diamondLabel) {
  125. // 尝试多个可能的路径
  126. const diamondLabelPaths = [
  127. 'ResourceNode/DiamondResourceNode/DiamondLabel',
  128. 'Sprite/ResourceNode/DiamondResourceNode/DiamondLabel',
  129. 'ResourceNode/DiamondLabel',
  130. 'DiamondResourceNode/DiamondLabel'
  131. ];
  132. for (const path of diamondLabelPaths) {
  133. const diamondLabelNode = this.node.getChildByPath(path);
  134. if (diamondLabelNode) {
  135. this.diamondLabel = diamondLabelNode.getComponent(Label);
  136. if (this.diamondLabel) {
  137. console.log(`[GameEnd] 自动找到diamondLabel,路径: ${path}`);
  138. break;
  139. }
  140. }
  141. }
  142. // 如果还是没找到,尝试递归查找
  143. if (!this.diamondLabel) {
  144. this.diamondLabel = this.findLabelByName(this.node, 'DiamondLabel');
  145. if (this.diamondLabel) {
  146. console.log('[GameEnd] 通过递归查找找到diamondLabel');
  147. }
  148. }
  149. }
  150. console.log('[GameEnd] UI组件自动查找完成 - moneyLabel:', !!this.moneyLabel, 'diamondLabel:', !!this.diamondLabel);
  151. // 如果仍然没有找到组件,打印节点结构用于调试
  152. if (!this.moneyLabel || !this.diamondLabel) {
  153. console.warn('[GameEnd] 部分UI组件未找到,打印节点结构用于调试:');
  154. this.printNodeStructure(this.node, 0, 3); // 最多打印3层
  155. }
  156. }
  157. /**
  158. * 递归查找指定名称的Label组件
  159. */
  160. private findLabelByName(node: Node, targetName: string): Label | null {
  161. // 检查当前节点
  162. if (node.name === targetName) {
  163. const label = node.getComponent(Label);
  164. if (label) return label;
  165. }
  166. // 递归检查子节点
  167. for (const child of node.children) {
  168. const result = this.findLabelByName(child, targetName);
  169. if (result) return result;
  170. }
  171. return null;
  172. }
  173. /**
  174. * 打印节点结构用于调试
  175. */
  176. private printNodeStructure(node: Node, depth: number, maxDepth: number) {
  177. if (depth > maxDepth) return;
  178. const indent = ' '.repeat(depth);
  179. const components = node.getComponents(Component).map(c => c.constructor.name).join(', ');
  180. console.log(`${indent}${node.name} [${components || 'No Components'}]`);
  181. for (const child of node.children) {
  182. this.printNodeStructure(child, depth + 1, maxDepth);
  183. }
  184. }
  185. /**
  186. * 绑定按钮事件
  187. */
  188. private bindButtonEvents() {
  189. if (this.doubleButton) {
  190. this.doubleButton.node.on(Button.EventType.CLICK, this.onDoubleButtonClick, this);
  191. }
  192. if (this.continueButton) {
  193. this.continueButton.node.on(Button.EventType.CLICK, this.onContinueButtonClick, this);
  194. }
  195. }
  196. /**
  197. * 设置事件监听器
  198. */
  199. private setupEventListeners() {
  200. console.log('[GameEnd] 开始设置事件监听器');
  201. const eventBus = EventBus.getInstance();
  202. // 监听游戏成功事件
  203. eventBus.on(GameEvents.GAME_SUCCESS, this.onGameSuccess, this);
  204. console.log('[GameEnd] 已注册GAME_SUCCESS事件监听器');
  205. // 监听游戏失败事件
  206. eventBus.on(GameEvents.GAME_DEFEAT, this.onGameDefeat, this);
  207. console.log('[GameEnd] 已注册GAME_DEFEAT事件监听器');
  208. // 监听UI重置事件
  209. eventBus.on(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  210. console.log('[GameEnd] 事件监听器设置完成');
  211. }
  212. /**
  213. * 初始化UI状态
  214. */
  215. private initializeUI() {
  216. // 重置状态
  217. this.hasDoubledReward = false;
  218. this.currentRewards = {money: 0, diamonds: 0};
  219. console.log('[GameEnd] UI状态初始化完成');
  220. }
  221. /**
  222. * 检查当前游戏状态并处理奖励(解决时序问题)
  223. */
  224. private checkAndHandleGameState() {
  225. console.log('[GameEnd] 检查当前游戏状态');
  226. if (!this.inGameManager) {
  227. console.log('[GameEnd] InGameManager未初始化,无法检查游戏状态');
  228. return;
  229. }
  230. const currentState = this.inGameManager.getCurrentState();
  231. console.log('[GameEnd] 当前游戏状态:', currentState);
  232. // 如果游戏已经结束,主动处理奖励
  233. if (currentState === GameState.SUCCESS) {
  234. console.log('[GameEnd] 检测到游戏成功状态,主动处理奖励');
  235. this.isGameSuccess = true;
  236. this.calculateAndShowRewards();
  237. } else if (currentState === GameState.DEFEAT) {
  238. console.log('[GameEnd] 检测到游戏失败状态,主动处理奖励');
  239. this.isGameSuccess = false;
  240. this.calculateAndShowRewards();
  241. }
  242. }
  243. /**
  244. * 处理游戏成功事件
  245. * 统一处理游戏成功逻辑:状态切换 + UI显示 + 奖励计算
  246. */
  247. private onGameSuccess() {
  248. console.log('[GameEnd] 接收到GAME_SUCCESS事件');
  249. console.log('[GameEnd] 游戏成功,开始统一处理流程');
  250. // 1. 设置游戏状态为成功(如果还未设置)
  251. if (this.inGameManager && this.inGameManager.getCurrentState() !== GameState.SUCCESS) {
  252. this.inGameManager.setCurrentState(GameState.SUCCESS);
  253. console.log('[GameEnd] 已将游戏状态切换为SUCCESS');
  254. }
  255. // 2. 播放游戏成功音效
  256. Audio.playUISound('data/弹球音效/win');
  257. console.log('[GameEnd] 已播放游戏成功音效');
  258. // 3. 设置EndLabel文本
  259. this.setEndLabelText('SUCCESS');
  260. // 4. 显示UI面板
  261. this.showEndPanelWithAnimation();
  262. // 5. 计算和显示奖励
  263. this.isGameSuccess = true;
  264. this.calculateAndShowRewards();
  265. }
  266. /**
  267. * 处理游戏失败事件
  268. * 统一处理游戏失败逻辑:状态切换 + UI显示 + 奖励计算
  269. */
  270. private onGameDefeat() {
  271. console.log('[GameEnd] 接收到GAME_DEFEAT事件');
  272. // 1. 设置游戏状态为失败(如果还未设置)
  273. if (this.inGameManager && this.inGameManager.getCurrentState() !== GameState.DEFEAT) {
  274. this.inGameManager.setCurrentState(GameState.DEFEAT);
  275. console.log('[GameEnd] 已将游戏状态切换为DEFEAT');
  276. }
  277. // 2. 播放游戏失败音效
  278. Audio.playUISound('data/弹球音效/lose');
  279. console.log('[GameEnd] 已播放游戏失败音效');
  280. // 3. 设置EndLabel文本
  281. this.setEndLabelText('DEFEAT');
  282. // 4. 显示UI面板
  283. this.showEndPanelWithAnimation();
  284. // 5. 计算和显示奖励
  285. this.isGameSuccess = false;
  286. this.calculateAndShowRewards();
  287. }
  288. /**
  289. * 计算并显示奖励
  290. */
  291. private async calculateAndShowRewards() {
  292. console.log('[GameEnd] 开始计算并显示奖励');
  293. if (!this.saveDataManager) {
  294. console.error('[GameEnd] SaveDataManager未初始化');
  295. return;
  296. }
  297. const currentLevel = this.saveDataManager.getCurrentLevel();
  298. console.log(`[GameEnd] 当前关卡: ${currentLevel}, 游戏成功: ${this.isGameSuccess}`);
  299. try {
  300. if (this.isGameSuccess) {
  301. // 游戏成功,给予完整奖励
  302. console.log('[GameEnd] 准备给予成功奖励');
  303. await this.saveDataManager.giveCompletionRewards(currentLevel);
  304. console.log('[GameEnd] 已给予成功奖励');
  305. } else {
  306. // 游戏失败,给予按比例奖励
  307. const totalWaves = this.inGameManager?.levelWaves?.length || 1;
  308. const completedWaves = this.inGameManager ? Math.max(0, this.inGameManager.getCurrentWave() - 1) : 0;
  309. console.log(`[GameEnd] 准备给予失败奖励,完成波数: ${completedWaves}/${totalWaves}`);
  310. await this.saveDataManager.giveFailureRewards(currentLevel, completedWaves, totalWaves);
  311. console.log(`[GameEnd] 已给予失败奖励,完成波数: ${completedWaves}/${totalWaves}`);
  312. }
  313. // 获取奖励数据并显示
  314. this.currentRewards = this.saveDataManager.getLastRewards();
  315. console.log('[GameEnd] 获取到的奖励数据:', this.currentRewards);
  316. this.updateRewardDisplay();
  317. // 显示结算面板(通过动画)
  318. this.showEndPanelWithAnimation();
  319. } catch (error) {
  320. console.error('[GameEnd] 计算奖励时出错:', error);
  321. }
  322. }
  323. /**
  324. * 更新奖励显示
  325. */
  326. private updateRewardDisplay() {
  327. console.log(`[GameEnd] 开始更新奖励显示 - 钞票: ${this.currentRewards.money}, 钻石: ${this.currentRewards.diamonds}`);
  328. // 检查moneyLabel绑定状态
  329. if (this.moneyLabel) {
  330. this.moneyLabel.string = this.currentRewards.money.toString();
  331. console.log(`[GameEnd] 钞票标签已更新: ${this.currentRewards.money}`);
  332. } else {
  333. console.error('[GameEnd] moneyLabel未绑定!请在编辑器中将Canvas/GameEnd/ResourceNode/MoneyResourceNode/MoneyLabel拖拽到GameEnd组件的moneyLabel属性');
  334. }
  335. // 检查diamondLabel绑定状态
  336. if (this.diamondLabel) {
  337. this.diamondLabel.string = this.currentRewards.diamonds.toString();
  338. console.log(`[GameEnd] 钻石标签已更新: ${this.currentRewards.diamonds}`);
  339. } else {
  340. console.error('[GameEnd] diamondLabel未绑定!请在编辑器中将Canvas/GameEnd/ResourceNode/DiamondResourceNode/DiamondLabel拖拽到GameEnd组件的diamondLabel属性');
  341. }
  342. console.log(`[GameEnd] 奖励显示更新完成`);
  343. }
  344. /**
  345. * 显示结算面板(通过动画)
  346. */
  347. private showEndPanelWithAnimation() {
  348. // 重置双倍奖励状态
  349. this.hasDoubledReward = false;
  350. if (this.doubleButton) {
  351. this.doubleButton.interactable = true;
  352. }
  353. // 播放显示动画(如果有的话)
  354. if (this.animationDuration > 0) {
  355. this.playShowAnimation();
  356. }
  357. console.log('[GameEnd] 结算面板已通过动画显示');
  358. }
  359. /**
  360. * 播放显示动画
  361. */
  362. private playShowAnimation() {
  363. if (!this.node) return;
  364. // 设置节点位置到屏幕中心
  365. this.centerNodeOnScreen();
  366. // 设置初始状态
  367. this.node.setScale(0.5, 0.5, 1);
  368. const uiOpacity = this.node.getComponent(UIOpacity);
  369. if (uiOpacity) {
  370. uiOpacity.opacity = 0;
  371. }
  372. // 播放缩放和淡入动画
  373. tween(this.node)
  374. .to(this.animationDuration, {
  375. scale: new Vec3(1, 1, 1)
  376. }, {
  377. easing: 'backOut'
  378. })
  379. .start();
  380. if (uiOpacity) {
  381. tween(uiOpacity)
  382. .to(this.animationDuration, {
  383. opacity: 255
  384. })
  385. .start();
  386. }
  387. console.log('[GameEnd] 播放显示动画');
  388. }
  389. /**
  390. * 将节点居中到屏幕中央
  391. */
  392. private centerNodeOnScreen() {
  393. if (!this.node) return;
  394. // 获取Canvas节点
  395. const canvas = find('Canvas');
  396. if (!canvas) {
  397. console.warn('[GameEnd] 未找到Canvas节点');
  398. return;
  399. }
  400. // 设置位置为(0, 0),这在Canvas坐标系中是屏幕中心
  401. this.node.setPosition(0, 0, 0);
  402. console.log('[GameEnd] 已将面板居中到屏幕中央');
  403. }
  404. /**
  405. * 双倍按钮点击事件
  406. */
  407. private onDoubleButtonClick() {
  408. // 播放UI点击音效
  409. Audio.playUISound('data/弹球音效/ui play');
  410. if (this.hasDoubledReward) {
  411. console.log('[GameEnd] 已经获得过双倍奖励');
  412. return;
  413. }
  414. console.log('[GameEnd] 点击双倍奖励按钮');
  415. // 这里可以添加观看广告的逻辑
  416. // 暂时直接给予双倍奖励
  417. this.giveDoubleReward();
  418. }
  419. /**
  420. * 给予双倍奖励
  421. */
  422. private giveDoubleReward() {
  423. if (!this.saveDataManager || this.hasDoubledReward) {
  424. return;
  425. }
  426. // 计算双倍奖励
  427. const doubleMoney = this.currentRewards.money;
  428. const doubleDiamonds = this.currentRewards.diamonds;
  429. // 添加额外奖励到玩家账户
  430. if (doubleMoney > 0) {
  431. this.saveDataManager.addMoney(doubleMoney, 'double_reward');
  432. }
  433. if (doubleDiamonds > 0) {
  434. this.saveDataManager.addDiamonds(doubleDiamonds, 'double_reward');
  435. }
  436. // 更新当前奖励显示(显示双倍后的数值)
  437. this.currentRewards.money += doubleMoney;
  438. this.currentRewards.diamonds += doubleDiamonds;
  439. this.updateRewardDisplay();
  440. // 标记已获得双倍奖励
  441. this.hasDoubledReward = true;
  442. // 禁用双倍按钮
  443. if (this.doubleButton) {
  444. this.doubleButton.interactable = false;
  445. }
  446. console.log(`[GameEnd] 双倍奖励已给予 - 额外钞票: ${doubleMoney}, 额外钻石: ${doubleDiamonds}`);
  447. // 触发货币变化事件
  448. EventBus.getInstance().emit(GameEvents.CURRENCY_CHANGED);
  449. }
  450. /**
  451. * 继续按钮点击事件
  452. */
  453. private onContinueButtonClick() {
  454. // 播放UI点击音效
  455. Audio.playUISound('data/弹球音效/ui play');
  456. console.log('[GameEnd] 点击继续按钮');
  457. // 派发事件给MoneyAni播放奖励动画
  458. const rewards = this.saveDataManager.getLastRewards();
  459. console.log('[GameEnd] 派发奖励动画事件,奖励数据:', rewards);
  460. EventBus.getInstance().emit('PLAY_REWARD_ANIMATION', {
  461. money: rewards.money,
  462. diamonds: rewards.diamonds
  463. });
  464. // 触发返回主菜单事件
  465. EventBus.getInstance().emit('CONTINUE_CLICK');
  466. // 隐藏结算面板(通过动画)
  467. this.hideEndPanelWithAnimation();
  468. }
  469. /**
  470. * 隐藏结算面板(通过动画)
  471. */
  472. private async hideEndPanelWithAnimation() {
  473. // 播放隐藏动画
  474. await this.fadeOutWithScale();
  475. console.log('[GameEnd] 结算面板已通过动画隐藏');
  476. }
  477. /**
  478. * 重置UI状态
  479. */
  480. private onResetUI() {
  481. console.log('[GameEnd] 重置UI状态');
  482. this.hideEndPanelWithAnimation();
  483. this.hasDoubledReward = false;
  484. this.currentRewards = {money: 0, diamonds: 0};
  485. }
  486. /**
  487. * 设置EndLabel文本
  488. * @param text 要显示的文本内容
  489. */
  490. private setEndLabelText(text: string) {
  491. if (this.endLabel) {
  492. this.endLabel.string = text;
  493. console.log(`[GameEnd] 已设置EndLabel文本为: ${text}`);
  494. } else {
  495. // 如果endLabel未绑定,尝试通过路径查找
  496. const endLabelNode = find('Canvas/GameEnd/Sprite/EndLabel');
  497. if (endLabelNode) {
  498. const labelComponent = endLabelNode.getComponent(Label);
  499. if (labelComponent) {
  500. labelComponent.string = text;
  501. console.log(`[GameEnd] 通过路径查找设置EndLabel文本为: ${text}`);
  502. } else {
  503. console.warn('[GameEnd] 找到EndLabel节点但无Label组件');
  504. }
  505. } else {
  506. console.warn('[GameEnd] 未找到EndLabel节点,请检查路径: Canvas/GameEnd/Sprite/EndLabel');
  507. }
  508. }
  509. }
  510. /**
  511. * 获取当前奖励信息(用于外部查询)
  512. */
  513. public getCurrentRewards(): {money: number, diamonds: number} {
  514. return {...this.currentRewards};
  515. }
  516. /**
  517. * 检查是否已获得双倍奖励
  518. */
  519. public hasGotDoubleReward(): boolean {
  520. return this.hasDoubledReward;
  521. }
  522. // === 动画方法 ===
  523. /**
  524. * 初始化为隐藏状态
  525. */
  526. private initializeHiddenState() {
  527. // 设置初始透明度为0
  528. if (this.node) {
  529. let uiOpacity = this.node.getComponent(UIOpacity);
  530. if (!uiOpacity) {
  531. uiOpacity = this.node.addComponent(UIOpacity);
  532. }
  533. uiOpacity.opacity = 0;
  534. }
  535. // 设置初始缩放为0
  536. if (this.node) {
  537. this.node.setScale(0, 0, 1);
  538. }
  539. console.log('[GameEnd] 初始化为隐藏状态');
  540. }
  541. /**
  542. * 淡入动画
  543. */
  544. public fadeIn(target?: Node, duration?: number): Promise<void> {
  545. const animTarget = target || this.node;
  546. const animDuration = duration !== undefined ? duration : this.animationDuration;
  547. if (!animTarget) {
  548. console.warn('[GameEnd] fadeIn: 未指定目标节点');
  549. return Promise.resolve();
  550. }
  551. return new Promise<void>((resolve) => {
  552. let uiOpacity = animTarget.getComponent(UIOpacity);
  553. if (!uiOpacity) {
  554. uiOpacity = animTarget.addComponent(UIOpacity);
  555. }
  556. Tween.stopAllByTarget(uiOpacity);
  557. uiOpacity.opacity = 0;
  558. tween(uiOpacity)
  559. .to(animDuration, { opacity: 255 }, { easing: 'quadOut' })
  560. .call(() => {
  561. resolve();
  562. })
  563. .start();
  564. });
  565. }
  566. /**
  567. * 淡出动画
  568. */
  569. public fadeOut(target?: Node, duration?: number): Promise<void> {
  570. const animTarget = target || this.node;
  571. const animDuration = duration !== undefined ? duration : this.animationDuration;
  572. if (!animTarget) {
  573. console.warn('[GameEnd] fadeOut: 未指定目标节点');
  574. return Promise.resolve();
  575. }
  576. return new Promise<void>((resolve) => {
  577. let uiOpacity = animTarget.getComponent(UIOpacity);
  578. if (!uiOpacity) {
  579. uiOpacity = animTarget.addComponent(UIOpacity);
  580. }
  581. Tween.stopAllByTarget(uiOpacity);
  582. tween(uiOpacity)
  583. .to(animDuration, { opacity: 0 }, { easing: 'quadIn' })
  584. .call(() => {
  585. resolve();
  586. })
  587. .start();
  588. });
  589. }
  590. /**
  591. * 缩放弹出动画
  592. */
  593. public scalePopIn(target?: Node, duration?: number): Promise<void> {
  594. const animTarget = target || this.node;
  595. const animDuration = duration !== undefined ? duration : this.animationDuration;
  596. if (!animTarget) {
  597. console.warn('[GameEnd] scalePopIn: 未指定目标节点');
  598. return Promise.resolve();
  599. }
  600. return new Promise<void>((resolve) => {
  601. Tween.stopAllByTarget(animTarget);
  602. animTarget.setScale(0, 0, 1);
  603. tween(animTarget)
  604. .to(animDuration, { scale: this._originalScale }, { easing: 'backOut' })
  605. .call(() => {
  606. resolve();
  607. })
  608. .start();
  609. });
  610. }
  611. /**
  612. * 缩放收缩动画
  613. */
  614. public scalePopOut(target?: Node, duration?: number): Promise<void> {
  615. const animTarget = target || this.node;
  616. const animDuration = duration !== undefined ? duration : this.animationDuration;
  617. if (!animTarget) {
  618. console.warn('[GameEnd] scalePopOut: 未指定目标节点');
  619. return Promise.resolve();
  620. }
  621. return new Promise<void>((resolve) => {
  622. Tween.stopAllByTarget(animTarget);
  623. tween(animTarget)
  624. .to(animDuration, { scale: new Vec3(0, 0, 1) }, { easing: 'backIn' })
  625. .call(() => {
  626. resolve();
  627. })
  628. .start();
  629. });
  630. }
  631. /**
  632. * 组合动画:淡入 + 缩放弹出
  633. */
  634. public async fadeInWithScale(fadeTarget?: Node, scaleTarget?: Node, duration?: number): Promise<void> {
  635. const promises: Promise<void>[] = [];
  636. if (fadeTarget || this.node) {
  637. promises.push(this.fadeIn(fadeTarget, duration));
  638. }
  639. if (scaleTarget || this.node) {
  640. promises.push(this.scalePopIn(scaleTarget, duration));
  641. }
  642. await Promise.all(promises);
  643. }
  644. /**
  645. * 组合动画:淡出 + 缩放收缩
  646. */
  647. public async fadeOutWithScale(fadeTarget?: Node, scaleTarget?: Node, duration?: number): Promise<void> {
  648. const promises: Promise<void>[] = [];
  649. if (fadeTarget || this.node) {
  650. promises.push(this.fadeOut(fadeTarget, duration));
  651. }
  652. if (scaleTarget || this.node) {
  653. promises.push(this.scalePopOut(scaleTarget, duration));
  654. }
  655. await Promise.all(promises);
  656. }
  657. /**
  658. * 停止所有动画
  659. */
  660. public stopAllAnimations() {
  661. if (this.node) {
  662. const uiOpacity = this.node.getComponent(UIOpacity);
  663. if (uiOpacity) {
  664. Tween.stopAllByTarget(uiOpacity);
  665. }
  666. Tween.stopAllByTarget(this.node);
  667. }
  668. }
  669. /**
  670. * 重置所有目标节点到初始状态
  671. */
  672. public resetToInitialState() {
  673. this.stopAllAnimations();
  674. if (this.node) {
  675. let uiOpacity = this.node.getComponent(UIOpacity);
  676. if (uiOpacity) {
  677. uiOpacity.opacity = 255;
  678. }
  679. this.node.setScale(this._originalScale);
  680. }
  681. }
  682. onDisable() {
  683. console.log('[GameEnd] onDisable方法被调用,节点已禁用');
  684. // 停止所有动画
  685. this.stopAllAnimations();
  686. // 清理事件监听
  687. const eventBus = EventBus.getInstance();
  688. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccess, this);
  689. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeat, this);
  690. eventBus.off(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  691. console.log('[GameEnd] 事件监听器已清理');
  692. }
  693. protected onDestroy() {
  694. // 停止所有动画
  695. this.stopAllAnimations();
  696. // 清理事件监听
  697. const eventBus = EventBus.getInstance();
  698. eventBus.off(GameEvents.GAME_SUCCESS, this.onGameSuccess, this);
  699. eventBus.off(GameEvents.GAME_DEFEAT, this.onGameDefeat, this);
  700. eventBus.off(GameEvents.RESET_UI_STATES, this.onResetUI, this);
  701. // 清理按钮事件
  702. if (this.doubleButton) {
  703. this.doubleButton.node.off(Button.EventType.CLICK, this.onDoubleButtonClick, this);
  704. }
  705. if (this.continueButton) {
  706. this.continueButton.node.off(Button.EventType.CLICK, this.onContinueButtonClick, this);
  707. }
  708. }
  709. }