-
Notifications
You must be signed in to change notification settings - Fork 21
/
RunInNFrames.ts
403 lines (365 loc) · 13.1 KB
/
RunInNFrames.ts
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import type { PlayerType } from "isaac-typescript-definitions";
import { ModCallback } from "isaac-typescript-definitions";
import { game } from "../../../core/cachedClasses";
import { Exported } from "../../../decorators";
import { ISCFeature } from "../../../enums/ISCFeature";
import { arrayRemoveInPlace } from "../../../functions/array";
import { restart } from "../../../functions/run";
import { Feature } from "../../private/Feature";
import type { RoomHistory } from "./RoomHistory";
/** Used for `runInNFrames` functions. */
interface QueuedFunction {
func: () => void;
frameCountToFire: int;
numRoomsEntered: int;
cancelIfRoomChanges: boolean;
}
/**
* Used for `setInterval` functions.
*
* The return value is whether to continue the function from firing.
*/
interface IntervalFunction extends QueuedFunction {
func: () => boolean;
numIntervalFrames: int;
}
const v = {
run: {
queuedGameFunctions: [] as QueuedFunction[],
queuedRenderFunctions: [] as QueuedFunction[],
intervalGameFunctions: [] as IntervalFunction[],
intervalRenderFunctions: [] as IntervalFunction[],
},
};
export class RunInNFrames extends Feature {
/** @internal */
public override v = v;
public override vConditionalFunc = (): boolean => false;
private readonly roomHistory: RoomHistory;
/** @internal */
constructor(roomHistory: RoomHistory) {
super();
this.featuresUsed = [ISCFeature.ROOM_HISTORY];
this.callbacksUsed = [
// 1
[ModCallback.POST_UPDATE, this.postUpdate],
// 2
[ModCallback.POST_RENDER, this.postRender],
];
this.roomHistory = roomHistory;
}
// ModCallback.POST_UPDATE (1)
private readonly postUpdate = (): void => {
const gameFrameCount = game.GetFrameCount();
const numRoomsEntered = this.roomHistory.getNumRoomsEntered();
checkExecuteQueuedFunctions(
v.run.queuedGameFunctions,
gameFrameCount,
numRoomsEntered,
);
checkExecuteIntervalFunctions(
v.run.intervalGameFunctions,
gameFrameCount,
numRoomsEntered,
);
};
// ModCallback.POST_RENDER (2)
private readonly postRender = (): void => {
const renderFrameCount = Isaac.GetFrameCount();
const numRoomsEntered = this.roomHistory.getNumRoomsEntered();
checkExecuteQueuedFunctions(
v.run.queuedRenderFunctions,
renderFrameCount,
numRoomsEntered,
);
checkExecuteIntervalFunctions(
v.run.intervalRenderFunctions,
renderFrameCount,
numRoomsEntered,
);
};
/**
* Helper function to restart on the next render frame. Useful because it is impossible to restart
* the game inside of the `POST_NEW_ROOM`, `POST_NEW_LEVEL`, or `POST_GAME_STARTED` callbacks when
* a run is first starting.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.RUN_IN_N_FRAMES`.
*
* @param character Optional. If specified, will restart the game as the specified character.
* @public
*/
@Exported
public restartNextRenderFrame(character?: PlayerType): void {
this.runNextRenderFrame(() => {
restart(character);
});
}
/**
* Supply a function to run N game frames from now in the `POST_UPDATE` callback.
*
* For a usage example, see the documentation for the `runNextGameFrame`, which is used in a
* similar way.
*
* Note that this function will not handle saving and quitting. If a player saving and quitting
* before the deferred function fires would cause a bug in your mod, then you should handle
* deferred functions manually using serializable data.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.RUN_IN_N_FRAMES`.
*
* @param func The function to run.
* @param numGameFrames The amount of game frames to wait before running the function.
* @param cancelIfRoomChanges Optional. Whether to cancel running the function if a new room is
* loaded in the interim. Default is false.
* @public
*/
@Exported
public runInNGameFrames(
func: () => void,
numGameFrames: int,
cancelIfRoomChanges = false,
): void {
const gameFrameCount = game.GetFrameCount();
const numRoomsEntered = this.roomHistory.getNumRoomsEntered();
const frameCountToFire = gameFrameCount + numGameFrames;
const queuedFunction: QueuedFunction = {
func,
frameCountToFire,
numRoomsEntered,
cancelIfRoomChanges,
};
v.run.queuedGameFunctions.push(queuedFunction);
}
/**
* Supply a function to run N render frames from now in the `POST_RENDER` callback.
*
* For a usage example, see the documentation for the `runNextGameFrame`, which is used in a
* similar way.
*
* Note that this function will not handle saving and quitting. If a player saving and quitting
* before the deferred function fires would cause a bug in your mod, then you should handle
* deferred functions manually using serializable data.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.RUN_IN_N_FRAMES`.
*
* @param func The function to run.
* @param numRenderFrames The amount of render frames to wait before running the function.
* @param cancelIfRoomChanges Optional. Whether to cancel running the function if a new room is
* loaded in the interim. Default is false.
* @public
*/
@Exported
public runInNRenderFrames(
func: () => void,
numRenderFrames: int,
cancelIfRoomChanges = false,
): void {
const renderFrameCount = Isaac.GetFrameCount();
const numRoomsEntered = this.roomHistory.getNumRoomsEntered();
const frameCountToFire = renderFrameCount + numRenderFrames;
const queuedFunction: QueuedFunction = {
func,
frameCountToFire,
numRoomsEntered,
cancelIfRoomChanges,
};
v.run.queuedRenderFunctions.push(queuedFunction);
}
/**
* Supply a function to run on the next `POST_UPDATE` callback.
*
* For example:
*
* ```ts
* const NUM_EXPLODER_EXPLOSIONS = 5;
*
* function useItemExploder(player: EntityPlayer) {
* playSound("exploderBegin");
* explode(player, NUM_EXPLODER_EXPLOSIONS);
* }
*
* function explode(player: EntityPlayer, numFramesLeft: int) {
* Isaac.Explode(player, undefined, 1);
* numFramesLeft -= 1;
* if (numFramesLeft === 0) {
* runNextFrame(() => {
* explode(player, numFramesLeft);
* });
* }
* }
* ```
*
* Note that this function will not handle saving and quitting. If a player saving and quitting
* before the deferred function fires would cause a bug in your mod, then you should handle
* deferred functions manually using serializable data.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.RUN_IN_N_FRAMES`.
*
* @param func The function to run.
* @param cancelIfRoomChanges Optional. Whether to cancel running the function if a new room is
* loaded in the interim. Default is false.
* @public
*/
@Exported
public runNextGameFrame(func: () => void, cancelIfRoomChanges = false): void {
this.runInNGameFrames(func, 1, cancelIfRoomChanges);
}
/**
* Supply a function to run on the next `POST_RENDER` callback.
*
* For a usage example, see the documentation for the `runNextGameFrame`, which is used in a
* similar way.
*
* Note that this function will not handle saving and quitting.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.RUN_IN_N_FRAMES`.
*
* @param func The function to run.
* @param cancelIfRoomChanges Optional. Whether to cancel running the function if a new room is
* loaded in the interim. Default is false.
* @public
*/
@Exported
public runNextRenderFrame(
func: () => void,
cancelIfRoomChanges = false,
): void {
this.runInNRenderFrames(func, 1, cancelIfRoomChanges);
}
/**
* Supply a function to be repeatedly run on an interval of N game frames in the `POST_UPDATE`
* callback. The function will continue to be fired until `false` is returned from the function.
*
* This is similar to the `setInterval` vanilla JavaScript function, except there is no
* corresponding `clearInterval` function. (Instead, the return value from the supplied function
* is used to stop the interval.)
*
* Note that this function will not handle saving and quitting. You must manually restart any
* intervals if the player saves and quits in the middle of a run.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.RUN_IN_N_FRAMES`.
*
* @param func The function to repeatedly run on an interval.
* @param numGameFrames The amount of game frames to wait between each run.
* @param runImmediately Whether to execute the function right now before waiting for the
* interval.
* @param cancelIfRoomChanges Optional. Whether to cancel running the function if a new room is
* loaded in the interim. Default is false.
* @public
*/
@Exported
public setIntervalGameFrames(
func: () => boolean,
numGameFrames: int,
runImmediately: boolean,
cancelIfRoomChanges = false,
): void {
if (runImmediately) {
const returnValue = func();
if (!returnValue) {
return;
}
}
const gameFrameCount = game.GetFrameCount();
const numRoomsEntered = this.roomHistory.getNumRoomsEntered();
const intervalFunction: IntervalFunction = {
func,
frameCountToFire: gameFrameCount + numGameFrames,
numRoomsEntered,
cancelIfRoomChanges,
numIntervalFrames: numGameFrames,
};
v.run.intervalGameFunctions.push(intervalFunction);
}
/**
* Supply a function to be repeatedly run on an interval of N render frames in the `POST_RENDER`
* callback. The function will continue to be fired until `false` is returned from the function.
*
* This is similar to the `setInterval` vanilla JavaScript function, except there is no
* corresponding `clearInterval` function. (Instead, the return value from the supplied function
* is used to stop the interval.)
*
* Note that this function will not handle saving and quitting. You must manually restart any
* intervals if the player saves and quits in the middle of a run.
*
* In order to use this function, you must upgrade your mod with `ISCFeature.RUN_IN_N_FRAMES`.
*
* @param func The function to repeatedly run on an interval.
* @param numRenderFrames The amount of game frames to wait between each run.
* @param runImmediately Whether to execute the function right now before waiting for the
* interval.
* @param cancelIfRoomChanges Optional. Whether to cancel running the function if a new room is
* loaded in the interim. Default is false.
* @public
*/
@Exported
public setIntervalRenderFrames(
func: () => boolean,
numRenderFrames: int,
runImmediately: boolean,
cancelIfRoomChanges = false,
): void {
if (runImmediately) {
const returnValue = func();
if (!returnValue) {
return;
}
}
const renderFrameCount = Isaac.GetFrameCount();
const numRoomsEntered = this.roomHistory.getNumRoomsEntered();
const intervalFunction: IntervalFunction = {
func,
frameCountToFire: renderFrameCount + numRenderFrames,
numRoomsEntered,
cancelIfRoomChanges,
numIntervalFrames: numRenderFrames,
};
v.run.intervalRenderFunctions.push(intervalFunction);
}
}
function checkExecuteQueuedFunctions(
// eslint-disable-next-line isaacscript/prefer-readonly-parameter-types
queuedFunctions: QueuedFunction[],
frameCount: int,
newNumRoomsEntered: int,
) {
const firingFunctions = queuedFunctions.filter(
({ frameCountToFire }) => frameCount >= frameCountToFire,
);
for (const firingFunction of firingFunctions) {
const { func, cancelIfRoomChanges, numRoomsEntered } = firingFunction;
if (!cancelIfRoomChanges || numRoomsEntered === newNumRoomsEntered) {
func();
}
arrayRemoveInPlace(queuedFunctions, firingFunction);
}
}
function checkExecuteIntervalFunctions(
// eslint-disable-next-line isaacscript/prefer-readonly-parameter-types
intervalFunctions: IntervalFunction[],
frameCount: int,
newNumRoomsEntered: int,
) {
const firingFunctions = intervalFunctions.filter(
({ frameCountToFire }) => frameCount >= frameCountToFire,
);
for (const firingFunction of firingFunctions) {
const { func, cancelIfRoomChanges, numRoomsEntered, numIntervalFrames } =
firingFunction;
let returnValue = false;
if (!cancelIfRoomChanges || numRoomsEntered === newNumRoomsEntered) {
returnValue = func();
}
arrayRemoveInPlace(intervalFunctions, firingFunction);
// Queue the next interval (as long as the function did not return false).
if (returnValue) {
const newIntervalFunction: IntervalFunction = {
func,
frameCountToFire: frameCount + numIntervalFrames,
numRoomsEntered,
cancelIfRoomChanges,
numIntervalFrames,
};
intervalFunctions.push(newIntervalFunction);
}
}
}