@inglorious/engine 23.0.2 → 23.0.3

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.
Files changed (70) hide show
  1. package/README.md +223 -0
  2. package/package.json +9 -5
  3. package/src/ai/movement/dynamic/align.js +1 -1
  4. package/src/ai/movement/dynamic/arrive.js +1 -1
  5. package/src/ai/movement/dynamic/evade.js +1 -1
  6. package/src/ai/movement/dynamic/face.js +1 -1
  7. package/src/ai/movement/dynamic/flee.js +1 -1
  8. package/src/ai/movement/dynamic/look-where-youre-going.js +1 -1
  9. package/src/ai/movement/dynamic/match-velocity.js +1 -1
  10. package/src/ai/movement/dynamic/pursue.js +1 -1
  11. package/src/ai/movement/dynamic/seek.js +1 -1
  12. package/src/ai/movement/dynamic/wander.js +1 -1
  13. package/src/ai/movement/kinematic/align.js +1 -1
  14. package/src/ai/movement/kinematic/arrive.js +1 -1
  15. package/src/ai/movement/kinematic/face.js +1 -1
  16. package/src/ai/movement/kinematic/flee.js +1 -1
  17. package/src/ai/movement/kinematic/seek.js +1 -1
  18. package/src/ai/movement/kinematic/wander-as-seek.js +1 -1
  19. package/src/ai/movement/kinematic/wander.js +1 -1
  20. package/src/animation/due.js +36 -0
  21. package/src/animation/sprite.js +1 -1
  22. package/src/behaviors/at-fps.js +49 -0
  23. package/src/behaviors/audio.js +82 -18
  24. package/src/behaviors/camera.js +1 -1
  25. package/src/behaviors/controls/dynamic/modern.js +2 -2
  26. package/src/behaviors/controls/dynamic/shooter.js +2 -2
  27. package/src/behaviors/controls/dynamic/tank.js +2 -2
  28. package/src/behaviors/controls/event-handlers.js +19 -16
  29. package/src/behaviors/controls/kinematic/modern.js +2 -2
  30. package/src/behaviors/controls/kinematic/shooter.js +2 -2
  31. package/src/behaviors/controls/kinematic/tank.js +2 -2
  32. package/src/behaviors/debug/collision.js +18 -4
  33. package/src/behaviors/fps.js +1 -1
  34. package/src/behaviors/game.js +14 -0
  35. package/src/behaviors/infinite-scroll.js +5 -2
  36. package/src/behaviors/input/controls.js +45 -12
  37. package/src/behaviors/input/gamepad.js +30 -29
  38. package/src/behaviors/input/hover.js +57 -0
  39. package/src/behaviors/input/input.js +22 -14
  40. package/src/behaviors/input/keyboard.js +79 -12
  41. package/src/behaviors/input/mouse.js +76 -11
  42. package/src/behaviors/input/pointer.js +39 -0
  43. package/src/behaviors/input/touch.js +2 -2
  44. package/src/behaviors/particles.js +124 -0
  45. package/src/behaviors/physics/bouncy.js +1 -1
  46. package/src/behaviors/physics/clamped.js +1 -1
  47. package/src/behaviors/physics/jumpable.js +7 -5
  48. package/src/behaviors/state-machine/fsm.js +55 -0
  49. package/src/behaviors/state-machine/mappings.js +59 -0
  50. package/src/behaviors/state-machine/scenes.js +89 -0
  51. package/src/behaviors/ui/button.js +26 -11
  52. package/src/behaviors/ui/slider.js +114 -0
  53. package/src/collision/detection.js +37 -6
  54. package/src/core/core-events.js +3 -0
  55. package/src/core/engine.js +70 -7
  56. package/src/core/loops/fixed.js +6 -1
  57. package/src/core/loops/index.js +23 -3
  58. package/src/core/middlewares/entity-pool/entity-pool-middleware.js +20 -2
  59. package/src/core/middlewares/entity-pool/entity-pools.js +4 -0
  60. package/src/core/types.js +50 -0
  61. package/src/movement/dynamic/modern.js +1 -1
  62. package/src/movement/dynamic/tank.js +1 -1
  63. package/src/movement/kinematic/modern.js +1 -1
  64. package/src/movement/kinematic/tank.js +1 -1
  65. package/src/physics/anchor.js +115 -0
  66. package/src/physics/bounds.js +34 -27
  67. package/src/physics/position.js +29 -11
  68. package/src/storage.js +53 -0
  69. package/src/test.js +185 -0
  70. package/src/behaviors/fsm.js +0 -31
@@ -1,27 +1,63 @@
1
1
  import { findCollision } from "@inglorious/engine/collision/detection.js"
2
2
  import { clampToBounds } from "@inglorious/engine/physics/bounds.js"
3
- import { zero } from "@inglorious/utils/vectors"
3
+ import { zero } from "@inglorious/utils/vector.js"
4
4
 
5
5
  export function mouse() {
6
6
  return {
7
7
  create(entity) {
8
8
  entity.collisions ??= {}
9
+
10
+ // Two shapes, for two different jobs. `bounds` is what the pointer is kept inside,
11
+ // and `hitbox` is the point that is tested against everything else -- so a mouse
12
+ // with only the first of them is a cursor that never touches anything, and every
13
+ // press it makes lands on the scene rather than on the control under it.
9
14
  entity.collisions.bounds ??= { shape: "point" }
15
+ entity.collisions.hitbox ??= { shape: "point" }
10
16
  },
11
17
 
12
18
  mouseMove(entity, position, api) {
13
- const game = api.getEntity("game")
19
+ move(entity, position, api)
20
+ },
21
+
22
+ mouseDown(entity, position, api) {
23
+ move(entity, position, api)
24
+
25
+ const pressed = findCollision(entity, api.getEntities())
26
+
27
+ // Remembered, because a release belongs to the press that started it rather than
28
+ // to whatever the pointer happens to be over when it is let go. Without this, a
29
+ // press that slides off its target -- which is what dragging a slider does -- is
30
+ // never released at all, and whatever was pressed stays pressed for good.
31
+ entity.pressedId = pressed?.id
32
+
33
+ if (pressed) {
34
+ api.notify("entityPress", pressed.id)
35
+ } else {
36
+ api.notify("scenePress", position)
37
+ }
38
+ },
39
+
40
+ mouseUp(entity, position, api) {
41
+ move(entity, position, api)
42
+
43
+ const released = entity.pressedId
14
44
 
15
- entity.position = position
45
+ entity.pressedId = undefined
16
46
 
17
- clampToBounds(entity, game.size)
47
+ if (released) {
48
+ api.notify("entityRelease", released)
49
+ } else {
50
+ api.notify("sceneRelease", position)
51
+ }
18
52
  },
19
53
 
20
54
  mouseClick(entity, position, api) {
21
- const entities = api.getEntities()
22
- const clickedEntity = findCollision(entity, entities)
23
- if (clickedEntity) {
24
- api.notify("entityClick", clickedEntity.id)
55
+ move(entity, position, api)
56
+
57
+ const clicked = findCollision(entity, api.getEntities())
58
+
59
+ if (clicked) {
60
+ api.notify("entityClick", clicked.id)
25
61
  } else {
26
62
  api.notify("sceneClick", position)
27
63
  }
@@ -36,17 +72,31 @@ export function trackMouse(parent, api, toGamePosition) {
36
72
  api,
37
73
  toGamePosition,
38
74
  )
75
+ const handleMouseDown = createHandler(
76
+ "mouseDown",
77
+ parent,
78
+ api,
79
+ toGamePosition,
80
+ )
81
+ const handleMouseUp = createHandler("mouseUp", parent, api, toGamePosition)
39
82
  const handleClick = createHandler("mouseClick", parent, api, toGamePosition)
40
- const handleWheel = createHandler("mouseWheel", parent, api, toGamePosition)
83
+ const handleMouseWheel = createHandler(
84
+ "mouseWheel",
85
+ parent,
86
+ api,
87
+ toGamePosition,
88
+ )
41
89
 
42
90
  return {
43
91
  onMouseMove: handleMouseMove,
44
92
  onClick: handleClick,
45
- onWheel: handleWheel,
93
+ onMouseDown: handleMouseDown,
94
+ onMouseUp: handleMouseUp,
95
+ onMouseWheel: handleMouseWheel,
46
96
  }
47
97
  }
48
98
 
49
- export function createMouse(overrides = {}) {
99
+ export function createMouseEntity(overrides = {}) {
50
100
  return {
51
101
  type: "Mouse",
52
102
  layer: 999, // A high layer value to ensure it's always rendered on top
@@ -73,3 +123,18 @@ function createHandler(type, parent, api, toGamePosition) {
73
123
  api.notify(type, position)
74
124
  }
75
125
  }
126
+
127
+ /**
128
+ * Puts the pointer where the browser last said it was.
129
+ *
130
+ * Every event that carries a position moves the mouse as well, not just the move: a
131
+ * press reports where the pointer is, and a mouse that stayed where it last drifted to
132
+ * draws the cursor somewhere other than where the finger is.
133
+ */
134
+ function move(entity, position, api) {
135
+ const game = api.getEntity("game")
136
+
137
+ entity.position = position
138
+
139
+ clampToBounds(entity, game.size)
140
+ }
@@ -0,0 +1,39 @@
1
+ const DEFAULT_ACTIONS = []
2
+
3
+ /**
4
+ * Turns pointer gestures into input actions, the same way the keyboard and the
5
+ * gamepad behaviors do. A click is a complete press, while a touch reports its
6
+ * press on `touchStart` and its release on `touchEnd`.
7
+ */
8
+ export function pointer() {
9
+ return {
10
+ mouseClick(entity, position, api) {
11
+ press(entity, api)
12
+ release(entity, api)
13
+ },
14
+
15
+ touchStart(entity, position, api) {
16
+ press(entity, api)
17
+ },
18
+
19
+ touchEnd(entity, position, api) {
20
+ release(entity, api)
21
+ },
22
+ }
23
+ }
24
+
25
+ export function createPointerEntity(actions = DEFAULT_ACTIONS) {
26
+ return { type: "Pointer", actions }
27
+ }
28
+
29
+ function press(entity, api) {
30
+ entity.actions.forEach((action) => {
31
+ api.notify("inputPress", { action })
32
+ })
33
+ }
34
+
35
+ function release(entity, api) {
36
+ entity.actions.forEach((action) => {
37
+ api.notify("inputRelease", { action })
38
+ })
39
+ }
@@ -1,7 +1,7 @@
1
1
  import { findCollision } from "@inglorious/engine/collision/detection.js"
2
2
  import { clampToBounds } from "@inglorious/engine/physics/bounds.js"
3
3
  import { v } from "@inglorious/utils/v.js"
4
- import { magnitude, subtract } from "@inglorious/utils/vectors"
4
+ import { magnitude, subtract } from "@inglorious/utils/vector.js"
5
5
 
6
6
  const MOVEMENT_THRESHOLD = 5
7
7
 
@@ -85,7 +85,7 @@ export function trackTouch(parent, api, toGamePosition) {
85
85
  }
86
86
  }
87
87
 
88
- export function createTouch() {
88
+ export function createTouchEntity() {
89
89
  return {
90
90
  type: "Touch",
91
91
  layer: 999, // A high layer value to ensure it's always rendered on top
@@ -0,0 +1,124 @@
1
+ import { random } from "@inglorious/utils/math/rng.js"
2
+ import { v } from "@inglorious/utils/v.js"
3
+ import {
4
+ multiply,
5
+ negate,
6
+ randomVector,
7
+ sum,
8
+ } from "@inglorious/utils/vector.js"
9
+
10
+ const FIRST_INDEX = 0
11
+ const SINGLE = 1
12
+ const OPAQUE = 1
13
+ const NO_LAYER = 0
14
+ const NO_SPREAD = 0
15
+ const NO_DEPTH = 0
16
+ const NO_TIME = 0
17
+ const WHOLE = 1
18
+ const DEFAULT_SIZE = 1
19
+ const PARTICLE_TYPE = "Particle"
20
+
21
+ // `size` and `spread` are rebuilt on every call rather than shared from a module
22
+ // constant, because entities hold on to them and vectors are mutable.
23
+ const DEFAULT_SIZE_VECTOR = () => v(DEFAULT_SIZE, DEFAULT_SIZE, NO_DEPTH)
24
+ const NO_SPREAD_VECTOR = () => v(NO_SPREAD, NO_SPREAD, NO_SPREAD)
25
+
26
+ /**
27
+ * A single particle: it drifts under an acceleration and fades out over its
28
+ * lifetime, then hands itself back to the pool.
29
+ *
30
+ * Pair it with whatever draws the particle, which is usually `renderImage` or
31
+ * `renderRectangle`:
32
+ *
33
+ * @example
34
+ * ```js
35
+ * types: { Particle: [{ render: renderImage }, particle()] }
36
+ * ```
37
+ */
38
+ export function particle() {
39
+ return {
40
+ update(entity, dt, api) {
41
+ entity.age += dt
42
+
43
+ if (entity.age >= entity.life) {
44
+ api.notify("despawn", entity)
45
+ return
46
+ }
47
+
48
+ entity.velocity = sum(entity.velocity, multiply(entity.acceleration, dt))
49
+ entity.position = sum(entity.position, multiply(entity.velocity, dt))
50
+
51
+ const spent = entity.age / entity.life
52
+ entity.opacity = entity.startOpacity * (WHOLE - spent)
53
+ },
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Emits a burst of particles, pooled like any other entity.
59
+ *
60
+ * Each particle picks its own lifetime, its own acceleration and its own place
61
+ * in the emission box, so one call reads as a burst rather than a grid.
62
+ *
63
+ * `acceleration` is a pair of vectors, and each particle's acceleration is drawn
64
+ * per axis between them: the low end is where a component starts, the high end
65
+ * where it can reach. `spread` is the half-extent of the box particles are
66
+ * emitted in, so `[5, 5, 0]` emits within five of the position on each axis.
67
+ *
68
+ * @example
69
+ * ```js
70
+ * // Debris falling away from something that was hit.
71
+ * emitBurst(api, {
72
+ * count: 64,
73
+ * position: v(100, 100, 0),
74
+ * size: v(2, 2, 0),
75
+ * lifetime: [0.5, 1],
76
+ * tint: "rgb(99, 155, 255)",
77
+ * acceleration: [v(-15, -80, 0), v(15, 0, 0)],
78
+ * spread: v(5, 5, 0),
79
+ * layer: LAYER_PARTICLES,
80
+ * image: { id: "particle", imageSize: [2, 2] },
81
+ * })
82
+ * ```
83
+ */
84
+ export function emitBurst(api, params) {
85
+ const {
86
+ count = SINGLE,
87
+ type = PARTICLE_TYPE,
88
+ size = DEFAULT_SIZE_VECTOR(),
89
+ lifetime = [WHOLE, WHOLE],
90
+ acceleration,
91
+ spread = NO_SPREAD_VECTOR(),
92
+ velocity,
93
+ opacity = OPAQUE,
94
+ layer = NO_LAYER,
95
+ image,
96
+ tint,
97
+ position = v(NO_SPREAD, NO_SPREAD, NO_DEPTH),
98
+ } = params
99
+
100
+ const [low, high] = acceleration ?? []
101
+
102
+ for (let index = FIRST_INDEX; index < count; index++) {
103
+ api.notify("spawn", {
104
+ type,
105
+ layer,
106
+ size,
107
+ image,
108
+ tint,
109
+ age: NO_TIME,
110
+ life: random(...lifetime),
111
+ startOpacity: opacity,
112
+ opacity,
113
+ position: within(position, spread),
114
+ velocity: velocity ?? v(NO_SPREAD, NO_SPREAD, NO_DEPTH),
115
+ acceleration: randomVector(low, high),
116
+ })
117
+ }
118
+ }
119
+
120
+ // A random point inside the box `spread` radiates around `centre`, which is the
121
+ // centre plus a random vector across the box's own range.
122
+ function within(centre, spread) {
123
+ return sum(centre, randomVector(negate(spread), spread))
124
+ }
@@ -1,4 +1,4 @@
1
- import { defaults, extend } from "@inglorious/utils/objects"
1
+ import { defaults, extend } from "@inglorious/utils/object.js"
2
2
  import { jump } from "@inglorious/utils/physics/jump.js"
3
3
 
4
4
  const DEFAULT_PARAMS = {
@@ -1,5 +1,5 @@
1
1
  import { clampToBounds } from "@inglorious/engine/physics/bounds.js"
2
- import { extend, merge } from "@inglorious/utils/objects"
2
+ import { extend, merge } from "@inglorious/utils/object.js"
3
3
 
4
4
  const DEFAULT_PARAMS = {
5
5
  collisionGroup: "bounds",
@@ -1,8 +1,8 @@
1
1
  import { findCollision } from "@inglorious/engine/collision/detection.js"
2
- import { defaults, extend } from "@inglorious/utils/objects"
2
+ import { defaults, extend } from "@inglorious/utils/object.js"
3
3
  import { applyGravity } from "@inglorious/utils/physics/gravity.js"
4
4
  import { jump } from "@inglorious/utils/physics/jump.js"
5
- import { angle, magnitude, zero } from "@inglorious/utils/vectors"
5
+ import { angle, magnitude, zero } from "@inglorious/utils/vector.js"
6
6
 
7
7
  const DEFAULT_PARAMS = {
8
8
  maxSpeed: 250,
@@ -32,10 +32,12 @@ export function jumpable(params) {
32
32
  entity.vy ??= 0
33
33
  },
34
34
 
35
- jump(entity, entityId, api) {
36
- type.jump?.(entity, entityId, api)
35
+ jump(entity, payload, api) {
36
+ type.jump?.(entity, payload, api)
37
37
 
38
- if (entityId === entity.id && entity.jumpsLeft) {
38
+ // An action's name is its address, so a jump is for whichever entity handles
39
+ // it. There is no id to compare against: the notification carries none.
40
+ if (entity.jumpsLeft) {
39
41
  entity.vy = jump(entity)
40
42
  entity.groundObject = undefined
41
43
  entity.jumpsLeft--
@@ -0,0 +1,55 @@
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
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Says what each state answers to.
3
+ *
4
+ * What a key, an axis or a button means is part of what a state *is*, not something
5
+ * decided once for the whole game: the same key answers one thing on a menu and another in
6
+ * play, and a state with nothing mapped of its own should be deaf rather than quietly
7
+ * carrying on answering whatever the last state set.
8
+ *
9
+ * Each state therefore says what it answers to, in the same place it says what it does.
10
+ * Composed onto the entity running the machine, which is what announces the move.
11
+ *
12
+ * @example
13
+ * ```js
14
+ * const types = {
15
+ * Game: [
16
+ * scenes(SCENES),
17
+ * mappings({
18
+ * menu: { ArrowUp: "moveItemUp", Btn0: "moveItemUp", Enter: "choose" },
19
+ * play: { ArrowLeft: "moveLeft", Btn4: "fire" },
20
+ * // Deaf on purpose: there is nothing to press here.
21
+ * credits: {},
22
+ * }),
23
+ * fsm({ menu: { choose: (e) => { e.state = "play" } }, play: {} }),
24
+ * ],
25
+ * }
26
+ * ```
27
+ *
28
+ * @param {Object.<string, object>} mappingsByState - What each state answers to.
29
+ * @returns {object} The behaviour.
30
+ */
31
+ export function mappings(mappingsByState) {
32
+ function apply(state, api) {
33
+ // A state with no keys of its own is deaf. Falling back to the last mapping instead
34
+ // would make a forgotten state carry on answering, which is the mistake this is
35
+ // written to prevent.
36
+ api.notify("mappingChange", mappingsByState[state] ?? {})
37
+ }
38
+
39
+ // Written as a decorator rather than a bare behaviour because behaviours compose by
40
+ // replacing, not by running in order: a type has one `create` and one `stateChange`, and
41
+ // the last behaviour to name one wins. So each handler does its own part and then hands
42
+ // on to whatever it displaced, the way the dynamic behaviours in the recipes do.
43
+ return (type) => ({
44
+ // A machine is already in its first state when it is made, and nothing announces that:
45
+ // it only announces moves. So the opening state would never be given its keys, and the
46
+ // game would begin answering whatever the mapping it was handed happened to be.
47
+ create(entity, payload, api) {
48
+ type.create?.(entity, payload, api)
49
+
50
+ apply(entity.state, api)
51
+ },
52
+
53
+ stateChange(entity, event, api) {
54
+ type.stateChange?.(entity, event, api)
55
+
56
+ apply(event.to, api)
57
+ },
58
+ })
59
+ }
@@ -0,0 +1,89 @@
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
+ }
@@ -1,17 +1,32 @@
1
+ import { fsm } from "@inglorious/engine/behaviors/state-machine/fsm.js"
2
+
3
+ /**
4
+ * A button that looks pressed while it is being pressed.
5
+ *
6
+ * It answers a press rather than a click, because a browser says `click` only after the
7
+ * pointer is already up again: a button that pressed on `click` would latch down and
8
+ * stay that way, with nothing left to lift it.
9
+ *
10
+ * It announces that it was pressed the way any state machine does -- through
11
+ * `stateChange` -- so whatever the button is *for* is said elsewhere rather than here:
12
+ * a button knows how it looks, not what it does.
13
+ */
1
14
  export function button() {
2
- return {
3
- states: {
4
- default: {
5
- entityClick(entity) {
6
- entity.state = "pressed"
7
- },
15
+ return fsm({
16
+ default: {
17
+ entityPress(entity, id) {
18
+ if (id !== entity.id) return
19
+
20
+ entity.state = "pressed"
8
21
  },
22
+ },
23
+
24
+ pressed: {
25
+ entityRelease(entity, id) {
26
+ if (id !== entity.id) return
9
27
 
10
- pressed: {
11
- entityRelease(entity) {
12
- entity.state = "default"
13
- },
28
+ entity.state = "default"
14
29
  },
15
30
  },
16
- }
31
+ })
17
32
  }