@inglorious/engine 23.0.2 → 23.0.4
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 +223 -0
- package/package.json +9 -5
- package/src/ai/movement/dynamic/align.js +1 -1
- package/src/ai/movement/dynamic/arrive.js +1 -1
- package/src/ai/movement/dynamic/evade.js +1 -1
- package/src/ai/movement/dynamic/face.js +1 -1
- package/src/ai/movement/dynamic/flee.js +1 -1
- package/src/ai/movement/dynamic/look-where-youre-going.js +1 -1
- package/src/ai/movement/dynamic/match-velocity.js +1 -1
- package/src/ai/movement/dynamic/pursue.js +1 -1
- package/src/ai/movement/dynamic/seek.js +1 -1
- package/src/ai/movement/dynamic/wander.js +1 -1
- package/src/ai/movement/kinematic/align.js +1 -1
- package/src/ai/movement/kinematic/arrive.js +1 -1
- package/src/ai/movement/kinematic/face.js +1 -1
- package/src/ai/movement/kinematic/flee.js +1 -1
- package/src/ai/movement/kinematic/seek.js +1 -1
- package/src/ai/movement/kinematic/wander-as-seek.js +1 -1
- package/src/ai/movement/kinematic/wander.js +1 -1
- package/src/animation/due.js +36 -0
- package/src/animation/sprite.js +1 -1
- package/src/behaviors/at-fps.js +49 -0
- package/src/behaviors/audio.js +82 -18
- package/src/behaviors/camera.js +1 -1
- package/src/behaviors/controls/dynamic/modern.js +2 -2
- package/src/behaviors/controls/dynamic/shooter.js +2 -2
- package/src/behaviors/controls/dynamic/tank.js +2 -2
- package/src/behaviors/controls/event-handlers.js +19 -16
- package/src/behaviors/controls/kinematic/modern.js +2 -2
- package/src/behaviors/controls/kinematic/shooter.js +2 -2
- package/src/behaviors/controls/kinematic/tank.js +2 -2
- package/src/behaviors/debug/collision.js +18 -4
- package/src/behaviors/fps.js +1 -1
- package/src/behaviors/game.js +14 -0
- package/src/behaviors/infinite-scroll.js +5 -2
- package/src/behaviors/input/controls.js +45 -12
- package/src/behaviors/input/gamepad.js +30 -29
- package/src/behaviors/input/hover.js +57 -0
- package/src/behaviors/input/input.js +22 -14
- package/src/behaviors/input/keyboard.js +79 -12
- package/src/behaviors/input/mouse.js +76 -11
- package/src/behaviors/input/pointer.js +39 -0
- package/src/behaviors/input/touch.js +2 -2
- package/src/behaviors/particles.js +124 -0
- package/src/behaviors/physics/bouncy.js +1 -1
- package/src/behaviors/physics/clamped.js +1 -1
- package/src/behaviors/physics/jumpable.js +7 -5
- package/src/behaviors/state-machine/fsm.js +55 -0
- package/src/behaviors/state-machine/mappings.js +59 -0
- package/src/behaviors/state-machine/scenes.js +89 -0
- package/src/behaviors/ui/button.js +26 -11
- package/src/behaviors/ui/slider.js +114 -0
- package/src/collision/detection.js +37 -6
- package/src/core/core-events.js +3 -0
- package/src/core/engine.js +70 -7
- package/src/core/loops/fixed.js +6 -1
- package/src/core/loops/index.js +23 -3
- package/src/core/middlewares/entity-pool/entity-pool-middleware.js +20 -2
- package/src/core/middlewares/entity-pool/entity-pools.js +4 -0
- package/src/core/types.js +50 -0
- package/src/movement/dynamic/modern.js +1 -1
- package/src/movement/dynamic/tank.js +1 -1
- package/src/movement/kinematic/modern.js +1 -1
- package/src/movement/kinematic/tank.js +1 -1
- package/src/physics/anchor.js +115 -0
- package/src/physics/bounds.js +34 -27
- package/src/physics/position.js +29 -11
- package/src/storage.js +53 -0
- package/src/test.js +185 -0
- package/src/behaviors/fsm.js +0 -31
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { face } from "@inglorious/engine/ai/movement/dynamic/face.js"
|
|
2
2
|
import { tankMove } from "@inglorious/engine/movement/dynamic/tank.js"
|
|
3
3
|
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
4
|
-
import { extend, merge } from "@inglorious/utils/
|
|
5
|
-
import { zero } from "@inglorious/utils/
|
|
4
|
+
import { extend, merge } from "@inglorious/utils/object.js"
|
|
5
|
+
import { zero } from "@inglorious/utils/vector.js"
|
|
6
6
|
|
|
7
7
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
8
8
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { tankMove } from "@inglorious/engine/movement/dynamic/tank.js"
|
|
2
|
-
import { extend, merge } from "@inglorious/utils/
|
|
3
|
-
import { zero } from "@inglorious/utils/
|
|
2
|
+
import { extend, merge } from "@inglorious/utils/object.js"
|
|
3
|
+
import { zero } from "@inglorious/utils/vector.js"
|
|
4
4
|
|
|
5
5
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
6
6
|
|
|
@@ -1,23 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns held input into movement flags on an entity.
|
|
3
|
+
*
|
|
4
|
+
* Each handler is named after the action it answers to, so it needs no addressing:
|
|
5
|
+
* the store already broadcasts `moveLeft` to every type that handles it, and the
|
|
6
|
+
* movement is stored on whichever entity asked for these.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```js
|
|
10
|
+
* const Actor = [modernControls(), createMovementEventHandlers(["moveLeft"])]
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
1
13
|
export function createMovementEventHandlers(events) {
|
|
2
14
|
return events.reduce((acc, eventName) => {
|
|
3
|
-
acc[eventName] = (entity
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
entityId = event
|
|
7
|
-
} else {
|
|
8
|
-
entityId = event.entityId
|
|
9
|
-
value = event.value
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (entityId === entity.id) {
|
|
13
|
-
entity.movement[eventName] = value ?? true
|
|
14
|
-
}
|
|
15
|
+
acc[eventName] = (entity) => {
|
|
16
|
+
entity.movement ??= {}
|
|
17
|
+
entity.movement[eventName] = true
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
acc[`${eventName}End`] = (entity
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
acc[`${eventName}End`] = (entity) => {
|
|
21
|
+
if (!entity.movement) return
|
|
22
|
+
|
|
23
|
+
entity.movement[eventName] = false
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
return acc
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { modernMove } from "@inglorious/engine/movement/kinematic/modern.js"
|
|
2
|
-
import { extend, merge } from "@inglorious/utils/
|
|
3
|
-
import { zero } from "@inglorious/utils/
|
|
2
|
+
import { extend, merge } from "@inglorious/utils/object.js"
|
|
3
|
+
import { zero } from "@inglorious/utils/vector.js"
|
|
4
4
|
|
|
5
5
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
6
6
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { face } from "@inglorious/engine/ai/movement/kinematic/face.js"
|
|
2
2
|
import { tankMove } from "@inglorious/engine/movement/kinematic/tank.js"
|
|
3
3
|
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
4
|
-
import { extend, merge } from "@inglorious/utils/
|
|
5
|
-
import { zero } from "@inglorious/utils/
|
|
4
|
+
import { extend, merge } from "@inglorious/utils/object.js"
|
|
5
|
+
import { zero } from "@inglorious/utils/vector.js"
|
|
6
6
|
|
|
7
7
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
8
8
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { tankMove } from "@inglorious/engine/movement/kinematic/tank.js"
|
|
2
|
-
import { extend, merge } from "@inglorious/utils/
|
|
3
|
-
import { zero } from "@inglorious/utils/
|
|
2
|
+
import { extend, merge } from "@inglorious/utils/object.js"
|
|
3
|
+
import { zero } from "@inglorious/utils/vector.js"
|
|
4
4
|
|
|
5
5
|
import { createMovementEventHandlers } from "../event-handlers.js"
|
|
6
6
|
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { collisionGroupOf } from "../../collision/detection.js"
|
|
2
|
+
|
|
3
|
+
const DEFAULT_GROUP = "hitbox"
|
|
4
|
+
const GREEN = "#00FF00"
|
|
5
|
+
|
|
1
6
|
export function collisionGizmos(params) {
|
|
2
7
|
return (type) => ({
|
|
3
8
|
render(entity, ctx, api) {
|
|
@@ -15,10 +20,19 @@ export function collisionGizmos(params) {
|
|
|
15
20
|
|
|
16
21
|
ctx.save()
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
})
|
|
23
|
+
// Resolved the same way the detection resolves it, so that a `solid` entity with
|
|
24
|
+
// no declared block still shows the box it is really using, and an entity with
|
|
25
|
+
// neither shows nothing rather than throwing.
|
|
26
|
+
Object.values(entity.collisions ?? {})
|
|
27
|
+
.concat([collisionGroupOf(entity, DEFAULT_GROUP)])
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
// Resolving the default group hands back the very object a declared block
|
|
30
|
+
// already gave, so keeping the first of each is what stops it being drawn twice.
|
|
31
|
+
.filter((collision, index, all) => all.indexOf(collision) === index)
|
|
32
|
+
.forEach((collision) => {
|
|
33
|
+
const render = params.shapes[collision.shape]
|
|
34
|
+
render?.({ ...entity, ...collision, color: GREEN }, ctx, api)
|
|
35
|
+
})
|
|
22
36
|
|
|
23
37
|
ctx.restore()
|
|
24
38
|
},
|
package/src/behaviors/fps.js
CHANGED
package/src/behaviors/game.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export function game() {
|
|
2
2
|
return {
|
|
3
|
+
// The store halts the world when it sees these, so that anything which moves by
|
|
4
|
+
// integrating a delta time stops without having to be told about it, and a type
|
|
5
|
+
// that declares `updatesWhilePaused` is exempt so an overlay keeps working.
|
|
6
|
+
//
|
|
7
|
+
// Setting the flag here as well is what makes `paused` readable, for the things
|
|
8
|
+
// that want to know rather than to be halted.
|
|
3
9
|
pause(entity) {
|
|
4
10
|
entity.paused = true
|
|
5
11
|
},
|
|
@@ -8,6 +14,14 @@ export function game() {
|
|
|
8
14
|
entity.paused = false
|
|
9
15
|
},
|
|
10
16
|
|
|
17
|
+
// Nothing halts on this one, because the loop is not the store's to halt. The flag
|
|
18
|
+
// is set here and the engine reads it after the frame, in the same place it reads
|
|
19
|
+
// `devMode` -- which is what lets any game offer a way out without reaching for the
|
|
20
|
+
// engine itself, and keeps a behaviour testable against a bare store.
|
|
21
|
+
quit(entity) {
|
|
22
|
+
entity.quit = true
|
|
23
|
+
},
|
|
24
|
+
|
|
11
25
|
keyboardKeyUp(entity, code) {
|
|
12
26
|
switch (code) {
|
|
13
27
|
case "KeyC":
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { mod, scale, sum } from "@inglorious/utils/
|
|
1
|
+
import { mod, scale, sum } from "@inglorious/utils/vector.js"
|
|
2
2
|
|
|
3
3
|
export function infiniteScroll() {
|
|
4
4
|
return {
|
|
5
|
-
update(entity, dt) {
|
|
5
|
+
update(entity, dt, api) {
|
|
6
|
+
// A paused game freezes the world, scrolling included.
|
|
7
|
+
if (api.getEntity("game")?.paused) return
|
|
8
|
+
|
|
6
9
|
entity.position = mod(
|
|
7
10
|
sum(entity.position, scale(entity.velocity, dt)),
|
|
8
11
|
entity.image.loop,
|
|
@@ -1,21 +1,54 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { createGamepadEntity, gamepad, gamepadsPoller } from "./gamepad.js"
|
|
2
|
+
import { createInputEntity, input } from "./input.js"
|
|
3
|
+
import { createKeyboardEntity, keyboard } from "./keyboard.js"
|
|
4
|
+
import { createPointerEntity, pointer } from "./pointer.js"
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
/**
|
|
7
|
+
* The types every control needs, composed once and listed under `types`.
|
|
8
|
+
*
|
|
9
|
+
* Each one is a bare behavior rather than a list of them, because nothing else
|
|
10
|
+
* defines these types for it to compose onto: the engine only appends to a type
|
|
11
|
+
* that is already a list, which is how a game keeps the built-in ones. To add to
|
|
12
|
+
* a control type, list the behaviors instead, as in `Keyboard: [keyboard(), …]`.
|
|
13
|
+
*
|
|
14
|
+
* There is one of each per game. A game has one keyboard, one pointer and one
|
|
15
|
+
* gamepad mapping, and an action's name is what addresses it: a type that handles
|
|
16
|
+
* `moveLeft` is the type that answers to it.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```js
|
|
20
|
+
* types: { ...controlTypes(), Game, Bird }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function controlTypes() {
|
|
6
24
|
return {
|
|
7
|
-
Keyboard:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
25
|
+
Keyboard: keyboard(),
|
|
26
|
+
Pointer: pointer(),
|
|
27
|
+
GamepadsPoller: gamepadsPoller(),
|
|
28
|
+
Gamepad: gamepad(),
|
|
29
|
+
Input: input(),
|
|
11
30
|
}
|
|
12
31
|
}
|
|
13
32
|
|
|
14
|
-
|
|
33
|
+
/**
|
|
34
|
+
* The entities that listen for input on the game's behalf, spread under `entities`.
|
|
35
|
+
* The mapping turns a key, axis or button into an action the rest of the game
|
|
36
|
+
* listens for by name.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```js
|
|
40
|
+
* entities: {
|
|
41
|
+
* ...createControlEntities({ Space: "press" }, ["sceneClick"]),
|
|
42
|
+
* game: { type: "Game" },
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function createControlEntities(mapping = {}, pointerActions = []) {
|
|
15
47
|
return {
|
|
16
48
|
gamepads: { type: "GamepadsPoller" },
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
49
|
+
keyboard: createKeyboardEntity(mapping),
|
|
50
|
+
gamepad: createGamepadEntity(mapping),
|
|
51
|
+
input: createInputEntity(),
|
|
52
|
+
pointer: createPointerEntity(pointerActions),
|
|
20
53
|
}
|
|
21
54
|
}
|
|
@@ -1,43 +1,36 @@
|
|
|
1
|
-
export function gamepadsPoller(
|
|
1
|
+
export function gamepadsPoller() {
|
|
2
2
|
return {
|
|
3
3
|
create(entity) {
|
|
4
4
|
entity.gamepadStateCache ??= {}
|
|
5
5
|
},
|
|
6
6
|
|
|
7
7
|
update(entity, dt, api) {
|
|
8
|
-
navigator.getGamepads().forEach((
|
|
9
|
-
if (
|
|
8
|
+
navigator.getGamepads().forEach((pad) => {
|
|
9
|
+
if (pad == null) return
|
|
10
10
|
|
|
11
|
-
const cache = (entity.gamepadStateCache[
|
|
11
|
+
const cache = (entity.gamepadStateCache[pad.index] ??= {
|
|
12
12
|
axes: [],
|
|
13
13
|
buttons: [],
|
|
14
14
|
})
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
pad.axes.forEach((axis, index) => {
|
|
17
17
|
if (axis === cache.axes[index]) return
|
|
18
18
|
|
|
19
19
|
api.notify("gamepadAxis", {
|
|
20
|
-
targetId: targetIds[gamepad.index],
|
|
21
20
|
axis: `Axis${index}`,
|
|
22
21
|
value: axis,
|
|
23
22
|
})
|
|
24
23
|
cache.axes[index] = axis
|
|
25
24
|
})
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
pad.buttons.forEach((button, index) => {
|
|
28
27
|
const wasPressed = cache.buttons[index]
|
|
29
28
|
const isPressed = button.pressed
|
|
30
29
|
|
|
31
30
|
if (isPressed && !wasPressed) {
|
|
32
|
-
api.notify("gamepadPress", {
|
|
33
|
-
targetId: targetIds[gamepad.index],
|
|
34
|
-
button: `Btn${index}`,
|
|
35
|
-
})
|
|
31
|
+
api.notify("gamepadPress", { button: `Btn${index}` })
|
|
36
32
|
} else if (!isPressed && wasPressed) {
|
|
37
|
-
api.notify("gamepadRelease", {
|
|
38
|
-
targetId: targetIds[gamepad.index],
|
|
39
|
-
button: `Btn${index}`,
|
|
40
|
-
})
|
|
33
|
+
api.notify("gamepadRelease", { button: `Btn${index}` })
|
|
41
34
|
}
|
|
42
35
|
|
|
43
36
|
cache.buttons[index] = isPressed
|
|
@@ -47,44 +40,52 @@ export function gamepadsPoller(targetIds = []) {
|
|
|
47
40
|
}
|
|
48
41
|
}
|
|
49
42
|
|
|
50
|
-
export function
|
|
43
|
+
export function gamepad() {
|
|
44
|
+
// What each button that is currently held was pressed as.
|
|
45
|
+
const pressedAs = {}
|
|
46
|
+
|
|
51
47
|
return {
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
mappingChange(entity, mapping) {
|
|
49
|
+
entity.mapping = mapping
|
|
50
|
+
},
|
|
54
51
|
|
|
52
|
+
gamepadAxis(entity, { axis, value }, api) {
|
|
55
53
|
const action = entity.mapping[axis]
|
|
56
54
|
if (!action) return
|
|
57
55
|
|
|
58
56
|
entity[action] = value
|
|
59
|
-
api.notify("inputAxis", {
|
|
57
|
+
api.notify("inputAxis", { action, value })
|
|
60
58
|
},
|
|
61
59
|
|
|
62
|
-
gamepadPress(entity, {
|
|
63
|
-
if (targetId !== entity.targetId) return
|
|
64
|
-
|
|
60
|
+
gamepadPress(entity, { button }, api) {
|
|
65
61
|
const action = entity.mapping[button]
|
|
66
62
|
if (!action) return
|
|
67
63
|
|
|
68
64
|
if (!entity[action]) {
|
|
69
65
|
entity[action] = true
|
|
70
|
-
api.notify("inputPress", {
|
|
66
|
+
api.notify("inputPress", { action })
|
|
71
67
|
}
|
|
68
|
+
|
|
69
|
+
// Remembered for the same reason as on the keyboard: a button can still be held
|
|
70
|
+
// when the mapping it was pressed under is changed under it.
|
|
71
|
+
pressedAs[button] = action
|
|
72
72
|
},
|
|
73
73
|
|
|
74
|
-
gamepadRelease(entity, {
|
|
75
|
-
|
|
74
|
+
gamepadRelease(entity, { button }, api) {
|
|
75
|
+
const action = pressedAs[button] ?? entity.mapping[button]
|
|
76
|
+
|
|
77
|
+
delete pressedAs[button]
|
|
76
78
|
|
|
77
|
-
const action = entity.mapping[button]
|
|
78
79
|
if (!action) return
|
|
79
80
|
|
|
80
81
|
if (entity[action]) {
|
|
81
82
|
entity[action] = false
|
|
82
|
-
api.notify("inputRelease", {
|
|
83
|
+
api.notify("inputRelease", { action })
|
|
83
84
|
}
|
|
84
85
|
},
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
export function
|
|
89
|
-
return { type: "
|
|
89
|
+
export function createGamepadEntity(mapping = {}) {
|
|
90
|
+
return { type: "Gamepad", mapping }
|
|
90
91
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { findCollision } from "@inglorious/engine/collision/detection.js"
|
|
2
|
+
|
|
3
|
+
const HAND = "hand"
|
|
4
|
+
const DEFAULT_CURSOR = "arrow"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A pointer that turns into a hand over anything there is room to press.
|
|
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.
|
|
11
|
+
* Written as a decorator rather than as part of the mouse itself, so a game that has
|
|
12
|
+
* nothing to press -- or a mouse that is meant to stay a crosshair whatever it is
|
|
13
|
+
* over -- simply leaves it out.
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} [cursors] - What the pointer is when it is not over anything.
|
|
16
|
+
* @param {string} [cursors.off] - The cursor away from anything pressable.
|
|
17
|
+
*/
|
|
18
|
+
export function hover({ off = DEFAULT_CURSOR } = {}) {
|
|
19
|
+
return (type) => ({
|
|
20
|
+
create(entity, payload, api) {
|
|
21
|
+
type.create?.(entity, payload, api)
|
|
22
|
+
|
|
23
|
+
entity.cursor ??= off
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
mouseMove(entity, position, api) {
|
|
27
|
+
type.mouseMove?.(entity, position, api)
|
|
28
|
+
|
|
29
|
+
point(entity, api, off)
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
mouseDown(entity, position, api) {
|
|
33
|
+
type.mouseDown?.(entity, position, api)
|
|
34
|
+
|
|
35
|
+
point(entity, api, off)
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
mouseUp(entity, position, api) {
|
|
39
|
+
type.mouseUp?.(entity, position, api)
|
|
40
|
+
|
|
41
|
+
point(entity, api, off)
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Puts the hand up or takes it down again.
|
|
48
|
+
*
|
|
49
|
+
* Something pressable under the pointer, or something pressed whatever the pointer has
|
|
50
|
+
* wandered off to since: a drag that started on a control should not flicker back
|
|
51
|
+
* while it is still being held.
|
|
52
|
+
*/
|
|
53
|
+
function point(entity, api, off) {
|
|
54
|
+
const held = entity.pressedId ?? findCollision(entity, api.getEntities())?.id
|
|
55
|
+
|
|
56
|
+
entity.cursor = held ? HAND : off
|
|
57
|
+
}
|
|
@@ -1,31 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns input actions into the events the rest of the game listens for.
|
|
3
|
+
*
|
|
4
|
+
* An action's name is its address: a type that handles `moveLeft` is the type that
|
|
5
|
+
* answers to it. Nothing has to say who the input was for, because the names in the
|
|
6
|
+
* mapping are the game's own vocabulary.
|
|
7
|
+
*
|
|
8
|
+
* This does not hold that vocabulary, and takes no part in changing it. What each key
|
|
9
|
+
* means is a question for whoever decides it -- see `mappings`, which is the usual
|
|
10
|
+
* reason -- and each device that reads a mapping is told directly, rather than being
|
|
11
|
+
* reached across the world by something that should not be writing to entities other
|
|
12
|
+
* than the one it was given.
|
|
13
|
+
*/
|
|
14
|
+
|
|
1
15
|
export function input() {
|
|
2
16
|
return {
|
|
3
|
-
inputAxis(entity, {
|
|
4
|
-
if (targetId !== entity.targetId) return
|
|
5
|
-
|
|
17
|
+
inputAxis(entity, { action, value }, api) {
|
|
6
18
|
entity[action] = value
|
|
7
19
|
|
|
8
|
-
api.notify(action, {
|
|
20
|
+
api.notify(action, { value })
|
|
9
21
|
},
|
|
10
22
|
|
|
11
|
-
inputPress(entity, {
|
|
12
|
-
if (targetId !== entity.targetId) return
|
|
13
|
-
|
|
23
|
+
inputPress(entity, { action }, api) {
|
|
14
24
|
entity[action] = true
|
|
15
25
|
|
|
16
|
-
api.notify(action
|
|
26
|
+
api.notify(action)
|
|
17
27
|
},
|
|
18
28
|
|
|
19
|
-
inputRelease(entity, {
|
|
20
|
-
if (targetId !== entity.targetId) return
|
|
21
|
-
|
|
29
|
+
inputRelease(entity, { action }, api) {
|
|
22
30
|
entity[action] = false
|
|
23
31
|
|
|
24
|
-
api.notify(`${action}End
|
|
32
|
+
api.notify(`${action}End`)
|
|
25
33
|
},
|
|
26
34
|
}
|
|
27
35
|
}
|
|
28
36
|
|
|
29
|
-
export function
|
|
30
|
-
return { type: "Input"
|
|
37
|
+
export function createInputEntity() {
|
|
38
|
+
return { type: "Input" }
|
|
31
39
|
}
|
|
@@ -1,52 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reports keys by their physical code, and the characters they produce.
|
|
3
|
+
*
|
|
4
|
+
* Actions are mapped by `code`, which is the physical key: that keeps WASD on the
|
|
5
|
+
* same fingers whatever layout is installed. Typing needs `key` instead, which is
|
|
6
|
+
* the character the press produced and so differs per layout and per Shift. Both
|
|
7
|
+
* are notified, so `keyboardKeyDown` stays layout-independent while
|
|
8
|
+
* `keyboardChar` carries something you can put in a field.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```js
|
|
12
|
+
* // Acting on a key, by position.
|
|
13
|
+
* const actions = { KeyW: "moveUp", Space: "press" }
|
|
14
|
+
*
|
|
15
|
+
* // Typing, by character.
|
|
16
|
+
* keyboardChar(entity, { character }) {
|
|
17
|
+
* entity.name += character
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
1
21
|
export function keyboard() {
|
|
2
22
|
let handleKeyDown, handleKeyUp
|
|
23
|
+
|
|
24
|
+
// What each key that is currently down was pressed as.
|
|
25
|
+
const pressedAs = {}
|
|
3
26
|
let currentDocument = null
|
|
4
27
|
|
|
28
|
+
function detach() {
|
|
29
|
+
currentDocument?.removeEventListener("keydown", handleKeyDown)
|
|
30
|
+
currentDocument?.removeEventListener("keyup", handleKeyUp)
|
|
31
|
+
}
|
|
32
|
+
|
|
5
33
|
return {
|
|
6
34
|
create(entity, payload, api) {
|
|
7
35
|
currentDocument = document.body.ownerDocument || document
|
|
8
36
|
|
|
9
|
-
handleKeyDown =
|
|
10
|
-
handleKeyUp =
|
|
37
|
+
handleKeyDown = createKeyDownHandler(api)
|
|
38
|
+
handleKeyUp = createKeyUpHandler(api)
|
|
11
39
|
|
|
12
40
|
currentDocument.addEventListener("keydown", handleKeyDown)
|
|
13
41
|
currentDocument.addEventListener("keyup", handleKeyUp)
|
|
14
42
|
},
|
|
15
43
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
44
|
+
// What this device answers to is set on this device. Each one that reads a mapping
|
|
45
|
+
// takes the change itself rather than being handed it, because the one thing a
|
|
46
|
+
// handler can be trusted to change is the entity it was given.
|
|
47
|
+
mappingChange(entity, mapping) {
|
|
48
|
+
entity.mapping = mapping
|
|
19
49
|
},
|
|
20
50
|
|
|
51
|
+
stop: detach,
|
|
52
|
+
|
|
53
|
+
// Being taken off the world has to let go of the document the same way stopping the
|
|
54
|
+
// game does. Without this an entity that is created and destroyed -- a screen's own
|
|
55
|
+
// keyboard, say -- leaves its listeners behind, and the next one added means two
|
|
56
|
+
// keyboards answering to the same key.
|
|
57
|
+
destroy: detach,
|
|
58
|
+
|
|
21
59
|
keyboardKeyDown(entity, keyCode, api) {
|
|
22
60
|
const action = entity.mapping[keyCode]
|
|
23
61
|
if (!action) return
|
|
24
62
|
|
|
25
63
|
if (!entity[action]) {
|
|
26
64
|
entity[action] = true
|
|
27
|
-
api.notify("inputPress", {
|
|
65
|
+
api.notify("inputPress", { action })
|
|
28
66
|
}
|
|
67
|
+
|
|
68
|
+
// Remembered, because a key can still be down when the mapping it was pressed under
|
|
69
|
+
// is changed under it -- and the release then has to go back to the action that was
|
|
70
|
+
// pressed rather than looking it up in a mapping that no longer names it.
|
|
71
|
+
pressedAs[keyCode] = action
|
|
29
72
|
},
|
|
30
73
|
|
|
31
74
|
keyboardKeyUp(entity, keyCode, api) {
|
|
32
|
-
const action = entity.mapping[keyCode]
|
|
75
|
+
const action = pressedAs[keyCode] ?? entity.mapping[keyCode]
|
|
76
|
+
|
|
77
|
+
delete pressedAs[keyCode]
|
|
78
|
+
|
|
33
79
|
if (!action) return
|
|
34
80
|
|
|
35
81
|
if (entity[action]) {
|
|
36
82
|
entity[action] = false
|
|
37
|
-
api.notify("inputRelease", {
|
|
83
|
+
api.notify("inputRelease", { action })
|
|
38
84
|
}
|
|
39
85
|
},
|
|
40
86
|
}
|
|
41
87
|
}
|
|
42
88
|
|
|
43
|
-
export function
|
|
44
|
-
return { type: "Keyboard",
|
|
89
|
+
export function createKeyboardEntity(mapping = {}) {
|
|
90
|
+
return { type: "Keyboard", mapping }
|
|
45
91
|
}
|
|
46
92
|
|
|
47
|
-
function
|
|
93
|
+
function createKeyDownHandler(api) {
|
|
48
94
|
return (event) => {
|
|
49
95
|
event.stopPropagation()
|
|
50
|
-
api.notify(
|
|
96
|
+
api.notify("keyboardKeyDown", event.code)
|
|
97
|
+
|
|
98
|
+
// Holding a key down arrives as further keydowns, which is what typing
|
|
99
|
+
// should do.
|
|
100
|
+
if (isCharacter(event.key)) {
|
|
101
|
+
api.notify("keyboardChar", { character: event.key })
|
|
102
|
+
}
|
|
51
103
|
}
|
|
52
104
|
}
|
|
105
|
+
|
|
106
|
+
function createKeyUpHandler(api) {
|
|
107
|
+
return (event) => {
|
|
108
|
+
event.stopPropagation()
|
|
109
|
+
api.notify("keyboardKeyUp", event.code)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Anything that is not a single printable character carries a word instead:
|
|
114
|
+
// modifiers, navigation and function keys among them.
|
|
115
|
+
const SINGLE_CHARACTER = 1
|
|
116
|
+
|
|
117
|
+
function isCharacter(key) {
|
|
118
|
+
return typeof key === "string" && key.length === SINGLE_CHARACTER
|
|
119
|
+
}
|