UIBallSkinPreview.ts 6.34 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 { PhysicsControl } from "../../kernel/physics/PhysicsControl";
import { BallSkinTable, TableName } from "../../kernel/table/TableDefine";
import SkinPreviewBall from "../../prefabs/SkinPreviewBall";
import { UIBase } from "../UIBase";
import { UIManager } from "../UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIBallSkinPreview extends UIBase {

    @property(cc.Node)
    imgTitle: cc.Node = null;

    @property(cc.Node)
    prg: cc.Node = null;

    private _curId: number = 0;

    //#region lifecycle 
    @property(cc.Node)
    btn_ad: cc.Node = null;
    @property(cc.Node)
    btn_start: cc.Node = null;
    @property(cc.Node)
    btn_bonus: cc.Node = null;
    @property(cc.Node)
    btn_sign: cc.Node = null;
    @property(cc.Node)
    btn_wheel: cc.Node = null;
    @property(cc.Node)
    btn_use: cc.Node = null;
    @property(cc.Node)
    btn_useing: cc.Node = null;
    openCount: number = 0;
    constructor() {
        super();
        // this._useCloseEffect = false;
        // this._usePopUpEffect = false;
    }

    protected onEnable(): void {

        getGlobalNode().on(GlobalEvent.BALL_SKIN_UNLOCK, this.refreshState, this);
        getGlobalNode().on(GlobalEvent.BALL_SKIN_USE, this.refreshState, this);

        this.refresh(this.enableParam);

        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_sign.position = this.btn_bonus.position;
        this.btn_wheel.position = this.btn_bonus.position;
        this.btn_use.position = this.btn_bonus.position;
        this.btn_useing.position = this.btn_bonus.position;
        this.btn_bonus.position = this.btn_ad.position;
        this.btn_ad.position = tempPos;
    }

    protected onPopUpEffectFinish(): void {

        this.node.getChildByName('physics').active = true;
        PhysicsControl.ins.setPhysicsEnable(true);
    }

    onBeforeClose(): void {
        super.onBeforeClose();
        this.node.getChildByName('physics').active = false;
        PhysicsControl.ins.setPhysicsEnable(false);
    }

    protected update(dt: number): void {
        PhysicsControl.ins.update(dt);
    }

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

    //#endregion

    //#region public method

    //#endregion

    //#region event

    onBtnCloseClick() {
        UIManager.ins.closeWindow(WindowName.BALL_SKIN_PREVIEW);

    }

    onPreClick() {
        if (this._curId > 1) {
            this.refresh(this._curId - 1);
        }
    }

    onNextClick() {
        let maxId = tMgr.getTableMaxID(TableName.BALL_SKIN);
        if (this._curId < maxId) {
            this.refresh(this._curId + 1);
        }
    }

    onBuyClick() {
        let wpos = this.node.getChildByName('btnDiamond').convertToWorldSpaceAR(cc.v3(0, 0, 0));
        let config = tMgr.getConfig(TableName.BALL_SKIN, this._curId) as BallSkinTable;
        if (SaveDataManager.ins.adjustDiamond(-config.Price, true, wpos)) {
            SaveDataManager.ins.unlockBallSkin(config.ID);
        }
    }

    onSignClick() {
        UIManager.ins.openWindow(WindowName.SIGN);
    }

    onWheelClick() {
        UIManager.ins.openWindow(WindowName.WHEEL);
    }

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

    onUseClick() {
        SaveDataManager.ins.useBallSkin(this._curId);
    }

    //#endregion

    //#region private method

    private refresh(skinId: number) {

        if (skinId != this._curId) {
            this._curId = skinId;

            let config = tMgr.getConfig(TableName.BALL_SKIN, skinId) as BallSkinTable;
            this.prg.getComponent(cc.ProgressBar).progress = 0.33 * config.Size;
            this.refreshState();

            cc.resources.load('ui/ballSkinName/name' + skinId.toString(), cc.SpriteFrame, (error, asset: cc.SpriteFrame) => {
                !error && (this.imgTitle.getComponent(cc.Sprite).spriteFrame = asset);
            })

            let ballNode = cc.find('physics/ball', this.node);
            ballNode.getComponent(SkinPreviewBall).setSkin(skinId);
        }
    }

    private refreshState() {
        let config = tMgr.getConfig(TableName.BALL_SKIN, this._curId) as BallSkinTable;

        let isUnlocked = SaveDataManager.ins.runtimeData.ballSkinUnlocked.includes(config.ID);
        let isUsing = (SaveDataManager.ins.runtimeData.ballSkinCurrent == config.ID);

        this.node.getChildByName('btnAd').getComponent(cc.Button).interactable = !isUnlocked;
        this.node.getChildByName('btnDiamond').active = !isUnlocked && config.Price > 0;
        if (this.node.getChildByName('btnDiamond').active) {
            this.node.getChildByName('btnDiamond').getComponentInChildren(cc.Label).string = config.Price.toString();
        }
        this.node.getChildByName('btnSign').active = !isUnlocked && config.Price == -1;
        this.node.getChildByName('btnWheel').active = !isUnlocked && config.Price == -2;

        this.node.getChildByName('btnUse').active = isUnlocked && !isUsing;
        this.node.getChildByName('btnUsing').active = isUnlocked && isUsing;
    }

    private setTrySkin() {
        SaveDataManager.ins.runtimeData.ballSkinTry = this._curId;
        SaveDataManager.ins.saveData();

        UIManager.ins.closeWindow(WindowName.BALL_SKIN_PREVIEW);

    }

    //#endregion

}