@inglorious/engine 23.0.1 → 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.
- package/README.md +223 -0
- package/package.json +12 -8
- package/src/ai/movement/dynamic/align.js +1 -1
- package/src/ai/movement/dynamic/arrive.js +1 -2
- package/src/ai/movement/dynamic/evade.js +7 -2
- package/src/ai/movement/dynamic/face.js +1 -2
- package/src/ai/movement/dynamic/flee.js +3 -2
- package/src/ai/movement/dynamic/look-where-youre-going.js +1 -1
- package/src/ai/movement/dynamic/match-velocity.js +3 -2
- package/src/ai/movement/dynamic/pursue.js +7 -2
- package/src/ai/movement/dynamic/seek.js +3 -2
- package/src/ai/movement/dynamic/wander.js +1 -2
- package/src/ai/movement/kinematic/align.js +1 -1
- package/src/ai/movement/kinematic/arrive.js +3 -2
- package/src/ai/movement/kinematic/face.js +1 -2
- package/src/ai/movement/kinematic/flee.js +3 -2
- package/src/ai/movement/kinematic/seek.js +3 -2
- package/src/ai/movement/kinematic/wander-as-seek.js +1 -2
- package/src/ai/movement/kinematic/wander.js +1 -2
- 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 -5
- 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 -3
- 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 -3
- 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 -7
- 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 -2
- package/src/movement/kinematic/modern.js +1 -1
- package/src/movement/kinematic/tank.js +1 -2
- package/src/physics/anchor.js +115 -0
- package/src/physics/bounds.js +35 -28
- 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
package/src/behaviors/audio.js
CHANGED
|
@@ -1,37 +1,102 @@
|
|
|
1
1
|
const DEFAULT_VOLUME = 1
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Fetches and decodes one sound. A sound that is missing, or that the browser
|
|
5
|
+
* cannot decode, is reported and skipped: losing one effect is not worth refusing
|
|
6
|
+
* to start.
|
|
7
|
+
*/
|
|
8
|
+
async function load(context, cache, name, url) {
|
|
9
|
+
try {
|
|
10
|
+
const response = await fetch(url)
|
|
11
|
+
|
|
12
|
+
if (!response.ok) {
|
|
13
|
+
console.warn(`Sound '${name}' could not be fetched from ${url}`)
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const audioBuffer = await context.decodeAudioData(
|
|
18
|
+
await response.arrayBuffer(),
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
cache.set(name, audioBuffer)
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.warn(`Sound '${name}' could not be loaded from ${url}`, error)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
5
26
|
|
|
27
|
+
export function audio() {
|
|
6
28
|
const audioBufferCache = new Map()
|
|
7
29
|
const activeSources = new Map()
|
|
8
30
|
|
|
31
|
+
// The context is made when something first asks for sound rather than when this is
|
|
32
|
+
// called, because the engine's own default configuration builds every behaviour as it is
|
|
33
|
+
// imported -- and a module that reaches for `window` on the way in cannot be imported
|
|
34
|
+
// where there is no window, which is anywhere a test runs.
|
|
35
|
+
let audioContext
|
|
36
|
+
|
|
37
|
+
function context() {
|
|
38
|
+
audioContext ??= new (window.AudioContext || window.webkitAudioContext)()
|
|
39
|
+
|
|
40
|
+
return audioContext
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resume() {
|
|
44
|
+
window.removeEventListener("pointerdown", resume)
|
|
45
|
+
window.removeEventListener("keydown", resume)
|
|
46
|
+
context().resume()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Both stopping the game and being taken off the world have to let go of the window and
|
|
50
|
+
// of anything still sounding over it. One function, because the two differ only in when
|
|
51
|
+
// they happen -- and a listener an entity that is no longer in the world forgot to take
|
|
52
|
+
// off would stay on the page for as long as the page is open.
|
|
53
|
+
function release() {
|
|
54
|
+
window.removeEventListener("pointerdown", resume)
|
|
55
|
+
window.removeEventListener("keydown", resume)
|
|
56
|
+
|
|
57
|
+
for (const source of activeSources.values()) {
|
|
58
|
+
source.stop()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
activeSources.clear()
|
|
62
|
+
}
|
|
63
|
+
|
|
9
64
|
return {
|
|
65
|
+
create() {
|
|
66
|
+
// Browsers keep an AudioContext suspended until the user interacts with
|
|
67
|
+
// the page, so playback would be silently dropped until then.
|
|
68
|
+
window.addEventListener("pointerdown", resume)
|
|
69
|
+
window.addEventListener("keydown", resume)
|
|
70
|
+
},
|
|
71
|
+
|
|
10
72
|
async init(entity) {
|
|
11
73
|
const sounds = entity.sounds || {}
|
|
12
74
|
|
|
13
75
|
await Promise.all(
|
|
14
|
-
Object.entries(sounds).map(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer)
|
|
18
|
-
audioBufferCache.set(name, audioBuffer)
|
|
19
|
-
}),
|
|
76
|
+
Object.entries(sounds).map(([name, { url }]) =>
|
|
77
|
+
load(context(), audioBufferCache, name, url),
|
|
78
|
+
),
|
|
20
79
|
)
|
|
21
80
|
},
|
|
22
81
|
|
|
23
82
|
soundPlay(entity, name) {
|
|
24
|
-
const { volume = DEFAULT_VOLUME, loop } = entity.sounds[name]
|
|
83
|
+
const { volume = DEFAULT_VOLUME, loop } = entity.sounds[name] || {}
|
|
25
84
|
const audioBuffer = audioBufferCache.get(name)
|
|
26
85
|
|
|
27
|
-
|
|
28
|
-
|
|
86
|
+
if (!audioBuffer) return
|
|
87
|
+
|
|
88
|
+
// Only one source per sound can be tracked, so playing a sound again
|
|
89
|
+
// replaces the previous one instead of stacking a new copy on top of it.
|
|
90
|
+
activeSources.get(name)?.stop()
|
|
91
|
+
|
|
92
|
+
const source = context().createBufferSource()
|
|
93
|
+
const gainNode = context().createGain()
|
|
29
94
|
|
|
30
95
|
source.buffer = audioBuffer
|
|
31
96
|
gainNode.gain.value = volume
|
|
32
97
|
|
|
33
98
|
source.connect(gainNode)
|
|
34
|
-
gainNode.connect(
|
|
99
|
+
gainNode.connect(context().destination)
|
|
35
100
|
|
|
36
101
|
source.loop = loop
|
|
37
102
|
source.start()
|
|
@@ -45,11 +110,10 @@ export function audio() {
|
|
|
45
110
|
activeSources.delete(name)
|
|
46
111
|
},
|
|
47
112
|
|
|
48
|
-
stop
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
113
|
+
stop: release,
|
|
114
|
+
|
|
115
|
+
// Taken off the world rather than the game stopping: the same letting go, because
|
|
116
|
+
// there is nothing left for anything still sounding to be sound over.
|
|
117
|
+
destroy: release,
|
|
54
118
|
}
|
|
55
119
|
}
|
package/src/behaviors/camera.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { arrive } from "@inglorious/engine/ai/movement/dynamic/arrive.js"
|
|
2
|
-
import {
|
|
3
|
-
defaults,
|
|
4
|
-
extend,
|
|
5
|
-
merge,
|
|
6
|
-
} from "@inglorious/utils/data-structures/objects.js"
|
|
7
2
|
import { lerp } from "@inglorious/utils/math/linear-interpolation.js"
|
|
3
|
+
import { defaults, extend, merge } from "@inglorious/utils/object.js"
|
|
8
4
|
|
|
9
5
|
const DEFAULT_PARAMS = {
|
|
10
6
|
zoom: 1,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { modernMove } from "@inglorious/engine/movement/dynamic/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/dynamic/face.js"
|
|
2
2
|
import { tankMove } from "@inglorious/engine/movement/dynamic/tank.js"
|
|
3
|
-
import { extend, merge } from "@inglorious/utils/data-structures/objects.js"
|
|
4
3
|
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
5
|
-
import {
|
|
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
|
-
import { extend, merge } from "@inglorious/utils/data-structures/objects.js"
|
|
4
3
|
import { pi } from "@inglorious/utils/math/trigonometry.js"
|
|
5
|
-
import {
|
|
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,9 +1,11 @@
|
|
|
1
|
-
import { scale } from "@inglorious/utils/
|
|
2
|
-
import { mod, sum } from "@inglorious/utils/math/vectors.js"
|
|
1
|
+
import { mod, scale, sum } from "@inglorious/utils/vector.js"
|
|
3
2
|
|
|
4
3
|
export function infiniteScroll() {
|
|
5
4
|
return {
|
|
6
|
-
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
|
+
|
|
7
9
|
entity.position = mod(
|
|
8
10
|
sum(entity.position, scale(entity.velocity, dt)),
|
|
9
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
|
}
|