PlotManager.js 33.6 KB

                (function() {
                    var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
                    var __module = nodeEnv ? module : {exports:{}};
                    var __filename = 'preview-scripts/assets/script/avg/PlotManager.js';
                    var __require = nodeEnv ? function (request) {
                        return cc.require(request);
                    } : function (request) {
                        return __quick_compile_project__.require(request, __filename);
                    };
                    function __define (exports, require, module) {
                        if (!nodeEnv) {__quick_compile_project__.registerModule(__filename, module);}"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();
                    }
                    if (nodeEnv) {
                        __define(__module.exports, __require, __module);
                    }
                    else {
                        __quick_compile_project__.registerModuleFunc(__filename, function () {
                            __define(__module.exports, __require, __module);
                        });
                    }
                })();
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImFzc2V0cy9zY3JpcHQvYXZnL1Bsb3RNYW5hZ2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUFBLG1EQUF5QztBQUN6QyxtREFBZ0Q7QUFDaEQsaURBQThDO0FBQzlDLHVEQUFvRDtBQUNwRCxxREFBb0g7QUFDcEgsaURBQW1HO0FBQ25HLGlEQUFzRDtBQUV0RCxJQUFpQixXQUFXLENBNE4zQjtBQTVORCxXQUFpQixXQUFXO0lBQ3hCLElBQUksT0FBTyxHQUFHLElBQUksd0JBQU8sQ0FBQztJQUMxQixJQUFJLFlBQVksR0FBbUIsRUFBRSxDQUFDO0lBRXRDLFNBQWdCLGVBQWU7UUFDM0IsT0FBTyxZQUFZLENBQUM7SUFDeEIsQ0FBQztJQUZlLDJCQUFlLGtCQUU5QixDQUFBO0lBRUQsMkJBQTJCO0lBQ2QsMEJBQWMsR0FBRyxPQUFPLENBQUMsV0FBVyxFQUFrQyxDQUFDO0lBRXBGLDRCQUE0QjtJQUNmLHlCQUFhLEdBQUcsT0FBTyxDQUFDLFdBQVcsRUFBNEQsQ0FBQztJQUVoRyw4QkFBa0IsR0FBRyxPQUFPLENBQUMsV0FBVyxFQUFrQyxDQUFDO0lBRXhGLElBQUksWUFBWSxHQUFHLHlCQUFhLENBQUMsZUFBZSxDQUFDO0lBRWpELElBQUksY0FBeUMsQ0FBQztJQUU5QyxTQUFnQixnQkFBZ0IsQ0FBQyxNQUFjO1FBQzNDLE9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3RbXTtcblxuICAgICAgICBwbG90U2VsZWN0aW9ucyA9IHt9O1xuXG4gICAgICAgIGxldCBpdGVtcyA9IEdhbWVSZWNvcmQuZ2V0Q3VycmVudFJlY29yZEl0ZW1zKCk7XG5cbiAgICAgICAgZm9yIChsZXQgcCBvZiBpdGVtcykge1xuICAgICAgICAgICAgaWYgKHAucykge1xuICAgICAgICAgICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgcC5wLmxlbmd0aDsgaSsrKSB7XG4gICAgICAgICAgICAgICAgICAgIGlmIChwLnNbaV0gIT09IHVuZGVmaW5lZCkge1xuICAgICAgICAgICAgICAgICAgICAgICAgcGxvdFNlbGVjdGlvbnNbcC5wW2ldXSA9IHAuc1tpXTtcbiAgICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH1cbiAgICAgICAgfVxuXG4gICAgICAgIC8vIOajgOafpeacquWujOW+hee7reWJp+aDhe+8jOWmguacieaWsOWJp+aDheS/ruaUueWtmOaho+aVsOaNrlxuICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHBsb3RzLmxlbmd0aDsgaSsrKSB7XG4gICAgICAgICAgICBsZXQgcGxvdElkID0gcGxvdHNbaV07XG4gICAgICAgICAgICBpZiAocGxvdElkID09PSBTcGVjaWFsUGxvdElkLlRvQmVDb250aW51ZWQpIHtcbiAgICAgICAgICAgICAgICBsZXQgaW5kZXggPSBpdGVtcy5sZW5ndGggLSAyO1xuICAgICAgICAgICAgICAgIGZvciAoOyBpbmRleCA+PSAwOyBpbmRleC0tKSB7XG4gICAgICAgICAgICAgICAgICAgIGlmIChpdGVtc1tpbmRleF0ucFtpXSA+PSAwKSB7XG4gICAgICAgICAgICAgICAgICAgICAgICBwbG90SWQgPSBpdGVtc1tpbmRleF0ucFtpXTtcbiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrO1xuICAgICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIGlmIChwbG90SWQgPCAwKSB0aHJvdyBuZXcgRXJyb3IoXCJSZWNvcmQgZXJyb3IuXCIpO1xuICAgICAgICAgICAgICAgIGxldCBwbG90ID0gKGF3YWl0IGdldFBsb3QocGxvdElkKSkhO1xuICAgICAgICAgICAgICAgIGxldCBzZWxzID0gaXRlbXNbaW5kZXhdLnM7XG4gICAgICAgICAgICAgICAgbGV0IG5leHRQbG90ID0gKGF3YWl0IGdldE5leHRQbG90KHBsb3QsIHNlbHMgPyBzZWxzW2ldIDogdW5kZWZpbmVkKSkhO1xuICAgICAgICAgICAgICAgIGlmIChuZXh0UGxvdC5pZCA+PSAwKSB7XG4gICAgICAgICAgICAgICAgICAgIGZvciAobGV0IGogPSBpbmRleCArIDE7IGogPCBpdGVtcy5sZW5ndGg7IGorKykgeyAvLyDkv67mlLnlrZjmoaPmlbDmja5cbiAgICAgICAgICAgICAgICAgICAgICAgIChpdGVtc1tpbmRleF0ucCBhcyBudW1iZXJbXSlbaV0gPSBuZXh0UGxvdC5pZDtcbiAgICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICBjdXJyZW50UGxvdHNbaV0gPSBuZXh0UGxvdCBhcyBSZWFkb25seVBsb3Q7XG4gICAgICAgICAgICB9XG4gICAgICAgIH1cblxuICAgICAgICBQbG90U3RhcnRFdmVudC5lbWl0KGN1cnJlbnRQbG90cyk7XG4gICAgICAgIHJldHVybiBjdXJyZW50UGxvdHM7XG4gICAgfVxuXG4gICAgZXhwb3J0IGFzeW5jIGZ1bmN0aW9uIHN0YXJ0TmV3UGxvdEJyYW5jaChwbG90SWQ6IG51bWJlcikge1xuICAgICAgICBsZXQgaW5kZXggPSBjdXJyZW50UGxvdHMuZmluZEluZGV4KHYgPT4gdiAmJiB2LmlkID09PSBwbG90SWQpO1xuICAgICAgICBpZiAoaW5kZXggPj0gMCkge1xuICAgICAgICAgICAgY29uc29sZS53YXJuKFwic3Rhc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ZDogbnVtYmVyO1xuICAgICAgICBpZiAoc2VsZWN0T3B0aW9uICE9PSB1bmRlZmluZWQgJiYgc2VsZWN0T3B0aW9uICE9PSBudWxsKSB7XG4gICAgICAgICAgICBpZiAocGxvdC5qdW1wLnR5cGUgIT09IFBsb3RKdW1wVHlwZS5TZWxlY3QpIHtcbiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoXCJQbG90IGp1bXAgdHlwZSBlcnJvclwiKTtcbiAgICAgICAgICAgIH1cbiAgICAgICAgICAgIGxldCBzZWxlY3RJbmRleDogbnVtYmVyO1xuICAgICAgICAgICAgaWYgKHR5cGVvZiBzZWxlY3RPcHRpb24gPT09IFwic3RyaW5nXCIpIHtcbiAgICAgICAgICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHBsb3QuanVtcC5qdW1wcy5sZW5ndGg7IGkrKykge1xuICAgICAgICAgICAgICAgICAgICBpZiAocGxvdC5qdW1wLmp1bXBzW2ldLmlkID09IHNlbGVjdE9wdGlvbikge1xuICAgICAgICAgICAgICAgICAgICAgICAgc2VsZWN0SW5kZXggPSBpO1xuICAgICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICBzZWxlY3RJbmRleCA9IHNlbGVjdE9wdGlvbjtcbiAgICAgICAgICAgIH1cbiAgICAgICAgICAgIGlmIChzZWxlY3RJbmRleCEgPj0gcGxvdC5qdW1wLmp1bXBzLmxlbmd0aCkge1xuICAgICAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcihcIkludmFsaWQgb3B0aW9uXCIpO1xuXG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBpZiAoc2F2ZVRvUmVjb3JkKSB7XG4gICAgICAgICAgICAgICAgR2FtZVJlY29yZC5zZXRTZWxlY3RPcHRpb25Gb3JQbG90KHBsb3QuaWQsIHNlbGVjdEluZGV4ISk7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBuZXh0UGxvdElkID0gcGxvdC5qdW1wLmp1bXBzW3NlbGVjdEluZGV4IV0udG9QbG90O1xuICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgaWYgKHBsb3QuanVtcC50eXBlICE9PSBQbG90SnVtcFR5cGUuQ29uZGl0aW9uKSB7XG4gICAgICAgICAgICAgICAgdGhyb3cgbmV3IEVycm9yKFwiUGxvdCBqdW1wIHR5cGUgZXJyb3JcIik7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBsZXQgY29uZGl0aW9ucyA9IHBsb3QuanVtcC5jb25kaXRpb25CcmFuY2hlcztcbiAgICAgICAgICAgIGlmIChjb25kaXRpb25zKSB7XG4gICAgICAgICAgICAgICAgdHJ5IHtcbiAgICAgICAgICAgICAgICAgICAgZm9yIChsZXQgY29uZCBvZiBjb25kaXRpb25zKSB7XG4gICAgICAgICAgICAgICAgICAgICAgICBpZiAoY29uZC5jb25kaXRpb24gJiYgZXZhbENvbmRpdGlvbkV4cHIoY29uZC5jb25kaXRpb24pKSB7XG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgbmV4dFBsb3RJZCA9IGNvbmQudG9QbG90O1xuICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrO1xuICAgICAgICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgfSBjYXRjaCAoZSkge1xuXG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICAgICAgaWYgKG5leHRQbG90SWQhID09PSB1bmRlZmluZWQpIG5leHRQbG90SWQgPSBwbG90Lmp1bXAudG9QbG90O1xuICAgICAgICB9XG4gICAgICAgIHJldHVybiBuZXh0UGxvdElkO1xuICAgIH1cblxuICAgIGV4cG9ydCBhc3luYyBmdW5jdGlvbiBjb21wbGV0ZVBsb3QocGxvdDogUmVhZG9ubHlQbG90LCBzZWxlY3RPcHRpb24/OiBudW1iZXIgfCBzdHJpbmc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bGV0IHJlY29yZEl0ZW1zID0gR2FtZVJlY29yZC5nZXRDdXJyZW50UmVjb3JkSXRlbXMoKTtcbiAgICAgICAgcGxvdFNlbGVjdGlvbnMgPSB7fTtcbiAgICAgICAgZm9yIChsZXQgcCBvZiByZWNvcmRJdGVtcykge1xuICAgICAgICAgICAgaWYgKHAucykge1xuICAgICAgICAgICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgcC5wLmxlbmd0aDsgaSsrKSB7XG4gICAgICAgICAgICAgICAgICAgIGlmIChwLnNbaV0gIT09IHVuZGVmaW5lZCkge1xuICAgICAgICAgICAgICAgICAgICAgICAgcGxvdFNlbGVjdGlvbnNbcC5wW2ldXSA9IHAuc1tpXTtcbiAgICAgICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH1cbiAgICAgICAgfVxuICAgICAgICBsZXQgcGxvdHMgPSByZWNvcmRJdGVtc1tyZWNvcmRJdGVtcy5sZW5ndGggLSAxXS5wO1xuICAgICAgICBjdXJyZW50UGxvdHMgPSBhd2FpdCBQcm9taXNlLmFsbChwbG90cy5tYXAodiA9PiBnZXRQbG90KHYpKSkgYXMgUmVhZG9ubHlQbG90W107XG4gICAgICAgIFBsb3RzUm9sbGJhY2tFdmVudC5lbWl0KGN1cnJlbnRQbG90cyk7XG4gICAgICAgIHJldHVybiBjdXJyZW50UGxvdHM7XG4gICAgfVxufSJdfQ==