MainInfoViewSubpresenter.ts 4.37 KB
import { delay } from "simba-utils";
import { GameRecord } from "../../../avg/AVG";
import { Subpresenter } from "../../../common/classbase/PresenterBase";
import { GameConstData } from "../../../common/gameplay/gamedata/GameConstData";
import { GameTextData } from "../../../common/gameplay/gamedata/GameTextData";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { StringUtils } from "../../../common/utils/StringUtils";
import { GamePropType } from "../../Enums";
import { GameModelManager } from "../../model/GameModelManager";
import { RegSubpresenter } from "../PresenterCCViewFactory";
import { UIUtils } from "../UIUtils";
import { MainInfoSubview, MainInfoSubviewProps, MainInfoSubviewType } from "../view/type/MainInfoSubviewiew";

@RegSubpresenter(MainInfoSubviewType)
export class MainInfoViewSubpresneter extends Subpresenter<MainInfoSubview> {

    constructor() {
        super();
        this._viewProps = {
            energy: 1,
            energycontent: "100%",
            energybackindex: 0,   //背景
            timecontent: "",
            onClick: this.onEnergyClickCallback
        };



    }

    updateEnergy = () => {
        // let player = GameModelManager.getPlayerData();
        // let e = player.getEnergy();
        // if (GameConfig.debug) {
        //     const OFFSETTIME = 1200000;
        //     const CYCLETIME = 1000;
        //     const ADDENERGY = GameConstData.GAME_CONST_PLOT_ENERGY_COST_VALUE;
        //     setTimeout(() => {
        //         let cachetime = GameRecord.globalVariables["autoaddenergy"];
        //         if (cachetime === undefined || e === 100) {
        //             cachetime = TimeManager.getTime() + OFFSETTIME;
        //             GameRecord.globalVariables["autoaddenergy"] = cachetime;
        //             GameRecord.autoSave();
        //         }
        //         let curtime = TimeManager.getTime();

        //         let deltime = (cachetime - curtime) / 1000;

        //         this._viewProps.timecontent = deltime.toFixed(0) + "秒";


        //         this.view.setProps(this._viewProps);

        //         this.updateEnergy();
        //     }, 1000);
        // }
    }

    onEnergyClickCallback = async () => {
        let player = GameModelManager.getPlayerData();
        let e = player.getEnergy();
        if (e == 0) {
            await this.tipAddEnergy();
        } else {
            UIManager.showToast(StringUtils.format(GameModelManager.getLanguageTxt(GameTextData.GAME_TEXT_MAIN_ENERGY_VALUE), e));
        }

    }

    async tipAddEnergy() {
        // let player = GameModelManager.getPlayerData();
        // // TODO 提示体力不足,跳转购买体力?
        // let ret = await UIUtils.showAddEnergy(GameModelManager.getItemConfig(GameConstData.GAME_CONST_ENERGY_PROPS_ID), player.getProps(GameConstData.GAME_CONST_ENERGY_PROPS_ID));
        // if (ret) {
        //     let itemconfig = GameModelManager.getItemConfig(GameConstData.GAME_CONST_ENERGY_PROPS_ID);
        //     player.addEnergy(itemconfig.energy_recover - GameConstData.GAME_CONST_PLOT_ENERGY_COST_VALUE);
        //     UIManager.showToast(GameModelManager.getLanguageTxt(GameTextData.GAME_TEXT_MAIN_ENERGY_ADADDSUCCESS_VALUE));
        // }
    }

    onOpen() {
        super.onOpen();
        this.initData();

    }

    _viewProps: MainInfoSubviewProps;

    initEnergy = () => {
        let player = GameModelManager.getPlayerData();
        let max = 100;
        let value = player.getEnergy();
        value = value ? value : 0;
        let percent = value / max;
        this._viewProps.energy = percent;
        this._viewProps.energycontent = value + "%";
        let index = 0;
        if (value >= 70 && value <= 100) {
            index = 0;
        } else if (value >= 21 && value <= 69) {
            index = 1;
        } else {
            index = 2;
        }
        this._viewProps.energybackindex = index;

        this.view.setProps(this._viewProps);
    }
    initData = async () => {
        while (!GameModelManager.getPlayerData()) await delay(0.1);
        let k = "prop_" + GameModelManager.getPlayerData().getConfig().id + "_" + GamePropType.Energy + "_num";
        GameRecord.onGlobalVariableChange(k, () => {
            this.initEnergy();
        });
        this.initEnergy();
        this.updateEnergy();
    }

    onOwnerEnterBackground() {
        // console.log("on owner enter background");
    }
}