PlotManager.js
9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"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();