UISign.ts 5.45 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, tMgr, WindowName } from "../../Global";
import { UserSkill } from "../../kernel/battle/BattleConst";
import { SignTable, TableName } from "../../kernel/table/TableDefine";
import SignItem from "../../prefabs/SignItem";
import { UIBase } from "../UIBase";
import { UIManager } from "../UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UISign extends UIBase {

    // private _itemList: SignItem[] = [];
    @property(cc.Node)
    btn_ad: cc.Node = null;
    @property(cc.Node)
    adNode: cc.Node = null;

    @property(cc.Node)
    btn_start: cc.Node = null;
    @property(cc.Node)
    btn_bonus: cc.Node = null;
    openCount: number = 0;

    protected onLoad(): void {
        // SaveDataManager.ins.runtimeData.loginRewardData.freshTime = 0;
    }

    onEnable() {
        this.refresh();

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

        this.btn_ad.getChildByName('Background').getChildByName('UI_General_Btn_yellow_ckgg').active = true;
        this.btn_ad.getChildByName('Background').getChildByName('UI_Finish_kxsx').active = false;

        this.openCount++;
        if (this.openCount > 1) {
            this.BtnPos_Change();
        }

    }

    //按钮位置轮换
    BtnPos_Change() {
        let tempPos = this.btn_start.position;
        this.btn_start.position = this.btn_bonus.position;
        this.btn_bonus.position = this.btn_ad.position;
        this.btn_ad.position = tempPos;
    }

    protected onDisable(): void {
        let tip = cc.find('main/tips', this.node);
        cc.Tween.stopAllByTarget(tip);
        tip.active = false;
    }
    hide_btn_ad() {
        this.btn_ad.active = false;
    }
    private refresh() {

        let num = cc.find('main/container', this.node).children.length;

        let now = SaveDataManager.ins.getNowTime();
        let valid = (now >= SaveDataManager.ins.runtimeData.loginRewardData.freshTime);
        // valid = true;

        if (valid && SaveDataManager.ins.runtimeData.loginRewardData.step >= 7) {
            SaveDataManager.ins.runtimeData.loginRewardData.step = 0;
            SaveDataManager.ins.saveData();
        }

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

            let item = (cc.find('main/container/SignItem' + (i + 1).toString(), this.node).getComponent(SignItem));
            item.setDay(i + 1);

            item.setLock(i + 1 <= SaveDataManager.ins.runtimeData.loginRewardData.step);

            item.setCurrent(i - SaveDataManager.ins.runtimeData.loginRewardData.step == 0);
        }

        // cc.find('main/btnDouble', this.node).getComponent(cc.Button).interactable = valid;
        // cc.find('main/btnGet', this.node).getComponent(cc.Button).interactable = valid; 

    }

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

    onDoubleClick() {

        let now = SaveDataManager.ins.getNowTime();
        let valid = (now >= SaveDataManager.ins.runtimeData.loginRewardData.freshTime);

        if (valid) {
            //AD 放完广告调 get(2)
            // HeyGamePlatform.instance.showVideoAd(ADID.ADID_ADDCOIN, (_result) => {
            if (true) {
                this.get(2);
            } else {
                console.log('视频播放失败');
            }
            // });
        }
        else {
            this.showTip();
        }

    }

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

    onGetClick() {
        let now = SaveDataManager.ins.getNowTime();
        let valid = (now >= SaveDataManager.ins.runtimeData.loginRewardData.freshTime);

        if (valid) {
            this.get(1);
        }
        else {
            this.showTip();
        }

    }

    private get(rate: number = 1) {

        let cur = SaveDataManager.ins.runtimeData.loginRewardData.step;
        let config = tMgr.getConfig(TableName.SIGN, cur + 1) as SignTable;

        if (config.Type == 1) {
            SaveDataManager.ins.adjustDiamond(config.Num * rate, true);
        }
        else if (config.Type == 2) {
            SaveDataManager.ins.setUserSkillNumDelta(UserSkill.BOMB, config.Num * rate);
        }
        else if (config.Type == 3) {
            // SaveDataManager.ins.setUserSkillNumDelta(UserSkill.SHURIKEN, config.Num * rate);
            SaveDataManager.ins.unlockBallSkin(2);
        }


        SaveDataManager.ins.runtimeData.loginRewardData.step++;
        let endTime = new Date();
        endTime.setHours(23, 59, 59);

        SaveDataManager.ins.runtimeData.loginRewardData.freshTime = endTime.getTime();

        SaveDataManager.ins.saveData();

        this.refresh();
    }

    private showTip() {
        let tip = cc.find('main/tips', this.node);

        tip.active = true;
        tip.scale = 0;
        cc.Tween.stopAllByTarget(tip);
        cc.tween(tip).to(0.3, { scale: 1 }, { easing: 'backOut' }).delay(2).to(0.3, { scale: 0 }, { easing: 'backIn' }).call(() => { tip.active = false; }).start();
    }
}