@inglorious/engine 24.1.4 → 24.1.5
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 +7 -7
- package/src/animation/due.js +6 -20
- package/src/behaviors/at-fps.js +6 -17
- package/src/core/engine.js +4 -7
- package/src/core/loops/index.js +6 -15
- package/src/core/types.js +2 -14
- package/src/systems/ticks.js +7 -20
- package/src/core/loops/elapsed.js +0 -22
- package/src/core/loops/flash.js +0 -13
- package/src/core/loops/lag.js +0 -26
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.1.
|
|
3
|
+
"version": "24.1.5",
|
|
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",
|
|
@@ -33,17 +33,17 @@
|
|
|
33
33
|
},
|
|
34
34
|
"sideEffects": false,
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@inglorious/store": "^12.
|
|
37
|
-
"@inglorious/utils": "^5.
|
|
36
|
+
"@inglorious/store": "^12.7.0",
|
|
37
|
+
"@inglorious/utils": "^5.4.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"vitest": "^5.
|
|
40
|
+
"vitest": "^5.1.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@inglorious/eslint-config": "1.2.0",
|
|
44
|
-
"prettier": "^3.9.
|
|
45
|
-
"vite": "^8.
|
|
46
|
-
"vitest": "^5.
|
|
44
|
+
"prettier": "^3.9.10",
|
|
45
|
+
"vite": "^8.3.8",
|
|
46
|
+
"vitest": "^5.1.0"
|
|
47
47
|
},
|
|
48
48
|
"engines": {
|
|
49
49
|
"node": ">= 26"
|
package/src/animation/due.js
CHANGED
|
@@ -2,32 +2,18 @@ const NO_TIME_PASSED = 0
|
|
|
2
2
|
const WITHIN = 1e-9
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Whether an interval has passed, for `atFps` and `ticks`. Returns the time to
|
|
6
|
+
* account for on this turn, so that six frames of 1/60 s add up to the 1/10 s
|
|
7
|
+
* an entity moving ten times a second must move by, and the time still to wait.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* arrived" is written down once rather than twice.
|
|
10
|
-
*
|
|
11
|
-
* It hands back the time to account for on this turn and the time still to come, rather
|
|
12
|
-
* than only saying yes: an entity held to ten frames a second is told how long it has
|
|
13
|
-
* been since its last move, because six frames of a sixtieth of a second have to come
|
|
14
|
-
* to a tenth of a second for a game moving by velocity times dt.
|
|
15
|
-
*
|
|
16
|
-
* This is not the engine's `Ticker`, which answers the same question and then forgets
|
|
17
|
-
* how long it took: it resets its counter to zero as it fires, so by the time the
|
|
18
|
-
* handler ran, the time it had just measured was gone.
|
|
19
|
-
*
|
|
20
|
-
* @param {number} interval - How long between turns, in seconds.
|
|
21
|
-
* @returns {Function} Given the time already waited and the time just passed.
|
|
9
|
+
* @param {number} interval - Seconds between turns.
|
|
10
|
+
* @returns {(clock: number, dt: number) => { since: number, elapsed?: number }}
|
|
22
11
|
*/
|
|
23
12
|
export function dueAt(interval) {
|
|
24
13
|
return function isDue(clock, dt) {
|
|
25
14
|
const since = clock + dt
|
|
26
15
|
|
|
27
|
-
//
|
|
28
|
-
// six of a sixtieth of a second come to a hair under a tenth of one, which would
|
|
29
|
-
// otherwise leave something moving ten times a second moving at nine. An interval of
|
|
30
|
-
// nothing is never short of anything, so that moves every time it is asked.
|
|
16
|
+
// Summed frame times fall a hair short of the interval they should reach.
|
|
31
17
|
if (since + interval * WITHIN < interval) return { since }
|
|
32
18
|
|
|
33
19
|
return { elapsed: since, since: NO_TIME_PASSED }
|
package/src/behaviors/at-fps.js
CHANGED
|
@@ -3,24 +3,13 @@ import { dueAt } from "../animation/due.js"
|
|
|
3
3
|
const ONE_SECOND = 1
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Updates an entity at its own rate rather than the loop's: the loop stays as
|
|
7
|
+
* fast as the screen, and the entity's `update` runs only when its interval
|
|
8
|
+
* has passed, with all the time since its last one. Put it last in the type's
|
|
9
|
+
* behaviors, so it wraps the `update` it holds back.
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* input and the screen smooth -- and each entity that wants to be slower keeps its own
|
|
11
|
-
* time and is only updated when that time has run out.
|
|
12
|
-
*
|
|
13
|
-
* The rate is a ceiling and not a floor: a frame that leaves more time than the rate
|
|
14
|
-
* allows carries all of it into the next update, so the entity still accounts for every
|
|
15
|
-
* second that really went by.
|
|
16
|
-
*
|
|
17
|
-
* Put it **last** in a type's behaviors. Each one wraps the ones before it and calls on
|
|
18
|
-
* to them, so the last one is the one the store reaches first; anywhere else it is
|
|
19
|
-
* wrapped in turn and never gets a say.
|
|
20
|
-
*
|
|
21
|
-
* @param {number} fps - How many times a second this entity is updated. `Infinity` for
|
|
22
|
-
* every frame, which is also what the engine's default is.
|
|
23
|
-
* @returns {Function} A behavior wrapping whatever it is put in front of.
|
|
11
|
+
* @param {number} fps - Updates a second. `Infinity` for every frame.
|
|
12
|
+
* @returns {Function} The behavior.
|
|
24
13
|
*/
|
|
25
14
|
export function atFps(fps) {
|
|
26
15
|
const isDue = dueAt(ONE_SECOND / fps)
|
package/src/core/engine.js
CHANGED
|
@@ -57,9 +57,6 @@ export class Engine {
|
|
|
57
57
|
constructor(...gameConfigs) {
|
|
58
58
|
this._config = extendWith(merger, DEFAULT_GAME_CONFIG, ...gameConfigs)
|
|
59
59
|
|
|
60
|
-
// Said before anything is made, because a type that was never declared is not an error
|
|
61
|
-
// anywhere after this point -- it augments into an empty type, and everything sent to
|
|
62
|
-
// the entity quietly goes nowhere.
|
|
63
60
|
assertTypesAreDeclared(this._config)
|
|
64
61
|
|
|
65
62
|
const devMode = this._config.entities.game?.devMode
|
|
@@ -80,6 +77,7 @@ export class Engine {
|
|
|
80
77
|
...this._config,
|
|
81
78
|
middlewares,
|
|
82
79
|
updateMode: "manual",
|
|
80
|
+
devMode: Boolean(devMode),
|
|
83
81
|
})
|
|
84
82
|
this._loop = new Loop[this._config.loop.type]()
|
|
85
83
|
|
|
@@ -112,10 +110,8 @@ export class Engine {
|
|
|
112
110
|
}
|
|
113
111
|
|
|
114
112
|
/**
|
|
115
|
-
* Stops the game engine, halting the loop and notifying the store.
|
|
116
|
-
*
|
|
117
|
-
* A game does this by notifying `quit`, which the `game` behaviour turns into a flag
|
|
118
|
-
* the engine reads after the frame.
|
|
113
|
+
* Stops the game engine, halting the loop and notifying the store. A game does
|
|
114
|
+
* this by notifying `quit`, which sets the flag the engine reads after each frame.
|
|
119
115
|
*/
|
|
120
116
|
stop() {
|
|
121
117
|
this._store.notify("stop")
|
|
@@ -137,6 +133,7 @@ export class Engine {
|
|
|
137
133
|
* @param {number} dt - Delta time since the last update in milliseconds.
|
|
138
134
|
*/
|
|
139
135
|
update(dt) {
|
|
136
|
+
// A frame already scheduled when the game quit still fires once.
|
|
140
137
|
if (this._store.getState().game?.quit) {
|
|
141
138
|
this.stop()
|
|
142
139
|
return
|
package/src/core/loops/index.js
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
1
|
import { AnimationFrameLoop } from "./animation-frame.js"
|
|
2
|
-
import { ElapsedLoop } from "./elapsed.js"
|
|
3
2
|
import { FixedLoop } from "./fixed.js"
|
|
4
|
-
import { FlashLoop } from "./flash.js"
|
|
5
|
-
import { LagLoop } from "./lag.js"
|
|
6
3
|
|
|
7
4
|
/**
|
|
8
|
-
* The loops a game can ask for, by name.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* world to turn slowly and the pointer to keep up does not choose a slower loop -- it
|
|
14
|
-
* says so per entity with `atFps`, and leaves the loop as fast as the screen takes it.
|
|
5
|
+
* The loops a game can ask for, by name. The loop decides when `update` fires,
|
|
6
|
+
* and so when the screen is drawn: `animationFrame` runs as fast as the screen
|
|
7
|
+
* takes it, `fixed` waits for its rate and makes everything wait with it. A game
|
|
8
|
+
* that wants the world to turn slowly and the pointer to keep up says so per
|
|
9
|
+
* entity with `atFps`, and leaves the loop fast.
|
|
15
10
|
*/
|
|
16
11
|
export const Loop = {
|
|
17
12
|
animationFrame: AnimationFrameLoop,
|
|
18
13
|
fixed: FixedLoop,
|
|
19
|
-
lag: LagLoop,
|
|
20
|
-
elapsed: ElapsedLoop,
|
|
21
|
-
flash: FlashLoop,
|
|
22
14
|
}
|
|
23
15
|
|
|
24
16
|
/**
|
|
25
|
-
* @typedef {"animationFrame" | "fixed"
|
|
26
|
-
* The name of a loop, as written in a game's `loop.type`.
|
|
17
|
+
* @typedef {"animationFrame" | "fixed"} LoopType - The name of a loop, as written in a game's `loop.type`.
|
|
27
18
|
*/
|
package/src/core/types.js
CHANGED
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
import { namesNear } from "@inglorious/utils/data-structures/string.js"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* A type that was never declared is not an error anywhere downstream: it augments into an
|
|
7
|
-
* empty type, so the entity stands there with none of the handlers, rendering or collision
|
|
8
|
-
* its type was meant to give it, and every event sent to it goes nowhere. Nothing about
|
|
9
|
-
* that looks wrong where it happens, which is why it is worth saying so before the game
|
|
10
|
-
* starts rather than after.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```js
|
|
14
|
-
* import { assertTypesAreDeclared } from "@inglorious/engine/core/types.js"
|
|
15
|
-
*
|
|
16
|
-
* assertTypesAreDeclared(config)
|
|
17
|
-
* ```
|
|
4
|
+
* Throws when an entity names a type the configuration does not declare, with
|
|
5
|
+
* the nearest declared names: such an entity would silently get no handlers.
|
|
18
6
|
*
|
|
19
7
|
* @param {object} config - A game configuration.
|
|
20
8
|
* @throws {Error} If an entity names a type the configuration does not declare.
|
package/src/systems/ticks.js
CHANGED
|
@@ -3,27 +3,14 @@ import { dueAt } from "../animation/due.js"
|
|
|
3
3
|
const ONE_SECOND = 1
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* A clock for a whole world that turns at
|
|
6
|
+
* A clock for a whole world that turns at its own rate: `atFps` for a world
|
|
7
|
+
* rather than an entity. It notifies `tick` (or `event`) with the elapsed time
|
|
8
|
+
* whenever a turn is due, so whatever turns with the world handles `tick`
|
|
9
|
+
* instead of `update`, and is left alone between turns while the loop keeps
|
|
10
|
+
* the pointer and controls at screen rate. It hears `pause` and `resume`
|
|
11
|
+
* itself, since the store still asks systems for `update` while halted.
|
|
7
12
|
*
|
|
8
|
-
*
|
|
9
|
-
* together -- a generation of Life, a day for foxes and rabbits -- has one clock, kept
|
|
10
|
-
* here, and notifies `tick` whenever a turn is due. Whatever turns with the world handles
|
|
11
|
-
* `tick` instead of `update`, and the loop stays as fast as the screen takes it, so the
|
|
12
|
-
* pointer and the controls never wait on the world.
|
|
13
|
-
*
|
|
14
|
-
* It is also what lets a large world be cheap between turns. An entity with `atFps` is
|
|
15
|
-
* still handed every `update` to keep its own time, so every one of them is written to on
|
|
16
|
-
* every frame; an entity that only handles `tick` is left alone until the world turns.
|
|
17
|
-
*
|
|
18
|
-
* The event carries the time the turn covers, as `atFps` does: six frames of a sixtieth
|
|
19
|
-
* of a second are a tenth of a second, and a world moving by velocity times dt has to
|
|
20
|
-
* move by all of it.
|
|
21
|
-
*
|
|
22
|
-
* Halting the store stops the entities updating, but systems are still asked, so the
|
|
23
|
-
* clock hears the pause for itself. Without that the world would carry on turning
|
|
24
|
-
* underneath a pause that everything else is honouring.
|
|
25
|
-
*
|
|
26
|
-
* @param {Object} [options] - How the world turns.
|
|
13
|
+
* @param {Object} [options]
|
|
27
14
|
* @param {number} [options.fps=Infinity] - Turns a second. `Infinity` for one a frame.
|
|
28
15
|
* @param {string} [options.event="tick"] - What a turn is called.
|
|
29
16
|
* @returns {Object} The system.
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
const ONE_SECOND = 1000
|
|
2
|
-
|
|
3
|
-
export class ElapsedLoop {
|
|
4
|
-
_shouldStop = false
|
|
5
|
-
|
|
6
|
-
start(engine) {
|
|
7
|
-
let previousTime = Date.now()
|
|
8
|
-
|
|
9
|
-
while (!this._shouldStop) {
|
|
10
|
-
const currentTime = Date.now()
|
|
11
|
-
const dt = currentTime - previousTime
|
|
12
|
-
|
|
13
|
-
engine.update(dt / ONE_SECOND)
|
|
14
|
-
|
|
15
|
-
previousTime = currentTime
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
stop() {
|
|
20
|
-
this._shouldStop = true
|
|
21
|
-
}
|
|
22
|
-
}
|
package/src/core/loops/flash.js
DELETED
package/src/core/loops/lag.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const ONE_SECOND = 1000
|
|
2
|
-
|
|
3
|
-
export class LagLoop {
|
|
4
|
-
_shouldStop = false
|
|
5
|
-
|
|
6
|
-
start(engine, msPerUpdate) {
|
|
7
|
-
let previousTime = Date.now()
|
|
8
|
-
let lag = 0
|
|
9
|
-
|
|
10
|
-
while (!this._shouldStop) {
|
|
11
|
-
const currentTime = Date.now()
|
|
12
|
-
const dt = currentTime - previousTime
|
|
13
|
-
previousTime = currentTime
|
|
14
|
-
lag += dt
|
|
15
|
-
|
|
16
|
-
while (lag >= msPerUpdate) {
|
|
17
|
-
engine.update(dt / ONE_SECOND)
|
|
18
|
-
lag -= msPerUpdate
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
stop() {
|
|
24
|
-
this._shouldStop = true
|
|
25
|
-
}
|
|
26
|
-
}
|