BallSkinItem.ts 5.09 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 { GlobalEvent, tMgr, WindowName } from "../Global";

import { getGlobalNode } from "../kernel/battle/BattleConst";
import { BallSkinTable, TableName } from "../kernel/table/TableDefine";
import { UIManager } from "../ui/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BallSkinItem extends cc.Component {

    protected onLoad(): void {
        getGlobalNode().on(GlobalEvent.BALL_SKIN_UNLOCK, this.refreshState, this);
        getGlobalNode().on(GlobalEvent.BALL_SKIN_USE, this.refreshState, this);

    }

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

    private _config: BallSkinTable = null;

    setBallSkinItem(id: number) {

        if (id > 0) {
            let config = tMgr.getConfig(TableName.BALL_SKIN, id) as BallSkinTable;

            if (config) {

                this._config = config;

                this.node.getChildByName('icon').active = true;
                cc.resources.load('battle/' + config.Img, cc.SpriteFrame, (error, asset) => {
                    if (!error) {
                        this.node.getChildByName('icon').getComponent(cc.Sprite).spriteFrame = asset as cc.SpriteFrame;
                    }
                })
            }
        }
        else {
            this.node.getChildByName('icon').active = false;
            this._config = null;
        }

        this.refreshState();

    }

    private refreshState() {

        let isAd = (this._config == null);
        let isUnlocked = !isAd && SaveDataManager.ins.runtimeData.ballSkinUnlocked.includes(this._config.ID);
        let isUsing = !isAd && (SaveDataManager.ins.runtimeData.ballSkinCurrent == this._config.ID);

        this.node.getChildByName('txtName').getComponent(cc.Label).string = (isAd ? "" : this._config.Name);

        //#region 显示尺寸

        this.node.getChildByName('imgSize1').active = (!isAd && this._config.Size == 1);
        this.node.getChildByName('imgSize2').active = (!isAd && this._config.Size == 2);
        this.node.getChildByName('imgSize3').active = (!isAd && this._config.Size == 3);

        //#endregion

        this.node.getChildByName('btnBuy').active = !isAd && !isUnlocked;
        this.node.getChildByName('btnSign').active = !isAd && !isUnlocked;
        this.node.getChildByName('btnWheel').active = !isAd && !isUnlocked;
        this.node.getChildByName('btnUse').active = !isAd && isUnlocked;
        this.node.getChildByName('btnUsing').active = !isAd && isUnlocked;
        this.node.getChildByName('btnAd').active = isAd;

        if (!isAd) {
            if (isUnlocked) {
                //已经解锁了
                this.node.getChildByName('btnUse').active = !isUsing;
                this.node.getChildByName('btnUsing').active = isUsing;
            }
            else {
                //未解锁
                this.node.getChildByName('btnBuy').active = (this._config.Price > 0);
                this.node.getChildByName('btnSign').active = (this._config.Price == -1);
                this.node.getChildByName('btnWheel').active = (this._config.Price == -2);

                if (this._config.Price > 0) {
                    this.node.getChildByName('btnBuy').getComponentInChildren(cc.Label).string = this._config.Price.toString();
                }
            }
        } else {

            this.node.getChildByName('btnAd').getChildByName('Background').getChildByName('UI_Skin_btn_cjhd').active = true;
            this.node.getChildByName('btnAd').getChildByName('Background').getChildByName('UI_Finish_kxsx').active = false;

            // this.node.getChildByName('nodeAd').getComponent(CustomNativeAdView).showNativeAd((result) => {
            //     if (result) {
            //         this.node.getChildByName('btnAd').active = true;
            //     } else {
            //         this.node.getChildByName('btnAd').active = false;
            //     }
            // });
        }
    }

    onBtnBuyClick() {
        if (this._config) {

            // let wpos = this.node.getChildByName('btnBuy').convertToWorldSpaceAR(cc.v3(0, 0, 0));
            // if (SaveDataManager.ins.adjustDiamond(-this._config.Price, true, wpos)) {
            //     SaveDataManager.ins.unlockBallSkin(this._config.ID);
            // }

            UIManager.ins.openWindow(WindowName.BALL_SKIN_PREVIEW, this._config.ID);
        }
    }

    onHideAdBtn() {
        this.node.getChildByName('btnAd').active = false;
    }

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

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

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

    onBtnUseClick() {
        if (this._config) {
            SaveDataManager.ins.useBallSkin(this._config.ID);
        }
    }

}