import { _decorator, Component, Label, Node, Sprite, v3, SpriteFrame } from 'cc'; import { UI_Dialog } from '../UI_Dialog'; import { aa, UIController, UIElement } from 'db://assets/scripts/aa'; import { rootMgr } from '../../../scene/RootMgr'; import { rotate_anim_ext, rotate_anim_stop } from '../../../util/Customize_Ani'; import { RewardData, RewardItem } from '../../../data/RewardData'; import { RewardDataManager } from '../../../data/RewardDataManager'; import { Layout_Win } from 'db://assets/dialog/Win/Layout_Win'; import { ItemKey, ItemService } from '../../../data/Item/ItemService'; import { Notify } from '../../../scene/GameConfing'; import { BgmName } from 'db://assets/scripts/Audio/BgmName'; import { audioManager } from 'db://assets/scripts/Audio/AudioManager'; const { ccclass, property } = _decorator; // 记录指针为 0 的位置 const round = { start: -276, s1: -176 + 276, s2: -43 + 276, s3: 31 + 276, s4: 163 + 276, s5: 276 + 276, } const roundLen = [ { value: 2, len: 0, num: 90 }, { value: 3, len: round.s1, num: 135 }, { value: 5, len: round.s2, num: 225 }, { value: 3, len: round.s3, num: 135 }, { value: 2, len: round.s4, num: 90 }] @UIController({ bundleName: 'dialog', path: 'Win/win' }) export class UI_Win extends UI_Dialog { @UIElement(Node) contineBtn: Node @UIElement(Node) shareBtn: Node @UIElement(Node) useCoinBtn: Node @UIElement(Node) needle: Node//指针 @UIElement(Node) light: Node @UIElement(Sprite) cocoTea: Sprite @UIElement(Label) cionLabel: Label @UIElement(Label) winLabel: Label @UIElement(Sprite) bottel: Sprite // 奖励图片数组 protected onCreated(): void { super.onCreated() audioManager.playOneShot(BgmName.win) this.contineBtn.on(Node.EventType.TOUCH_END, this.playContine, this) this.shareBtn.on(Node.EventType.TOUCH_END, this.onShareBtn, this) this.useCoinBtn.on(Node.EventType.TOUCH_END, this.onUseCoinBtnClick, this) this.bottel.node.active = false this.lightAnim() // 显示当前关卡的奖励 this.showStageReward(); } private showStageReward() { const rewards = RewardDataManager.instance.getAllRewards(); // 找出第一个未解锁的奖励(按数组顺序) let reward: RewardItem | null = null; for (let i = 0; i < rewards.length; i++) { if (!rewards[i].unlocked) { reward = rewards[i]; break; } } if (!reward) { this.cocoTea.node.active = false; this.winLabel.string = "恭喜通过本关" this.bottel.node.active=true return; } // 所有奖励都已解锁 // 解锁奖励 RewardDataManager.instance.unlockReward(reward.id); RewardDataManager.instance.saveRewards(); // 显示奖励图片 if (this.cocoTea) { const layoutWin = this.node.getComponent(Layout_Win); this.cocoTea.node.active = true; this.cocoTea.spriteFrame = layoutWin.rewardSprites[reward.id]; this.winLabel.string = "恭喜获得" + reward.name; } rootMgr.game.evt.emit('rewardUnlocked'); } // 通知收藏面板更新 notifyCollectPanel(reward: RewardItem) { // 发送事件通知收藏面板更新 rootMgr.game.evt.emit('rewardUnlocked'); } len: number = 0 speed: number = 500 * 1.5 stop = false protected onUpdate(dt: number): void { if (this.stop) return // 指针移动逻辑 let lenAll = round.s5 this.len += this.speed * dt this.len = this.len % (2 * (lenAll)) let endx = this.len > lenAll ? round.start + 2 * lenAll - this.len : round.start + this.len this.needle.position = v3(endx, this.needle.position.y, 0) // 更新金币显示 this.updateCoinLabel(); } private updateCoinLabel() { if (!this.cionLabel) return; // 计算当前指针位置对应的奖励值 const currentReward = this.getCurrentRewardValue(); // 更新金币 this.cionLabel.string = currentReward.toString(); } // 获取当前指针位置的奖励值 private getCurrentRewardValue(): number { const pointerX = this.needle.position.x; let rewardValue = 0; // 默认值 // 查找指针所在的奖励区间 for (let i = roundLen.length - 1; i >= 0; i--) { const section = roundLen[i]; const sectionStart = round.start + section.len; if (pointerX >= sectionStart) { rewardValue = section.num; break; } } return rewardValue; } playContine() { // this.stop = true // for (let i = roundLen.length - 1; i >= 0; i--) { // let r = roundLen[i] // if (this.needle.position.x >= r.len + round.start) { // // debugger // // alert(r.value) // break // } // } const currentReward = 45 rootMgr.dataControl.getCompent(ItemService).getRewarded(ItemKey.coin, currentReward); rootMgr.game.evt.emit('save');//保存 rootMgr.game.evt.emit('toNextStage') this.close() } onUseCoinBtnClick() { // 获取当前指针位置对应的奖励值 const currentReward = this.getCurrentRewardValue(); // 观看广告 const adSuccess = aa.sdk.playRewardAd('金币翻倍奖励'); if (adSuccess) { // 广告观看成功奖励金币 rootMgr.dataControl.getCompent(ItemService).getRewarded(ItemKey.coin, currentReward); rootMgr.game.evt.emit('save');//保存 Notify("金币获取成功") console.log(`恭喜获得${currentReward}金币`); } rootMgr.game.evt.emit('toNextStage')//下一关 this.close() } onShareBtn() { aa.sdk.shareAppMessageAsync(); console.log("分享"); } lightAnim() { rotate_anim_ext(this.light, 'loop', { duration: 3, // 每圈旋转时间(秒) angle: 360, // 每圈旋转角度(360度) }); } onClose() { rotate_anim_stop(this.light); // 停止旋转动画 super.onClose(); } }