@inglorious/engine 24.2.2 → 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 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 (including plain JavaScript, TypeScript, and IngloriousScript):
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
@@ -16,6 +16,14 @@ pnpm create @inglorious/game
16
16
 
17
17
  This sets up a small playable game with a development server and tests, and can make it an installable PWA or an Android and iOS app with Capacitor.
18
18
 
19
+ To add the engine to an existing project, install the store with it:
20
+
21
+ ```bash
22
+ pnpm add @inglorious/engine @inglorious/store @inglorious/renderer-2d
23
+ ```
24
+
25
+ The engine creates the store for you, but the store is your game's: it is a peer dependency, so the engine and your code (`fsm`, `scenes`, `patch`) share one copy.
26
+
19
27
  ## Features
20
28
 
21
29
  - **Functional & Data-Oriented**: Uses a single, immutable state object as the source of truth, inspired by functional programming principles.
@@ -426,9 +434,8 @@ Because building things is fun! This project is as much an exploration of softwa
426
434
 
427
435
  The core engine relies on a few key, lightweight packages:
428
436
 
429
- - [mutative](https://www.npmjs.com/package/mutative): For enabling ergonomic immutable updates with structural sharing.
430
437
  - [@inglorious/utils](https://www.npmjs.com/package/@inglorious/utils): A collection of small, pure utility functions for things like vector math and data manipulation.
431
- - [@inglorious/store](https://www.npmjs.com/package/@inglorious/store): The environment-agnostic core state management library.
438
+ - [@inglorious/store](https://www.npmjs.com/package/@inglorious/store) (peer dependency, install it yourself): The environment-agnostic core state management library, which uses [mutative](https://www.npmjs.com/package/mutative) for ergonomic immutable updates with structural sharing.
432
439
 
433
440
  ## Quick Start Example
434
441
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/engine",
3
- "version": "24.2.2",
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,10 +41,10 @@
41
41
  },
42
42
  "sideEffects": false,
43
43
  "dependencies": {
44
- "@inglorious/store": "^12.9.4",
45
- "@inglorious/utils": "^5.4.4"
44
+ "@inglorious/utils": "^5.6.0"
46
45
  },
47
46
  "peerDependencies": {
47
+ "@inglorious/store": "^12.9.5",
48
48
  "vitest": "^5.1.1"
49
49
  },
50
50
  "devDependencies": {
@@ -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. 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.
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({ fps = Infinity, event = "tick" } = {}) {
19
- const isDue = dueAt(ONE_SECOND / fps)
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 = 0
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 { since, elapsed } = isDue(clock, dt)
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 = since
61
+ clock += covered
39
62
 
40
- if (elapsed === undefined) return
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
- api.notify(event, elapsed)
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. */
@@ -1,12 +1,17 @@
1
1
  import type { GameSystem } from "../index"
2
2
 
3
3
  /**
4
- * Notifies `event` with the elapsed seconds `fps` times a second, for a world
5
- * that turns at its own rate. Stops on `pause`, starts again on `resume`.
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`, every frame. */
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