@inglorious/engine 24.4.0 → 24.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/engine",
3
- "version": "24.4.0",
3
+ "version": "24.5.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",
@@ -6,8 +6,10 @@ const DEFAULT_CURSOR = "arrow"
6
6
  /**
7
7
  * A pointer that turns into a hand over anything there is room to press.
8
8
  *
9
- * Anything with a hitbox is something a press would land on, so the pointer going
10
- * over one of those is the game saying "this does something" the way a desktop does.
9
+ * Anything with a hitbox that answers `entityPress` is something a press would do
10
+ * something to, so the pointer going over one of those is the game saying "this does
11
+ * something" the way a desktop does. A hitbox that only blocks, like a veil behind a
12
+ * dialog, keeps the arrow.
11
13
  * Written as a decorator rather than as part of the mouse itself, so a game that has
12
14
  * nothing to press -- or a mouse that is meant to stay a crosshair whatever it is
13
15
  * over -- simply leaves it out.
@@ -44,7 +46,9 @@ export function hover({ off = DEFAULT_CURSOR } = {}) {
44
46
  }
45
47
 
46
48
  /**
47
- * Puts the hand up or takes it down again.
49
+ * Puts the hand up or takes it down again, and tells what it went over and off:
50
+ * `entityHoverStart` and `entityHoverEnd` with the entity's id, which `button()` turns
51
+ * into `hovered` so it can light up.
48
52
  *
49
53
  * Something pressable under the pointer, or something pressed whatever the pointer has
50
54
  * wandered off to since: a drag that started on a control should not flicker back
@@ -53,5 +57,21 @@ export function hover({ off = DEFAULT_CURSOR } = {}) {
53
57
  function point(entity, api, off) {
54
58
  const held = entity.pressedId ?? findCollision(entity, api.getEntities())?.id
55
59
 
56
- entity.cursor = held ? HAND : off
60
+ entity.cursor = held && answersPresses(held, api) ? HAND : off
61
+
62
+ if (held === entity.hoveredId) return
63
+ if (entity.hoveredId) api.notify("entityHoverEnd", entity.hoveredId)
64
+ if (held) api.notify("entityHoverStart", held)
65
+ entity.hoveredId = held
66
+ }
67
+
68
+ /**
69
+ * Whether pressing an entity does anything: its type answers `entityPress`. Something
70
+ * can be in the way of the pointer without being pressable, such as a veil behind a
71
+ * dialog that stops clicks reaching the scene, and a hand over it would promise a press
72
+ * that does nothing.
73
+ */
74
+ function answersPresses(id, api) {
75
+ const entity = api.getEntity(id)
76
+ return Boolean(entity && api.getType(entity.type)?.entityPress)
57
77
  }
@@ -10,23 +10,67 @@ import { fsm } from "@inglorious/store/behaviors/state-machine/fsm.js"
10
10
  * It announces that it was pressed the way any state machine does -- through
11
11
  * `stateChange` -- so whatever the button is *for* is said elsewhere rather than here:
12
12
  * a button knows how it looks, not what it does.
13
+ *
14
+ * While the pointer is over it, `hovered` is true (from `hover()` on the mouse), so it
15
+ * can light up.
16
+ *
17
+ * A finger presses it too, from `entityTouchStart` to `entityTouchEnd`: give it a
18
+ * `touch` collision as well as its `hitbox`, since that is what the touch looks for.
13
19
  */
14
20
  export function button() {
15
21
  return fsm({
16
22
  default: {
17
- entityPress(entity, id) {
18
- if (id !== entity.id) return
19
-
20
- entity.state = "pressed"
21
- },
23
+ entityPress: press,
24
+ entityTouchStart: press,
25
+ ...HOVER,
22
26
  },
23
27
 
24
28
  pressed: {
25
- entityRelease(entity, id) {
26
- if (id !== entity.id) return
27
-
28
- entity.state = "default"
29
+ ...HOVER,
30
+ entityRelease: release,
31
+ entityTouchEnd(entity, { targetId }) {
32
+ release(entity, targetId)
29
33
  },
30
34
  },
31
35
  })
32
36
  }
37
+
38
+ function press(entity, id) {
39
+ if (id !== entity.id) return
40
+
41
+ entity.state = "pressed"
42
+ }
43
+
44
+ function release(entity, id) {
45
+ if (id !== entity.id) return
46
+
47
+ entity.state = "default"
48
+ }
49
+
50
+ /**
51
+ * What a button is for: notifies its `action` when it is pressed. A behaviour next to
52
+ * `button()`, which only knows how a button looks.
53
+ *
54
+ * @example
55
+ * ```js
56
+ * types: { Button: [button(), pressable(), { render: renderButton }] }
57
+ * entities: { pause: { type: "Button", action: "togglePause", label: "Pause" } }
58
+ * ```
59
+ */
60
+ export function pressable() {
61
+ return {
62
+ stateChange(entity, { entityId, to }, api) {
63
+ if (entityId === entity.id && to === "pressed") api.notify(entity.action)
64
+ },
65
+ }
66
+ }
67
+
68
+ const HOVER = {
69
+ entityHoverStart(entity, id) {
70
+ if (id === entity.id) entity.hovered = true
71
+ },
72
+
73
+ entityHoverEnd(entity, id) {
74
+ if (id === entity.id) entity.hovered = false
75
+ },
76
+ }
@@ -7,9 +7,9 @@ import * as segment from "@inglorious/utils/math/segment.js"
7
7
  import { filter } from "@inglorious/utils/object.js"
8
8
  import { add, zero } from "@inglorious/utils/vector.js"
9
9
 
10
+ import { drawOrder } from "../core/draw-order.js"
10
11
  import { anchorOffset, shapeAnchor } from "../physics/anchor.js"
11
12
 
12
- const Z = 2
13
13
  const RECTANGLE = "rectangle"
14
14
 
15
15
  const Shape = {
@@ -22,7 +22,8 @@ const Shape = {
22
22
  }
23
23
 
24
24
  /**
25
- * Finds the first collision between a point and a list of entities.
25
+ * Finds the first collision between a point and a list of entities: the one drawn on
26
+ * top, when several overlap (the reverse of `drawOrder`).
26
27
  *
27
28
  * @param {Point} entity - The point to check for collisions.
28
29
  * @param {Options} options - Options for collision detection.
@@ -35,7 +36,7 @@ export function findCollision(entity, entities, collisionGroup = "hitbox") {
35
36
  )
36
37
 
37
38
  return Object.values(otherEntities)
38
- .toSorted((a, b) => a.position[Z] - b.position[Z])
39
+ .toSorted((a, b) => drawOrder(b, a))
39
40
  .find((target) => collidesWith(entity, target, collisionGroup))
40
41
  }
41
42
 
@@ -0,0 +1,22 @@
1
+ const DEFAULT_LAYER = 0
2
+ const Y = 1
3
+ const Z = 2
4
+
5
+ /**
6
+ * The order things are drawn in, as a comparator: by `layer`, then from low to high
7
+ * (`y`), then from the back of the scene to the front (high `z` first).
8
+ *
9
+ * Shared by the renderers, which draw in this order, and by hit testing, which looks in
10
+ * the reverse one: what the pointer lands on is what is drawn on top.
11
+ *
12
+ * @param {{ layer?: number, position: number[] }} a
13
+ * @param {{ layer?: number, position: number[] }} b
14
+ * @returns {number}
15
+ */
16
+ export function drawOrder(a, b) {
17
+ return (
18
+ (a.layer ?? DEFAULT_LAYER) - (b.layer ?? DEFAULT_LAYER) ||
19
+ a.position[Y] - b.position[Y] ||
20
+ b.position[Z] - a.position[Z]
21
+ )
22
+ }
@@ -2,7 +2,14 @@ import type { Decorator, Entity, GameType } from "../../index"
2
2
 
3
3
  export interface ButtonEntity extends Entity {
4
4
  state?: "default" | "pressed"
5
+ /** True while the pointer is over it. */
6
+ hovered?: boolean
7
+ /** What `pressable()` notifies when it is pressed. */
8
+ action?: string
5
9
  }
6
10
 
7
- /** Looks pressed from `entityPress` to `entityRelease`, and announces it through `stateChange`. */
11
+ /** Looks pressed from `entityPress` to `entityRelease` (or `entityTouchStart` to `entityTouchEnd`), and announces it through `stateChange`. */
8
12
  export function button(): Decorator<GameType<ButtonEntity>>
13
+
14
+ /** Notifies the button's `action` when it is pressed. */
15
+ export function pressable(): GameType<ButtonEntity>
@@ -0,0 +1,5 @@
1
+ /** The order things are drawn in: by `layer`, then by `y`, then back (high `z`) to front. */
2
+ export function drawOrder(
3
+ a: { layer?: number; position: readonly number[] },
4
+ b: { layer?: number; position: readonly number[] },
5
+ ): number