import { _decorator, Component, Node, Button, Sprite, tween, Vec3, Label } from 'cc'; import { Http } from '../core/net/Http'; import { OpenWindow } from '../core/ui/window/OpenWindow'; import { WindowManager } from '../core/ui/window/WindowManager'; import { WindowOpenMode } from '../core/ui/window/WindowOpenMode'; import { Utils } from '../core/utils/Utils'; import { g } from '../Data/g'; import { HttpErrorCode } from '../Data/HttpErrorCode'; import { platform } from '../Data/platform'; import { GetType } from '../UI/GetWindow/GetType'; const { ccclass, property } = _decorator; @ccclass('TurntableManage') export class TurntableManage extends Component { @property({ type: Node }) public turntable: Node = null; @property({ type: Sprite }) public startButton: Sprite = null; @property({ type: Sprite }) public closeButton: Sprite = null; @property({ type: Http }) public http: Http = null; @property({ type: Label }) public lastLabel: Label = null; private openWindowCom: OpenWindow = null; /** * 奖品ID:资源映射 */ private resourceMap: Map; /** * ID:角度映射 */ private idMap: Map; private _times: number = 0; private nextWindows: string[]; set times(times: number) { this._times = times; this.lastLabel.string = this._times + ""; } async start() { this.idMap = new Map(); this.idMap.set(1, 0); this.idMap.set(2, 300); this.idMap.set(3, 240); this.idMap.set(4, 60); this.idMap.set(5, 120); this.idMap.set(6, 180); this.resourceMap = new Map(); this.resourceMap.set(1, GetType.RedBag); this.resourceMap.set(2, GetType.Diamond); this.resourceMap.set(3, GetType.RedBag); this.resourceMap.set(4, GetType.exp); this.resourceMap.set(5, GetType.Diamond); this.resourceMap.set(6, GetType.exp); await this.queryLast(); this.openWindowCom = this.node.parent.getComponent(OpenWindow); } /** * 视频播放完成开始抽奖 */ async onVideoOver() { //请求服务器获取的奖品ID,奖品数量 let data = await this.http.send("/api/turntable/turntable", { adData: g.adData }); if (data.code == 0) { platform.reportThinking("ad_end", JSON.stringify({ ad_id: "turntable", ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel(), award: data.data.value })); platform.umUp("turntable");//转盘埋点 this.startTween(data.data.id, data.data.value); } } /** * 查询剩余次数 */ async queryLast() { let data = await this.http.send("/api/turntable/GetTurntableTimes"); if (data.code == 0) { this.times = Number(data.data); } } /** * 关闭界面 */ // onClose() { // // this.node.parent.destroy(); // } /** * 接收参数 */ onParam(params: any[]) { this.nextWindows = params; } onClosing(closeCallback: () => void) { closeCallback(); if (this.nextWindows && this.nextWindows.length) { WindowManager.open(this.nextWindows.shift(), WindowOpenMode.NotCloseAndAdd, this.nextWindows); } } /** * 观看视频 */ async onVideo() { let applyReturn = await this.http.send("/api/ad/watchVideoAD"); //可以观看视频 if (applyReturn.code == 0) { platform.reportThinking("ad_init", JSON.stringify({ ad_id: "turntable", ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel() })); //播放视频 let adData = await g.showRewardVideo(); if (adData) { this.onVideoOver(); } else { //视频观看失败 WindowManager.showTips("视频观看时间不足"); } } else if (applyReturn.code == 106) { WindowManager.showTips("今日视频次数已用完,请明日再继续"); } else if (applyReturn.code == HttpErrorCode.VideoADCD) { WindowManager.showTips("请求频繁,请稍后再试"); } else { WindowManager.showTips("视频准备中,请稍后再试"); } } /** * 开始转盘 * @param angle 奖品角度 * @param prizedNum 奖励数 */ private isTurnning = false; private startTween(prizeId: number, prizedNum: string) { this.startButton.getComponent(Button).interactable = false; this.closeButton.getComponent(Button).interactable = false; let angle = this.idMap.get(prizeId); this.isTurnning = true; tween(this.turntable) .call(() => { this.turntable.eulerAngles = new Vec3(this.turntable.eulerAngles.x, this.turntable.eulerAngles.y, this.turntable.eulerAngles.z % 360); }) .to(1, { eulerAngles: new Vec3(this.turntable.eulerAngles.x, this.turntable.eulerAngles.y, -360) }, { easing: "cubicIn" }) .to(4, { eulerAngles: new Vec3(this.turntable.eulerAngles.x, this.turntable.eulerAngles.y, -1800 + angle) }, { easing: "cubicOut" }) .call(() => { if (prizeId == 1 || prizeId == 3) { platform.reportThinking("envelope_increase", JSON.stringify({ previous_number: g.userData.bonus, increase_number: prizedNum, current_number: g.userData.bonus + prizedNum, reasons: "turntable" })); } else if (prizeId == 2 || prizeId == 5) { platform.reportThinking("diamond_increase", JSON.stringify({ previous_number: g.userData.bonus, increase_number: prizedNum, current_number: g.userData.bonus + prizedNum, reasons: "turntable" })); } this.openWindowCom.open([{ count: prizedNum, type: this.resourceMap.get(prizeId), needBezierEffect: true }], this.node); console.log(this._times); this.times = --this._times; this.startButton.getComponent(Button).interactable = true; this.closeButton.getComponent(Button).interactable = true; this.isTurnning = false; }).start(); } update() { if (this._times == 0) { this.startButton.grayscale = true; this.startButton.getComponent(Button).interactable = false; } else { if (!this.isTurnning) { this.startButton.grayscale = false; this.startButton.getComponent(Button).interactable = true; } } } }