86449bea-7b4f-45c9-b8ec-afd43345e746.js
9.34 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"use strict";
cc._RF.push(module, '86449vqe09Fybjsr9QzRedG', 'ActionManager');
// script/avg/ActionManager.ts
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionManager = void 0;
const ActionModel_1 = require("./model/ActionModel");
const EditorEvents_1 = require("./EditorEvents");
const GameRecord_1 = require("./game-data/GameRecord");
const simba_random_1 = require("simba-random");
const AudioManager_1 = require("../common/gameplay/managers/AudioManager");
const runningExecutors = new Set();
let _affectRecord = true;
class ActionExecutor {
constructor(action) {
this.sleep = (t) => {
return new Promise((resolve) => {
this._timerHandle = setTimeout(() => {
this._timerHandle = undefined;
resolve();
}, t * 1000);
});
};
this.execute = async () => {
runningExecutors.add(this);
if (this._action.trigger === ActionModel_1.TriggerType.Click) { // wait for click events
// await new Promise((resolve) => {
// this._disposable = ClickEvent.once(() => {
// resolve();
// })
// });
}
if (this._action.trigger === ActionModel_1.TriggerType.Event) {
if (!this._action.triggerEvent)
return;
return new Promise((resolve) => {
this._disposable = EditorEvents_1.EditorEvents.emitter.once(this._action.triggerEvent, async () => {
let result = await this.executeInternal();
resolve();
return result;
});
});
}
else {
return this.executeInternal();
}
};
this.executeInternal = async () => {
if (this._action.delay) {
await this.sleep(this._action.delay);
}
let finished = true;
if (this.nextExecutor && this.nextExecutor._action.trigger === ActionModel_1.TriggerType.PreviousStart) {
[finished] = await Promise.all([this.onExec(), this.nextExecutor.execute()]);
}
else {
finished = await this.onExec();
if (this.nextExecutor) {
await this.nextExecutor.execute();
}
}
if (finished) {
runningExecutors.delete(this);
}
};
this.stop = () => {
this.onStop();
if (this._disposable)
this._disposable.dispose();
if (this._timerHandle)
clearTimeout(this._timerHandle);
};
this._action = action;
}
}
class ModifyVariableExecutor extends ActionExecutor {
onExec() {
if (!_affectRecord)
return Promise.resolve(true);
try {
let action = this._action;
let varValue = GameRecord_1.GameRecord.getVariableValue(action.target);
let value = action.oprand.value;
if (action.oprand.type === ActionModel_1.OperandType.Const) {
if (typeof varValue === "number") {
value = Number.parseFloat(value);
}
}
else if (action.oprand.type === ActionModel_1.OperandType.Random) {
let arr = value.split("-");
let min = Number.parseFloat(arr[0]);
let max = Number.parseFloat(arr[1]);
value = simba_random_1.Random.range(min, max);
}
else if (action.oprand.type === ActionModel_1.OperandType.Variable) {
value = GameRecord_1.GameRecord.getVariableValue(value);
}
switch (action.operator) {
case ActionModel_1.VariableOperator.Assign:
break;
case ActionModel_1.VariableOperator.Plus:
varValue += value;
break;
case ActionModel_1.VariableOperator.Minus:
varValue -= value;
break;
case ActionModel_1.VariableOperator.Multiply:
varValue *= value;
break;
case ActionModel_1.VariableOperator.Divide:
varValue /= value;
break;
case ActionModel_1.VariableOperator.Modulo:
varValue %= value;
break;
}
GameRecord_1.GameRecord.setVariableValue(action.target, varValue);
}
catch (e) {
console.error(e);
}
return Promise.resolve(true);
}
onStop() {
}
}
class PlayAudioExecutor extends ActionExecutor {
async onExec() {
let action = this._action;
if (action.audioType === "effect") {
if (action.stopPreviousSound) {
AudioManager_1.AudioManager.stopAllEffect();
}
if (action.loopCount <= 0) {
this.audioId = await AudioManager_1.AudioManager.playEffect(action.filePath, 0);
return false;
}
else {
AudioManager_1.AudioManager.playEffect(action.filePath, action.loopCount);
}
}
else {
if (!action.filePath) {
AudioManager_1.AudioManager.stopMusic();
}
else {
AudioManager_1.AudioManager.playMusic(action.filePath);
}
if (_affectRecord) {
GameRecord_1.GameRecord.recordVariables.bgm = action.filePath;
}
}
return true;
}
onStop() {
if (this.audioId !== undefined) {
AudioManager_1.AudioManager.stopEffect(this.audioId);
}
}
}
class EmitEventExecutor extends ActionExecutor {
async onExec() {
if (this._action.emitEvent) {
await Promise.all(EditorEvents_1.EditorEvents.emitter.emit(this._action.emitEvent, this._action.param));
}
return Promise.resolve(true);
}
onStop() {
}
}
var ActionManager;
(function (ActionManager) {
function getExecutor(action) {
if (action.type === ActionModel_1.ActionType.ModifyVariable) {
return new ModifyVariableExecutor(action);
}
else if (action.type === ActionModel_1.ActionType.PlayAudio) {
return new PlayAudioExecutor(action);
}
else if (action.type === ActionModel_1.ActionType.EmitEvent) {
return new EmitEventExecutor(action);
}
return undefined;
}
let stopped = false;
let prevActions;
async function executeActions(actionData, filter, affectRecord = true) {
// if (prevActions === actionData) {
// throw new Error("Action already executed.");
// }
// prevActions = actionData;
_affectRecord = affectRecord;
if (!stopped) {
stop();
}
stopped = false;
let firstActionExecutor;
let triggerType = ActionModel_1.TriggerType.PreviousStart;
let executors = [];
let noWaitExecutors = [];
if (actionData.actions) {
let currExecutor;
for (let i = 0; i < actionData.actions.length; i++) {
if (i > 0 && actionData.actions[i].trigger === ActionModel_1.TriggerType.Event) {
if (firstActionExecutor) {
executors.push(firstActionExecutor);
firstActionExecutor = undefined;
}
}
let action = actionData.actions[i];
if (filter && !filter(action)) {
continue;
}
let executor = getExecutor(action);
if (executor) {
if (!firstActionExecutor) {
firstActionExecutor = executor;
triggerType = action.trigger;
currExecutor = executor;
}
else if (currExecutor) {
currExecutor.nextExecutor = executor;
currExecutor = executor;
}
}
}
if (firstActionExecutor) {
if (triggerType === ActionModel_1.TriggerType.PreviousStart || triggerType === ActionModel_1.TriggerType.PreviousEnd) {
executors.push(firstActionExecutor);
}
else {
noWaitExecutors.push(firstActionExecutor);
}
}
}
if (noWaitExecutors.length) {
noWaitExecutors.forEach((v) => v.execute());
}
let promises = executors.map(v => v.execute());
if (executors.length) {
await Promise.all(promises);
}
if (stopped)
return;
}
ActionManager.executeActions = executeActions;
function stop() {
if (stopped)
return;
stopped = true;
for (let executor of runningExecutors) {
executor.stop();
}
runningExecutors.clear();
//add line
// prevActions = undefined;
}
ActionManager.stop = stop;
})(ActionManager = exports.ActionManager || (exports.ActionManager = {}));
cc._RF.pop();