-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntimeTickLoop.js
More file actions
76 lines (68 loc) · 1.74 KB
/
Copy pathruntimeTickLoop.js
File metadata and controls
76 lines (68 loc) · 1.74 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
/*
Toolbox Aid
David Quesenberry
06/02/2026
runtimeTickLoop.js
*/
export const RUNTIME_TICK_LOOP_ERRORS = Object.freeze({
FIXED_DELTA_INVALID: "RUNTIME_TICK_FIXED_DELTA_INVALID",
});
export function createRuntimeTickLoop({ fixedDeltaMs }) {
if (!Number.isFinite(fixedDeltaMs) || fixedDeltaMs <= 0) {
return createTickResult({
tick: null,
errors: [
createTickError(
RUNTIME_TICK_LOOP_ERRORS.FIXED_DELTA_INVALID,
"Runtime tick loop requires explicit positive fixedDeltaMs.",
"fixedDeltaMs"
),
],
});
}
return createTickResult({
tick: Object.freeze({
frame: 0,
elapsedMs: 0,
fixedDeltaMs,
deltaSeconds: fixedDeltaMs / 1000,
}),
errors: [],
});
}
export function advanceRuntimeTick(tick) {
if (!tick || !Number.isFinite(tick.fixedDeltaMs) || tick.fixedDeltaMs <= 0) {
return createTickResult({
tick: null,
errors: [
createTickError(
RUNTIME_TICK_LOOP_ERRORS.FIXED_DELTA_INVALID,
"Runtime tick advance requires a valid fixedDeltaMs.",
"tick.fixedDeltaMs"
),
],
});
}
const deltaSeconds = Number.isFinite(tick.deltaSeconds) && tick.deltaSeconds > 0
? tick.deltaSeconds
: tick.fixedDeltaMs / 1000;
return createTickResult({
tick: Object.freeze({
frame: tick.frame + 1,
elapsedMs: tick.elapsedMs + tick.fixedDeltaMs,
fixedDeltaMs: tick.fixedDeltaMs,
deltaSeconds,
}),
errors: [],
});
}
function createTickResult({ tick, errors }) {
return Object.freeze({
valid: errors.length === 0,
tick,
errors: Object.freeze(errors),
});
}
function createTickError(code, message, path) {
return Object.freeze({ code, message, path });
}