@inglorious/engine 24.1.4 → 24.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -10
- package/package.json +18 -8
- package/src/animation/due.js +6 -20
- package/src/behaviors/at-fps.js +6 -17
- package/src/behaviors/controls/event-handlers.js +4 -3
- package/src/behaviors/physics/bouncy.js +1 -1
- package/src/core/engine.js +4 -7
- package/src/core/loops/index.js +6 -15
- package/src/core/types.js +2 -14
- package/src/physics/bounds.js +1 -1
- package/src/systems/ticks.js +7 -20
- package/types/ai/movement/dynamic/align.d.ts +19 -0
- package/types/ai/movement/dynamic/arrive.d.ts +22 -0
- package/types/ai/movement/dynamic/evade.d.ts +16 -0
- package/types/ai/movement/dynamic/face.d.ts +10 -0
- package/types/ai/movement/dynamic/flee.d.ts +13 -0
- package/types/ai/movement/dynamic/look-where-youre-going.d.ts +11 -0
- package/types/ai/movement/dynamic/match-velocity.d.ts +11 -0
- package/types/ai/movement/dynamic/pursue.d.ts +16 -0
- package/types/ai/movement/dynamic/seek.d.ts +13 -0
- package/types/ai/movement/dynamic/wander.d.ts +11 -0
- package/types/ai/movement/kinematic/align.d.ts +12 -0
- package/types/ai/movement/kinematic/arrive.d.ts +17 -0
- package/types/ai/movement/kinematic/face.d.ts +9 -0
- package/types/ai/movement/kinematic/flee.d.ts +13 -0
- package/types/ai/movement/kinematic/seek.d.ts +13 -0
- package/types/ai/movement/kinematic/wander-as-seek.d.ts +10 -0
- package/types/ai/movement/kinematic/wander.d.ts +4 -0
- package/types/animation/due.d.ts +8 -0
- package/types/animation/sprite.d.ts +31 -0
- package/types/animation/ticker.d.ts +25 -0
- package/types/behaviors/at-fps.d.ts +15 -0
- package/types/behaviors/audio.d.ts +25 -0
- package/types/behaviors/camera.d.ts +29 -0
- package/types/behaviors/controls/dynamic/modern.d.ts +39 -0
- package/types/behaviors/controls/dynamic/shooter.d.ts +34 -0
- package/types/behaviors/controls/dynamic/tank.d.ts +40 -0
- package/types/behaviors/controls/event-handlers.d.ts +20 -0
- package/types/behaviors/controls/kinematic/modern.d.ts +37 -0
- package/types/behaviors/controls/kinematic/shooter.d.ts +29 -0
- package/types/behaviors/controls/kinematic/tank.d.ts +35 -0
- package/types/behaviors/debug/collision.d.ts +7 -0
- package/types/behaviors/fps.d.ts +21 -0
- package/types/behaviors/game.d.ts +9 -0
- package/types/behaviors/images.d.ts +21 -0
- package/types/behaviors/infinite-scroll.d.ts +15 -0
- package/types/behaviors/input/controls.d.ts +29 -0
- package/types/behaviors/input/gamepad.d.ts +29 -0
- package/types/behaviors/input/hover.d.ts +15 -0
- package/types/behaviors/input/input.d.ts +19 -0
- package/types/behaviors/input/keyboard.d.ts +26 -0
- package/types/behaviors/input/mappings.d.ts +16 -0
- package/types/behaviors/input/mouse.d.ts +44 -0
- package/types/behaviors/input/pointer.d.ts +19 -0
- package/types/behaviors/input/touch.d.ts +46 -0
- package/types/behaviors/particles.d.ts +44 -0
- package/types/behaviors/physics/bouncy.d.ts +20 -0
- package/types/behaviors/physics/clamped.d.ts +20 -0
- package/types/behaviors/physics/jumpable.d.ts +34 -0
- package/types/behaviors/ui/button.d.ts +8 -0
- package/types/behaviors/ui/slider.d.ts +26 -0
- package/types/collision/detection.d.ts +31 -0
- package/types/core/core-events.d.ts +2 -0
- package/types/core/engine.d.ts +28 -0
- package/types/core/loops/animation-frame.d.ts +7 -0
- package/types/core/loops/fixed.d.ts +7 -0
- package/types/core/loops/index.d.ts +16 -0
- package/types/core/middlewares/entity-pool/entity-pool-middleware.d.ts +4 -0
- package/types/core/middlewares/entity-pool/entity-pool.d.ts +9 -0
- package/types/core/middlewares/entity-pool/entity-pools.d.ts +12 -0
- package/types/core/types.d.ts +4 -0
- package/types/index.d.ts +194 -0
- package/types/main.d.ts +1 -0
- package/types/movement/dynamic/modern.d.ts +20 -0
- package/types/movement/dynamic/tank.d.ts +15 -0
- package/types/movement/kinematic/modern.d.ts +12 -0
- package/types/movement/kinematic/tank.d.ts +12 -0
- package/types/physics/anchor.d.ts +37 -0
- package/types/physics/bounds.d.ts +27 -0
- package/types/physics/position.d.ts +8 -0
- package/types/systems/sprite-animation.d.ts +4 -0
- package/types/systems/ticks.d.ts +12 -0
- package/types/test.d.ts +36 -0
- package/src/core/loops/elapsed.js +0 -22
- package/src/core/loops/flash.js +0 -13
- package/src/core/loops/lag.js +0 -26
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Run the following command in your terminal and follow the prompts to create a ne
|
|
|
14
14
|
pnpm create @inglorious/game
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
This
|
|
17
|
+
This sets up a small playable game with a development server and tests, and can make it an installable PWA or an Android and iOS app with Capacitor.
|
|
18
18
|
|
|
19
19
|
## Features
|
|
20
20
|
|
|
@@ -247,6 +247,36 @@ which is private, and whose `extras` are the engine's own.
|
|
|
247
247
|
**Dev mode freezes what the store publishes**, which is right for a game in a browser and
|
|
248
248
|
wrong for a test that has to put a ball where a brick is. `createGame` turns it off.
|
|
249
249
|
|
|
250
|
+
## TypeScript
|
|
251
|
+
|
|
252
|
+
The engine and the 2D renderer ship their types. Each module's types sit next to it, as in `@inglorious/engine/behaviors/physics/clamped.js`, and what they share is in `@inglorious/engine`: `GameConfig`, `GameEntity` for the `game` entity, `Entity` for anything in the world, `GameApi`, and `GameType`, `Handler` and `Render` to write types with:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
import type { Entity, GameConfig, GameType } from "@inglorious/engine"
|
|
256
|
+
import { clamped } from "@inglorious/engine/behaviors/physics/clamped.js"
|
|
257
|
+
import { renderCircle } from "@inglorious/renderer-2d/shapes/circle.js"
|
|
258
|
+
import { add, scale, type Vector3 } from "@inglorious/utils/vector.js"
|
|
259
|
+
|
|
260
|
+
interface BallEntity extends Entity {
|
|
261
|
+
position: Vector3
|
|
262
|
+
velocity: Vector3
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const Ball: GameType<BallEntity> = {
|
|
266
|
+
render: renderCircle,
|
|
267
|
+
update(entity, dt: number) {
|
|
268
|
+
entity.position = add(entity.position, scale(entity.velocity, dt))
|
|
269
|
+
},
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export default {
|
|
273
|
+
types: { Ball: [Ball, clamped()] },
|
|
274
|
+
entities: {},
|
|
275
|
+
} satisfies GameConfig
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
A renderer checks the entity it draws: `renderText` wants a font size, so a type whose entity has a `Vector3` size cannot use it. In tests, `game.entity<BallEntity>("ball")` reads an entity with its type.
|
|
279
|
+
|
|
250
280
|
## Documentation
|
|
251
281
|
|
|
252
282
|
The best way to get started is with the official documentation, which includes a **[Quick Start Guide](https://inglorious-engine.vercel.app/?path=/docs/quick-start--docs)**.
|
|
@@ -406,7 +436,7 @@ The core engine relies on a few key, lightweight packages:
|
|
|
406
436
|
|
|
407
437
|
If you prefer to avoid a build step, you can run the engine directly in the browser. The example below demonstrates how to set up a minimal game using ES modules, an `importmap`, and the 2D canvas renderer.
|
|
408
438
|
|
|
409
|
-
An import map knows nothing of a package's `exports`, so
|
|
439
|
+
An import map knows nothing of a package's `exports`, so it lists the shortcuts the engine itself imports; in your own code, import other files by their real path: `@inglorious/store/behaviors/state-machine/fsm.js` rather than `@inglorious/store/fsm`. Serve the folder over HTTP (for example with `npx serve`): browsers don't load modules from files opened directly.
|
|
410
440
|
|
|
411
441
|
```html
|
|
412
442
|
<!DOCTYPE html>
|
|
@@ -414,18 +444,21 @@ An import map knows nothing of a package's `exports`, so without a bundler impor
|
|
|
414
444
|
<body>
|
|
415
445
|
<canvas id="canvas" width="800" height="600"></canvas>
|
|
416
446
|
|
|
417
|
-
<script
|
|
418
|
-
window.process = { env: "development" }
|
|
447
|
+
<script>
|
|
448
|
+
window.process = { env: { NODE_ENV: "development" } }
|
|
419
449
|
</script>
|
|
420
450
|
|
|
421
451
|
<script type="importmap">
|
|
422
452
|
{
|
|
423
453
|
"imports": {
|
|
424
|
-
"mutative": "https://
|
|
425
|
-
"@inglorious/utils/": "https://
|
|
426
|
-
"@inglorious/
|
|
427
|
-
"@inglorious/
|
|
428
|
-
"@inglorious/
|
|
454
|
+
"mutative": "https://cdn.jsdelivr.net/npm/mutative@2/dist/mutative.esm.mjs",
|
|
455
|
+
"@inglorious/utils/": "https://cdn.jsdelivr.net/npm/@inglorious/utils@5/src/",
|
|
456
|
+
"@inglorious/utils/functions": "https://cdn.jsdelivr.net/npm/@inglorious/utils@5/src/functions/index.js",
|
|
457
|
+
"@inglorious/utils/object.js": "https://cdn.jsdelivr.net/npm/@inglorious/utils@5/src/data-structures/object.js",
|
|
458
|
+
"@inglorious/utils/vector.js": "https://cdn.jsdelivr.net/npm/@inglorious/utils@5/src/math/vector.js",
|
|
459
|
+
"@inglorious/store/": "https://cdn.jsdelivr.net/npm/@inglorious/store@12/src/",
|
|
460
|
+
"@inglorious/engine/": "https://cdn.jsdelivr.net/npm/@inglorious/engine@24/src/",
|
|
461
|
+
"@inglorious/renderer-2d/": "https://cdn.jsdelivr.net/npm/@inglorious/renderer-2d@22/src/",
|
|
429
462
|
"game": "/game.js"
|
|
430
463
|
}
|
|
431
464
|
}
|
|
@@ -433,7 +466,7 @@ An import map knows nothing of a package's `exports`, so without a bundler impor
|
|
|
433
466
|
|
|
434
467
|
<script
|
|
435
468
|
type="module"
|
|
436
|
-
src="https://
|
|
469
|
+
src="https://cdn.jsdelivr.net/npm/@inglorious/engine@24/src/main.js"
|
|
437
470
|
></script>
|
|
438
471
|
</body>
|
|
439
472
|
</html>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.2.0",
|
|
4
4
|
"description": "A JavaScript game engine written with global state, immutability, and pure functions in mind. Have fun(ctional programming) with it!",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,10 +22,18 @@
|
|
|
22
22
|
],
|
|
23
23
|
"type": "module",
|
|
24
24
|
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./types/index.d.ts"
|
|
27
|
+
},
|
|
28
|
+
"./*.js": {
|
|
29
|
+
"types": "./types/*.d.ts",
|
|
30
|
+
"import": "./src/*.js"
|
|
31
|
+
},
|
|
25
32
|
"./*": "./src/*"
|
|
26
33
|
},
|
|
27
34
|
"files": [
|
|
28
35
|
"src",
|
|
36
|
+
"types",
|
|
29
37
|
"!src/**/*.test.js"
|
|
30
38
|
],
|
|
31
39
|
"publishConfig": {
|
|
@@ -33,17 +41,18 @@
|
|
|
33
41
|
},
|
|
34
42
|
"sideEffects": false,
|
|
35
43
|
"dependencies": {
|
|
36
|
-
"@inglorious/store": "^12.2
|
|
37
|
-
"@inglorious/utils": "^5.2
|
|
44
|
+
"@inglorious/store": "^12.9.2",
|
|
45
|
+
"@inglorious/utils": "^5.4.2"
|
|
38
46
|
},
|
|
39
47
|
"peerDependencies": {
|
|
40
|
-
"vitest": "^5.
|
|
48
|
+
"vitest": "^5.1.1"
|
|
41
49
|
},
|
|
42
50
|
"devDependencies": {
|
|
43
51
|
"@inglorious/eslint-config": "1.2.0",
|
|
44
|
-
"prettier": "^3.
|
|
45
|
-
"
|
|
46
|
-
"
|
|
52
|
+
"prettier": "^3.10.1",
|
|
53
|
+
"typescript": "~7.0.2",
|
|
54
|
+
"vite": "^8.3.8",
|
|
55
|
+
"vitest": "^5.1.1"
|
|
47
56
|
},
|
|
48
57
|
"engines": {
|
|
49
58
|
"node": ">= 26"
|
|
@@ -57,6 +66,7 @@
|
|
|
57
66
|
"format": "prettier --write .",
|
|
58
67
|
"lint": "eslint .",
|
|
59
68
|
"test:watch": "vitest",
|
|
60
|
-
"test": "vitest run"
|
|
69
|
+
"test": "vitest run",
|
|
70
|
+
"test:types": "tsc -p type-tests"
|
|
61
71
|
}
|
|
62
72
|
}
|
package/src/animation/due.js
CHANGED
|
@@ -2,32 +2,18 @@ const NO_TIME_PASSED = 0
|
|
|
2
2
|
const WITHIN = 1e-9
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Whether an interval has passed, for `atFps` and `ticks`. Returns the time to
|
|
6
|
+
* account for on this turn, so that six frames of 1/60 s add up to the 1/10 s
|
|
7
|
+
* an entity moving ten times a second must move by, and the time still to wait.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* arrived" is written down once rather than twice.
|
|
10
|
-
*
|
|
11
|
-
* It hands back the time to account for on this turn and the time still to come, rather
|
|
12
|
-
* than only saying yes: an entity held to ten frames a second is told how long it has
|
|
13
|
-
* been since its last move, because six frames of a sixtieth of a second have to come
|
|
14
|
-
* to a tenth of a second for a game moving by velocity times dt.
|
|
15
|
-
*
|
|
16
|
-
* This is not the engine's `Ticker`, which answers the same question and then forgets
|
|
17
|
-
* how long it took: it resets its counter to zero as it fires, so by the time the
|
|
18
|
-
* handler ran, the time it had just measured was gone.
|
|
19
|
-
*
|
|
20
|
-
* @param {number} interval - How long between turns, in seconds.
|
|
21
|
-
* @returns {Function} Given the time already waited and the time just passed.
|
|
9
|
+
* @param {number} interval - Seconds between turns.
|
|
10
|
+
* @returns {(clock: number, dt: number) => { since: number, elapsed?: number }}
|
|
22
11
|
*/
|
|
23
12
|
export function dueAt(interval) {
|
|
24
13
|
return function isDue(clock, dt) {
|
|
25
14
|
const since = clock + dt
|
|
26
15
|
|
|
27
|
-
//
|
|
28
|
-
// six of a sixtieth of a second come to a hair under a tenth of one, which would
|
|
29
|
-
// otherwise leave something moving ten times a second moving at nine. An interval of
|
|
30
|
-
// nothing is never short of anything, so that moves every time it is asked.
|
|
16
|
+
// Summed frame times fall a hair short of the interval they should reach.
|
|
31
17
|
if (since + interval * WITHIN < interval) return { since }
|
|
32
18
|
|
|
33
19
|
return { elapsed: since, since: NO_TIME_PASSED }
|
package/src/behaviors/at-fps.js
CHANGED
|
@@ -3,24 +3,13 @@ import { dueAt } from "../animation/due.js"
|
|
|
3
3
|
const ONE_SECOND = 1
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Updates an entity at its own rate rather than the loop's: the loop stays as
|
|
7
|
+
* fast as the screen, and the entity's `update` runs only when its interval
|
|
8
|
+
* has passed, with all the time since its last one. Put it last in the type's
|
|
9
|
+
* behaviors, so it wraps the `update` it holds back.
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* input and the screen smooth -- and each entity that wants to be slower keeps its own
|
|
11
|
-
* time and is only updated when that time has run out.
|
|
12
|
-
*
|
|
13
|
-
* The rate is a ceiling and not a floor: a frame that leaves more time than the rate
|
|
14
|
-
* allows carries all of it into the next update, so the entity still accounts for every
|
|
15
|
-
* second that really went by.
|
|
16
|
-
*
|
|
17
|
-
* Put it **last** in a type's behaviors. Each one wraps the ones before it and calls on
|
|
18
|
-
* to them, so the last one is the one the store reaches first; anywhere else it is
|
|
19
|
-
* wrapped in turn and never gets a say.
|
|
20
|
-
*
|
|
21
|
-
* @param {number} fps - How many times a second this entity is updated. `Infinity` for
|
|
22
|
-
* every frame, which is also what the engine's default is.
|
|
23
|
-
* @returns {Function} A behavior wrapping whatever it is put in front of.
|
|
11
|
+
* @param {number} fps - Updates a second. `Infinity` for every frame.
|
|
12
|
+
* @returns {Function} The behavior.
|
|
24
13
|
*/
|
|
25
14
|
export function atFps(fps) {
|
|
26
15
|
const isDue = dueAt(ONE_SECOND / fps)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Turns held input into movement flags on an entity
|
|
2
|
+
* Turns held input into movement flags on an entity: `true` while a key or button
|
|
3
|
+
* is held, and the axis's value for a stick, from -1 to 1.
|
|
3
4
|
*
|
|
4
5
|
* Each handler is named after the action it answers to, so it needs no addressing:
|
|
5
6
|
* the store already broadcasts `moveLeft` to every type that handles it, and the
|
|
@@ -12,9 +13,9 @@
|
|
|
12
13
|
*/
|
|
13
14
|
export function createMovementEventHandlers(events) {
|
|
14
15
|
return events.reduce((acc, eventName) => {
|
|
15
|
-
acc[eventName] = (entity) => {
|
|
16
|
+
acc[eventName] = (entity, payload) => {
|
|
16
17
|
entity.movement ??= {}
|
|
17
|
-
entity.movement[eventName] = true
|
|
18
|
+
entity.movement[eventName] = payload?.value ?? true
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
acc[`${eventName}End`] = (entity) => {
|
package/src/core/engine.js
CHANGED
|
@@ -57,9 +57,6 @@ export class Engine {
|
|
|
57
57
|
constructor(...gameConfigs) {
|
|
58
58
|
this._config = extendWith(merger, DEFAULT_GAME_CONFIG, ...gameConfigs)
|
|
59
59
|
|
|
60
|
-
// Said before anything is made, because a type that was never declared is not an error
|
|
61
|
-
// anywhere after this point -- it augments into an empty type, and everything sent to
|
|
62
|
-
// the entity quietly goes nowhere.
|
|
63
60
|
assertTypesAreDeclared(this._config)
|
|
64
61
|
|
|
65
62
|
const devMode = this._config.entities.game?.devMode
|
|
@@ -80,6 +77,7 @@ export class Engine {
|
|
|
80
77
|
...this._config,
|
|
81
78
|
middlewares,
|
|
82
79
|
updateMode: "manual",
|
|
80
|
+
devMode: Boolean(devMode),
|
|
83
81
|
})
|
|
84
82
|
this._loop = new Loop[this._config.loop.type]()
|
|
85
83
|
|
|
@@ -112,10 +110,8 @@ export class Engine {
|
|
|
112
110
|
}
|
|
113
111
|
|
|
114
112
|
/**
|
|
115
|
-
* Stops the game engine, halting the loop and notifying the store.
|
|
116
|
-
*
|
|
117
|
-
* A game does this by notifying `quit`, which the `game` behaviour turns into a flag
|
|
118
|
-
* the engine reads after the frame.
|
|
113
|
+
* Stops the game engine, halting the loop and notifying the store. A game does
|
|
114
|
+
* this by notifying `quit`, which sets the flag the engine reads after each frame.
|
|
119
115
|
*/
|
|
120
116
|
stop() {
|
|
121
117
|
this._store.notify("stop")
|
|
@@ -137,6 +133,7 @@ export class Engine {
|
|
|
137
133
|
* @param {number} dt - Delta time since the last update in milliseconds.
|
|
138
134
|
*/
|
|
139
135
|
update(dt) {
|
|
136
|
+
// A frame already scheduled when the game quit still fires once.
|
|
140
137
|
if (this._store.getState().game?.quit) {
|
|
141
138
|
this.stop()
|
|
142
139
|
return
|
package/src/core/loops/index.js
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
1
|
import { AnimationFrameLoop } from "./animation-frame.js"
|
|
2
|
-
import { ElapsedLoop } from "./elapsed.js"
|
|
3
2
|
import { FixedLoop } from "./fixed.js"
|
|
4
|
-
import { FlashLoop } from "./flash.js"
|
|
5
|
-
import { LagLoop } from "./lag.js"
|
|
6
3
|
|
|
7
4
|
/**
|
|
8
|
-
* The loops a game can ask for, by name.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* world to turn slowly and the pointer to keep up does not choose a slower loop -- it
|
|
14
|
-
* says so per entity with `atFps`, and leaves the loop as fast as the screen takes it.
|
|
5
|
+
* The loops a game can ask for, by name. The loop decides when `update` fires,
|
|
6
|
+
* and so when the screen is drawn: `animationFrame` runs as fast as the screen
|
|
7
|
+
* takes it, `fixed` waits for its rate and makes everything wait with it. A game
|
|
8
|
+
* that wants the world to turn slowly and the pointer to keep up says so per
|
|
9
|
+
* entity with `atFps`, and leaves the loop fast.
|
|
15
10
|
*/
|
|
16
11
|
export const Loop = {
|
|
17
12
|
animationFrame: AnimationFrameLoop,
|
|
18
13
|
fixed: FixedLoop,
|
|
19
|
-
lag: LagLoop,
|
|
20
|
-
elapsed: ElapsedLoop,
|
|
21
|
-
flash: FlashLoop,
|
|
22
14
|
}
|
|
23
15
|
|
|
24
16
|
/**
|
|
25
|
-
* @typedef {"animationFrame" | "fixed"
|
|
26
|
-
* The name of a loop, as written in a game's `loop.type`.
|
|
17
|
+
* @typedef {"animationFrame" | "fixed"} LoopType - The name of a loop, as written in a game's `loop.type`.
|
|
27
18
|
*/
|
package/src/core/types.js
CHANGED
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
import { namesNear } from "@inglorious/utils/data-structures/string.js"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
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
|
-
* ```
|
|
4
|
+
* Throws when an entity names a type the configuration does not declare, with
|
|
5
|
+
* the nearest declared names: such an entity would silently get no handlers.
|
|
18
6
|
*
|
|
19
7
|
* @param {object} config - A game configuration.
|
|
20
8
|
* @throws {Error} If an entity names a type the configuration does not declare.
|
package/src/physics/bounds.js
CHANGED
package/src/systems/ticks.js
CHANGED
|
@@ -3,27 +3,14 @@ import { dueAt } from "../animation/due.js"
|
|
|
3
3
|
const ONE_SECOND = 1
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* A clock for a whole world that turns at
|
|
6
|
+
* A clock for a whole world that turns at its own rate: `atFps` for a world
|
|
7
|
+
* rather than an entity. It notifies `tick` (or `event`) with the elapsed time
|
|
8
|
+
* whenever a turn is due, so whatever turns with the world handles `tick`
|
|
9
|
+
* instead of `update`, and is left alone between turns while the loop keeps
|
|
10
|
+
* the pointer and controls at screen rate. It hears `pause` and `resume`
|
|
11
|
+
* itself, since the store still asks systems for `update` while halted.
|
|
7
12
|
*
|
|
8
|
-
*
|
|
9
|
-
* together -- a generation of Life, a day for foxes and rabbits -- has one clock, kept
|
|
10
|
-
* here, and notifies `tick` whenever a turn is due. Whatever turns with the world handles
|
|
11
|
-
* `tick` instead of `update`, and the loop stays as fast as the screen takes it, so the
|
|
12
|
-
* pointer and the controls never wait on the world.
|
|
13
|
-
*
|
|
14
|
-
* It is also what lets a large world be cheap between turns. An entity with `atFps` is
|
|
15
|
-
* still handed every `update` to keep its own time, so every one of them is written to on
|
|
16
|
-
* every frame; an entity that only handles `tick` is left alone until the world turns.
|
|
17
|
-
*
|
|
18
|
-
* The event carries the time the turn covers, as `atFps` does: six frames of a sixtieth
|
|
19
|
-
* of a second are a tenth of a second, and a world moving by velocity times dt has to
|
|
20
|
-
* move by all of it.
|
|
21
|
-
*
|
|
22
|
-
* Halting the store stops the entities updating, but systems are still asked, so the
|
|
23
|
-
* clock hears the pause for itself. Without that the world would carry on turning
|
|
24
|
-
* underneath a pause that everything else is honouring.
|
|
25
|
-
*
|
|
26
|
-
* @param {Object} [options] - How the world turns.
|
|
13
|
+
* @param {Object} [options]
|
|
27
14
|
* @param {number} [options.fps=Infinity] - Turns a second. `Infinity` for one a frame.
|
|
28
15
|
* @param {string} [options.event="tick"] - What a turn is called.
|
|
29
16
|
* @returns {Object} The system.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Kinematic, Steering } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_TARGET_RADIUS: number
|
|
4
|
+
export const DEFAULT_SLOW_RADIUS: number
|
|
5
|
+
export const DEFAULT_TIME_TO_TARGET: number
|
|
6
|
+
|
|
7
|
+
export interface AlignOptions {
|
|
8
|
+
targetRadius?: number
|
|
9
|
+
slowRadius?: number
|
|
10
|
+
timeToTarget?: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Turns towards the target's orientation, at most `maxAngularAcceleration`. */
|
|
14
|
+
export function align<TEntity extends Omit<Kinematic, "position">>(
|
|
15
|
+
entity: TEntity,
|
|
16
|
+
target: { orientation: number },
|
|
17
|
+
dt: number,
|
|
18
|
+
options?: AlignOptions,
|
|
19
|
+
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DynamicMovement,
|
|
3
|
+
Kinematic,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_TARGET_RADIUS: number
|
|
9
|
+
export const DEFAULT_SLOW_RADIUS: number
|
|
10
|
+
export const DEFAULT_TIME_TO_TARGET: number
|
|
11
|
+
|
|
12
|
+
/** Accelerates towards the target, slowing within `slowRadius` to stop within `targetRadius`. */
|
|
13
|
+
export function arrive<TEntity extends Kinematic>(
|
|
14
|
+
entity: TEntity,
|
|
15
|
+
target: Pick<SteeringTarget, "position">,
|
|
16
|
+
dt: number,
|
|
17
|
+
options?: {
|
|
18
|
+
targetRadius?: number
|
|
19
|
+
slowRadius?: number
|
|
20
|
+
timeToTarget?: number
|
|
21
|
+
},
|
|
22
|
+
): Steering<TEntity, DynamicMovement>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DynamicMovement,
|
|
3
|
+
Kinematic,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_MAX_PREDICTION: number
|
|
9
|
+
|
|
10
|
+
/** Flees from where the target will be, up to `maxPrediction` seconds ahead. */
|
|
11
|
+
export function evade<TEntity extends Kinematic>(
|
|
12
|
+
entity: TEntity,
|
|
13
|
+
target: Required<Pick<SteeringTarget, "position" | "velocity">>,
|
|
14
|
+
dt: number,
|
|
15
|
+
options?: { maxPrediction?: number },
|
|
16
|
+
): Steering<TEntity, DynamicMovement>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Kinematic, Steering, SteeringTarget } from "../../../index"
|
|
2
|
+
import type { AlignOptions } from "./align"
|
|
3
|
+
|
|
4
|
+
/** Turns to face the target. */
|
|
5
|
+
export function face<TEntity extends Kinematic>(
|
|
6
|
+
entity: TEntity,
|
|
7
|
+
target: Pick<SteeringTarget, "position">,
|
|
8
|
+
dt: number,
|
|
9
|
+
options?: AlignOptions,
|
|
10
|
+
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DynamicMovement,
|
|
3
|
+
Kinematic,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
/** Accelerates away from the target at `maxAcceleration`. */
|
|
9
|
+
export function flee<TEntity extends Kinematic>(
|
|
10
|
+
entity: TEntity,
|
|
11
|
+
target: Pick<SteeringTarget, "position">,
|
|
12
|
+
dt: number,
|
|
13
|
+
): Steering<TEntity, DynamicMovement>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Kinematic, Steering } from "../../../index"
|
|
2
|
+
import type { AlignOptions } from "./align"
|
|
3
|
+
|
|
4
|
+
/** Turns to face where it is going. */
|
|
5
|
+
export function lookWhereYoureGoing<
|
|
6
|
+
TEntity extends Omit<Kinematic, "position">,
|
|
7
|
+
>(
|
|
8
|
+
entity: TEntity,
|
|
9
|
+
dt: number,
|
|
10
|
+
options?: AlignOptions,
|
|
11
|
+
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DynamicMovement, Kinematic } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_TIME_TO_TARGET: number
|
|
4
|
+
|
|
5
|
+
/** Accelerates to the target's velocity. */
|
|
6
|
+
export function matchVelocity(
|
|
7
|
+
entity: Kinematic,
|
|
8
|
+
target: { velocity: import("@inglorious/utils/vector.js").Vector3 },
|
|
9
|
+
dt: number,
|
|
10
|
+
options?: { timeToTarget?: number },
|
|
11
|
+
): Required<DynamicMovement>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DynamicMovement,
|
|
3
|
+
Kinematic,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_MAX_PREDICTION: number
|
|
9
|
+
|
|
10
|
+
/** Seeks where the target will be, up to `maxPrediction` seconds ahead. */
|
|
11
|
+
export function pursue<TEntity extends Kinematic>(
|
|
12
|
+
entity: TEntity,
|
|
13
|
+
target: Required<Pick<SteeringTarget, "position" | "velocity">>,
|
|
14
|
+
dt: number,
|
|
15
|
+
options?: { maxPrediction?: number },
|
|
16
|
+
): Steering<TEntity, DynamicMovement>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DynamicMovement,
|
|
3
|
+
Kinematic,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
/** Accelerates towards the target at `maxAcceleration`. */
|
|
9
|
+
export function seek<TEntity extends Kinematic>(
|
|
10
|
+
entity: TEntity,
|
|
11
|
+
target: Pick<SteeringTarget, "position">,
|
|
12
|
+
dt: number,
|
|
13
|
+
): Steering<TEntity, DynamicMovement>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DynamicMovement, Kinematic, Steering } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_WANDER_OFFSET: number
|
|
4
|
+
export const DEFAULT_WANDER_RADIUS: number
|
|
5
|
+
|
|
6
|
+
/** Seeks a random point on a circle `wanderOffset` ahead. */
|
|
7
|
+
export function wander<TEntity extends Kinematic>(
|
|
8
|
+
entity: TEntity,
|
|
9
|
+
dt: number,
|
|
10
|
+
options?: { wanderOffset?: number; wanderRadius?: number },
|
|
11
|
+
): Steering<TEntity, DynamicMovement>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Kinematic, Steering } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_TARGET_RADIUS: number
|
|
4
|
+
export const DEFAULT_TIME_TO_TARGET: number
|
|
5
|
+
|
|
6
|
+
/** Turns towards the target's orientation, at most `maxAngularSpeed`. */
|
|
7
|
+
export function align<TEntity extends Omit<Kinematic, "position">>(
|
|
8
|
+
entity: TEntity,
|
|
9
|
+
target: { orientation: number },
|
|
10
|
+
dt: number,
|
|
11
|
+
options?: { targetRadius?: number; timeToTarget?: number },
|
|
12
|
+
): Steering<TEntity, { orientation: number }>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Kinematic,
|
|
3
|
+
KinematicMovement,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_TARGET_RADIUS: number
|
|
9
|
+
export const DEFAULT_TIME_TO_TARGET: number
|
|
10
|
+
|
|
11
|
+
/** Heads for the target, slowing down to stop within `targetRadius`. */
|
|
12
|
+
export function arrive<TEntity extends Kinematic>(
|
|
13
|
+
entity: TEntity,
|
|
14
|
+
target: Pick<SteeringTarget, "position">,
|
|
15
|
+
dt: number,
|
|
16
|
+
options?: { targetRadius?: number; timeToTarget?: number },
|
|
17
|
+
): Steering<TEntity, KinematicMovement>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Kinematic, Steering, SteeringTarget } from "../../../index"
|
|
2
|
+
|
|
3
|
+
/** Turns to face the target. */
|
|
4
|
+
export function face<TEntity extends Kinematic>(
|
|
5
|
+
entity: TEntity,
|
|
6
|
+
target: Pick<SteeringTarget, "position">,
|
|
7
|
+
dt: number,
|
|
8
|
+
options?: { targetRadius?: number; timeToTarget?: number },
|
|
9
|
+
): Steering<TEntity, { orientation: number }>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Kinematic,
|
|
3
|
+
KinematicMovement,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
/** Runs from the target at `maxSpeed`. */
|
|
9
|
+
export function flee<TEntity extends Kinematic>(
|
|
10
|
+
entity: TEntity,
|
|
11
|
+
target: Pick<SteeringTarget, "position">,
|
|
12
|
+
dt: number,
|
|
13
|
+
): Steering<TEntity, KinematicMovement>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Kinematic,
|
|
3
|
+
KinematicMovement,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
7
|
+
|
|
8
|
+
/** Heads for the target at `maxSpeed`. */
|
|
9
|
+
export function seek<TEntity extends Kinematic>(
|
|
10
|
+
entity: TEntity,
|
|
11
|
+
target: Pick<SteeringTarget, "position">,
|
|
12
|
+
dt: number,
|
|
13
|
+
): Steering<TEntity, KinematicMovement>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Kinematic, KinematicMovement, Steering } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_WANDER_RADIUS: number
|
|
4
|
+
|
|
5
|
+
/** Seeks a random point `wanderRadius` ahead. */
|
|
6
|
+
export function wanderAsSeek<TEntity extends Kinematic>(
|
|
7
|
+
entity: TEntity,
|
|
8
|
+
dt: number,
|
|
9
|
+
options?: { wanderRadius?: number },
|
|
10
|
+
): Steering<TEntity, KinematicMovement>
|