@inglorious/engine 24.1.3 → 24.1.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 +1 -5
- package/package.json +4 -4
- package/src/ai/movement/dynamic/align.js +1 -1
- package/src/animation/due.js +0 -1
- package/src/animation/sprite.js +0 -2
- package/src/animation/ticker.js +0 -5
- package/src/behaviors/at-fps.js +0 -4
- package/src/behaviors/audio.js +0 -8
- package/src/behaviors/camera.js +0 -8
- package/src/behaviors/debug/collision.js +0 -5
- package/src/behaviors/game.js +0 -10
- package/src/behaviors/infinite-scroll.js +0 -1
- package/src/behaviors/input/gamepad.js +0 -3
- package/src/behaviors/input/keyboard.js +0 -11
- package/src/behaviors/input/mappings.js +0 -7
- package/src/behaviors/input/mouse.js +1 -5
- package/src/behaviors/input/touch.js +1 -2
- package/src/behaviors/particles.js +0 -4
- package/src/behaviors/physics/jumpable.js +0 -21
- package/src/behaviors/ui/slider.js +0 -8
- package/src/collision/detection.js +1 -1
- package/src/core/core-events.js +0 -2
- package/src/core/engine.js +1 -21
- package/src/core/loops/fixed.js +0 -3
- package/src/core/loops/index.js +0 -8
- package/src/core/middlewares/entity-pool/entity-pool-middleware.js +0 -4
- package/src/physics/bounds.js +0 -2
- package/src/systems/ticks.js +0 -3
- package/src/test.js +0 -14
- package/src/storage.js +0 -53
package/README.md
CHANGED
|
@@ -11,10 +11,6 @@ The fastest way to start a new Inglorious Engine project is by using the officia
|
|
|
11
11
|
Run the following command in your terminal and follow the prompts to create a new game from a variety of templates (including plain JavaScript, TypeScript, and IngloriousScript):
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
|
-
# with npm
|
|
15
|
-
npm create @inglorious/game@latest
|
|
16
|
-
|
|
17
|
-
# with pnpm
|
|
18
14
|
pnpm create @inglorious/game
|
|
19
15
|
```
|
|
20
16
|
|
|
@@ -280,7 +276,7 @@ FP has many advantages:
|
|
|
280
276
|
- **Simplified Persistence**: Saving and loading a game is as simple as serializing and deserializing a single JSON object.
|
|
281
277
|
- **Simplified Networking**: For multiplayer games, you don't need to synchronize complex objects. You just send small, serializable `event` objects over the network. Each client processes the same event with the same pure event handler, guaranteeing their game states stay in sync.
|
|
282
278
|
|
|
283
|
-
7. **Leverage the Full JavaScript Ecosystem**: As a pure JavaScript engine, you have immediate access to the world's largest software repository: npm. Need advanced physics, complex AI, or a specific UI library? Integrate it with a simple `
|
|
279
|
+
7. **Leverage the Full JavaScript Ecosystem**: As a pure JavaScript engine, you have immediate access to the world's largest software repository: npm. Need advanced physics, complex AI, or a specific UI library? Integrate it with a simple `pnpm add`. You aren't limited to the built-in features or proprietary plugin ecosystem of a monolithic engine like Godot or Unity.
|
|
284
280
|
|
|
285
281
|
## Architecture: State Management
|
|
286
282
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.1.
|
|
3
|
+
"version": "24.1.4",
|
|
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,8 +33,8 @@
|
|
|
33
33
|
},
|
|
34
34
|
"sideEffects": false,
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@inglorious/store": "^12.
|
|
37
|
-
"@inglorious/utils": "^5.
|
|
36
|
+
"@inglorious/store": "^12.2.0",
|
|
37
|
+
"@inglorious/utils": "^5.2.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"vitest": "^5.0.0"
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"vitest": "^5.0.0"
|
|
47
47
|
},
|
|
48
48
|
"engines": {
|
|
49
|
-
"node": ">=
|
|
49
|
+
"node": ">= 26"
|
|
50
50
|
},
|
|
51
51
|
"peerDependenciesMeta": {
|
|
52
52
|
"vitest": {
|
|
@@ -43,7 +43,7 @@ export function align(
|
|
|
43
43
|
} else {
|
|
44
44
|
targetAngularSpeed = (distance * maxAngularSpeed) / slowRadius
|
|
45
45
|
}
|
|
46
|
-
targetAngularSpeed *= direction / distance
|
|
46
|
+
targetAngularSpeed *= direction / distance
|
|
47
47
|
|
|
48
48
|
const angularSpeedDelta = targetAngularSpeed - angularSpeed
|
|
49
49
|
|
package/src/animation/due.js
CHANGED
package/src/animation/sprite.js
CHANGED
|
@@ -90,10 +90,8 @@ function fromAngle(entity, states) {
|
|
|
90
90
|
const directions = states.length
|
|
91
91
|
const slice = (2 * pi()) / directions
|
|
92
92
|
|
|
93
|
-
// Normalize orientation to [0, 2*PI)
|
|
94
93
|
const normalizedOrientation = mod(toRange(entity.orientation), 2 * pi())
|
|
95
94
|
|
|
96
|
-
// Shift by half a slice so that 0 rad is in the middle of the first slice, then find the index
|
|
97
95
|
const index = Math.floor(
|
|
98
96
|
mod(normalizedOrientation + slice / 2, 2 * pi()) / slice,
|
|
99
97
|
)
|
package/src/animation/ticker.js
CHANGED
|
@@ -24,19 +24,14 @@ export const Ticker = {
|
|
|
24
24
|
|
|
25
25
|
const { speed, defaultValue = DEFAULT_VALUE } = target
|
|
26
26
|
|
|
27
|
-
// The `state` property on a sprite is used to declare the desired animation.
|
|
28
|
-
// The Ticker needs its own internal property to track the *current* animation
|
|
29
|
-
// to detect when it changes. We'll use `target.animation`.
|
|
30
27
|
if (state !== target.animation) {
|
|
31
28
|
target.animation = state
|
|
32
29
|
target.counter = COUNTER_RESET
|
|
33
30
|
target.value = defaultValue
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
// Always ensure the public `state` property reflects the intended animation for `onTick`.
|
|
37
33
|
target.state = state
|
|
38
34
|
|
|
39
|
-
// Ensure properties are initialized on the first run for a given target.
|
|
40
35
|
target.counter ??= COUNTER_RESET
|
|
41
36
|
target.value ??= defaultValue
|
|
42
37
|
|
package/src/behaviors/at-fps.js
CHANGED
|
@@ -39,10 +39,6 @@ export function atFps(fps) {
|
|
|
39
39
|
|
|
40
40
|
if (elapsed === undefined) return
|
|
41
41
|
|
|
42
|
-
// The whole of the time this entity's turn covers, rather than the length of the
|
|
43
|
-
// frame that happened to land on it: six frames of a sixtieth of a second are a
|
|
44
|
-
// tenth of a second, and a game moving by velocity times dt has to move by all of
|
|
45
|
-
// it or it runs six times too slowly for asking to move ten times a second.
|
|
46
42
|
type.update?.(entity, elapsed, api)
|
|
47
43
|
},
|
|
48
44
|
})
|
package/src/behaviors/audio.js
CHANGED
|
@@ -22,10 +22,6 @@ export function audio() {
|
|
|
22
22
|
context().resume()
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
// Both stopping the game and being taken off the world have to let go of the window and
|
|
26
|
-
// of anything still sounding over it. One function, because the two differ only in when
|
|
27
|
-
// they happen -- and a listener an entity that is no longer in the world forgot to take
|
|
28
|
-
// off would stay on the page for as long as the page is open.
|
|
29
25
|
function release() {
|
|
30
26
|
window.removeEventListener("pointerdown", resume)
|
|
31
27
|
window.removeEventListener("keydown", resume)
|
|
@@ -61,8 +57,6 @@ export function audio() {
|
|
|
61
57
|
|
|
62
58
|
if (!audioBuffer) return
|
|
63
59
|
|
|
64
|
-
// Only one source per sound can be tracked, so playing a sound again
|
|
65
|
-
// replaces the previous one instead of stacking a new copy on top of it.
|
|
66
60
|
activeSources.get(name)?.stop()
|
|
67
61
|
|
|
68
62
|
const source = context().createBufferSource()
|
|
@@ -88,8 +82,6 @@ export function audio() {
|
|
|
88
82
|
|
|
89
83
|
stop: release,
|
|
90
84
|
|
|
91
|
-
// Taken off the world rather than the game stopping: the same letting go, because
|
|
92
|
-
// there is nothing left for anything still sounding to be sound over.
|
|
93
85
|
destroy: release,
|
|
94
86
|
}
|
|
95
87
|
}
|
package/src/behaviors/camera.js
CHANGED
|
@@ -22,29 +22,22 @@ export function camera(params) {
|
|
|
22
22
|
create(entity) {
|
|
23
23
|
defaults(entity, params)
|
|
24
24
|
entity.targetZoom = entity.zoom
|
|
25
|
-
// Cache the initial size to calculate the viewport in dev mode
|
|
26
25
|
if (entity.size) {
|
|
27
26
|
entity.baseSize = [...entity.size]
|
|
28
27
|
}
|
|
29
28
|
},
|
|
30
29
|
|
|
31
30
|
update(entity, dt, api) {
|
|
32
|
-
// Follow target
|
|
33
31
|
if (entity.targetId) {
|
|
34
32
|
const target = api.getEntity(entity.targetId)
|
|
35
33
|
if (target) {
|
|
36
|
-
// The camera will "arrive" at the target's position.
|
|
37
|
-
// You can tweak the parameters in the entity definition
|
|
38
|
-
// to change how the camera follows the target.
|
|
39
34
|
merge(entity, arrive(entity, target, dt))
|
|
40
35
|
}
|
|
41
36
|
}
|
|
42
37
|
|
|
43
|
-
// Smooth zoom
|
|
44
38
|
if (entity.zoom !== entity.targetZoom) {
|
|
45
39
|
entity.zoom = lerp(entity.zoom, entity.targetZoom, entity.zoomSpeed)
|
|
46
40
|
|
|
47
|
-
// Update the visual size of the camera's viewport for dev mode
|
|
48
41
|
if (entity.baseSize) {
|
|
49
42
|
entity.size[X] = entity.baseSize[X] / entity.zoom
|
|
50
43
|
entity.size[Y] = entity.baseSize[Y] / entity.zoom
|
|
@@ -54,7 +47,6 @@ export function camera(params) {
|
|
|
54
47
|
|
|
55
48
|
mouseWheel(entity, deltaY) {
|
|
56
49
|
const delta = Math.sign(deltaY)
|
|
57
|
-
// Scrolling down (positive deltaY) should zoom out (decrease zoom value)
|
|
58
50
|
entity.targetZoom -= delta * entity.zoomSpeed * entity.zoomSensitivity
|
|
59
51
|
entity.targetZoom = Math.max(
|
|
60
52
|
entity.minZoom,
|
|
@@ -20,14 +20,9 @@ export function collisionGizmos(params) {
|
|
|
20
20
|
|
|
21
21
|
ctx.save()
|
|
22
22
|
|
|
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
23
|
Object.values(entity.collisions ?? {})
|
|
27
24
|
.concat([collisionGroupOf(entity, DEFAULT_GROUP)])
|
|
28
25
|
.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
26
|
.filter((collision, index, all) => all.indexOf(collision) === index)
|
|
32
27
|
.forEach((collision) => {
|
|
33
28
|
const render = params.shapes[collision.shape]
|
package/src/behaviors/game.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
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.
|
|
9
3
|
pause(entity) {
|
|
10
4
|
entity.paused = true
|
|
11
5
|
},
|
|
@@ -14,10 +8,6 @@ export function game() {
|
|
|
14
8
|
entity.paused = false
|
|
15
9
|
},
|
|
16
10
|
|
|
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
11
|
quit(entity) {
|
|
22
12
|
entity.quit = true
|
|
23
13
|
},
|
|
@@ -41,7 +41,6 @@ export function gamepadsPoller() {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export function gamepad() {
|
|
44
|
-
// What each button that is currently held was pressed as.
|
|
45
44
|
const pressedAs = {}
|
|
46
45
|
|
|
47
46
|
return {
|
|
@@ -66,8 +65,6 @@ export function gamepad() {
|
|
|
66
65
|
api.notify("inputPress", { action })
|
|
67
66
|
}
|
|
68
67
|
|
|
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
68
|
pressedAs[button] = action
|
|
72
69
|
},
|
|
73
70
|
|
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
export function keyboard() {
|
|
22
22
|
let handleKeyDown, handleKeyUp
|
|
23
23
|
|
|
24
|
-
// What each key that is currently down was pressed as.
|
|
25
24
|
const pressedAs = {}
|
|
26
25
|
let currentDocument = null
|
|
27
26
|
|
|
@@ -41,9 +40,6 @@ export function keyboard() {
|
|
|
41
40
|
currentDocument.addEventListener("keyup", handleKeyUp)
|
|
42
41
|
},
|
|
43
42
|
|
|
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
43
|
mappingChange(entity, mapping) {
|
|
48
44
|
entity.mapping = mapping
|
|
49
45
|
},
|
|
@@ -65,9 +61,6 @@ export function keyboard() {
|
|
|
65
61
|
api.notify("inputPress", { action })
|
|
66
62
|
}
|
|
67
63
|
|
|
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
64
|
pressedAs[keyCode] = action
|
|
72
65
|
},
|
|
73
66
|
|
|
@@ -95,8 +88,6 @@ function createKeyDownHandler(api) {
|
|
|
95
88
|
event.stopPropagation()
|
|
96
89
|
api.notify("keyboardKeyDown", event.code)
|
|
97
90
|
|
|
98
|
-
// Holding a key down arrives as further keydowns, which is what typing
|
|
99
|
-
// should do.
|
|
100
91
|
if (isCharacter(event.key)) {
|
|
101
92
|
api.notify("keyboardChar", { character: event.key })
|
|
102
93
|
}
|
|
@@ -110,8 +101,6 @@ function createKeyUpHandler(api) {
|
|
|
110
101
|
}
|
|
111
102
|
}
|
|
112
103
|
|
|
113
|
-
// Anything that is not a single printable character carries a word instead:
|
|
114
|
-
// modifiers, navigation and function keys among them.
|
|
115
104
|
const SINGLE_CHARACTER = 1
|
|
116
105
|
|
|
117
106
|
function isCharacter(key) {
|
|
@@ -30,16 +30,9 @@
|
|
|
30
30
|
*/
|
|
31
31
|
export function mappings(mappingsByState) {
|
|
32
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
33
|
api.notify("mappingChange", mappingsByState[state] ?? {})
|
|
37
34
|
}
|
|
38
35
|
|
|
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
36
|
return (type) => ({
|
|
44
37
|
// A machine is already in its first state when it is made, and nothing announces that:
|
|
45
38
|
// it only announces moves. So the opening state would never be given its keys, and the
|
|
@@ -8,10 +8,6 @@ export function mouse() {
|
|
|
8
8
|
create(entity) {
|
|
9
9
|
entity.collisions ??= {}
|
|
10
10
|
|
|
11
|
-
// Two shapes, for two different jobs. `bounds` is what the pointer is kept inside,
|
|
12
|
-
// and `hitbox` is the point that is tested against everything else -- so a mouse
|
|
13
|
-
// with only the first of them is a cursor that never touches anything, and every
|
|
14
|
-
// press it makes lands on the scene rather than on the control under it.
|
|
15
11
|
entity.collisions.bounds ??= { shape: "point" }
|
|
16
12
|
entity.collisions.hitbox ??= { shape: "point" }
|
|
17
13
|
},
|
|
@@ -100,7 +96,7 @@ export function trackMouse(parent, api, toGamePosition) {
|
|
|
100
96
|
export function createMouseEntity(overrides = {}) {
|
|
101
97
|
return {
|
|
102
98
|
type: "Mouse",
|
|
103
|
-
layer: 999,
|
|
99
|
+
layer: 999,
|
|
104
100
|
position: zero(),
|
|
105
101
|
...overrides,
|
|
106
102
|
}
|
|
@@ -89,7 +89,7 @@ export function trackTouch(parent, api, toGamePosition) {
|
|
|
89
89
|
export function createTouchEntity() {
|
|
90
90
|
return {
|
|
91
91
|
type: "Touch",
|
|
92
|
-
layer: 999,
|
|
92
|
+
layer: 999,
|
|
93
93
|
positions: [],
|
|
94
94
|
collisions: {
|
|
95
95
|
bounds: { shape: "point" },
|
|
@@ -108,7 +108,6 @@ function createHandler(type, parent, api, toGamePosition) {
|
|
|
108
108
|
return
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
// touchend doesn't have touches anymore, but it has changedTouches
|
|
112
111
|
const touches = event.touches.length ? event.touches : event.changedTouches
|
|
113
112
|
|
|
114
113
|
Array.from(touches).forEach((touch, index) => {
|
|
@@ -18,8 +18,6 @@ const WHOLE = 1
|
|
|
18
18
|
const DEFAULT_SIZE = 1
|
|
19
19
|
const PARTICLE_TYPE = "Particle"
|
|
20
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
21
|
const DEFAULT_SIZE_VECTOR = () => v(DEFAULT_SIZE, DEFAULT_SIZE, NO_DEPTH)
|
|
24
22
|
const NO_SPREAD_VECTOR = () => v(NO_SPREAD, NO_SPREAD, NO_SPREAD)
|
|
25
23
|
|
|
@@ -117,8 +115,6 @@ export function emitBurst(api, params) {
|
|
|
117
115
|
}
|
|
118
116
|
}
|
|
119
117
|
|
|
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
118
|
function within(centre, spread) {
|
|
123
119
|
return sum(centre, randomVector(negate(spread), spread))
|
|
124
120
|
}
|
|
@@ -36,8 +36,6 @@ export function jumpable(params) {
|
|
|
36
36
|
jump(entity, payload, api) {
|
|
37
37
|
type.jump?.(entity, payload, api)
|
|
38
38
|
|
|
39
|
-
// An action's name is its address, so a jump is for whichever entity handles
|
|
40
|
-
// it. There is no id to compare against: the notification carries none.
|
|
41
39
|
if (entity.jumpsLeft) {
|
|
42
40
|
entity.vy = jump(entity)
|
|
43
41
|
entity.groundObject = undefined
|
|
@@ -63,7 +61,6 @@ export function jumpable(params) {
|
|
|
63
61
|
}
|
|
64
62
|
const [prevX, prevY] = [...entity.position]
|
|
65
63
|
|
|
66
|
-
// 1. HORIZONTAL MOVEMENT & RESOLUTION
|
|
67
64
|
entity.position[X] += entity.velocity[X] * dt
|
|
68
65
|
const entityLeft = entity.position[X] - width / HALF
|
|
69
66
|
const entityRight = entity.position[X] + width / HALF
|
|
@@ -72,7 +69,6 @@ export function jumpable(params) {
|
|
|
72
69
|
if (collisionX) {
|
|
73
70
|
const vx = entity.velocity[X]
|
|
74
71
|
|
|
75
|
-
// Check if moving right and crossing the platform's left edge
|
|
76
72
|
const prevRight = prevX + width / HALF
|
|
77
73
|
const platformLeft = collisionX.position[X] - collisionX.size[X] / HALF
|
|
78
74
|
if (vx > NO_VELOCITY && prevRight <= platformLeft) {
|
|
@@ -81,7 +77,6 @@ export function jumpable(params) {
|
|
|
81
77
|
entity.velocity[X] = 0
|
|
82
78
|
}
|
|
83
79
|
|
|
84
|
-
// Check if moving left and crossing the platform's right edge
|
|
85
80
|
const prevLeft = prevX - width / HALF
|
|
86
81
|
const platformRight = collisionX.position[X] + collisionX.size[X] / HALF
|
|
87
82
|
if (vx < NO_VELOCITY && prevLeft >= platformRight) {
|
|
@@ -91,7 +86,6 @@ export function jumpable(params) {
|
|
|
91
86
|
}
|
|
92
87
|
}
|
|
93
88
|
|
|
94
|
-
// 2. VERTICAL MOVEMENT & RESOLUTION
|
|
95
89
|
const wasOnGround = entity.groundObject
|
|
96
90
|
entity.groundObject = undefined
|
|
97
91
|
|
|
@@ -104,7 +98,6 @@ export function jumpable(params) {
|
|
|
104
98
|
const prevBottom = prevY - height / HALF
|
|
105
99
|
const platformTop = collisionY.position[Y] + collisionY.size[Y] / HALF
|
|
106
100
|
|
|
107
|
-
// Landing on top of a platform (one-way platform logic)
|
|
108
101
|
if (entity.vy <= NO_VELOCITY && prevBottom >= platformTop) {
|
|
109
102
|
const entityBottom = entity.position[Y] - height / HALF
|
|
110
103
|
const penetration = platformTop - entityBottom
|
|
@@ -113,7 +106,6 @@ export function jumpable(params) {
|
|
|
113
106
|
entity.groundObject = collisionY
|
|
114
107
|
entity.jumpsLeft = entity.maxJumps
|
|
115
108
|
|
|
116
|
-
// Only notify on the frame we actually land, not every frame we're on the ground.
|
|
117
109
|
if (!wasOnGround) {
|
|
118
110
|
api.notify("land", {
|
|
119
111
|
entityId: entity.id,
|
|
@@ -121,19 +113,6 @@ export function jumpable(params) {
|
|
|
121
113
|
})
|
|
122
114
|
}
|
|
123
115
|
}
|
|
124
|
-
|
|
125
|
-
// Hitting head on bottom of a platform
|
|
126
|
-
// else if (entity.vy > 0) {
|
|
127
|
-
// const prevTop = prevY + height / HALF
|
|
128
|
-
// const platformBottom =
|
|
129
|
-
// collisionY.position[Y] - collisionY.size[Y] / HALF
|
|
130
|
-
// if (prevTop <= platformBottom) {
|
|
131
|
-
// const entityTop = entity.position[Y] + height / HALF
|
|
132
|
-
// const penetration = entityTop - platformBottom
|
|
133
|
-
// if (penetration > 0) entity.position[Y] -= penetration
|
|
134
|
-
// entity.vy = 0
|
|
135
|
-
// }
|
|
136
|
-
// }
|
|
137
116
|
}
|
|
138
117
|
|
|
139
118
|
entity.orientation = magnitude(entity.velocity)
|
|
@@ -3,13 +3,10 @@ import { clamp, snap } from "@inglorious/utils/math/number.js"
|
|
|
3
3
|
const X = 0
|
|
4
4
|
const NO_EXTENT = 0
|
|
5
5
|
const HALF = 2
|
|
6
|
-
// How far along the track a point is, from nothing of the way to all of it.
|
|
7
6
|
const START_OF_TRACK = 0
|
|
8
7
|
const END_OF_TRACK = 1
|
|
9
8
|
const DEFAULT_MIN = 0
|
|
10
9
|
const DEFAULT_MAX = 1
|
|
11
|
-
// A step of nothing means every pixel the pointer moves is a step of its own, which is
|
|
12
|
-
// what a slider wants when nothing has asked for round numbers.
|
|
13
10
|
const WHOLE = 0
|
|
14
11
|
|
|
15
12
|
/**
|
|
@@ -40,9 +37,6 @@ export function slider({
|
|
|
40
37
|
|
|
41
38
|
entity.collisions ??= {}
|
|
42
39
|
|
|
43
|
-
// A slider with a size is a slider there is room to grab. Saying so here means a
|
|
44
|
-
// game does not have to describe the same rectangle twice: once to draw it and
|
|
45
|
-
// once to be hit by it.
|
|
46
40
|
entity.collisions.hitbox ??= { shape: "rectangle" }
|
|
47
41
|
|
|
48
42
|
entity.min ??= min
|
|
@@ -58,8 +52,6 @@ export function slider({
|
|
|
58
52
|
entity.isDragging = true
|
|
59
53
|
},
|
|
60
54
|
|
|
61
|
-
// The pointer is told to every entity, so a slider that is not being dragged has to
|
|
62
|
-
// ignore what it hears. Only a dragged one takes its value from where it stands.
|
|
63
55
|
mouseMove(entity, position, api) {
|
|
64
56
|
if (!entity.isDragging) return
|
|
65
57
|
|
package/src/core/core-events.js
CHANGED
|
@@ -12,8 +12,6 @@ export const coreEvents = [
|
|
|
12
12
|
"inputRelease",
|
|
13
13
|
"mouseMove",
|
|
14
14
|
"mouseClick",
|
|
15
|
-
// Ending the game is the one player's own business: a `quit` reaching another client
|
|
16
|
-
// would stop a game they are still playing.
|
|
17
15
|
"quit",
|
|
18
16
|
"touchStart",
|
|
19
17
|
"touchMove",
|
package/src/core/engine.js
CHANGED
|
@@ -20,7 +20,6 @@ import { Loop } from "./loops/index.js"
|
|
|
20
20
|
import { entityPoolMiddleware } from "./middlewares/entity-pool/entity-pool-middleware.js"
|
|
21
21
|
import { assertTypesAreDeclared } from "./types.js"
|
|
22
22
|
|
|
23
|
-
// Default game configuration
|
|
24
23
|
/**
|
|
25
24
|
* @type {{loop: {type: LoopType, fps: number}, systems: Array, types: Object, entities: Object}}
|
|
26
25
|
* `loop.type` is a {@link LoopType} and defaults to "animationFrame"; `loop.fps` is
|
|
@@ -46,7 +45,7 @@ const DEFAULT_GAME_CONFIG = {
|
|
|
46
45
|
},
|
|
47
46
|
}
|
|
48
47
|
|
|
49
|
-
const ONE_SECOND = 1000
|
|
48
|
+
const ONE_SECOND = 1000
|
|
50
49
|
|
|
51
50
|
/**
|
|
52
51
|
* Engine class responsible for managing the game loop, state, and rendering.
|
|
@@ -63,16 +62,13 @@ export class Engine {
|
|
|
63
62
|
// the entity quietly goes nowhere.
|
|
64
63
|
assertTypesAreDeclared(this._config)
|
|
65
64
|
|
|
66
|
-
// Determine devMode from the entities config
|
|
67
65
|
const devMode = this._config.entities.game?.devMode
|
|
68
66
|
this._devMode = devMode
|
|
69
67
|
|
|
70
68
|
const middlewares = []
|
|
71
69
|
|
|
72
|
-
// Always add entity pool middleware
|
|
73
70
|
middlewares.push(entityPoolMiddleware())
|
|
74
71
|
|
|
75
|
-
// Add multiplayer middleware if needed
|
|
76
72
|
const multiplayer = this._config.entities.game?.multiplayer
|
|
77
73
|
if (multiplayer) {
|
|
78
74
|
middlewares.push(
|
|
@@ -141,14 +137,6 @@ export class Engine {
|
|
|
141
137
|
* @param {number} dt - Delta time since the last update in milliseconds.
|
|
142
138
|
*/
|
|
143
139
|
update(dt) {
|
|
144
|
-
// A game that has asked to quit is not updated again, and the loop that would have
|
|
145
|
-
// asked is cancelled on the way past. Checking before the world moves is what makes
|
|
146
|
-
// quitting mean the game is finished, rather than leaving the engine running a frame
|
|
147
|
-
// behind a loop that has stopped asking.
|
|
148
|
-
//
|
|
149
|
-
// Before the notify rather than after it, because `stop` drains the queue on its way
|
|
150
|
-
// out: an `update` queued and then abandoned would be picked up by that drain and
|
|
151
|
-
// handed to the world as a last tick from a game that is already over.
|
|
152
140
|
if (this._store.getState().game?.quit) {
|
|
153
141
|
this.stop()
|
|
154
142
|
return
|
|
@@ -158,15 +146,11 @@ export class Engine {
|
|
|
158
146
|
const processedEvents = this._store.update()
|
|
159
147
|
const entities = this._store.getState()
|
|
160
148
|
|
|
161
|
-
// And a quit answered during that update -- which is how a key press reaches one --
|
|
162
|
-
// ends the loop on the spot rather than a frame later. The frame that answered it
|
|
163
|
-
// still ran to its end: a quit is given between frames, not in the middle of one.
|
|
164
149
|
if (entities.game?.quit) {
|
|
165
150
|
this.stop()
|
|
166
151
|
return
|
|
167
152
|
}
|
|
168
153
|
|
|
169
|
-
// Check for devMode changes and connect/disconnect dev tools accordingly.
|
|
170
154
|
const newDevMode = entities.game?.devMode
|
|
171
155
|
if (newDevMode !== this._devMode) {
|
|
172
156
|
if (newDevMode) {
|
|
@@ -192,10 +176,6 @@ export class Engine {
|
|
|
192
176
|
}
|
|
193
177
|
|
|
194
178
|
function merger(targetValue, sourceValue) {
|
|
195
|
-
// The default types and systems are lists of behaviors. A game that declares one
|
|
196
|
-
// of them as a plain object is deep-merged anyway, but a decorator is a function
|
|
197
|
-
// and would replace the list outright, so this is what keeps the built-in
|
|
198
|
-
// behaviors underneath it.
|
|
199
179
|
if (
|
|
200
180
|
isArray(targetValue) &&
|
|
201
181
|
!isVector(targetValue) &&
|
package/src/core/loops/fixed.js
CHANGED
|
@@ -15,9 +15,6 @@ export class FixedLoop {
|
|
|
15
15
|
|
|
16
16
|
previousTime = currentTime
|
|
17
17
|
|
|
18
|
-
// The rest of the interval, not the interval on top of the work: sleeping what
|
|
19
|
-
// was already spent as well halves the rate, and a frame that overran its budget
|
|
20
|
-
// starts the next one straight away rather than falling further behind.
|
|
21
18
|
await sleep(Math.max(NO_WAIT, msPerUpdate - (Date.now() - currentTime)))
|
|
22
19
|
}
|
|
23
20
|
}
|
package/src/core/loops/index.js
CHANGED
|
@@ -4,8 +4,6 @@ import { FixedLoop } from "./fixed.js"
|
|
|
4
4
|
import { FlashLoop } from "./flash.js"
|
|
5
5
|
import { LagLoop } from "./lag.js"
|
|
6
6
|
|
|
7
|
-
// @see https://gameprogrammingpatterns.com/game-loop.html
|
|
8
|
-
|
|
9
7
|
/**
|
|
10
8
|
* The loops a game can ask for, by name.
|
|
11
9
|
*
|
|
@@ -16,16 +14,10 @@ import { LagLoop } from "./lag.js"
|
|
|
16
14
|
* says so per entity with `atFps`, and leaves the loop as fast as the screen takes it.
|
|
17
15
|
*/
|
|
18
16
|
export const Loop = {
|
|
19
|
-
// Every frame, as fast as the browser will take one. The default, and the one to keep
|
|
20
|
-
// when something in the game decides its own rate.
|
|
21
17
|
animationFrame: AnimationFrameLoop,
|
|
22
|
-
// One frame every `fps`, measured from the end of the last.
|
|
23
18
|
fixed: FixedLoop,
|
|
24
|
-
// One frame every `fps`, catching up however far behind it fell.
|
|
25
19
|
lag: LagLoop,
|
|
26
|
-
// As fast as the machine allows, with no cap at all.
|
|
27
20
|
elapsed: ElapsedLoop,
|
|
28
|
-
// As fast as the machine allows, with nothing drawing.
|
|
29
21
|
flash: FlashLoop,
|
|
30
22
|
}
|
|
31
23
|
|
|
@@ -12,8 +12,6 @@ export function entityPoolMiddleware() {
|
|
|
12
12
|
store.extras ??= {}
|
|
13
13
|
store.extras.getAllActivePoolEntities = () => pools.getAllActiveEntities()
|
|
14
14
|
|
|
15
|
-
// Pooled entities live outside of the store, so they are merged into the
|
|
16
|
-
// given entities to take part in collision detection just like the others.
|
|
17
15
|
store.extras.findCollision = (
|
|
18
16
|
entity,
|
|
19
17
|
entities = store.getState(),
|
|
@@ -53,8 +51,6 @@ export function entityPoolMiddleware() {
|
|
|
53
51
|
const entityIds = eventMap.getEntitiesForEvent(event.type)
|
|
54
52
|
for (const id of entityIds) {
|
|
55
53
|
const entity = pools.activeEntitiesById.get(id)
|
|
56
|
-
// An id can outlive its entity, if it was pooled away between the
|
|
57
|
-
// lookup and here. There is nothing to hand the event to.
|
|
58
54
|
if (!entity) continue
|
|
59
55
|
|
|
60
56
|
const type = store.getType(entity.type)
|
package/src/physics/bounds.js
CHANGED
|
@@ -41,8 +41,6 @@ const ClampToBoundsByShape = {
|
|
|
41
41
|
const collision = entity.collisions[collisionGroup]
|
|
42
42
|
const size = collision.size ?? entity.size
|
|
43
43
|
|
|
44
|
-
// An anchored box leaves uneven room on either side of its position, so the
|
|
45
|
-
// anchor itself decides how close to the bounds it may get.
|
|
46
44
|
const { before: low, after: high } = anchorMargins(
|
|
47
45
|
shapeAnchor(entity, collision),
|
|
48
46
|
size,
|
package/src/systems/ticks.js
CHANGED
|
@@ -52,9 +52,6 @@ export function ticks({ fps = Infinity, event = "tick" } = {}) {
|
|
|
52
52
|
|
|
53
53
|
if (elapsed === undefined) return
|
|
54
54
|
|
|
55
|
-
// Notified rather than called, so that the turn reaches entities and systems alike,
|
|
56
|
-
// in the same pass: the store drains the queue before it publishes, and anything
|
|
57
|
-
// the turn notifies in its own right follows it in.
|
|
58
55
|
api.notify(event, elapsed)
|
|
59
56
|
},
|
|
60
57
|
}
|
package/src/test.js
CHANGED
|
@@ -32,23 +32,13 @@ const DEFAULT_HOLD_FRAMES = 30
|
|
|
32
32
|
* })
|
|
33
33
|
*/
|
|
34
34
|
export function createGame(gameConfig, extendWith = {}) {
|
|
35
|
-
// Dev mode makes the store freeze what it publishes, which is right for a game being
|
|
36
|
-
// looked at in a browser and wrong for a test that has to put a ball where a brick is.
|
|
37
|
-
// Set here rather than in each test file, where it depends on being done before the
|
|
38
|
-
// first game is made.
|
|
39
35
|
gameConfig.entities.game.devMode = false
|
|
40
36
|
|
|
41
|
-
// What the browser would have given the game, so that a test file has nothing to set up
|
|
42
|
-
// before it makes one. Doing it here rather than leaving it to the caller means a test
|
|
43
|
-
// cannot forget and be met with "AudioContext is not a constructor".
|
|
44
37
|
setupBrowser()
|
|
45
38
|
|
|
46
39
|
const engine = new Engine(gameConfig)
|
|
47
40
|
const playedSounds = []
|
|
48
41
|
|
|
49
|
-
// Sounds are watched where they are handled rather than where they are asked for, so
|
|
50
|
-
// this records what the game actually reached for. Recording the request instead would
|
|
51
|
-
// not catch a sound wired to the wrong state.
|
|
52
42
|
vi.spyOn(engine._store.getType("Audio"), "soundPlay").mockImplementation(
|
|
53
43
|
function soundPlay(_, name) {
|
|
54
44
|
playedSounds.push(name)
|
|
@@ -150,10 +140,6 @@ export function createGame(gameConfig, extendWith = {}) {
|
|
|
150
140
|
* @returns {void}
|
|
151
141
|
*/
|
|
152
142
|
function setupBrowser() {
|
|
153
|
-
// A store grows an audio entity of its own accord, and audio waits for the player to
|
|
154
|
-
// touch the page before it will make a sound -- so stepping any game at all reaches
|
|
155
|
-
// for `window`. Under node there is none, so a stand-in is put up; under jsdom there
|
|
156
|
-
// already is one, and replacing it would take away everything the real one is for.
|
|
157
143
|
if (typeof window === "undefined") {
|
|
158
144
|
vi.stubGlobal("window", {
|
|
159
145
|
addEventListener: vi.fn(),
|
package/src/storage.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
const DEFAULT_STORAGE = () => globalThis.localStorage
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Reads a value back as JSON.
|
|
5
|
-
*
|
|
6
|
-
* Storage that is absent -- a server render, a private window, a test -- reads as the
|
|
7
|
-
* fallback rather than throwing, so a caller does not have to know whether it is running
|
|
8
|
-
* somewhere that has any. A value that cannot be parsed is treated the same way: what is
|
|
9
|
-
* stored is not this function's to defend, and refusing to start over it would be worse
|
|
10
|
-
* than starting again.
|
|
11
|
-
*
|
|
12
|
-
* @param {string} key - Where the value is kept.
|
|
13
|
-
* @param {*} fallback - What to answer when there is nothing usable to read.
|
|
14
|
-
* @param {Storage} [storage] - Where to read from. Defaults to the browser's own.
|
|
15
|
-
* @returns {*} The stored value, or the fallback.
|
|
16
|
-
*/
|
|
17
|
-
export function readJSON(key, fallback, storage) {
|
|
18
|
-
const raw = store(storage)?.getItem(key)
|
|
19
|
-
|
|
20
|
-
if (raw === null || raw === undefined) return fallback
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
return JSON.parse(raw)
|
|
24
|
-
} catch {
|
|
25
|
-
return fallback
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Writes a value down as JSON, and answers whether it took.
|
|
31
|
-
*
|
|
32
|
-
* Storage can be full, absent, or refusing writes for its own reasons, and none of those
|
|
33
|
-
* is worth throwing over: a game that cannot remember a score can still be played. The
|
|
34
|
-
* answer is returned so that a caller which does care -- a test, mostly -- can say so.
|
|
35
|
-
*
|
|
36
|
-
* @param {string} key - Where to keep the value.
|
|
37
|
-
* @param {*} value - The value to keep.
|
|
38
|
-
* @param {Storage} [storage] - Where to write to. Defaults to the browser's own.
|
|
39
|
-
* @returns {boolean} `true` if the value was written.
|
|
40
|
-
*/
|
|
41
|
-
export function writeJSON(key, value, storage) {
|
|
42
|
-
try {
|
|
43
|
-
store(storage)?.setItem(key, JSON.stringify(value))
|
|
44
|
-
|
|
45
|
-
return true
|
|
46
|
-
} catch {
|
|
47
|
-
return false
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function store(storage) {
|
|
52
|
-
return storage ?? DEFAULT_STORAGE()
|
|
53
|
-
}
|