LeveRankItem.ts 4.83 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 { GamePlayTable, TableName } from "../kernel/table/TableDefine";
import { UIManager } from "../ui/UIManager";
import { UITitleData } from "../ui/uiView/UITitleData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelRankItem extends cc.Component {

    private _levelId: number = 0;
    private _lineLayer: cc.Node;
    private _line: cc.Node;

    protected override onLoad(): void {
        this._line = this.node.getChildByName('line');
    }

    protected override start(): void {
        getGlobalNode().on(GlobalEvent.LEVEL_STAR_CHANGE, this.onStarChange, this);
    }

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

    setLevelId(id: number) {
        this._levelId = id;
        this.refresh();
    }

    setLineLayer(node: cc.Node) {
        this._lineLayer = node;
        // this._line.removeFromParent();
        // this._lineLayer.addChild(this._line);
        // this._line.active = true;
    }

    refresh() {
        let isUnlocked = (this._levelId <= SaveDataManager.ins.runtimeData.curLevel);


        this.node.getChildByName('btn').getComponentInChildren(cc.Label).string = this._levelId.toString();

        let curNext = (this._levelId == SaveDataManager.ins.runtimeData.curLevel + 1);

        if (!isUnlocked && curNext) {
            this.node.getChildByName('back').active = false;
            this.node.getChildByName('btn').active = true;
            let backSp = this.node.getChildByName('btn').getChildByName('Background').getComponent(cc.Sprite);
            backSp.spriteFrame = this.node.getChildByName('linkRes').getChildByName('UI_gqlb_2').getComponent(cc.Sprite).spriteFrame;
            this.node.getChildByName('imgCur').active = true;
        }
        else {

            this.node.getChildByName('btn').active = isUnlocked;
            if (isUnlocked) {
                let backSp = this.node.getChildByName('btn').getChildByName('Background').getComponent(cc.Sprite);
                backSp.spriteFrame = this.node.getChildByName('linkRes').getChildByName('UI_gqlb_1').getComponent(cc.Sprite).spriteFrame;
            }
            this.node.getChildByName('back').active = !isUnlocked;
            this.node.getChildByName('imgCur').active = false;
        }

        let star = 0;
        if (SaveDataManager.ins.runtimeData.levelScoreMap[this._levelId]) {
            star = SaveDataManager.ins.runtimeData.levelScoreMap[this._levelId];
        }

        let starContainer = cc.find('btn/Background/starContainer', this.node);
        for (let i = 0; i < 3; ++i) {
            let starNode = starContainer.children[i];
            starNode.getComponent(cc.Sprite).spriteFrame = this.node.getChildByName('linkRes').getChildByName('star' + (i + 1 <= star ? '1' : '2')).getComponent(cc.Sprite).spriteFrame;
        }

        this.scheduleOnce(this.updateLine.bind(this), 0);
    }

    private updateLine() {

        if (this._line.parent != this._lineLayer) {
            this._line.removeFromParent();
            this._lineLayer.addChild(this._line);
            this._line.active = true;
        }

        let nextLv = this._levelId + 1;

        let maxLv = (tMgr.getConfig(TableName.GAME_PLAY, 22) as GamePlayTable).Value;
        if (nextLv > maxLv) {
            this._line.active = false;
            return;
        }

        let nextIsLihgt = (nextLv <= SaveDataManager.ins.runtimeData.curLevel + 1);
        this._line.getComponent(cc.Sprite).spriteFrame = (this.node.getChildByName('linkRes').getChildByName('line' + (nextIsLihgt ? '1' : '2'))).getComponent(cc.Sprite).spriteFrame;
        let isEnd = (this._levelId % 5 == 0);
        let isReverse = (Math.ceil(this._levelId / 5) % 2 == 0); //是否反向

        if (isEnd) {
            this._line.angle = 90;
            this._line.setPosition(this.node.position.x, this.node.position.y - this.node.height / 2);
        }
        else {
            this._line.angle = 0;
            this._line.setPosition(this.node.position.x + this.node.width / 2 * (isReverse ? -1 : 1), this.node.position.y);
        }
    }

    onStarChange(level: number) {
        if (level == this._levelId || level == this._levelId - 1) {
            this.refresh();
        }
    }

    onBtnClick() {
        UITitleData.id = this._levelId;
        // UIManager.ins.closeWindow(WindowName.LEVEL_RANK);
        UIManager.ins.openWindow(WindowName.PREVIEW, UITitleData.id)
    }

}