LikeLevelSubpresenter.ts 1.9 KB
import { GameRecord } from "../../../avg/AVG";
import { RoleType } from "../../../avg/EditorEnums";
import { Subpresenter } from "../../../common/classbase/PresenterBase";
import { GameConfig } from "../../../GameConfig";
import { GameModelManager } from "../../model/GameModelManager";
import GameRoleDataModel from "../../model/GameRoleDataModel";
import { RegSubpresenter } from "../PresenterCCViewFactory";
import { LikeLevelView, LikeLevelViewType } from "../view/type/LikeLevelSubview";

@RegSubpresenter(LikeLevelViewType)
export class LikeLevelSubpresenter extends Subpresenter<LikeLevelView> {
    onOpen() {
        super.onOpen();
        if (!(this.owner as any)["getRoleId"]) {
            console.error("owner need provide roleId by getRoleId");
            return;
        }
        let roleId = (this.owner as any)["getRoleId"]() as number;
        let roleData = GameModelManager.getRoleData(roleId)!;
        if (roleData.getConfig().RoleType === RoleType.Role_Major) {
            this.updateView(roleData);
            this._disposable.add(GameRecord.onRecordVariableChange("like" + roleId, () => {
                this.updateView(roleData);
            }));
            this._disposable.add(GameRecord.onGlobalVariableChange("like" + roleId, () => {
                this.updateView(roleData);
            }));
        } else {
            let likestr = GameConfig.debug ? "0" : "";
            this.view.setProps({ visible: false, level: 0, percent: 0, like: likestr });
        }
    }

    updateView(role: GameRoleDataModel) {
        let data = role.getRoleLikeLevel();
        data.currLike = data.currLike ? data.currLike : 0;
        let v = role.getRoleLike();
        let value = GameConfig.debug && v ? v as unknown as string : "";
        this.view.setProps({ visible: true, level: data.level ? data.level : 0, percent: data.level === data.maxLevel ? 1 : (data.currLike / data.nextLevelLike), like: value });
    }
}