UIBattleUserSkillBtn.ts 4.16 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 { BattleManager } from "../../kernel/battle/BattleManager";
import { TableName, UserSkillTable } from "../../kernel/table/TableDefine";
import { UIManager } from "../UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIBattleUserSkillBtn extends cc.Component {

    private _icon: cc.Sprite;
    private _lockNode: cc.Node;
    private _numNode: cc.Node;
    private _txtNum: cc.Label;

    private _locked: boolean = true;

    private _skillConfig: UserSkillTable = null;

    //#region  LIFE-CYCLE CALLBACKS:

    onLoad() {

        this._icon = this.node.getChildByName('icon').getComponent(cc.Sprite);
        this._lockNode = this.node.getChildByName('imgLock');
        this._numNode = this.node.getChildByName('nodeNum');
        this._txtNum = this._numNode.getComponentInChildren(cc.Label);

    }

    protected onEnable(): void {
        getGlobalNode().on(GlobalEvent.SKILL_NUM_CHANGE, this.onSkillNumChange, this);
    }

    protected onDisable(): void {
        getGlobalNode().off(GlobalEvent.SKILL_NUM_CHANGE, this.onSkillNumChange, this);
    }

    // update (dt) {}

    //#endregion

    //#region public method

    setId(skillId: number) {

        this._skillConfig = tMgr.getConfig(TableName.USER_SKILL, skillId) as UserSkillTable;

        if (this._skillConfig) {
            // this.setlo
            cc.resources.load('ui/battle/' + this._skillConfig.Icon, cc.SpriteFrame, (error, asset) => {
                if (!error) {
                    this._icon.spriteFrame = asset as cc.SpriteFrame;
                }
            });

            this._locked = BattleManager.ins.curBattle.levelId < this._skillConfig.UnlockLevel;

            if (BattleManager.ins.curBattle.levelId == 0) {
                this._locked = false;
            }

            this._icon.node.active = !this._locked;
            this._numNode.active = !this._locked;
            this._lockNode.active = this._locked;

            let free = (BattleManager.ins.curBattle.levelId == this._skillConfig.UnlockLevel);
            this.node.getChildByName('hand').active = free;

            if (!this._locked || free) {
                this.updateNum(free);
            }
        }
    }

    // setLock(value: boolean) {
    //     this._locked = value;

    //     this._icon.node.active = !value;
    //     this._addNode.active = !value;
    //     this._numNode.active = !value;

    //     this._lockNode.active = value;

    // }

    //#endregion

    //#region event

    onClick() {
        if (!this._locked) {

            let free = (BattleManager.ins.curBattle.levelId == this._skillConfig.UnlockLevel);
            if (this.node.getChildByName('hand').active) {
                this.node.getChildByName('hand').active = false;
            }

            let num = SaveDataManager.ins.getUserSkillNum(this._skillConfig.ID);

            if (num > 0 || free) {
                BattleManager.ins.curBattle.useSkill(this._skillConfig.ID, free);
            }
            else {
                UIManager.ins.openWindow(WindowName.USER_SKILL_INFO, this._skillConfig.ID);
            }

        }
    }

    private onSkillNumChange(skillId: number) {
        if (skillId == this._skillConfig.ID) {
            this.updateNum(false);
        }
    }

    //#endregion

    //#region private method

    private updateNum(free: boolean) {


        //读取本地存档
        let num = SaveDataManager.ins.getUserSkillNum(this._skillConfig.ID);

        this._numNode.active = (num > 0 || free);
        this._txtNum.string = num.toString();

        this._numNode.getChildByName('txtNum').active = !free;
        this._numNode.getChildByName('imgFree').active = free;
    }

    //#endregion
}