import BtnClosePanel from "../../component/BtnClosePanel";
import SetGray from "../../component/SetGray";
import { AdFun } from "../../data/AdData";
import { VideoAdType } from "../../data/GameData";
import TurnableItem from "./TurnableItem";
const { ccclass, property } = cc._decorator;
/**
* 转盘
* @author 邹勇 kaka
*/
@ccclass
export default class Turnable extends cc.Component {
@property(cc.Node)
node_turn: cc.Node = null;
@property(cc.Node)
node_items: cc.Node[] = [];
@property(SetGray)
btn_draw: SetGray = null;
@property(cc.Sprite)
btn_close: cc.Sprite = null;
@property(cc.RichText)
lbl_left: cc.RichText = null;
@property(cc.ParticleSystem)
starAni: cc.ParticleSystem = null;
@property(sp.Skeleton)
baozhaAni: sp.Skeleton = null;
@property(cc.RichText)
private lbl_probability: cc.RichText = null;
@property(cc.Node)
private node_point: cc.Node = null;
private audioId: number;
private endAngle = 0
private startDraw = false
private slowDis = 720
private slowAngle = 0
private perAdd = 10
private perMinus = 0
private rewardWeights: number[];
private act_rotate: cc.ActionInterval = null;
start() {
this.baozhaAni.node.active = false;
this.node_point.active = false;
this.initData();
this.act_rotate = cc.repeatForever(cc.rotateBy(16, 360));
this.node_turn.runAction(this.act_rotate);
}
/**
* 初始化奖励和抽奖次数
*/
private initData() {
this.rewardWeights = [0, 0, 0];
if (gData.turnable.config) {
for (let i = 0; i < gData.turnable.config.length; i++) {
let sc = this.node_items[i].getComponent(TurnableItem);
let type = parseInt(gData.turnable.config[i].type);
this.rewardWeights[type - 1]++;
sc.initData(type, i);
}
}
gData.turnable.requestData();
this.updateLeftTimes();
}
/** 点击抽奖 */
clickDraw() {
if (gData.turnable.leftTimes > 0) {
mk.ad.videoAdType = VideoAdType.video_init_5;
mk.ad.watchAd((success: boolean) => {
mk.console.log("watchAD:" + success);
if (success) {
gData.adData.watchVideo(AdFun.turntable);
//gData.gameData.popTableScreenADLogic(14);
}
});
}
}
private async play(type) {
this.audioId = await mk.audio.playEffect("turnableplay", true);
this.btn_close.getComponent(BtnClosePanel).block_click = true;
//停止转动动作
if (this.act_rotate) {
this.node_turn.stopAction(this.act_rotate);
this.act_rotate = null;
}
this.node_point.active = true;
this.perAdd = 10;
this.node_turn.angle += 360 * 5;
let index = 0;
let i = 0;
while (i < this.rewardWeights.length - 1) {
if (i < type - 1) {
index += this.rewardWeights[i];
i++;
}
else {
index += Math.floor(Math.random() * this.rewardWeights[i]);
break;
}
}
this.endAngle = index * -60;
this.slowAngle = this.endAngle + this.slowDis;
this.perMinus = this.perAdd / (this.slowDis / (this.perAdd * 0.5));
this.startDraw = true;
this.starAni.resetSystem()
this.starAni.node.active = true
// this.node_turn.angle = 1800;
// let tar = index * 60;
// cc.tween(this.node_turn)
// .to(3, { angle: tar })
// .call(this.playOver, this).start();
}
private async playOver() {
mk.audio.stopEffect(this.audioId);
this.btn_close.getComponent(BtnClosePanel).block_click = false;
mk.audio.playEffect("turnplateDrawEnd");
this.updateLeftTimes();
this.node_point.runAction(cc.blink(0.7, 3));
await mk.time.WaitForSeconds(0.7);
mk.ui.openPanel("module/reward/reward");
mk.data.setTAEventUser(1, 'turntable_total', 1);
}
updateLeftTimes() {
this.lbl_left.string = `剩余:${gData.turnable.leftTimes}次`;
if (gData.turnable.leftTimes <= 0) {
this.btn_draw.setGray(true)
}
//已抽奖次数
let times = 10 - gData.turnable.leftTimes;
if (times == 0)
this.lbl_probability.string = `当前概率: 45%`;
else {
if (times >= 10) {
times = 9;
}
let rate = times * 5 + 45;
//this.lbl_probability.string = `当前概率: 5%+${rate}%`;
this.lbl_probability.string = `当前概率: ${rate}%`;
}
}
update() {
if (gData.turnable.requestFinish) {
this.updateLeftTimes();
gData.turnable.requestFinish = false;
}
if (gData.turnable.adData) {
if (gData.turnable.adData && gData.turnable.adData.videoRedMoney) {
let type = gData.turnable.adData.videoRedMoney.redMoneyType;
this.play(type);
gData.reward.data = gData.turnable.adData.videoRedMoney.videoRewardList;
}
gData.turnable.adData = null;
}
if (this.startDraw) {
this.node_turn.angle -= this.perAdd;
//开始减速
if (this.node_turn.angle <= this.slowAngle) {
if (this.perAdd <= this.perMinus * 4) {
this.perAdd -= this.perMinus * 0.01;
}
else {
this.perAdd -= this.perMinus;
}
}
if (this.node_turn.angle <= this.endAngle) {
this.startDraw = false;
this.starAni.node.active = false
this.baozhaAni.node.active = true
this.baozhaAni.setAnimation(0, 'animation', false)
this.playOver();
}
}
}
onDestroy() {
gData.gameData.popTableScreenADLogic(3);
}
onClickClose() {
mk.audio.playEffect("button");
}
}