@inglorious/engine 24.1.5 → 24.2.1

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.
Files changed (77) hide show
  1. package/README.md +43 -10
  2. package/package.json +18 -8
  3. package/src/behaviors/controls/event-handlers.js +4 -3
  4. package/src/behaviors/physics/bouncy.js +1 -1
  5. package/src/physics/bounds.js +1 -1
  6. package/types/ai/movement/dynamic/align.d.ts +19 -0
  7. package/types/ai/movement/dynamic/arrive.d.ts +22 -0
  8. package/types/ai/movement/dynamic/evade.d.ts +16 -0
  9. package/types/ai/movement/dynamic/face.d.ts +10 -0
  10. package/types/ai/movement/dynamic/flee.d.ts +13 -0
  11. package/types/ai/movement/dynamic/look-where-youre-going.d.ts +11 -0
  12. package/types/ai/movement/dynamic/match-velocity.d.ts +11 -0
  13. package/types/ai/movement/dynamic/pursue.d.ts +16 -0
  14. package/types/ai/movement/dynamic/seek.d.ts +13 -0
  15. package/types/ai/movement/dynamic/wander.d.ts +11 -0
  16. package/types/ai/movement/kinematic/align.d.ts +12 -0
  17. package/types/ai/movement/kinematic/arrive.d.ts +17 -0
  18. package/types/ai/movement/kinematic/face.d.ts +9 -0
  19. package/types/ai/movement/kinematic/flee.d.ts +13 -0
  20. package/types/ai/movement/kinematic/seek.d.ts +13 -0
  21. package/types/ai/movement/kinematic/wander-as-seek.d.ts +10 -0
  22. package/types/ai/movement/kinematic/wander.d.ts +4 -0
  23. package/types/animation/due.d.ts +8 -0
  24. package/types/animation/sprite.d.ts +31 -0
  25. package/types/animation/ticker.d.ts +25 -0
  26. package/types/behaviors/at-fps.d.ts +15 -0
  27. package/types/behaviors/audio.d.ts +25 -0
  28. package/types/behaviors/camera.d.ts +29 -0
  29. package/types/behaviors/controls/dynamic/modern.d.ts +39 -0
  30. package/types/behaviors/controls/dynamic/shooter.d.ts +34 -0
  31. package/types/behaviors/controls/dynamic/tank.d.ts +40 -0
  32. package/types/behaviors/controls/event-handlers.d.ts +20 -0
  33. package/types/behaviors/controls/kinematic/modern.d.ts +37 -0
  34. package/types/behaviors/controls/kinematic/shooter.d.ts +29 -0
  35. package/types/behaviors/controls/kinematic/tank.d.ts +35 -0
  36. package/types/behaviors/debug/collision.d.ts +7 -0
  37. package/types/behaviors/fps.d.ts +21 -0
  38. package/types/behaviors/game.d.ts +9 -0
  39. package/types/behaviors/images.d.ts +21 -0
  40. package/types/behaviors/infinite-scroll.d.ts +15 -0
  41. package/types/behaviors/input/controls.d.ts +29 -0
  42. package/types/behaviors/input/gamepad.d.ts +29 -0
  43. package/types/behaviors/input/hover.d.ts +15 -0
  44. package/types/behaviors/input/input.d.ts +19 -0
  45. package/types/behaviors/input/keyboard.d.ts +26 -0
  46. package/types/behaviors/input/mappings.d.ts +16 -0
  47. package/types/behaviors/input/mouse.d.ts +44 -0
  48. package/types/behaviors/input/pointer.d.ts +19 -0
  49. package/types/behaviors/input/touch.d.ts +46 -0
  50. package/types/behaviors/particles.d.ts +44 -0
  51. package/types/behaviors/physics/bouncy.d.ts +20 -0
  52. package/types/behaviors/physics/clamped.d.ts +20 -0
  53. package/types/behaviors/physics/jumpable.d.ts +34 -0
  54. package/types/behaviors/ui/button.d.ts +8 -0
  55. package/types/behaviors/ui/slider.d.ts +26 -0
  56. package/types/collision/detection.d.ts +31 -0
  57. package/types/core/core-events.d.ts +2 -0
  58. package/types/core/engine.d.ts +28 -0
  59. package/types/core/loops/animation-frame.d.ts +7 -0
  60. package/types/core/loops/fixed.d.ts +7 -0
  61. package/types/core/loops/index.d.ts +16 -0
  62. package/types/core/middlewares/entity-pool/entity-pool-middleware.d.ts +4 -0
  63. package/types/core/middlewares/entity-pool/entity-pool.d.ts +9 -0
  64. package/types/core/middlewares/entity-pool/entity-pools.d.ts +12 -0
  65. package/types/core/types.d.ts +4 -0
  66. package/types/index.d.ts +194 -0
  67. package/types/main.d.ts +1 -0
  68. package/types/movement/dynamic/modern.d.ts +20 -0
  69. package/types/movement/dynamic/tank.d.ts +15 -0
  70. package/types/movement/kinematic/modern.d.ts +12 -0
  71. package/types/movement/kinematic/tank.d.ts +12 -0
  72. package/types/physics/anchor.d.ts +37 -0
  73. package/types/physics/bounds.d.ts +27 -0
  74. package/types/physics/position.d.ts +8 -0
  75. package/types/systems/sprite-animation.d.ts +4 -0
  76. package/types/systems/ticks.d.ts +12 -0
  77. package/types/test.d.ts +36 -0
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 will set up a complete project with a development server ready to go.
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 without a bundler import files by their real path: `@inglorious/store/behaviors/state-machine/fsm.js` rather than `@inglorious/store/fsm`.
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 type="text/javascript">
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://unpkg.com/mutative@latest/dist/mutative.esm.mjs",
425
- "@inglorious/utils/": "https://unpkg.com/@inglorious%2Futils@latest/src/",
426
- "@inglorious/store/": "https://unpkg.com/@inglorious%2Fstore@latest/src/",
427
- "@inglorious/engine/": "https://unpkg.com/@inglorious%2Fengine@latest/src/",
428
- "@inglorious/renderer-2d/": "https://unpkg.com/@inglorious%2Frenderer-2d@latest/src/",
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://unpkg.com/@inglorious%2Fengine@latest/src/main.js"
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.1.5",
3
+ "version": "24.2.1",
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.7.0",
37
- "@inglorious/utils": "^5.4.0"
44
+ "@inglorious/store": "^12.9.3",
45
+ "@inglorious/utils": "^5.4.3"
38
46
  },
39
47
  "peerDependencies": {
40
- "vitest": "^5.1.0"
48
+ "vitest": "^5.1.1"
41
49
  },
42
50
  "devDependencies": {
43
- "@inglorious/eslint-config": "1.2.0",
44
- "prettier": "^3.9.10",
51
+ "@inglorious/eslint-config": "1.2.1",
52
+ "prettier": "^3.10.1",
53
+ "typescript": "~7.0.2",
45
54
  "vite": "^8.3.8",
46
- "vitest": "^5.1.0"
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
  }
@@ -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) => {
@@ -15,7 +15,7 @@ export function bouncy(params) {
15
15
  defaults(entity, params)
16
16
  },
17
17
 
18
- land(entity, entityId) {
18
+ land(entity, { entityId }) {
19
19
  if (entity.id === entityId) {
20
20
  entity.vy = jump(entity) * entity.bounciness
21
21
  entity.groundObject = undefined
@@ -88,7 +88,7 @@ export function clampToBounds(
88
88
  }
89
89
 
90
90
  export function flip(entity, [maxX, maxZ]) {
91
- const { x, z } = entity.position
91
+ const [x, , z] = entity.position
92
92
 
93
93
  entity.collisions ??= {}
94
94
  entity.collisions.bounds ??= {}
@@ -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>
@@ -0,0 +1,4 @@
1
+ import type { Kinematic, KinematicMovement } from "../../../index"
2
+
3
+ /** Moves at `maxSpeed`, turning a random amount up to `maxAngularSpeed` each step. */
4
+ export function wander(entity: Kinematic, dt: number): KinematicMovement
@@ -0,0 +1,8 @@
1
+ /** Whether an interval has passed: `elapsed` is set when it has, `since` is the clock to carry. */
2
+ export interface Due {
3
+ elapsed?: number
4
+ since: number
5
+ }
6
+
7
+ /** Holds something to `interval` seconds: ask with the clock carried so far and the frame's seconds. */
8
+ export function dueAt(interval: number): (clock: number, dt: number) => Due
@@ -0,0 +1,31 @@
1
+ import type { Entity } from "../index"
2
+ import type { TickerTarget } from "./ticker"
3
+
4
+ /** A sprite's frames per animation, and the animation it plays. */
5
+ export interface SpriteState extends TickerTarget {
6
+ /** Each animation's frames, as tile indices; see `flippedHorizontally`. */
7
+ frames: Record<string, number[]>
8
+ state?: string
9
+ }
10
+
11
+ export interface SpriteEntity extends Entity {
12
+ sprite: SpriteState
13
+ }
14
+
15
+ export const Sprite: {
16
+ /** The state among `states` that the entity's orientation points to. */
17
+ fromAngle(entity: SpriteEntity, states: string[]): string
18
+ move2(entity: SpriteEntity): string
19
+ move4(entity: SpriteEntity): string
20
+ move6(entity: SpriteEntity): string
21
+ move8(entity: SpriteEntity): string
22
+ /** Advances the animation, and notifies `spriteAnimationEnd` on its last frame. */
23
+ play(
24
+ animation: string,
25
+ params: {
26
+ entity: SpriteEntity
27
+ dt: number
28
+ notify: (type: string, payload?: unknown) => void
29
+ },
30
+ ): void
31
+ }
@@ -0,0 +1,25 @@
1
+ /** What a ticker counts on: it ticks every `speed` seconds. */
2
+ export interface TickerTarget {
3
+ speed: number
4
+ defaultValue?: number
5
+ value?: number
6
+ counter?: number
7
+ state?: string
8
+ animation?: string
9
+ [key: string]: unknown
10
+ }
11
+
12
+ export const Ticker: {
13
+ /** Adds `dt` to the target's counter, and calls `onTick` every `speed` seconds. */
14
+ tick<TTarget extends TickerTarget>(params: {
15
+ target: TTarget
16
+ state?: string
17
+ dt: number
18
+ onTick?: (
19
+ target: TTarget,
20
+ dt: number,
21
+ options: Record<string, unknown>,
22
+ ) => void
23
+ [option: string]: unknown
24
+ }): void
25
+ }
@@ -0,0 +1,15 @@
1
+ import type { Decorator, Entity, Handler, UpdateHandler } from "../index"
2
+
3
+ export interface AtFpsEntity extends Entity {
4
+ /** The seconds since the entity's last update. */
5
+ fpsClock?: number
6
+ }
7
+
8
+ /**
9
+ * Updates the entity `fps` times a second rather than every frame, with all
10
+ * the time since its last update. Put it last in the type's behaviors.
11
+ */
12
+ export function atFps(fps: number): Decorator<{
13
+ create: Handler<AtFpsEntity, unknown>
14
+ update: UpdateHandler<AtFpsEntity>
15
+ }>
@@ -0,0 +1,25 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+
3
+ import type { Handler } from "../index"
4
+
5
+ /** A sound to load, by name. */
6
+ export interface Sound {
7
+ url: string
8
+ /** From 0 to 1. Defaults to 1. */
9
+ volume?: number
10
+ loop?: boolean
11
+ }
12
+
13
+ export interface AudioEntity extends BaseEntity {
14
+ sounds: Record<string, Sound>
15
+ }
16
+
17
+ /** Loads the entity's sounds and plays them on `soundPlay`, by name. */
18
+ export function audio(): {
19
+ create: Handler<AudioEntity>
20
+ init: (entity: AudioEntity) => Promise<void>
21
+ soundPlay: Handler<AudioEntity, string>
22
+ soundStop: Handler<AudioEntity, string>
23
+ stop: () => void
24
+ destroy: () => void
25
+ }
@@ -0,0 +1,29 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Entity, Handler, UpdateHandler } from "../index"
4
+
5
+ export interface CameraParams {
6
+ zoom?: number
7
+ /** How much of the way to the target zoom it goes each frame. Defaults to 0.1. */
8
+ zoomSpeed?: number
9
+ zoomSensitivity?: number
10
+ minZoom?: number
11
+ maxZoom?: number
12
+ /** The entity it follows. */
13
+ targetId?: string | null
14
+ }
15
+
16
+ export interface CameraEntity extends Entity, CameraParams {
17
+ position: Vector3
18
+ /** The camera the renderer looks through. */
19
+ isActive?: boolean
20
+ targetZoom?: number
21
+ baseSize?: Vector3
22
+ }
23
+
24
+ /** Follows `targetId` and zooms with the mouse wheel. */
25
+ export function camera(params?: CameraParams): {
26
+ create: Handler<CameraEntity>
27
+ update: UpdateHandler<CameraEntity>
28
+ mouseWheel: Handler<CameraEntity, number>
29
+ }
@@ -0,0 +1,39 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Decorator, Handler, UpdateHandler } from "../../../index"
4
+ import type { MovementHandlers, MovingEntity } from "../event-handlers"
5
+
6
+ export interface ModernAccelerationParams {
7
+ /** Defaults to 500. */
8
+ maxAcceleration?: number
9
+ }
10
+
11
+ export interface ModernAccelerationEntity extends MovingEntity {
12
+ position: Vector3
13
+ maxAcceleration?: number
14
+ maxSpeed?: number
15
+ friction?: number
16
+ }
17
+
18
+ type ModernActions =
19
+ | "moveLeft"
20
+ | "moveRight"
21
+ | "moveUp"
22
+ | "moveDown"
23
+ | "moveLeftRight"
24
+ | "moveUpDown"
25
+
26
+ export type ModernAccelerationHandlers = MovementHandlers<ModernActions> & {
27
+ create: Handler<ModernAccelerationEntity, unknown>
28
+ update: UpdateHandler<ModernAccelerationEntity>
29
+ }
30
+
31
+ /** Sets the acceleration from the arrows or a stick, in screen directions. */
32
+ export function modernAcceleration(
33
+ params?: ModernAccelerationParams,
34
+ ): Decorator<ModernAccelerationHandlers>
35
+
36
+ /** Accelerates with what `modernAcceleration` sets. */
37
+ export function modernControls(
38
+ params?: ModernAccelerationParams,
39
+ ): Decorator<ModernAccelerationHandlers>
@@ -0,0 +1,34 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Decorator, Handler, UpdateHandler } from "../../../index"
4
+ import type { MovementHandlers, MovingEntity } from "../event-handlers"
5
+
6
+ export interface ShooterParams {
7
+ /** Defaults to 250. */
8
+ maxSpeed?: number
9
+ /** Radians a second. Defaults to a full turn. */
10
+ maxAngularSpeed?: number
11
+ /** Defaults to 500. */
12
+ maxAcceleration?: number
13
+ }
14
+
15
+ export interface ShooterEntity extends MovingEntity {
16
+ position: Vector3
17
+ orientation?: number
18
+ maxSpeed?: number
19
+ maxAngularSpeed?: number
20
+ /** Defaults to 500. */
21
+ maxAcceleration?: number
22
+ friction?: number
23
+ }
24
+
25
+ type ShooterActions =
26
+ "moveLeft" | "moveRight" | "moveUp" | "moveDown" | "move" | "strafe" | "turn"
27
+
28
+ /** Accelerates relative to where it faces, and faces the `mouse` entity. */
29
+ export function shooterControls(params?: ShooterParams): Decorator<
30
+ MovementHandlers<ShooterActions> & {
31
+ create: Handler<ShooterEntity, unknown>
32
+ update: UpdateHandler<ShooterEntity>
33
+ }
34
+ >