@inglorious/engine 23.0.2 → 23.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { clamp, snap } from "@inglorious/utils/math/number.js"
|
|
2
|
+
|
|
3
|
+
const X = 0
|
|
4
|
+
const NO_EXTENT = 0
|
|
5
|
+
const HALF = 2
|
|
6
|
+
// How far along the track a point is, from nothing of the way to all of it.
|
|
7
|
+
const START_OF_TRACK = 0
|
|
8
|
+
const END_OF_TRACK = 1
|
|
9
|
+
const DEFAULT_MIN = 0
|
|
10
|
+
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
|
+
const WHOLE = 0
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A control that is dragged rather than clicked.
|
|
17
|
+
*
|
|
18
|
+
* It follows the pointer from the moment it is pressed until the moment it is released,
|
|
19
|
+
* which is why it answers `entityPress` and `mouseMove` rather than `entityClick`: a
|
|
20
|
+
* slider is read by where the finger is, not by where it lands.
|
|
21
|
+
*
|
|
22
|
+
* Every change is announced as `valueChange`, carrying the id of the slider that moved
|
|
23
|
+
* and the number it moved to. Whoever wants to do something about a change is told
|
|
24
|
+
* about it rather than left to go looking for it, and each slider reads its own id back
|
|
25
|
+
* out of the report so that thirteen of them can be on the screen at once.
|
|
26
|
+
*
|
|
27
|
+
* @param {Object} [bounds] - The range the value is kept inside.
|
|
28
|
+
* @param {number} [bounds.min] - The lowest value it will take.
|
|
29
|
+
* @param {number} [bounds.max] - The highest.
|
|
30
|
+
* @param {number} [bounds.step] - How much the value moves at a time.
|
|
31
|
+
*/
|
|
32
|
+
export function slider({
|
|
33
|
+
min = DEFAULT_MIN,
|
|
34
|
+
max = DEFAULT_MAX,
|
|
35
|
+
step = WHOLE,
|
|
36
|
+
} = {}) {
|
|
37
|
+
return (type) => ({
|
|
38
|
+
create(entity, payload, api) {
|
|
39
|
+
type.create?.(entity, payload, api)
|
|
40
|
+
|
|
41
|
+
entity.collisions ??= {}
|
|
42
|
+
|
|
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
|
+
entity.collisions.hitbox ??= { shape: "rectangle" }
|
|
47
|
+
|
|
48
|
+
entity.min ??= min
|
|
49
|
+
entity.max ??= max
|
|
50
|
+
entity.step ??= step
|
|
51
|
+
entity.value ??= entity.min
|
|
52
|
+
entity.isDragging ??= false
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
entityPress(entity, id) {
|
|
56
|
+
if (id !== entity.id) return
|
|
57
|
+
|
|
58
|
+
entity.isDragging = true
|
|
59
|
+
},
|
|
60
|
+
|
|
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
|
+
mouseMove(entity, position, api) {
|
|
64
|
+
if (!entity.isDragging) return
|
|
65
|
+
|
|
66
|
+
set(entity, valueUnder(entity, position[X]), api)
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
entityRelease(entity, id) {
|
|
70
|
+
if (id !== entity.id) return
|
|
71
|
+
|
|
72
|
+
entity.isDragging = false
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Sets the value and says so, if it has actually moved.
|
|
79
|
+
*
|
|
80
|
+
* The announcement is only made when the number really changed, because a drag across a
|
|
81
|
+
* slider with a step of a hundredth crosses a hundred announcements of which all but one
|
|
82
|
+
* are the same number said again.
|
|
83
|
+
*/
|
|
84
|
+
function set(entity, value, api) {
|
|
85
|
+
if (value === entity.value) return
|
|
86
|
+
|
|
87
|
+
entity.value = value
|
|
88
|
+
api.notify("valueChange", { entityId: entity.id, value })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The value that falls under a point on the track.
|
|
93
|
+
*
|
|
94
|
+
* Read off the track the same way the renderer draws it -- centred on the position
|
|
95
|
+
* rather than hung off an anchor -- so that where a slider is grabbed is exactly where
|
|
96
|
+
* it is seen, whatever it is anchored to.
|
|
97
|
+
*/
|
|
98
|
+
function valueUnder(entity, x) {
|
|
99
|
+
const [width = NO_EXTENT] = entity.size ?? []
|
|
100
|
+
|
|
101
|
+
const start = entity.position[X] - width / HALF
|
|
102
|
+
const across = width
|
|
103
|
+
? clamp((x - start) / width, START_OF_TRACK, END_OF_TRACK)
|
|
104
|
+
: START_OF_TRACK
|
|
105
|
+
|
|
106
|
+
return atStep(entity, entity.min + across * (entity.max - entity.min))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The nearest value the slider is willing to sit on. */
|
|
110
|
+
function atStep(entity, value) {
|
|
111
|
+
const bounded = clamp(value, entity.min, entity.max)
|
|
112
|
+
|
|
113
|
+
return entity.step ? snap(bounded, entity.step) : bounded
|
|
114
|
+
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
import { anchorOffset, shapeAnchor } from "@inglorious/engine/physics/anchor.js"
|
|
1
2
|
import * as circle from "@inglorious/utils/math/circle.js"
|
|
2
3
|
import * as hitmask from "@inglorious/utils/math/hitmask.js"
|
|
3
4
|
import * as line from "@inglorious/utils/math/line.js"
|
|
4
5
|
import * as point from "@inglorious/utils/math/point.js"
|
|
5
6
|
import * as rectangle from "@inglorious/utils/math/rectangle.js"
|
|
6
7
|
import * as segment from "@inglorious/utils/math/segment.js"
|
|
7
|
-
import { filter } from "@inglorious/utils/
|
|
8
|
-
import { add, zero } from "@inglorious/utils/
|
|
8
|
+
import { filter } from "@inglorious/utils/object.js"
|
|
9
|
+
import { add, zero } from "@inglorious/utils/vector.js"
|
|
9
10
|
|
|
10
11
|
const Z = 2 // Z-axis index.
|
|
12
|
+
const RECTANGLE = "rectangle"
|
|
11
13
|
|
|
12
14
|
const Shape = {
|
|
13
15
|
circle,
|
|
@@ -28,7 +30,7 @@ const Shape = {
|
|
|
28
30
|
export function findCollision(entity, entities, collisionGroup = "hitbox") {
|
|
29
31
|
const otherEntities = filter(
|
|
30
32
|
entities,
|
|
31
|
-
(id,
|
|
33
|
+
(id, other) => id !== entity.id && collisionGroupOf(other, collisionGroup),
|
|
32
34
|
)
|
|
33
35
|
|
|
34
36
|
return Object.values(otherEntities)
|
|
@@ -86,16 +88,45 @@ export function findCollisions(entity, target, collisionGroup = "hitbox") {
|
|
|
86
88
|
|
|
87
89
|
/**
|
|
88
90
|
* Correctly calculates the absolute position and size of an entity's
|
|
89
|
-
* collision shape, including any offsets.
|
|
91
|
+
* collision shape, including any offsets and the anchor the shape hangs from.
|
|
92
|
+
*
|
|
93
|
+
* The shape is reported by its centre, which is where the collision maths wants
|
|
94
|
+
* it, so an anchored shape is shifted off its position by half the room it does
|
|
95
|
+
* not take up on that side.
|
|
96
|
+
*/
|
|
97
|
+
/**
|
|
98
|
+
* The collision an entity has in a group, if it has one.
|
|
99
|
+
*
|
|
100
|
+
* Exported so that anything drawing a collision draws the one that actually happens,
|
|
101
|
+
* rather than a second guess at it.
|
|
102
|
+
*
|
|
103
|
+
* A `solid` entity has one without being told what shape it is: the shape is its size.
|
|
104
|
+
* That is the shape almost everything solid wants, and naming it every time is three
|
|
105
|
+
* lines of configuration saying nothing.
|
|
106
|
+
*
|
|
107
|
+
* It is asked for rather than assumed from the presence of a `size`, because plenty of
|
|
108
|
+
* things have a size and are not in the way: a line of text, a frame counter, anything
|
|
109
|
+
* measured in pixels rather than in space.
|
|
90
110
|
*/
|
|
111
|
+
export function collisionGroupOf(entity, collisionGroup) {
|
|
112
|
+
const collision = entity.collisions?.[collisionGroup]
|
|
113
|
+
|
|
114
|
+
if (collision) return collision
|
|
115
|
+
|
|
116
|
+
return entity.solid ? { shape: RECTANGLE } : null
|
|
117
|
+
}
|
|
118
|
+
|
|
91
119
|
function getCollisionShape(entity, collisionGroup = "hitbox") {
|
|
92
|
-
const collision = entity
|
|
120
|
+
const collision = collisionGroupOf(entity, collisionGroup)
|
|
93
121
|
if (!collision) {
|
|
94
122
|
return null
|
|
95
123
|
}
|
|
96
124
|
|
|
125
|
+
const size = collision.size ?? entity.size
|
|
126
|
+
|
|
97
127
|
const position = add(
|
|
98
128
|
entity.position,
|
|
129
|
+
anchorOffset(shapeAnchor(entity, collision), size),
|
|
99
130
|
collision.offset ?? zero(),
|
|
100
131
|
entity.offset ?? zero(),
|
|
101
132
|
)
|
|
@@ -103,7 +134,7 @@ function getCollisionShape(entity, collisionGroup = "hitbox") {
|
|
|
103
134
|
return {
|
|
104
135
|
...collision,
|
|
105
136
|
position,
|
|
106
|
-
size
|
|
137
|
+
size,
|
|
107
138
|
radius: collision.radius ?? entity.radius,
|
|
108
139
|
}
|
|
109
140
|
}
|
package/src/core/core-events.js
CHANGED
|
@@ -12,6 +12,9 @@ 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
|
+
"quit",
|
|
15
18
|
"touchStart",
|
|
16
19
|
"touchMove",
|
|
17
20
|
"touchEnd",
|
package/src/core/engine.js
CHANGED
|
@@ -6,17 +6,27 @@ import { createDevtools } from "@inglorious/store/client/devtools.js"
|
|
|
6
6
|
import { multiplayerMiddleware } from "@inglorious/store/client/multiplayer-middleware.js"
|
|
7
7
|
import { createStore } from "@inglorious/store/store.js"
|
|
8
8
|
import { augmentType } from "@inglorious/store/types.js"
|
|
9
|
-
import {
|
|
10
|
-
|
|
9
|
+
import {
|
|
10
|
+
ensureArray,
|
|
11
|
+
isArray,
|
|
12
|
+
} from "@inglorious/utils/data-structures/array.js"
|
|
13
|
+
import { isFunction } from "@inglorious/utils/functions"
|
|
14
|
+
import { extendWith, isObject } from "@inglorious/utils/object.js"
|
|
11
15
|
import { v } from "@inglorious/utils/v.js"
|
|
12
|
-
import { isVector } from "@inglorious/utils/
|
|
16
|
+
import { isVector } from "@inglorious/utils/vector.js"
|
|
13
17
|
|
|
14
18
|
import { coreEvents } from "./core-events.js"
|
|
15
19
|
import { Loop } from "./loops/index.js"
|
|
16
20
|
import { entityPoolMiddleware } from "./middlewares/entity-pool/entity-pool-middleware.js"
|
|
21
|
+
import { assertTypesAreDeclared } from "./types.js"
|
|
17
22
|
|
|
18
23
|
// Default game configuration
|
|
19
|
-
|
|
24
|
+
/**
|
|
25
|
+
* @type {{loop: {type: LoopType, fps: number}, systems: Array, types: Object, entities: Object}}
|
|
26
|
+
* `loop.type` is a {@link LoopType} and defaults to "animationFrame"; `loop.fps` is
|
|
27
|
+
* what the loops that have a rate measure themselves against, and is ignored by the
|
|
28
|
+
* ones that do not.
|
|
29
|
+
*/
|
|
20
30
|
const DEFAULT_GAME_CONFIG = {
|
|
21
31
|
loop: { type: "animationFrame", fps: 60 },
|
|
22
32
|
|
|
@@ -48,6 +58,11 @@ export class Engine {
|
|
|
48
58
|
constructor(...gameConfigs) {
|
|
49
59
|
this._config = extendWith(merger, DEFAULT_GAME_CONFIG, ...gameConfigs)
|
|
50
60
|
|
|
61
|
+
// Said before anything is made, because a type that was never declared is not an error
|
|
62
|
+
// anywhere after this point -- it augments into an empty type, and everything sent to
|
|
63
|
+
// the entity quietly goes nowhere.
|
|
64
|
+
assertTypesAreDeclared(this._config)
|
|
65
|
+
|
|
51
66
|
// Determine devMode from the entities config
|
|
52
67
|
const devMode = this._config.entities.game?.devMode
|
|
53
68
|
this._devMode = devMode
|
|
@@ -102,6 +117,9 @@ export class Engine {
|
|
|
102
117
|
|
|
103
118
|
/**
|
|
104
119
|
* Stops the game engine, halting the loop and notifying the store.
|
|
120
|
+
*
|
|
121
|
+
* A game does this by notifying `quit`, which the `game` behaviour turns into a flag
|
|
122
|
+
* the engine reads after the frame.
|
|
105
123
|
*/
|
|
106
124
|
stop() {
|
|
107
125
|
this._store.notify("stop")
|
|
@@ -109,15 +127,45 @@ export class Engine {
|
|
|
109
127
|
this._loop.stop()
|
|
110
128
|
}
|
|
111
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Returns the current game state.
|
|
132
|
+
* @returns {Entities} The entities of the current state.
|
|
133
|
+
*/
|
|
134
|
+
getState() {
|
|
135
|
+
return this._store.getState()
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
/**
|
|
113
139
|
* Updates the game state.
|
|
140
|
+
*
|
|
114
141
|
* @param {number} dt - Delta time since the last update in milliseconds.
|
|
115
142
|
*/
|
|
116
143
|
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
|
+
if (this._store.getState().game?.quit) {
|
|
153
|
+
this.stop()
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
|
|
117
157
|
this._store.notify("update", dt)
|
|
118
158
|
const processedEvents = this._store.update()
|
|
119
159
|
const entities = this._store.getState()
|
|
120
160
|
|
|
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
|
+
if (entities.game?.quit) {
|
|
165
|
+
this.stop()
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
|
|
121
169
|
// Check for devMode changes and connect/disconnect dev tools accordingly.
|
|
122
170
|
const newDevMode = entities.game?.devMode
|
|
123
171
|
if (newDevMode !== this._devMode) {
|
|
@@ -144,12 +192,27 @@ export class Engine {
|
|
|
144
192
|
}
|
|
145
193
|
|
|
146
194
|
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.
|
|
147
199
|
if (
|
|
148
200
|
isArray(targetValue) &&
|
|
149
201
|
!isVector(targetValue) &&
|
|
150
|
-
|
|
151
|
-
!isVector(sourceValue)
|
|
202
|
+
isBehavior(sourceValue)
|
|
152
203
|
) {
|
|
153
|
-
return [...targetValue, ...sourceValue]
|
|
204
|
+
return [...targetValue, ...ensureArray(sourceValue)]
|
|
154
205
|
}
|
|
155
206
|
}
|
|
207
|
+
|
|
208
|
+
function isBehavior(value) {
|
|
209
|
+
if (isArray(value)) return !isVector(value)
|
|
210
|
+
|
|
211
|
+
return isObject(value) || isFunction(value)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @typedef {Object.<string, Object>} Entity - An object representing an entity.
|
|
216
|
+
* @typedef {Object.<string, Entity>} Entities - A collection of entities indexed by id.
|
|
217
|
+
* @typedef {import("./loops/index.js").LoopType} LoopType - Which loop drives the game.
|
|
218
|
+
*/
|
package/src/core/loops/fixed.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const ONE_SECOND = 1000
|
|
2
|
+
const NO_WAIT = 0
|
|
2
3
|
|
|
3
4
|
export class FixedLoop {
|
|
4
5
|
_shouldStop = false
|
|
@@ -13,7 +14,11 @@ export class FixedLoop {
|
|
|
13
14
|
engine.update(dt / ONE_SECOND)
|
|
14
15
|
|
|
15
16
|
previousTime = currentTime
|
|
16
|
-
|
|
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
|
+
await sleep(Math.max(NO_WAIT, msPerUpdate - (Date.now() - currentTime)))
|
|
17
22
|
}
|
|
18
23
|
}
|
|
19
24
|
|
package/src/core/loops/index.js
CHANGED
|
@@ -6,10 +6,30 @@ import { LagLoop } from "./lag.js"
|
|
|
6
6
|
|
|
7
7
|
// @see https://gameprogrammingpatterns.com/game-loop.html
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* The loops a game can ask for, by name.
|
|
11
|
+
*
|
|
12
|
+
* Which loop drives the game is the one thing that decides when `update` is fired, and
|
|
13
|
+
* so when the screen is drawn as well: a loop that waits waits for everything, which is
|
|
14
|
+
* what `fixed` is for and what makes a pointer on it wait too. A game that wants the
|
|
15
|
+
* world to turn slowly and the pointer to keep up does not choose a slower loop -- it
|
|
16
|
+
* says so per entity with `atFps`, and leaves the loop as fast as the screen takes it.
|
|
17
|
+
*/
|
|
9
18
|
export const Loop = {
|
|
10
|
-
|
|
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
|
+
animationFrame: AnimationFrameLoop,
|
|
22
|
+
// One frame every `fps`, measured from the end of the last.
|
|
11
23
|
fixed: FixedLoop,
|
|
12
|
-
|
|
24
|
+
// One frame every `fps`, catching up however far behind it fell.
|
|
13
25
|
lag: LagLoop,
|
|
14
|
-
|
|
26
|
+
// As fast as the machine allows, with no cap at all.
|
|
27
|
+
elapsed: ElapsedLoop,
|
|
28
|
+
// As fast as the machine allows, with nothing drawing.
|
|
29
|
+
flash: FlashLoop,
|
|
15
30
|
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {"animationFrame" | "fixed" | "lag" | "elapsed" | "flash"} LoopType -
|
|
34
|
+
* The name of a loop, as written in a game's `loop.type`.
|
|
35
|
+
*/
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { findCollision } from "@inglorious/engine/collision/detection.js"
|
|
1
2
|
import { createApi } from "@inglorious/store/api.js"
|
|
2
3
|
import { EventMap } from "@inglorious/store/event-map.js"
|
|
3
4
|
|
|
@@ -11,6 +12,19 @@ export function entityPoolMiddleware() {
|
|
|
11
12
|
store.extras ??= {}
|
|
12
13
|
store.extras.getAllActivePoolEntities = () => pools.getAllActiveEntities()
|
|
13
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
|
+
store.extras.findCollision = (
|
|
18
|
+
entity,
|
|
19
|
+
entities = store.getState(),
|
|
20
|
+
collisionGroup,
|
|
21
|
+
) =>
|
|
22
|
+
findCollision(
|
|
23
|
+
entity,
|
|
24
|
+
{ ...entities, ...pools.getAllActiveEntitiesById() },
|
|
25
|
+
collisionGroup,
|
|
26
|
+
)
|
|
27
|
+
|
|
14
28
|
const game = store.getState().game
|
|
15
29
|
if (game.devMode) {
|
|
16
30
|
store.extras.getEntityPoolsStats = () => pools.getStats()
|
|
@@ -23,14 +37,14 @@ export function entityPoolMiddleware() {
|
|
|
23
37
|
case "spawn": {
|
|
24
38
|
const entity = pools.acquire(event.payload)
|
|
25
39
|
const type = store.getType(entity.type)
|
|
26
|
-
eventMap.addEntity(entity.id, type)
|
|
40
|
+
eventMap.addEntity(entity.id, type, entity.type)
|
|
27
41
|
break
|
|
28
42
|
}
|
|
29
43
|
|
|
30
44
|
case "despawn": {
|
|
31
45
|
const entity = pools.recycle(event.payload)
|
|
32
46
|
const type = store.getType(entity.type)
|
|
33
|
-
eventMap.removeEntity(entity.id, type)
|
|
47
|
+
eventMap.removeEntity(entity.id, type, entity.type)
|
|
34
48
|
break
|
|
35
49
|
}
|
|
36
50
|
|
|
@@ -38,6 +52,10 @@ export function entityPoolMiddleware() {
|
|
|
38
52
|
const entityIds = eventMap.getEntitiesForEvent(event.type)
|
|
39
53
|
for (const id of entityIds) {
|
|
40
54
|
const entity = pools.activeEntitiesById.get(id)
|
|
55
|
+
// An id can outlive its entity, if it was pooled away between the
|
|
56
|
+
// lookup and here. There is nothing to hand the event to.
|
|
57
|
+
if (!entity) continue
|
|
58
|
+
|
|
41
59
|
const type = store.getType(entity.type)
|
|
42
60
|
const handle = type[event.type]
|
|
43
61
|
handle?.(entity, event.payload, api)
|
|
@@ -8,6 +8,10 @@ export class EntityPools {
|
|
|
8
8
|
return this._activeEntitiesById
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
getAllActiveEntitiesById() {
|
|
12
|
+
return Object.fromEntries(this._activeEntitiesById)
|
|
13
|
+
}
|
|
14
|
+
|
|
11
15
|
acquire(props) {
|
|
12
16
|
this.lazyInit(props)
|
|
13
17
|
const entity = this._pools.get(props.type).acquire(props)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { namesNear } from "@inglorious/utils/data-structures/string.js"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks that every entity in a configuration has a type that was declared.
|
|
5
|
+
*
|
|
6
|
+
* A type that was never declared is not an error anywhere downstream: it augments into an
|
|
7
|
+
* empty type, so the entity stands there with none of the handlers, rendering or collision
|
|
8
|
+
* its type was meant to give it, and every event sent to it goes nowhere. Nothing about
|
|
9
|
+
* that looks wrong where it happens, which is why it is worth saying so before the game
|
|
10
|
+
* starts rather than after.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```js
|
|
14
|
+
* import { assertTypesAreDeclared } from "@inglorious/engine/core/types.js"
|
|
15
|
+
*
|
|
16
|
+
* assertTypesAreDeclared(config)
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @param {object} config - A game configuration.
|
|
20
|
+
* @throws {Error} If an entity names a type the configuration does not declare.
|
|
21
|
+
*/
|
|
22
|
+
export function assertTypesAreDeclared(config) {
|
|
23
|
+
const declared = Object.keys(config.types ?? {})
|
|
24
|
+
|
|
25
|
+
const undeclared = Object.entries(config.entities ?? {}).filter(
|
|
26
|
+
([, entity]) => entity?.type && !declared.includes(entity.type),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
if (!undeclared.length) return
|
|
30
|
+
|
|
31
|
+
const problems = undeclared.map(([id, entity]) => {
|
|
32
|
+
const near = namesNear(entity.type, declared)
|
|
33
|
+
|
|
34
|
+
return [
|
|
35
|
+
` ${id} has type "${entity.type}", which is not declared`,
|
|
36
|
+
near.length
|
|
37
|
+
? ` did you mean ${near.map((name) => `"${name}"`).join(" or ")}?`
|
|
38
|
+
: ` declared types are: ${declared.join(", ")}`,
|
|
39
|
+
].join("\n")
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
throw new Error(
|
|
43
|
+
[
|
|
44
|
+
`${undeclared.length} of the entities in this game name a type that is not declared.`,
|
|
45
|
+
"An undeclared type gives an entity no handlers at all, so everything sent to it is",
|
|
46
|
+
"silently dropped.",
|
|
47
|
+
...problems,
|
|
48
|
+
].join("\n"),
|
|
49
|
+
)
|
|
50
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { toRange } from "@inglorious/utils/math/trigonometry.js"
|
|
2
2
|
import { applyFriction } from "@inglorious/utils/physics/friction.js"
|
|
3
|
-
import { clamp, multiply, rotate, sum, zero } from "@inglorious/utils/
|
|
3
|
+
import { clamp, multiply, rotate, sum, zero } from "@inglorious/utils/vector.js"
|
|
4
4
|
|
|
5
5
|
const DEFAULT_MAX_ACCELERATION = 0
|
|
6
6
|
const DEFAULT_MAX_SPEED = 0
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { v } from "@inglorious/utils/v.js"
|
|
2
|
+
|
|
3
|
+
const X = 0
|
|
4
|
+
const Y = 1
|
|
5
|
+
const Z = 2
|
|
6
|
+
const HALF = 2
|
|
7
|
+
const HALVED = 0.5
|
|
8
|
+
const NO_EXTENT = 0
|
|
9
|
+
const AT_EDGE = 0
|
|
10
|
+
const AT_FAR_EDGE = 1
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A shape is centred on its position unless it says otherwise.
|
|
14
|
+
*/
|
|
15
|
+
export const CENTER = v(HALVED, HALVED, HALVED)
|
|
16
|
+
export const DEFAULT_ANCHOR = CENTER
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The nine points a shape can be anchored by, named rather than written out.
|
|
20
|
+
*
|
|
21
|
+
* Writing `anchor: [0, 1]` says what it means only to whoever has the anchor module
|
|
22
|
+
* open, and nothing stops `[0, 1]` from being read as bottom-left when it is top-left.
|
|
23
|
+
* Naming them costs an import and removes the reading.
|
|
24
|
+
*/
|
|
25
|
+
export const TOP_LEFT = v(AT_EDGE, AT_EDGE, AT_EDGE)
|
|
26
|
+
export const TOP_CENTER = v(HALVED, AT_EDGE, AT_EDGE)
|
|
27
|
+
export const TOP_RIGHT = v(AT_FAR_EDGE, AT_EDGE, AT_EDGE)
|
|
28
|
+
export const LEFT = v(AT_EDGE, HALVED, AT_EDGE)
|
|
29
|
+
export const RIGHT = v(AT_FAR_EDGE, HALVED, AT_EDGE)
|
|
30
|
+
export const BOTTOM_LEFT = v(AT_EDGE, AT_FAR_EDGE, AT_EDGE)
|
|
31
|
+
export const BOTTOM_CENTER = v(HALVED, AT_FAR_EDGE, AT_EDGE)
|
|
32
|
+
export const BOTTOM_RIGHT = v(AT_FAR_EDGE, AT_FAR_EDGE, AT_EDGE)
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The anchor an entity's shapes are positioned by.
|
|
36
|
+
*
|
|
37
|
+
* An anchor names the point of a shape that sits on the position, counting from
|
|
38
|
+
* its top-left corner: `[0.5, 0.5]` is the middle, `[0, 1]` its bottom-left,
|
|
39
|
+
* `[1, 0]` its top-right. Anchors count from the top on both vertical axes
|
|
40
|
+
* because that is what you see on screen — the renderer flips `y` and `z` on the
|
|
41
|
+
* way to the canvas, so `[0, 1]` puts a shape on the floor rather than in it.
|
|
42
|
+
*
|
|
43
|
+
* A third coordinate may be given for the depth axis; it defaults to the middle,
|
|
44
|
+
* so a two-component anchor works for sprites and for flat 2D bodies alike.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```js
|
|
48
|
+
* import { BOTTOM_LEFT, CENTER } from "@inglorious/engine/physics/anchor.js"
|
|
49
|
+
*
|
|
50
|
+
* // A platform described by its bottom-left corner, which is what you stand on.
|
|
51
|
+
* { type: "Platform", position: v(0, 0, 0), size: v(64, 16, 0), anchor: BOTTOM_LEFT }
|
|
52
|
+
*
|
|
53
|
+
* // A body straddling its position, which is the default.
|
|
54
|
+
* { type: "Sprite", position: v(10, 10, 0), size: v(4, 4, 0), anchor: CENTER }
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export function entityAnchor(entity) {
|
|
58
|
+
return entity.anchor ?? entity.image?.anchor ?? DEFAULT_ANCHOR
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The anchor a collision shape is positioned by. A shape can override the one its
|
|
63
|
+
* entity uses, so that a sprite and its hitbox can be pinned differently.
|
|
64
|
+
*/
|
|
65
|
+
export function shapeAnchor(entity, collision) {
|
|
66
|
+
return collision.anchor ?? entityAnchor(entity)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* How far the centre of a box sits from the point it is anchored to, per axis.
|
|
71
|
+
*
|
|
72
|
+
* Every axis works the same way, because the anchor counts from the same end the
|
|
73
|
+
* coordinate does.
|
|
74
|
+
*/
|
|
75
|
+
export function anchorOffset(anchor, size) {
|
|
76
|
+
const [anchorX = HALVED, anchorY = HALVED, anchorZ = HALVED] = anchor ?? []
|
|
77
|
+
const [width, height, depth] = extentsOf(size)
|
|
78
|
+
|
|
79
|
+
return v(
|
|
80
|
+
(HALVED - anchorX) * width,
|
|
81
|
+
(HALVED - anchorY) * height,
|
|
82
|
+
(HALVED - anchorZ) * depth,
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* How much room a box takes on either side of the point it is anchored to, as
|
|
88
|
+
* `before` and `after` vectors: what lies below and behind the point, and what
|
|
89
|
+
* lies above and in front of it. Together the two are always the full extent,
|
|
90
|
+
* whatever the anchor is.
|
|
91
|
+
*
|
|
92
|
+
* A shape anchored at `[0, 0]` therefore leaves nothing below it and its whole
|
|
93
|
+
* height above it, which is what lets it rest on a floor.
|
|
94
|
+
*/
|
|
95
|
+
export function anchorMargins(anchor, size) {
|
|
96
|
+
const offset = anchorOffset(anchor, size)
|
|
97
|
+
const [width, height, depth] = halfExtentsOf(size)
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
before: v(width - offset[X], height - offset[Y], depth - offset[Z]),
|
|
101
|
+
after: v(width + offset[X], height + offset[Y], depth + offset[Z]),
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function extentsOf(size) {
|
|
106
|
+
const [width = NO_EXTENT, height = NO_EXTENT, depth = NO_EXTENT] = size ?? []
|
|
107
|
+
|
|
108
|
+
return [width, height, depth]
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function halfExtentsOf(size) {
|
|
112
|
+
const [width, height, depth] = extentsOf(size)
|
|
113
|
+
|
|
114
|
+
return [width / HALF, height / HALF, depth / HALF]
|
|
115
|
+
}
|