import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { Checkpoint } from "../Checkpoint";
import { CheckpointModelLevelComp } from "../model/CheckpointModelLevel";
import { InitCheckpointComp } from "./InitCheckpoint";

/**
 * 关卡升级
 */
@ecs.register('CheckpointUpgrade')
export class CheckpointUpgradeComp extends ecs.Comp {
    /** 当前等级 */
    lv: number = 0;

    reset() {
        this.lv = 0;
    }
}

export class CheckpointUpgradeSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(CheckpointUpgradeComp, CheckpointModelLevelComp);
    }

    entityEnter(e: Checkpoint): void {
        let rlm = e.CheckpointModelLevel;
        let ru = e.CheckpointUpgrade;

        if (ru.lv == 0)
            rlm.vm.lv++;                   // 提升一级
        else
            rlm.vm.lv = ru.lv;             // 设置等级

        // 当前等级配置
        rlm.rtluCurrent.init(rlm.vm.lv);

        // 下个关卡配置
        rlm.rtluNext.init(rlm.vm.lv + 1);
        rlm.vm.stationCount = rlm.rtluNext.stationCount;
        rlm.vm.gridCount = rlm.rtluNext.gridCount;
        rlm.vm.peopleCount = rlm.rtluNext.peopleCount;

        e.remove(CheckpointUpgradeComp);
        e.add(InitCheckpointComp)
    }
}