GameRoleDataModel.ts 6.52 KB
import { DeepReadonlyObject } from "simba-utils";
import { IRole } from "../../config/Role";
import { GameRecord } from "../../avg/game-data/GameRecord";
import { GameModelManager } from "./GameModelManager";
import { ConfigManager } from "../../common/gameplay/managers/ConfigManager";
import { itemConfig } from "../../config/ItemConfig";
import { relationLevelConfig } from "../../config/RelationLevelConfig";
import { FaceType } from "../../avg/EditorEnums";

export default class GameRoleDataModel {
    private _cfg: DeepReadonlyObject<IRole> = undefined as unknown as DeepReadonlyObject<IRole>;
    private _goldcoin: number = 0;//金币
    private _clothcoin: number = 0;//服装币
    private _skins: number[] = [];
    private _curSkin: number = -1;
    private _skinMaps: Set<number> = new Set<number>();
    private _roleName: string = "";
    private _energyValue: number = 0;//灵力值

    private _itemMaps: Map<number, number> = new Map<number, number>();

    public setConfig(cfg: DeepReadonlyObject<IRole>) {
        this._cfg = cfg;
        this.initDate();
    }

    public getConfig() {
        return this._cfg;
    }

    initDate() {
        this.initSkin();
        this.initCoin();
        this.initData();
    }

    hasSkin(id: number) {
        return this._skinMaps.has(id);
    }

    getRoleLike() {
        return GameRecord.globalVariables["like" + this._cfg.id] + GameRecord.recordVariables["like" + this._cfg.id];
    }

    getPortrait(face = FaceType.Normal, skin?: number) { // TODO 默认皮肤更改?
        if (skin === undefined && this._curSkin > 0) skin = this._curSkin;
        return "textures/portrait/" + this._cfg.id + "/" + (skin !== undefined ? skin + "/" : "") + face;
    }

    getBust(skin?: number) {
        if (skin === undefined && this._curSkin > 0) skin = this._curSkin;
        let ret = "textures/bust/" + this._cfg.id;
        if (skin) {
            ret += skin;
        }
        return ret;
    }

    getHeadIcon(small = false) {
        let ret = "textures/head_icon/" + (small ? "small/" : "large/") + this._cfg.id;
        if (this._curSkin > 0) {
            ret += "_" + this._curSkin;
        }
        return ret;
    }

    getRoleLikeLevel() {
        let configs = ConfigManager.getAllConfig(relationLevelConfig);
        let level = 0;
        let like = this.getRoleLike();
        const maxLevel = Object.keys(configs).length;
        let levelValue = 0;
        for (let id = 1; id <= maxLevel; id++) {
            levelValue = configs[id].relation_value;
            if (like >= levelValue) {
                like -= levelValue;
                level = id;
            } else {
                break;
            }
        }
        return { level, currLike: like, nextLevelLike: levelValue, maxLevel };
    }

    addSkin(id: number) {
        this._skinMaps.add(id);
        let skinsstr = "";
        let index = 0;
        for (let value of this._skinMaps) {
            skinsstr += value;
            if (index != this._skinMaps.size - 1) {
                skinsstr += ",";
            }
            ++index;
        }

        GameRecord.globalVariables["skins_" + this._cfg.id + "_skins"] = skinsstr;
        GameRecord.autoSave();
    }

    initSkin() {
        let curSkin = GameRecord.globalVariables["curskin_" + this._cfg.id + "_skin"];
        this._curSkin = curSkin === undefined ? this._cfg['drawing'] : curSkin;

        let skins = GameRecord.globalVariables["skins_" + this._cfg.id + "_skins"] as string;
        skins = skins === undefined ? "" : skins;
        let skinSpilt = skins.split(",");
        for (let index = 0; index < skinSpilt.length; ++index) {
            this._skinMaps.add(parseInt(skinSpilt[index]));
        }
    }

    initCoin() {
        let goldcoin = GameRecord.globalVariables["goldcoin_" + this._cfg.id + "_num"] as number;
        this._goldcoin = goldcoin === undefined ? 0 : goldcoin;
        let clothcoin = GameRecord.globalVariables["clothcoin_" + this._cfg.id + "_num"] as number;
        this._clothcoin = clothcoin === undefined ? 0 : clothcoin;
    }

    initData() {
        this._itemMaps.clear();
        let props = GameModelManager.getItemConfigs();
        for (let key in props) {
            let value = props[key];
            let recordNum = GameRecord.globalVariables["prop_" + this._cfg.id + value.id + "_num"] as number;
            recordNum = recordNum === undefined ? 0 : recordNum;
            this._itemMaps.set(value.id, recordNum);
        }

        this._energyValue = GameRecord.globalVariables["energy_" + this._cfg.id];
        this._energyValue = this._energyValue === undefined ? 0 : this._energyValue;
    }

    public getProps(id: number) {
        return this._itemMaps.get(id);
    }

    public addProps(id: number, num: number) {
        let value = this._itemMaps.get(id) as number;
        value += num;
        value = Math.max(0, value);
        this._itemMaps.set(id, value);
        GameRecord.globalVariables["prop_" + this._cfg.id + "" + id + "_num"] = value;
        GameRecord.autoSave();
    }

    public getEnergy(isceil: boolean = true): number {
        if (isceil) {
            return Math.ceil(this._energyValue);
        }
        return this._energyValue;
    }

    public addEnergy(value: number) {
        let max = 100;
        let min = 0;
        this._energyValue += value;
        this._energyValue = Math.max(min, this._energyValue);
        this._energyValue = Math.min(max, this._energyValue);
        GameRecord.globalVariables["energy_" + this._cfg.id] = this._energyValue;
        GameRecord.autoSave();
    }

    public getGoldCoin(): number {
        return this._goldcoin;
    }

    public addGoldCoin(addvalue: number) {
        this._goldcoin += addvalue;
        GameRecord.globalVariables["goldcoin_" + this._cfg.id + "_num"] = this._goldcoin;
        GameRecord.autoSave();
    }

    public getClothCoin() {
        return this._clothcoin;
        //return 10000;
    }

    public addClothCoin(addvalue: number) {
        this._clothcoin += addvalue;

        GameRecord.globalVariables["clothcoin_" + this._cfg.id + "_num"] = this._clothcoin;
        GameRecord.autoSave();
    }

    public getRoleName() {
        if (!this._roleName || this._roleName === "") {
            return this._cfg.name;
        } else {
            return this._roleName;
        }
    }

    public setRoleName(name: string) {
        this._roleName = name;
    }

}