@inglorious/engine 24.12.0 → 24.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/behaviors/audio.js +20 -0
- package/src/core/core-events.js +1 -0
- package/src/core/engine.js +41 -0
- package/src/core/loops/animation-frame.js +10 -6
- package/src/core/loops/fixed.js +11 -5
- package/src/core/loops/max-dt.js +26 -0
- package/types/core/loops/animation-frame.d.ts +7 -2
- package/types/core/loops/fixed.d.ts +7 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.12.
|
|
3
|
+
"version": "24.12.1",
|
|
4
4
|
"description": "A JavaScript game engine written with global state, immutability, and pure functions in mind. Have fun(ctional programming) with it!",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
package/src/behaviors/audio.js
CHANGED
|
@@ -9,6 +9,8 @@ export function audio() {
|
|
|
9
9
|
// imported -- and a module that reaches for `window` on the way in cannot be imported
|
|
10
10
|
// where there is no window, which is anywhere a test runs.
|
|
11
11
|
let audioContext
|
|
12
|
+
// Whether the sound was quieted because the page was hidden, to bring it back.
|
|
13
|
+
let hushed = false
|
|
12
14
|
|
|
13
15
|
function context() {
|
|
14
16
|
audioContext ??= new (window.AudioContext || window.webkitAudioContext)()
|
|
@@ -80,6 +82,24 @@ export function audio() {
|
|
|
80
82
|
activeSources.delete(name)
|
|
81
83
|
},
|
|
82
84
|
|
|
85
|
+
// A hidden page goes quiet, and its sound comes back with it. Browsers keep
|
|
86
|
+
// playing Web Audio in a background tab, so a game would go on sounding from a
|
|
87
|
+
// tab the player has left. Suspending the context rather than stopping the
|
|
88
|
+
// sources keeps every sound where it was, music included. A context still
|
|
89
|
+
// waiting for the user's first gesture is left waiting: resuming it without one
|
|
90
|
+
// is refused by autoplay policy (https://developer.chrome.com/blog/autoplay).
|
|
91
|
+
visibilityChange(entity, state) {
|
|
92
|
+
if (state === "hidden") {
|
|
93
|
+
if (audioContext?.state !== "running") return
|
|
94
|
+
audioContext.suspend()
|
|
95
|
+
hushed = true
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
if (!hushed) return
|
|
99
|
+
hushed = false
|
|
100
|
+
audioContext.resume()
|
|
101
|
+
},
|
|
102
|
+
|
|
83
103
|
stop: release,
|
|
84
104
|
|
|
85
105
|
destroy: release,
|
package/src/core/core-events.js
CHANGED
package/src/core/engine.js
CHANGED
|
@@ -103,6 +103,10 @@ export class Engine {
|
|
|
103
103
|
*/
|
|
104
104
|
start() {
|
|
105
105
|
this._store.notify("start")
|
|
106
|
+
globalThis.document?.addEventListener?.(
|
|
107
|
+
"visibilitychange",
|
|
108
|
+
this._onVisibilityChange,
|
|
109
|
+
)
|
|
106
110
|
this._loop.start(this, ONE_SECOND / this._config.loop.fps)
|
|
107
111
|
}
|
|
108
112
|
|
|
@@ -111,11 +115,48 @@ export class Engine {
|
|
|
111
115
|
* this by notifying `quit`, which sets the flag the engine reads after each frame.
|
|
112
116
|
*/
|
|
113
117
|
stop() {
|
|
118
|
+
globalThis.document?.removeEventListener?.(
|
|
119
|
+
"visibilitychange",
|
|
120
|
+
this._onVisibilityChange,
|
|
121
|
+
)
|
|
114
122
|
this._store.notify("stop")
|
|
115
123
|
this._store.update()
|
|
116
124
|
this._loop.stop()
|
|
117
125
|
}
|
|
118
126
|
|
|
127
|
+
/**
|
|
128
|
+
* The page was hidden (another tab, a minimised window) or shown again: the game
|
|
129
|
+
* hears `visibilityChange` with the document's `visibilityState`, "hidden" or
|
|
130
|
+
* "visible", named after the DOM event it comes from.
|
|
131
|
+
*
|
|
132
|
+
* Hidden, it is heard at once rather than queued for the next frame, because
|
|
133
|
+
* browsers stop animation frames in a hidden page and throttle its timers, so the
|
|
134
|
+
* next frame may come only when the page is shown again: too late to quiet the
|
|
135
|
+
* sound. See MDN, Page Visibility API
|
|
136
|
+
* (https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API), and
|
|
137
|
+
* `requestAnimationFrame` ("paused in most browsers when running in background
|
|
138
|
+
* tabs").
|
|
139
|
+
*
|
|
140
|
+
* Shown, the loop leaves out the time it was away (`skipGap`), so the game carries
|
|
141
|
+
* on where it was rather than playing minutes in one frame; the cap on a frame's
|
|
142
|
+
* time (`loops/max-dt.js`) stays for the gaps no event reports. It is heard in the
|
|
143
|
+
* next frame, as any event.
|
|
144
|
+
*
|
|
145
|
+
* Nothing pauses by itself: whether to pause, open a menu or carry on is the
|
|
146
|
+
* game's to decide, and pausing here would unpause, on return, a game the player
|
|
147
|
+
* had paused.
|
|
148
|
+
*/
|
|
149
|
+
_onVisibilityChange = () => {
|
|
150
|
+
const state = document.visibilityState
|
|
151
|
+
if (state === "hidden") {
|
|
152
|
+
this._store.notify("visibilityChange", state)
|
|
153
|
+
this._store.update()
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
this._loop.skipGap()
|
|
157
|
+
this._store.notify("visibilityChange", state)
|
|
158
|
+
}
|
|
159
|
+
|
|
119
160
|
/**
|
|
120
161
|
* Returns the current game state.
|
|
121
162
|
* @returns {Entities} The entities of the current state.
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { cappedDt } from "./max-dt.js"
|
|
2
2
|
|
|
3
3
|
export class AnimationFrameLoop {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
start(engine) {
|
|
4
|
+
start(engine, msPerUpdate) {
|
|
5
|
+
this._msPerUpdate = msPerUpdate
|
|
6
|
+
this._previousTime = new Date()
|
|
8
7
|
this._tick(engine)
|
|
9
8
|
}
|
|
10
9
|
|
|
10
|
+
/** Measures the next frame from now: the time since the last one is not played. */
|
|
11
|
+
skipGap() {
|
|
12
|
+
this._previousTime = new Date()
|
|
13
|
+
}
|
|
14
|
+
|
|
11
15
|
stop() {
|
|
12
16
|
window.cancelAnimationFrame(this._id)
|
|
13
17
|
this._id = null
|
|
@@ -18,7 +22,7 @@ export class AnimationFrameLoop {
|
|
|
18
22
|
this._id = window.requestAnimationFrame(() => this._tick(engine))
|
|
19
23
|
const dt = currentTime - this._previousTime
|
|
20
24
|
|
|
21
|
-
engine.update(dt
|
|
25
|
+
engine.update(cappedDt(dt, this._msPerUpdate))
|
|
22
26
|
|
|
23
27
|
this._previousTime = currentTime
|
|
24
28
|
}
|
package/src/core/loops/fixed.js
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
|
-
|
|
1
|
+
import { cappedDt } from "./max-dt.js"
|
|
2
|
+
|
|
2
3
|
const NO_WAIT = 0
|
|
3
4
|
|
|
4
5
|
export class FixedLoop {
|
|
5
6
|
_shouldStop = false
|
|
6
7
|
|
|
7
8
|
async start(engine, msPerUpdate) {
|
|
8
|
-
|
|
9
|
+
this._previousTime = Date.now()
|
|
9
10
|
|
|
10
11
|
while (!this._shouldStop) {
|
|
11
12
|
const currentTime = Date.now()
|
|
12
|
-
const dt = currentTime -
|
|
13
|
+
const dt = currentTime - this._previousTime
|
|
13
14
|
|
|
14
|
-
engine.update(dt
|
|
15
|
+
engine.update(cappedDt(dt, msPerUpdate))
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
this._previousTime = currentTime
|
|
17
18
|
|
|
18
19
|
await sleep(Math.max(NO_WAIT, msPerUpdate - (Date.now() - currentTime)))
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
/** Measures the next frame from now: the time since the last one is not played. */
|
|
24
|
+
skipGap() {
|
|
25
|
+
this._previousTime = Date.now()
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
stop() {
|
|
23
29
|
this._shouldStop = true
|
|
24
30
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const ONE_SECOND = 1000
|
|
2
|
+
// The longest frame a loop reports, in milliseconds. A quarter of a second, as in
|
|
3
|
+
// Glenn Fiedler's "Fix Your Timestep!"
|
|
4
|
+
// (https://gafferongames.com/post/fix_your_timestep/), where it keeps a fixed-step
|
|
5
|
+
// world from the "spiral of death": a long frame owes many steps, those steps make
|
|
6
|
+
// the next frame long, and so on. It is well above any frame a working game makes
|
|
7
|
+
// (it allows down to 4 frames a second), so it never slows a slow device; a much
|
|
8
|
+
// lower cap, such as 1/60 s, would: at 30 frames a second the game would run at half
|
|
9
|
+
// speed, and a world turned faster than real time could never keep up.
|
|
10
|
+
const MAX_FRAME = 250
|
|
11
|
+
const NO_RATE = 0
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The milliseconds a frame may cover: `elapsed` as measured, but at most a quarter
|
|
15
|
+
* of a second, or one interval when the loop's own rate is slower than that. A tab
|
|
16
|
+
* left in the background stops its animation frames and slows its timers (Chrome
|
|
17
|
+
* wakes chained timers at most once a minute after five minutes hidden:
|
|
18
|
+
* https://developer.chrome.com/blog/timer-throttling-in-chrome-88), so the first
|
|
19
|
+
* frame back can measure minutes: passed on whole, it would jump the world that far
|
|
20
|
+
* ahead in one step. The engine leaves out the time a page was hidden anyway
|
|
21
|
+
* (`skipGap`); the cap is for the gaps no event reports: a debugger's breakpoint, a
|
|
22
|
+
* computer waking from sleep, a long freeze, and Node, where there is no page.
|
|
23
|
+
*/
|
|
24
|
+
export function cappedDt(elapsed, msPerUpdate = NO_RATE) {
|
|
25
|
+
return Math.min(elapsed, Math.max(MAX_FRAME, msPerUpdate)) / ONE_SECOND
|
|
26
|
+
}
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { Updatable } from "./index"
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Updates once per animation frame, as fast as the screen takes it. A frame covers
|
|
5
|
+
* at most a quarter of a second, so a tab back from the background does not jump.
|
|
6
|
+
*/
|
|
4
7
|
export class AnimationFrameLoop {
|
|
5
|
-
start(engine: Updatable): void
|
|
8
|
+
start(engine: Updatable, msPerUpdate?: number): void
|
|
9
|
+
/** Measures the next frame from now: the time since the last one is not played. */
|
|
10
|
+
skipGap(): void
|
|
6
11
|
stop(): void
|
|
7
12
|
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import type { Updatable } from "./index"
|
|
2
2
|
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Updates every `msPerUpdate` milliseconds. A frame covers at most a quarter of a
|
|
5
|
+
* second, or one interval if that is longer, so a tab back from the background
|
|
6
|
+
* does not jump.
|
|
7
|
+
*/
|
|
4
8
|
export class FixedLoop {
|
|
5
9
|
start(engine: Updatable, msPerUpdate: number): Promise<void>
|
|
10
|
+
/** Measures the next frame from now: the time since the last one is not played. */
|
|
11
|
+
skipGap(): void
|
|
6
12
|
stop(): void
|
|
7
13
|
}
|