@inglorious/engine 24.3.0 → 24.4.0
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/README.md +1 -1
- package/package.json +2 -2
- package/src/systems/ticks.js +43 -15
- package/types/index.d.ts +2 -0
- package/types/systems/ticks.d.ts +8 -3
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ A JavaScript game engine written with global state, immutability, and pure funct
|
|
|
8
8
|
|
|
9
9
|
The fastest way to start a new Inglorious Engine project is by using the official scaffolding tool.
|
|
10
10
|
|
|
11
|
-
Run the following command in your terminal and follow the prompts to create a new game from a variety of templates
|
|
11
|
+
Run the following command in your terminal and follow the prompts to create a new game from a variety of templates. IngloriousScript, JavaScript with vector math, is the default; plain JavaScript, TypeScript and IngloriousScript with TypeScript are there too:
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
14
|
pnpm create @inglorious/game
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.4.0",
|
|
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",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@inglorious/utils": "^5.
|
|
44
|
+
"@inglorious/utils": "^5.6.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@inglorious/store": "^12.9.5",
|
package/src/systems/ticks.js
CHANGED
|
@@ -1,25 +1,42 @@
|
|
|
1
|
-
import { dueAt } from "../animation/due.js"
|
|
2
|
-
|
|
3
1
|
const ONE_SECOND = 1
|
|
2
|
+
const DEFAULT_MAX_PER_FRAME = 64
|
|
3
|
+
const NORMAL_SPEED = 1
|
|
4
|
+
const NO_TIME = 0
|
|
5
|
+
// Summed frame times fall a hair short of the interval they should reach.
|
|
6
|
+
const WITHIN = 1e-9
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* A clock for a whole world that turns at its own rate: `atFps` for a world
|
|
7
|
-
* rather than an entity.
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* rather than an entity. Whatever turns with the world handles `tick` (or
|
|
11
|
+
* `event`) instead of `update`, and is left alone between turns while the loop
|
|
12
|
+
* keeps the pointer and controls at screen rate.
|
|
13
|
+
*
|
|
14
|
+
* Each turn covers exactly one interval, `1 / fps` seconds, and is notified on
|
|
15
|
+
* its own: time left over carries on to the next frame, so the world turns at
|
|
16
|
+
* its rate whatever the screen's, and a frame that covers several intervals
|
|
17
|
+
* turns several times. At most `maxPerFrame` turns happen in one frame; a world
|
|
18
|
+
* that cannot keep up drops the time it is behind rather than racing to catch
|
|
19
|
+
* up for ever.
|
|
20
|
+
*
|
|
21
|
+
* `game.timeScale` (1 when absent) is how fast the clock runs: 4 turns the world
|
|
22
|
+
* four times as often, 0 holds it. It hears `pause` and `resume` itself, since
|
|
23
|
+
* the store still asks systems for `update` while halted.
|
|
12
24
|
*
|
|
13
25
|
* @param {Object} [options]
|
|
14
|
-
* @param {number} [options.fps=Infinity] - Turns a second. `Infinity` for one a frame.
|
|
26
|
+
* @param {number} [options.fps=Infinity] - Turns a second. `Infinity` for one a frame, covering the frame.
|
|
15
27
|
* @param {string} [options.event="tick"] - What a turn is called.
|
|
28
|
+
* @param {number} [options.maxPerFrame=64] - The most turns in one frame.
|
|
16
29
|
* @returns {Object} The system.
|
|
17
30
|
*/
|
|
18
|
-
export function ticks({
|
|
19
|
-
|
|
31
|
+
export function ticks({
|
|
32
|
+
fps = Infinity,
|
|
33
|
+
event = "tick",
|
|
34
|
+
maxPerFrame = DEFAULT_MAX_PER_FRAME,
|
|
35
|
+
} = {}) {
|
|
36
|
+
const interval = ONE_SECOND / fps
|
|
20
37
|
|
|
21
38
|
let halted = false
|
|
22
|
-
let clock =
|
|
39
|
+
let clock = NO_TIME
|
|
23
40
|
|
|
24
41
|
return {
|
|
25
42
|
pause() {
|
|
@@ -33,13 +50,24 @@ export function ticks({ fps = Infinity, event = "tick" } = {}) {
|
|
|
33
50
|
update(entities, dt, api) {
|
|
34
51
|
if (halted) return
|
|
35
52
|
|
|
36
|
-
const
|
|
53
|
+
const timeScale = entities.game?.timeScale ?? NORMAL_SPEED
|
|
54
|
+
const covered = dt * timeScale
|
|
55
|
+
|
|
56
|
+
if (!interval) {
|
|
57
|
+
if (covered) api.notify(event, covered)
|
|
58
|
+
return
|
|
59
|
+
}
|
|
37
60
|
|
|
38
|
-
clock
|
|
61
|
+
clock += covered
|
|
39
62
|
|
|
40
|
-
|
|
63
|
+
let turns = 0
|
|
64
|
+
while (clock + interval * WITHIN >= interval && turns < maxPerFrame) {
|
|
65
|
+
api.notify(event, interval)
|
|
66
|
+
clock -= interval
|
|
67
|
+
turns++
|
|
68
|
+
}
|
|
41
69
|
|
|
42
|
-
|
|
70
|
+
if (turns === maxPerFrame) clock = NO_TIME
|
|
43
71
|
},
|
|
44
72
|
}
|
|
45
73
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -64,6 +64,8 @@ export interface GameEntity extends BaseEntity {
|
|
|
64
64
|
/** Draws collision gizmos; `C` toggles it. */
|
|
65
65
|
debug?: boolean
|
|
66
66
|
paused?: boolean
|
|
67
|
+
/** How fast the `ticks` clock runs: 1 when absent, 4 four times as fast, 0 holds it. */
|
|
68
|
+
timeScale?: number
|
|
67
69
|
/** Stops the engine after the current frame. */
|
|
68
70
|
quit?: boolean
|
|
69
71
|
/** Draws images without smoothing. */
|
package/types/systems/ticks.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import type { GameSystem } from "../index"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Notifies `event`
|
|
5
|
-
*
|
|
4
|
+
* Notifies `event` `fps` times a second, for a world that turns at its own rate.
|
|
5
|
+
* Each turn covers exactly `1 / fps` seconds; leftover time carries on to the
|
|
6
|
+
* next frame, and a long frame turns several times, up to `maxPerFrame`.
|
|
7
|
+
* `game.timeScale` speeds the clock up or slows it down. Stops on `pause`,
|
|
8
|
+
* starts again on `resume`.
|
|
6
9
|
*/
|
|
7
10
|
export function ticks(options?: {
|
|
8
|
-
/** Defaults to `Infinity
|
|
11
|
+
/** Defaults to `Infinity`: one turn a frame, covering the frame. */
|
|
9
12
|
fps?: number
|
|
10
13
|
/** Defaults to `"tick"`. */
|
|
11
14
|
event?: string
|
|
15
|
+
/** Defaults to 64. Time beyond it is dropped rather than caught up on. */
|
|
16
|
+
maxPerFrame?: number
|
|
12
17
|
}): GameSystem
|