import { _decorator, Component, Node, Button, tween, Vec3, JsonAsset, Sprite, RichText, Animation, Vec4, color, director } from 'cc';
import { RewardVideoSystem } from '../ad/RewardVideoSystem';
import { DataSystem } from '../core/data/DataSystem';
import { Http } from '../core/net/Http';
import { HttpSystem } from '../core/net/HttpSystem';
import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
import { Sound } from '../core/sound/Sound';
import { SoundSystem } from '../core/sound/SoundSystem';
import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
import { WindowSystem } from '../core/ui/window/WindowSystem';
import { HttpErrorCode } from '../data/HttpErrorCode';
import { platform } from '../data/jsb/platform';
import { UserData } from '../data/UserData';
import { RedPointData } from '../main/RedPointData';
import { ReportThinking } from '../ReportThinking';
const { ccclass, property, requireComponent } = _decorator;
@ccclass('Draw')
@requireComponent(Http)
export class Draw extends Component {
@property({ tooltip: "抽奖按钮", type: Node }) public drawBtn: Node;
@property({ tooltip: "规则按钮", type: Node }) public ruleBtn: Node;
@property({ tooltip: "关闭按钮", type: Node }) public closeBtn: Node;
@property({ tooltip: "转盘", type: Node }) public turntable: Node;
@property({ tooltip: "免费抽奖按钮文本", type: Node }) public freeBtnShow: Node;
@property({ tooltip: "视频抽奖按钮文本", type: Node }) public vidoeBtnShow: Node;
@property({ tooltip: "剩余次数文本", type: RichText }) public times: RichText;
@property({ tooltip: "跑马灯动画", type: Animation }) public turnLightAnim: Animation;
@property({ tooltip: "中奖动画", type: Animation }) public winAnim: Animation;
@property({ tooltip: "中奖选框", type: Sprite }) public winChooseBox: Sprite;
@property({ tooltip: "音效组件", type: Sound }) public sound: Sound;
private freeTimes = 0;//免费抽奖次数
private videoTimes = 0;//视频抽奖次数
private turntableConfig;//转盘配置
private rotaryRotation = 0;//角度
private winId = 0;//中奖id
private prizeNum = 0;//中奖数目
start() {
}
public setDrawTimes(freeTimes: number, videoTimes: number, config: any) {
this.freeTimes = freeTimes;
this.videoTimes = videoTimes;
this.turntableConfig = config;
this.rotaryRotation = 360 / config.length;
videoTimes && (this.drawBtn.getComponent(Button).interactable = true);
}
private async toGetVideo() {
let result = await this.getComponent(Http).send("/api/ad/watchVideoAD");
if (result && result.code == 0) {//可以观看视频
ReportThinking.ad_init('turntable');
let adData = await RewardVideoSystem.show();
if (adData) {//视频观看成功
let _result = await this.getComponent(Http).send("/api/draw/draw", { free: false, adData: adData.obj });
if (_result && _result.code == 0) {
this.prizeNum = _result.data.num;
ReportThinking.ad_end('turntable', this.turntableConfig[_result.data.index - 1].item == 3 ? this.prizeNum : 0);
this.rotaryTable(_result.data.index);
} else {//返回错误信息
if (_result && _result.code == HttpErrorCode.VideoADCD) {
WindowSystem.showTips('视频观看时间不足');
} else {
WindowSystem.showTips('抽奖失败');
}
this.initBtn(false);
}
} else {//视频错误
WindowSystem.showTips('视频观看时间不足');
this.initBtn(false);
}
} else {//返回错误,无法观看视频
WindowSystem.showTips('视频次数不足');
this.initBtn(false);
}
}
//抽奖
private async draw() {
this.initBtn(true);
if (this.freeTimes) {
let result = await this.getComponent(Http).send("/api/draw/draw", { free: true });
if (result && result.code == 0) {
ReportThinking.turn(result.data.index, result.data.num);
this.prizeNum = result.data.num;
this.rotaryTable(result.data.index);
} else {//返回错误信息
WindowSystem.showTips('抽奖失败');
this.initBtn(false);
}
return;
}
if (this.videoTimes) {
this.toGetVideo();
return;
}
}
private rotaryTable(index: number) {
this.winId = index;
this.drawBtn.getComponent(Button).interactable = false;
// this.turnLightAnim.play();
let t = tween(this.turntable);
this.turntable.eulerAngles = new Vec3(this.turntable.eulerAngles.x, this.turntable.eulerAngles.y, this.turntable.eulerAngles.z % 360);
t.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 + (this.rotaryRotation * (index - 1))) }, { easing: "cubicOut" })
.call(() => {
this.sound.play(1);
this.winAnim.play();//播放中奖动画
})
.start();
this.updateTimes();
}
//中奖动画结束回调
public winAnimBack() {
this.winChooseBox.color = color(255, 255, 255, 255);
this.scheduleOnce(() => {
// this.turnLightAnim.stop();
this.showPrize();
}, 0.5);
}
//展示奖品
public async showPrize() {
let pName = '';
switch (this.turntableConfig[this.winId - 1].item) {
case 1://粮草
pName = 'money';
break;
case 2://元宝
pName = 'diamond';
break;
case 3://红包币
pName = 'bonus';
break;
case 4:
pName = 'prestige';
break;
}
let userData = DataSystem.getData(UserData);
ReportThinking.currency_increase(pName, userData[pName], this.prizeNum, this.prizeNum + userData[pName], 'turntable');
await WindowSystem.open("prefabs/ui/turntable/turntablePrizePopup", WindowOpenMode.NotCloseAndCover,
[{ icon: this.turntableConfig[this.winId - 1].icon, type: this.turntableConfig[this.winId - 1].item, num: this.prizeNum }]);
this.prizeNum = 0;//重置奖品数目
this.winChooseBox.color = color(255, 255, 255, 0);
this.updataUI();
}
//更新转盘剩余次数
public updateTimes() {
if (this.freeTimes) {
this.freeTimes--;
} else {
this.videoTimes--;
}
this.times.string = "剩余次数:" + (this.freeTimes + this.videoTimes) + "";
DataSystem.getData(RedPointData).canDraw = this.freeTimes + this.videoTimes > 0;
}
//ui刷新
private updataUI() {
this.initBtn(false);
if (!this.freeTimes) {
this.freeBtnShow.active = false;
this.vidoeBtnShow.active = true;
}
if (!this.videoTimes) {
this.drawBtn.getComponent(Button).interactable = false;//按键事件禁用
this.drawBtn.getComponent(Sprite).grayscale = true;//按键置灰
}
}
private initBtn(isDraw: boolean) {
this.drawBtn.getComponent(Button).interactable = !isDraw;//按键事件
this.drawBtn.getComponent(Sprite).grayscale = isDraw;//按键置灰
this.ruleBtn.active = !isDraw;
this.closeBtn.active = !isDraw;
}
private turntableRotation = 0;
update() {
if (this.turntableRotation != (Math.round(this.turntable.eulerAngles.z / 10) * 10) && (Math.round(this.turntable.eulerAngles.z / 10) * 10) % 20 == 0) {
this.turntableRotation = (Math.round(this.turntable.eulerAngles.z / 10) * 10);
// console.log("角度:" + this.turntable.eulerAngles.z);
// console.log("转盘音效");
this.sound.play(0);
}
}
}