@inglorious/engine 23.0.7 → 24.0.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
@@ -29,17 +29,18 @@ This will set up a complete project with a development server ready to go.
29
29
  - **Pausing without permission checks**: `notify("pause")` halts the world, so nothing that moves has to ask whether it is allowed to.
30
30
  - **A way out that works anywhere**: `notify("quit")` ends the game, from any state, without a handler of its own.
31
31
  - **Sprites in one call**: `crop()` points an entity at a frame of a sheet without you having to remember which half of the crop lives where. See [renderer-2d](https://github.com/iceonfire/inglorious-forge/tree/main/packages/renderer-2d).
32
- - **Screens that share a world**: `scenes()` puts a state's entities up and takes the last state's down, leaving whatever the two have in common standing.
32
+ - **Screens that share a world**: `scenes()` puts a state's entities up and takes the last state's down, leaving whatever the two have in common standing. It and `fsm()` are general-purpose behaviors, so they live in [Inglorious Store](https://github.com/IngloriousCoderz/inglorious-forge/tree/main/packages/store).
33
33
  - **A mistyped type is an error**: the engine refuses a configuration whose entity names a type nothing declared, and says which type was nearly meant.
34
34
 
35
35
  ### Scenes
36
36
 
37
37
  A state machine says when it moves; `scenes()` says what each state is made of. Pair them
38
- and each screen is a description of what should be standing there:
38
+ and each screen is a description of what should be standing there. Neither knows anything
39
+ about games, so both come from `@inglorious/store`:
39
40
 
40
41
  ```javascript
41
- import { fsm } from "@inglorious/engine/behaviors/fsm.js"
42
- import { scenes } from "@inglorious/engine/behaviors/scenes.js"
42
+ import { fsm } from "@inglorious/store/fsm"
43
+ import { scenes } from "@inglorious/store/scenes"
43
44
 
44
45
  const SCENES = {
45
46
  title: () => [createTitleEntity(), createMenuEntity()],
@@ -409,6 +410,8 @@ The core engine relies on a few key, lightweight packages:
409
410
 
410
411
  If you prefer to avoid a build step, you can run the engine directly in the browser. The example below demonstrates how to set up a minimal game using ES modules, an `importmap`, and the 2D canvas renderer.
411
412
 
413
+ An import map knows nothing of a package's `exports`, so without a bundler import files by their real path: `@inglorious/store/behaviors/state-machine/fsm.js` rather than `@inglorious/store/fsm`.
414
+
412
415
  ```html
413
416
  <!DOCTYPE html>
414
417
  <html lang="en">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/engine",
3
- "version": "23.0.7",
3
+ "version": "24.0.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",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "sideEffects": false,
35
35
  "dependencies": {
36
- "@inglorious/store": "11.2.0",
36
+ "@inglorious/store": "11.3.0",
37
37
  "@inglorious/utils": "5.0.0"
38
38
  },
39
39
  "peerDependencies": {
@@ -1,4 +1,4 @@
1
- import { fsm } from "@inglorious/engine/behaviors/state-machine/fsm.js"
1
+ import { fsm } from "@inglorious/store/behaviors/state-machine/fsm.js"
2
2
 
3
3
  /**
4
4
  * A button that looks pressed while it is being pressed.
@@ -1,55 +0,0 @@
1
- const DEFAULT_STATE = "default"
2
-
3
- export function fsm(states) {
4
- const uniqueEventNames = [
5
- ...new Set(Object.values(states).flatMap(Object.keys)),
6
- ]
7
-
8
- return (type) => {
9
- return {
10
- create(entity, payload, api) {
11
- type.create?.(entity, payload, api)
12
-
13
- entity.state ??= DEFAULT_STATE
14
- },
15
-
16
- ...uniqueEventNames.reduce(
17
- (acc, eventName) => ({
18
- ...acc,
19
-
20
- [eventName](entity, event, api) {
21
- const from = entity.state
22
-
23
- // A state answers for itself, and what it has to say replaces what the type
24
- // would have said. The type-stage handler is the default -- the thing every
25
- // state would otherwise do -- and a state takes it over by naming the same
26
- // event, which is how a state can take the way out for itself rather than
27
- // being quit by, without whatever answers by default having to know that
28
- // state exists.
29
- const handler = states[entity.state]?.[eventName] ?? type[eventName]
30
-
31
- handler?.(entity, event, api)
32
-
33
- announce(entity, from, api)
34
- },
35
- }),
36
- {},
37
- ),
38
- }
39
- }
40
- }
41
-
42
- /**
43
- * A machine announces where it has landed, but only when it has actually moved: a
44
- * handler that reads the state without changing it should not wake anything up.
45
- *
46
- * The announcement is broadcast and carries the id, rather than being addressed to
47
- * this entity, because what wants to hear it is usually something else.
48
- */
49
- function announce(entity, from, api) {
50
- const to = entity.state
51
-
52
- if (to === from) return
53
-
54
- api.notify("stateChange", { entityId: entity.id, from, to })
55
- }
@@ -1,89 +0,0 @@
1
- import { isDeepEqual } from "@inglorious/utils/object.js"
2
-
3
- /**
4
- * Puts an entity up, and takes down whatever the state before it had up.
5
- *
6
- * A state machine says when it moves; this says what each state is made of, and is
7
- * usually the pair to `fsm`. Together they cover the ordinary case of a game that is a
8
- * few screens and a world that survives the move between them -- a menu has nothing on it
9
- * that the play has, and two states of the same play have all of it in common.
10
- *
11
- * Only the difference between two states is touched. Anything they have in common is
12
- * left standing as it is, which is what carries whatever the two share from one into the
13
- * next without rebuilding it, and so without losing whatever the world has already done
14
- * to it.
15
- *
16
- * A thing that stands on both sides but is not configured the same way is patched rather
17
- * than rebuilt, so one state may take over part of it -- a keyboard whose keys mean
18
- * something else there -- without the rest of it being torn up to make room.
19
- *
20
- * @example
21
- * ```js
22
- * import { scenes } from "@inglorious/engine/behaviors/state-machine/scenes.js"
23
- * import { fsm } from "@inglorious/engine/behaviors/state-machine/fsm.js"
24
- *
25
- * const SCENES = {
26
- * title: () => [createTitleEntity(), createMenuEntity()],
27
- * play: () => [createPaddleEntity(), createBallEntity()],
28
- * }
29
- *
30
- * const types = {
31
- * Game: [scenes(SCENES), fsm({ title: { start(e) { e.state = "play" } } })],
32
- * }
33
- *
34
- * const entities = { game: { type: "Game", state: "title" } }
35
- * ```
36
- *
37
- * @param {object} scenesByState - What stands in each state, given the state and the
38
- * entity asking. Returns the entity configurations to have standing.
39
- * @returns {object} The behaviour.
40
- */
41
- export function scenes(scenesByState) {
42
- function build(entity, state, api) {
43
- const leaving = entity.scene ?? []
44
- const arriving = scenesByState[state]?.(entity) ?? []
45
-
46
- const leavingIds = leaving.map(({ id }) => id)
47
- const arrivingIds = arriving.map(({ id }) => id)
48
-
49
- for (const { id } of leaving) {
50
- if (arrivingIds.includes(id)) continue
51
-
52
- api.notify("remove", id)
53
- }
54
-
55
- for (const added of arriving) {
56
- if (!leavingIds.includes(added.id)) {
57
- api.notify("add", added)
58
-
59
- continue
60
- }
61
-
62
- // The same thing stands on both sides of the move, so it is neither added nor
63
- // removed -- but it may be wanted to be a different thing now, and then only what
64
- // has actually changed is merged over it. That is what leaves a shared thing where
65
- // the world had put it when the next state says nothing different about it.
66
- const previous = leaving.find(({ id }) => id === added.id)
67
-
68
- if (!isDeepEqual(previous, added)) {
69
- api.notify("patch", added)
70
- }
71
- }
72
-
73
- entity.scene = arriving
74
- }
75
-
76
- return {
77
- create(entity, payload, api) {
78
- build(entity, entity.state, api)
79
- },
80
-
81
- // The announcement is broadcast and carries the id, so that anything else listening
82
- // for it can tell which machine moved. This one is only interested in its own.
83
- stateChange(entity, { entityId, to }, api) {
84
- if (entityId !== entity.id) return
85
-
86
- build(entity, to, api)
87
- },
88
- }
89
- }