ExtraStoryModelManager.ts
5.46 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
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();