UIWheel.ts 7.13 KB
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { SaveDataManager } from "../../component/SaveDataManager";
import { ADID, GlobalEvent, tMgr, WindowName } from "../../Global";
import { getGlobalNode } from "../../kernel/battle/BattleConst";
import { TableName, WheelTable } from "../../kernel/table/TableDefine";
import WheelItem from "../../prefabs/WheelItem";
import { UIBase } from "../UIBase";
import { UIManager } from "../UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIWheel extends UIBase {

    //#region  LIFE-CYCLE CALLBACKS:

    @property(cc.Integer)
    public radius: number = 1
    @property(cc.Prefab)
    itemPrefab: cc.Prefab = null;
    @property(cc.Node)
    adNode: cc.Node = null;
    @property(cc.Node)
    btn_ad: cc.Node = null;

    private _wheelNode: cc.Node;
    private _tweenObject = { progress: 0 };

    /**权重列表 */
    private _weightList: number[] = [];

    private _reward: WheelTable;

    start() {
        // [3]

        this._wheelNode = cc.find('main/wheel', this.node);

        this.btn_ad.active = true;


        let num = tMgr.getTableCount(TableName.WHEEL);
        let rad = -2 * Math.PI / num;
        // let deg = slib.MathUtil.getDegreeByRadian(rad);
        let vec = new cc.Vec2(0, this.radius);

        for (let i = 0; i < num; ++i) {

            if (i > 0) {
                vec.rotateSelf(rad)
            }

            let config = tMgr.getConfig(TableName.WHEEL, i + 1) as WheelTable;
            this._weightList.push(config.Rate);

            let foo = cc.instantiate(this.itemPrefab);
            foo.getComponent(WheelItem).setId(i + 1);
            foo.setPosition(vec.x, vec.y);
            // foo.angle = i * deg;
            foo.scale = 0.7;

            cc.find('main/UI_wheel_zp', this.node).addChild(foo);

        }

        this.refresh();
    }

    hide_btn_ad() {
        this.btn_ad.active = false;
    }

    protected onEnable(): void {
        this.refresh();
        this.btn_ad.active = true;
        // this.adNode.getComponent(CustomNativeAdView).showNativeAd((result) => {
        //     if (result == true) {
        //         this.btn_ad.active = true;
        //     } else {
        //         this.btn_ad.active = false;
        //     }
        // });


        this.btn_ad.getChildByName('Background').getChildByName('UI_wheel_btn_ckgg').active = true;
        this.btn_ad.getChildByName('Background').getChildByName('UI_wheel_btn_kxsx').active = false;

    }

    protected onDisable(): void {

    }

    protected onLoad(): void {
        getGlobalNode().on(GlobalEvent.WHEEL_REWARD_GET, this.refresh, this);
    }

    protected onDestroy(): void {
        getGlobalNode().targetOff(this);
    }

    private refresh() {
        cc.find('main/UI_wheel_zp/UI_wheel_cjcs', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum > 0);

        cc.find('main/btnGo', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum > 0);
        cc.find('main/btnAdGo', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum <= 0);
        // cc.find('main/btn_kxsx', this.node).active = (SaveDataManager.ins.runtimeData.freeWheelNum <= 0);

        let container = cc.find('main/UI_wheel_zp', this.node);
        for (let i = 0; i < container.childrenCount; ++i) {
            let cmpt = container.children[i].getComponent(WheelItem);
            cmpt && cmpt.refresh();
        }
    }

    private go() {

        this._wheelNode.angle = 0;

        let configId = 0;
        // if (SaveDataManager.ins.runtimeData.freeWheelNum) {
        //     //免费的,固定奖励
        //     configId = 1;
        // }
        // else {
        configId = slib.MathUtil.randomTestByWeightList(this._weightList) + 1;
        // }

        this._reward = tMgr.getConfig(TableName.WHEEL, configId) as WheelTable;
        // log('奖品', configId);


        let num = tMgr.getTableCount(TableName.WHEEL);
        let rad = 2 * Math.PI / num;
        let deg = slib.MathUtil.getDegreeByRadian(rad);

        let total = -6 * 360 + (-deg) * (configId - 1);

        this._tweenObject.progress = 0;

        cc.tween(this._tweenObject).to(5, { progress: 1 }, {
            easing: 'sineOut',
            onUpdate: (target, ratio) => {

                // this._wheelNode.setRotationFromEuler(0, 0, total * ratio);
                this._wheelNode.angle = total * (target['progress']);
            }
        }).call(this.onTweenComplete.bind(this)).start();

        // SoundManager.ins.playEffect(SOUND_EFFECT.WHEEL);
    }

    private onTweenComplete() {
        // log('complete');

        if (SaveDataManager.ins.runtimeData.freeWheelNum > 0) {
            SaveDataManager.ins.runtimeData.freeWheelNum--;
        }


        // if (this._reward.Type == 1) {
        //     //钻石
        //     // uiMgr().open('UI/UIGet', { type: 3, num: this._reward.Num });
        // }
        // else if (this._reward.Type == 2) {
        //     //炸弹
        //     // battleMgr().battle.startSkill(2);
        //     // uiMgr().open('UI/UIGet', { type: 2, num: 1 });
        // }
        // else if (this._reward.Type == 3) {
        //     //分裂炮
        //     // uiMgr().open('UI/UIGet', { type: 1, num: 1 });
        //     // battleMgr().battle.startSkill(1);
        // }
        // else if (this._reward.Type == 4) {
        //     //飞轮
        // }
        // else if (this._reward.Type == 5) {
        //     //西瓜球
        // }

        UIManager.ins.openWindow(WindowName.GET, this._reward);

        SaveDataManager.ins.saveData();

        // console.log('reward', this._reward.ID);

        cc.find('main/btnClose', this.node).getComponent(cc.Button).interactable = true;
        cc.find('main/btnAd', this.node).getComponent(cc.Button).interactable = true;
        cc.find('main/btnAdGo', this.node).getComponent(cc.Button).interactable = true;
        cc.find('main/btnGo', this.node).getComponent(cc.Button).interactable = true;

        this.refresh();
    }

    onCloseClick() {
        UIManager.ins.closeWindow(WindowName.WHEEL);
    }

    onBtnAdClick() {
        //AD 纯放广告
    }

    onBtnGoClick() {
        if (SaveDataManager.ins.runtimeData.freeWheelNum > 0) {
            //免费次数
            this.go();
        }
        else {
            //AD 放广告,播完后调用 go
            // HeyGamePlatform.instance.showVideoAd(ADID.ADID_ADDCOIN, (_result) => {
            if (true) {
                this.go();
            } else {
                console.log('视频播放失败');
            }
            // });

        }

        cc.find('main/btnClose', this.node).getComponent(cc.Button).interactable = false;
        cc.find('main/btnAd', this.node).getComponent(cc.Button).interactable = false;
        cc.find('main/btnGo', this.node).getComponent(cc.Button).interactable = false;
        cc.find('main/btnAdGo', this.node).getComponent(cc.Button).interactable = false;
    }

    //#endregion
}