PlotManager.js 9.62 KB
"use strict";
cc._RF.push(module, '53477m/IolGY50slmiyXtaj', 'PlotManager');
// script/avg/PlotManager.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlotManager = void 0;
const simba_eventkit_1 = require("simba-eventkit");
const ActionManager_1 = require("./ActionManager");
const EditorEvents_1 = require("./EditorEvents");
const GameRecord_1 = require("./game-data/GameRecord");
const PlotsData_1 = require("./game-data/PlotsData");
const PlotModel_1 = require("./model/PlotModel");
const PlotUtils_1 = require("./utils/PlotUtils");
var PlotManager;
(function (PlotManager) {
    let emitter = new simba_eventkit_1.Emitter;
    let currentPlots = [];
    function getCurrentPlots() {
        return currentPlots;
    }
    PlotManager.getCurrentPlots = getCurrentPlots;
    // 剧情开始,需要在特定场景展现、执行Action等
    PlotManager.PlotStartEvent = emitter.createEvent();
    // 剧情将要开始,可根据将要开始的剧情修改存档数据保存
    PlotManager.PlotWillStart = emitter.createEvent();
    PlotManager.PlotsRollbackEvent = emitter.createEvent();
    let customPlotId = PlotModel_1.SpecialPlotId.CustomPlotStart;
    let plotSelections;
    function getPlotSelection(plotId) {
        return plotSelections[plotId];
    }
    PlotManager.getPlotSelection = getPlotSelection;
    async function init() {
        await PlotsData_1.initPlotsData();
        GameRecord_1.GameRecord.initRecords();
        customPlotId = GameRecord_1.GameRecord.globalVariables.customPlotId;
        PlotsData_1.initCustomPlots(GameRecord_1.GameRecord.globalVariables.customPlots);
        EditorEvents_1.EditorEvents.START_PLOT_BRANCH.on((param) => {
            let plotId = parseInt(param);
            return startNewPlotBranch(plotId);
        });
    }
    PlotManager.init = init;
    // Add custom plot and return plot id
    function addCustomPlot(plot) {
        plot.id = customPlotId--;
        PlotsData_1.addCustomPlotData(plot);
        GameRecord_1.GameRecord.globalVariables.customPlotId = customPlotId;
        GameRecord_1.GameRecord.globalVariables.customPlots = PlotsData_1.getChapterPlots(-100);
        GameRecord_1.GameRecord.autoSave();
    }
    PlotManager.addCustomPlot = addCustomPlot;
    function addCustomPlots(plots) {
        for (let plot of plots) {
            plot.id = customPlotId--;
            PlotsData_1.addCustomPlotData(plot);
        }
        GameRecord_1.GameRecord.globalVariables.customPlotId = customPlotId;
        GameRecord_1.GameRecord.globalVariables.customPlots = PlotsData_1.getChapterPlots(-100);
        GameRecord_1.GameRecord.autoSave();
    }
    PlotManager.addCustomPlots = addCustomPlots;
    async function start(recordIndex = 0) {
        let plots = GameRecord_1.GameRecord.startGameWithRecord(recordIndex);
        currentPlots = await Promise.all(plots.map(v => PlotsData_1.getPlot(v)));
        plotSelections = {};
        let items = GameRecord_1.GameRecord.getCurrentRecordItems();
        for (let p of items) {
            if (p.s) {
                for (let i = 0; i < p.p.length; i++) {
                    if (p.s[i] !== undefined) {
                        plotSelections[p.p[i]] = p.s[i];
                    }
                }
            }
        }
        // 检查未完待续剧情,如有新剧情修改存档数据
        for (let i = 0; i < plots.length; i++) {
            let plotId = plots[i];
            if (plotId === PlotModel_1.SpecialPlotId.ToBeContinued) {
                let index = items.length - 2;
                for (; index >= 0; index--) {
                    if (items[index].p[i] >= 0) {
                        plotId = items[index].p[i];
                        break;
                    }
                }
                if (plotId < 0)
                    throw new Error("Record error.");
                let plot = (await PlotsData_1.getPlot(plotId));
                let sels = items[index].s;
                let nextPlot = (await getNextPlot(plot, sels ? sels[i] : undefined));
                if (nextPlot.id >= 0) {
                    for (let j = index + 1; j < items.length; j++) { // 修改存档数据
                        items[index].p[i] = nextPlot.id;
                    }
                }
                currentPlots[i] = nextPlot;
            }
        }
        PlotManager.PlotStartEvent.emit(currentPlots);
        return currentPlots;
    }
    PlotManager.start = start;
    async function startNewPlotBranch(plotId) {
        let index = currentPlots.findIndex(v => v && v.id === plotId);
        if (index >= 0) {
            console.warn("startNewPlotBranch: plot already exist.", plotId);
            return -1;
        }
        let plot = await PlotsData_1.getPlot(plotId);
        await Promise.all(PlotManager.PlotWillStart.emit(plot, index));
        let history = GameRecord_1.GameRecord.getCurrentRecordItems();
        index = history[history.length - 1].p.length;
        let plots = GameRecord_1.GameRecord.startPlot(plotId, index);
        currentPlots = await Promise.all(plots.map(v => PlotsData_1.getPlot(v)));
        PlotManager.PlotStartEvent.emit(currentPlots);
        return index;
    }
    PlotManager.startNewPlotBranch = startNewPlotBranch;
    async function getNextPlot(plot, selectOption) {
        let id = getNextPlotId(plot, selectOption);
        return PlotsData_1.getPlot(id);
    }
    PlotManager.getNextPlot = getNextPlot;
    function getNextPlotId(plot, selectOption, saveToRecord) {
        let nextPlotId;
        if (selectOption !== undefined && selectOption !== null) {
            if (plot.jump.type !== PlotModel_1.PlotJumpType.Select) {
                throw new Error("Plot jump type error");
            }
            let selectIndex;
            if (typeof selectOption === "string") {
                for (let i = 0; i < plot.jump.jumps.length; i++) {
                    if (plot.jump.jumps[i].id == selectOption) {
                        selectIndex = i;
                    }
                }
            }
            else {
                selectIndex = selectOption;
            }
            if (selectIndex >= plot.jump.jumps.length) {
                throw new Error("Invalid option");
            }
            if (saveToRecord) {
                GameRecord_1.GameRecord.setSelectOptionForPlot(plot.id, selectIndex);
            }
            nextPlotId = plot.jump.jumps[selectIndex].toPlot;
        }
        else {
            if (plot.jump.type !== PlotModel_1.PlotJumpType.Condition) {
                throw new Error("Plot jump type error");
            }
            let conditions = plot.jump.conditionBranches;
            if (conditions) {
                try {
                    for (let cond of conditions) {
                        if (cond.condition && PlotUtils_1.evalConditionExpr(cond.condition)) {
                            nextPlotId = cond.toPlot;
                            break;
                        }
                    }
                }
                catch (e) {
                }
            }
            if (nextPlotId === undefined)
                nextPlotId = plot.jump.toPlot;
        }
        return nextPlotId;
    }
    async function completePlot(plot, selectOption, customData) {
        let plotIndex = currentPlots.findIndex(v => v && v.id === plot.id);
        if (plotIndex < 0)
            throw new Error("Invalid plot");
        GameRecord_1.GameRecord.startTransaction();
        if (selectOption !== undefined) {
            await Promise.all(EditorEvents_1.EditorEvents.emitter.emit("SELECT_OPTION_" + ((typeof selectOption === "number") ? selectOption + 1 : selectOption)));
        }
        await Promise.all(EditorEvents_1.EditorEvents.PLOT_END.emit(plot.id.toString()));
        ActionManager_1.ActionManager.stop();
        let nextPlotId = getNextPlotId(plot, selectOption, true);
        if (customData) {
            GameRecord_1.GameRecord.setPlotCustomData(plotIndex, customData);
        }
        let nextPlot = await PlotsData_1.getPlot(nextPlotId);
        await Promise.all(PlotManager.PlotWillStart.emit(nextPlot, plotIndex));
        let plots = GameRecord_1.GameRecord.startPlot(nextPlotId, plotIndex);
        GameRecord_1.GameRecord.endTransaction();
        if (typeof selectOption === "number") {
            plotSelections[plot.id] = selectOption;
        }
        currentPlots = await Promise.all(plots.map(v => PlotsData_1.getPlot(v)));
        PlotManager.PlotStartEvent.emit(currentPlots);
        return currentPlots;
    }
    PlotManager.completePlot = completePlot;
    async function rollbackToPlot(plot, reverseFind = false, preserveVars) {
        GameRecord_1.GameRecord.rollbackToPlot(plot, reverseFind, preserveVars);
        return rollbackFinished();
    }
    PlotManager.rollbackToPlot = rollbackToPlot;
    function rollbackToIndex(index, preserveVars) {
        GameRecord_1.GameRecord.rollbackToIndex(index, preserveVars);
        return rollbackFinished();
    }
    PlotManager.rollbackToIndex = rollbackToIndex;
    async function rollbackFinished() {
        let recordItems = GameRecord_1.GameRecord.getCurrentRecordItems();
        plotSelections = {};
        for (let p of recordItems) {
            if (p.s) {
                for (let i = 0; i < p.p.length; i++) {
                    if (p.s[i] !== undefined) {
                        plotSelections[p.p[i]] = p.s[i];
                    }
                }
            }
        }
        let plots = recordItems[recordItems.length - 1].p;
        currentPlots = await Promise.all(plots.map(v => PlotsData_1.getPlot(v)));
        PlotManager.PlotsRollbackEvent.emit(currentPlots);
        return currentPlots;
    }
})(PlotManager = exports.PlotManager || (exports.PlotManager = {}));

cc._RF.pop();