import { _decorator, Color, Component, director, Label, Node, Sprite, tween, UITransform, Vec3, } from "cc"; import { EventDispatcher } from "../libs/EventDispatcher"; import { GlobalData } from "../config/GlobalData"; import AudioEffect from "../libs/AudioEffect"; const { ccclass, property } = _decorator; @ccclass("ResultModal") export class ResultModal extends Component { // 本局得分的 label @property(Label) scoreLabel: Label = null; // 最佳得分 label @property(Label) bestScoreLabel: Label = null; // 总的金币数 label @property(Label) totalScoreLabel: Label = null; // 回到首页的按钮节点 @property(Node) backToHomeBtn: Node = null; // 提示节点 @property(Node) tipNode: Node = null; start() { // 隐藏结果弹窗 this.node.active = false; // 设置位置 this.node.setPosition(0, 0); // 注册打开结果弹窗的事件 EventDispatcher.getTarget().on( EventDispatcher.SHOW_RESULT_MODAL, this.openModal, this, ); } // 打开结果弹窗 openModal() { // 显示结果弹窗 this.node.active = true; // 设置当前得分 this.scoreLabel.string = GlobalData.curRoundTotalScore.toString(); // 累计总的金币数 GlobalData.totalScore += GlobalData.curRoundTotalScore; // 设置总的金币数 this.setTotalScoreLabel(); // 设置当前用户最好的得分成绩 this.setCurUserBestScoreLabel(); // 执行相关动效 this.scheduleOnce(async () => { await this.playGoldEffect(); // 再执行提示信息的动画 this.playTipAni(); // 显示回到首页的按钮 this.backToHomeBtn.active = true; }, 1); } // 关闭游戏结束弹窗方法 closeModal() { this.node.active = false; // 隐藏回到首页的按钮 this.backToHomeBtn.active = false; // 重置分数 this.scoreLabel.string = "0"; } setTotalScoreLabel() { this.totalScoreLabel.string = GlobalData.totalScore.toString(); } setCurUserBestScoreLabel() { const bestScore = GlobalData.bestScore; // 如果当前成绩大于最好成绩 更新最好成绩 if (GlobalData.curRoundTotalScore > bestScore) { GlobalData.bestScore = GlobalData.curRoundTotalScore; } // 设置最佳得分 label this.bestScoreLabel.string = GlobalData.bestScore.toString(); } setBestScoreLabel() {} // 播放金币特效 async playGoldEffect() { // 获取 gold coins 节点 const goldCoinsNode = this.node .getChildByName("panel") .getChildByName("gold_coins"); // 开启此节点 goldCoinsNode.active = true; // 播放撒金币音效 AudioEffect.playGoldAudio(); // 0.5 s 后执行入账音效 this.scheduleOnce(() => { AudioEffect.playCoinsEntryAudio(); }, 0.5); // 获取所有金币子节点 const goldCoinsList = goldCoinsNode.children; // 获取显示金币数量的标签节点 const goldCoinsNumNode = this.node .getChildByName("gold_label") .getChildByName("gold_num"); // 获取目标节点的位置 const targetPos = goldCoinsNumNode.getWorldPosition(); // 遍历金币子节点 for (let i = 0; i < goldCoinsList.length; i++) { // 克隆位置 const targetPosColne = targetPos.clone(); // 执行单个金币的动画 const goldCoin = goldCoinsList[i]; await this.playSingleGoldAni(goldCoin, targetPosColne); } } // 执行单个金币的动画 async playSingleGoldAni(goldCoin: Node, targetPos: Vec3) { return new Promise((resolve) => { tween(goldCoin) .by(0.2, { position: new Vec3(0, -30, 0), }) .call(() => { // 开始执行下一个 resolve(true); // 获取金币的世界坐标 const goleCoinWorldPos = goldCoin.getWorldPosition(); // 计算金币节点与目标距离的差值 const diffPost = targetPos.subtract(goleCoinWorldPos); // 再缓动到目标位置 tween(goldCoin) .by(0.5, { position: diffPost }) .call(() => { // 金币消失 goldCoin.active = false; }) .start(); }) .start(); }); } // 执行提示动画 playTipAni() { // 设置提示 dino 颜色 const dinoInfo = GlobalData.currentDinoInfo; const dinoColor = new Color(dinoInfo.dinoColor); // 设置提示 dino 颜色 this.tipNode.getChildByName("dino").getComponent(Sprite).color = dinoColor; // 开启提示 this.tipNode.active = true; // 获取提示的位置 const oriPos = this.tipNode.getPosition().clone(); // 获取提示的宽度 const tipWidth = this.tipNode.getComponent(UITransform).width + 5; // 执行动画 tween(this.tipNode) .to(0.3, { position: new Vec3(oriPos.x - tipWidth, oriPos.y, 0), }) .delay(0.8) .to(0.3, { position: oriPos, }) .call(() => { // 隐藏提示1 this.tipNode.active = false; // 重置节点位置 this.tipNode.setPosition(oriPos); }) .start(); } // 回到首页 backToHome() { // 播放点击音效 AudioEffect.playClickAudio(); director.loadScene("home"); } update(deltaTime: number) {} }