ExtraStoryModelManager.ts 5.46 KB
import { GameRecord, getPlot, getPlotsIndex, PlotManager, SpecialPlotId } from "../../avg/AVG";
import { DatingEventStatus } from "./DatingEventSceneModel";
import { GameModelManager } from "./GameModelManager";

/**
 * 番外剧情管理类
 */
class ExtraStoryModelManager1 {
    /**是否正在创建 */
    creating: boolean = false;
    /**
     * 全局存档(g值)中开过分支的数据
     */
    itemArr: {
        /**开始剧情id */
        startPlot: number;
        /**
         * 存档中gameRecords数组索引
         */
        index: number;
    }[];

    /**是否在番外中 */
    private _isInExtraStoryScene:boolean = false;

    
    public get isInExtraStoryScene() : boolean {
        return this._isInExtraStoryScene;
    }

    /**开启分支剧情,
    * plots:如果需要同时开启多个分支剧情,则数组中存储这些分支中对应的起始剧情id
    */
    async  startBranches(plots: number[],callback?:Function) {
        if (this.creating) return;
        this.creating = true;
        if (!plots.length) {
            console.warn("no plots to mark as branch start plot id");
            return;
        }
        for (let i = 0; i < plots.length; i++) {
            let pid = plots[i];
            if (SpecialPlotId.ToBeContinued === pid || SpecialPlotId.End === pid) {
                console.error("you can not start a branch with SpecialPlotId, id is = ", pid);
                continue;
            }
            let p = await getPlot(pid);
            if (!p) {
                console.warn("no plot which plot id is = ", pid);
            }
            let itemArrIndex: number = this.checkIsBranchStarted(pid);
            let recordsLength = GameRecord.getRecords().length;
            let index = recordsLength;
            if (itemArrIndex != -1) {
                index = this.itemArr[itemArrIndex].index;
            }
            await PlotManager.startGameWithRecordIndex(index, pid);
            this._isInExtraStoryScene = true;
            this.pushBranchToRecord(pid, index);
            callback && callback(i,pid);
            console.log("getCurrentPlots: ", PlotManager.getCurrentPlots());

        }
        this.creating = false;
    }

    /**
    * 
    * @param plotId 检测指定剧情下的分支是否已经开启
    * @returns 
    */
    private checkIsBranchStarted(plotId: number): number {
        const branchRecordKey: string = "BRANCH_INDEX_ENTRY";
        let ret: number = -1;
        let itemArr: { startPlot: number, index: number }[] = [];
        let recordStr: string = GameRecord.globalVariables[branchRecordKey] as string;
        if (recordStr && "" !== recordStr.trim()) {
            itemArr = JSON.parse(recordStr) as { startPlot: number, index: number }[];
        }
        ret = itemArr.findIndex((v) => v.startPlot === plotId);
        this.itemArr = itemArr;
        return ret;
    }

    /**
     * 
     * @param plotId 将分支计入存档
     * @param index 
     */
    private pushBranchToRecord(plotId: number, index: number) {
        const branchRecordKey: string = "BRANCH_INDEX_ENTRY";
        let exist: boolean = false;
        let itemArr: { startPlot: number, index: number }[] = [];
        let recordStr: string = GameRecord.globalVariables[branchRecordKey] as string;
        if (recordStr && "" !== recordStr.trim()) {
            itemArr = JSON.parse(recordStr) as { startPlot: number, index: number }[];
        }
        exist = itemArr.findIndex((v) => v.startPlot === plotId) >= 0;
        if (!exist) {
            itemArr.push({
                startPlot: plotId,
                index: index
            });
            GameRecord.globalVariables[branchRecordKey] = JSON.stringify(itemArr);
            GameRecord.saveRecord();
        }
        this.itemArr = itemArr;
    }




    /**
     * 获取分支剧情状态(约会状态)
     * @param plotSceneTypeId 剧情类型(DateSceneConfig的id)id
     * @param start_plot_id 起始剧情id
     * @returns 剧情状态
     */
    getBranchStatus(plotSceneTypeId: number, start_plot_id: number): DatingEventStatus {
        let s = DatingEventStatus.New;
        let curPs = PlotManager.getCurrentPlots();
        let curP = curPs[0];
        let progress = curP.plotSceneTypeId === plotSceneTypeId && (curP.id != SpecialPlotId.ToBeContinued && curP.id != SpecialPlotId.End);
        let isNew = curP.plotSceneTypeId === plotSceneTypeId && curP.id === start_plot_id;
        s = isNew ? DatingEventStatus.New : progress ? DatingEventStatus.InProgress : DatingEventStatus.Completed;
        console.log('当前剧情状态',s);
        return s;
    }

    /**
     * 进入番外
     * @param startPlotId 开始剧情id
     */
    async enterExtraStoryByStartPlotId(startPlotId: number) {
        let index = this.checkIsBranchStarted(startPlotId);
        if (index >= 0) {
            await PlotManager.startGameWithRecordIndex(this.itemArr[index].index, startPlotId);
            this._isInExtraStoryScene = true;
            GameModelManager.currPlots = [...PlotManager.getCurrentPlots()];
        } else {
            console.error('番外分支没有开启请先开启调用:startBranches');
        }
    }

    /**
     * 退出番外
     */
    async exitExtraStory() {
        let firstPlot = getPlotsIndex().firstPlot;
        await PlotManager.startGameWithRecordIndex(0, firstPlot);
        GameModelManager.currPlots = [...PlotManager.getCurrentPlots()];
        this._isInExtraStoryScene = false;
    }

}

export let ExtraStoryModelManager = new ExtraStoryModelManager1();