@inglorious/engine 24.1.5 → 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.
Files changed (77) hide show
  1. package/README.md +43 -10
  2. package/package.json +17 -7
  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
@@ -0,0 +1,31 @@
1
+ import type { Collision, Entity } from "../index"
2
+
3
+ /**
4
+ * The first of `entities` that collides with `entity` in `collisionGroup`,
5
+ * nearest first. Defaults to the `hitbox` group.
6
+ */
7
+ export function findCollision<TTarget extends Entity>(
8
+ entity: Entity,
9
+ entities: Record<string, TTarget>,
10
+ collisionGroup?: string,
11
+ ): TTarget | undefined
12
+
13
+ /** Whether two entities collide in `collisionGroup`. */
14
+ export function collidesWith(
15
+ entity: Entity,
16
+ target: Entity,
17
+ collisionGroup?: string,
18
+ ): boolean
19
+
20
+ /** Whether a hitmask collides with an entity: `false` for every other shape. */
21
+ export function findCollisions(
22
+ entity: Entity,
23
+ target: Entity,
24
+ collisionGroup?: string,
25
+ ): boolean
26
+
27
+ /** The collision an entity has in a group: its own, or its size if it is `solid`. */
28
+ export function collisionGroupOf(
29
+ entity: Entity,
30
+ collisionGroup: string,
31
+ ): Collision | null
@@ -0,0 +1,2 @@
1
+ /** The events the engine sends on its own every frame or on every input, kept out of the DevTools. */
2
+ export const coreEvents: string[]
@@ -0,0 +1,28 @@
1
+ import type { EntitiesState } from "@inglorious/store"
2
+
3
+ import type { GameConfig } from "../index"
4
+
5
+ /** Runs a game: merges its configurations, drives its loop and updates its store. */
6
+ export class Engine {
7
+ /**
8
+ * Later configurations extend earlier ones, after the engine's own `Game`,
9
+ * `Audio` and `Images` types and entities: a type that is a list of behaviors
10
+ * gets the new ones appended.
11
+ */
12
+ constructor(...gameConfigs: GameConfig[])
13
+
14
+ /** Runs each entity's `init`, such as loading sounds and images. */
15
+ init(): Promise<unknown[]>
16
+
17
+ /** Notifies `start` and starts the loop. */
18
+ start(): void
19
+
20
+ /** Notifies `stop` and stops the loop. A game stops itself by notifying `quit`. */
21
+ stop(): void
22
+
23
+ /** The entities as they are now. */
24
+ getState(): EntitiesState
25
+
26
+ /** Runs a frame, `dt` seconds after the last one. */
27
+ update(dt: number): void
28
+ }
@@ -0,0 +1,7 @@
1
+ import type { Updatable } from "./index"
2
+
3
+ /** Updates once per animation frame, as fast as the screen takes it. */
4
+ export class AnimationFrameLoop {
5
+ start(engine: Updatable): void
6
+ stop(): void
7
+ }
@@ -0,0 +1,7 @@
1
+ import type { Updatable } from "./index"
2
+
3
+ /** Updates every `msPerUpdate` milliseconds. */
4
+ export class FixedLoop {
5
+ start(engine: Updatable, msPerUpdate: number): Promise<void>
6
+ stop(): void
7
+ }
@@ -0,0 +1,16 @@
1
+ import { AnimationFrameLoop } from "./animation-frame"
2
+ import { FixedLoop } from "./fixed"
3
+
4
+ /** The name of a loop, as written in a game's `loop.type`. */
5
+ export type LoopType = "animationFrame" | "fixed"
6
+
7
+ /** What a loop drives: anything with an `update` that takes seconds. */
8
+ export interface Updatable {
9
+ update(dt: number): void
10
+ }
11
+
12
+ /** The loops a game can ask for, by name. */
13
+ export const Loop: {
14
+ animationFrame: typeof AnimationFrameLoop
15
+ fixed: typeof FixedLoop
16
+ }
@@ -0,0 +1,4 @@
1
+ import type { Middleware } from "@inglorious/store"
2
+
3
+ /** Handles `spawn` and `despawn` with entity pools, and adds them to the api. */
4
+ export function entityPoolMiddleware(): Middleware
@@ -0,0 +1,9 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+
3
+ /** Recycles entities of one type rather than making new ones. */
4
+ export class EntityPool<TEntity extends BaseEntity = BaseEntity> {
5
+ populate(factory: () => TEntity, count: number): void
6
+ acquire(props: TEntity): TEntity & { id: string }
7
+ recycle(entity: TEntity): TEntity
8
+ getStats(): { active: number; inactive: number }
9
+ }
@@ -0,0 +1,12 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+
3
+ /** An entity pool per type. */
4
+ export class EntityPools<TEntity extends BaseEntity = BaseEntity> {
5
+ readonly activeEntitiesById: Map<string, TEntity & { id: string }>
6
+ getAllActiveEntitiesById(): Record<string, TEntity & { id: string }>
7
+ acquire(props: TEntity): TEntity & { id: string }
8
+ recycle(props: TEntity): TEntity
9
+ getStats(): Record<string, { active: number; inactive: number }>
10
+ lazyInit(entity: TEntity): void
11
+ getAllActiveEntities(): (TEntity & { id: string })[]
12
+ }
@@ -0,0 +1,4 @@
1
+ import type { GameConfig } from "../index"
2
+
3
+ /** Throws when an entity names a type the configuration does not declare. */
4
+ export function assertTypesAreDeclared(config: GameConfig): void
@@ -0,0 +1,194 @@
1
+ import type {
2
+ Api,
3
+ BaseEntity,
4
+ EntitiesState,
5
+ Stored,
6
+ System,
7
+ } from "@inglorious/store"
8
+ import type { MultiplayerMiddlewareConfig } from "@inglorious/store/client/multiplayer-middleware.js"
9
+ import type { Vector3 } from "@inglorious/utils/vector.js"
10
+
11
+ import type { LoopType } from "./core/loops/index"
12
+
13
+ /** A collision shape's kind. */
14
+ export type Shape =
15
+ "circle" | "line" | "point" | "rectangle" | "segment" | "hitmask"
16
+
17
+ /**
18
+ * The point a box hangs from, as fractions of its size on each axis: `[0, 0]`
19
+ * is its bottom-left corner, `[0.5, 0.5, 0.5]` its centre. A missing axis is
20
+ * centred.
21
+ */
22
+ export type Anchor = readonly [x: number, y: number, z?: number]
23
+
24
+ /** How an entity collides in one group, such as `hitbox` or `bounds`. */
25
+ export interface Collision {
26
+ shape: Shape
27
+ /** Defaults to the entity's size. */
28
+ size?: Vector3
29
+ /** Defaults to the entity's radius. */
30
+ radius?: number
31
+ /** Added to the entity's position. */
32
+ offset?: Vector3
33
+ /** Defaults to the entity's anchor. */
34
+ anchor?: Anchor
35
+ [key: string]: unknown
36
+ }
37
+
38
+ /** An entity in the world: what the engine's behaviors and renderers read. */
39
+ export interface Entity extends BaseEntity {
40
+ position?: Vector3
41
+ size?: Vector3
42
+ radius?: number
43
+ offset?: Vector3
44
+ anchor?: Anchor
45
+ /** In radians, counterclockwise from the x axis. */
46
+ orientation?: number
47
+ velocity?: Vector3
48
+ acceleration?: Vector3
49
+ /** Drawn over entities on lower layers. Defaults to 0. */
50
+ layer?: number
51
+ /** Collides as a rectangle of its size in every group it names no shape for. */
52
+ solid?: boolean
53
+ /** Its collision shapes, by group. */
54
+ collisions?: Record<string, Collision>
55
+ }
56
+
57
+ /** The `game` entity: the world's size and the switches the engine reads. */
58
+ export interface GameEntity extends BaseEntity {
59
+ /** The world's width and height. */
60
+ size: Vector3 | readonly [width: number, height: number]
61
+ backgroundColor?: string
62
+ /** Connects the Redux DevTools; `D` toggles it. */
63
+ devMode?: boolean
64
+ /** Draws collision gizmos; `C` toggles it. */
65
+ debug?: boolean
66
+ paused?: boolean
67
+ /** Stops the engine after the current frame. */
68
+ quit?: boolean
69
+ /** Draws images without smoothing. */
70
+ pixelated?: boolean
71
+ /** Shares events with a multiplayer server. */
72
+ multiplayer?: MultiplayerMiddlewareConfig
73
+ }
74
+
75
+ /** The api the engine hands every handler: the store's, and the entity pools'. */
76
+ export interface GameApi<
77
+ TEntity extends BaseEntity = BaseEntity,
78
+ TState extends EntitiesState<TEntity> = EntitiesState<TEntity>,
79
+ > extends Api<TEntity, TState> {
80
+ /** The pooled entities in play, from `spawn`. */
81
+ getAllActivePoolEntities: () => Stored<Entity>[]
82
+ /** The first entity that collides with `entity`, pooled ones included. */
83
+ findCollision: (
84
+ entity: Entity,
85
+ entities?: Record<string, Entity>,
86
+ collisionGroup?: string,
87
+ ) => Stored<Entity> | undefined
88
+ /** How many entities each pool holds, in `devMode`. */
89
+ getEntityPoolsStats?: () => Record<
90
+ string,
91
+ { active: number; inactive: number }
92
+ >
93
+ }
94
+
95
+ /** An event handler: the entity, the event's payload and the api. */
96
+ export type EventHandler<
97
+ TEntity extends BaseEntity = Entity,
98
+ TApi extends GameApi = GameApi,
99
+ > = (entity: Stored<TEntity>, payload: any, api: TApi) => unknown
100
+
101
+ /** Draws an entity on a 2D canvas, from its position. */
102
+ export type Render<
103
+ TEntity extends BaseEntity = Entity,
104
+ TApi extends GameApi = GameApi,
105
+ > = (entity: TEntity, ctx: CanvasRenderingContext2D, api: TApi) => void
106
+
107
+ /** A type for one kind of entity: its event handlers, by event name, and its `render`. */
108
+ export type GameType<
109
+ TEntity extends BaseEntity = Entity,
110
+ TApi extends GameApi = GameApi,
111
+ > = {
112
+ render?: Render<TEntity, TApi>
113
+ [handler: string]: EventHandler<TEntity, TApi> | undefined
114
+ }
115
+
116
+ /** A behavior: a type, or a function that extends the type it is given. */
117
+ export type GameBehavior<
118
+ TEntity extends BaseEntity = Entity,
119
+ TApi extends GameApi = GameApi,
120
+ > =
121
+ | GameType<TEntity, TApi>
122
+ | ((type: GameType<TEntity, TApi>) => GameType<TEntity, TApi>)
123
+
124
+ /** A behavior for any entity, whatever entity it is written for. */
125
+ export type AnyGameBehavior =
126
+ GameType<never, never> | ((type: never) => GameType<never, never>)
127
+
128
+ /** What a game's `types` holds for one type name: a behavior, or several. */
129
+ export type GameTypeDefinition = AnyGameBehavior | AnyGameBehavior[]
130
+
131
+ /** An entity as a game's configuration lists it: its type, and anything else. */
132
+ export type EntityConfig = BaseEntity & { [key: string]: unknown }
133
+
134
+ /** A game, or a part of one such as a renderer: the engine merges them in order. */
135
+ export interface GameConfig {
136
+ /** Which loop drives the game, and the rate the fixed one keeps. */
137
+ loop?: { type?: LoopType; fps?: number }
138
+ types?: Record<string, GameTypeDefinition>
139
+ entities?: Record<string, BaseEntity | EntityConfig>
140
+ systems?: (System | GameSystem)[]
141
+ }
142
+
143
+ /** Any type, whatever entity it is for: what a decorator is handed. */
144
+ export type AnyGameType = GameType<never, never>
145
+
146
+ /** A handler for entities like `TEntity`, with payloads like `TPayload`. */
147
+ export type Handler<TEntity extends BaseEntity, TPayload = void> = (
148
+ entity: Stored<TEntity>,
149
+ payload: TPayload,
150
+ api: GameApi,
151
+ ) => void
152
+
153
+ /** An `update` handler: the seconds since the last frame are its payload. */
154
+ export type UpdateHandler<TEntity extends BaseEntity> = Handler<TEntity, number>
155
+
156
+ /** A behavior that extends the type it is given with `THandlers`. */
157
+ export type Decorator<THandlers> = (type: AnyGameType) => THandlers
158
+
159
+ /** A system: its handlers receive every entity rather than one. */
160
+ export type GameSystem = {
161
+ [event: string]: (entities: EntitiesState, payload: any, api: GameApi) => void
162
+ }
163
+
164
+ /** What the steering behaviors read from the entity they move. */
165
+ export interface Kinematic {
166
+ position: Vector3
167
+ velocity?: Vector3
168
+ /** In radians. */
169
+ orientation?: number
170
+ maxSpeed?: number
171
+ maxAcceleration?: number
172
+ /** Radians a second. */
173
+ maxAngularSpeed?: number
174
+ maxAngularAcceleration?: number
175
+ angularSpeed?: number
176
+ }
177
+
178
+ /** What a steering behavior moves towards or away from. */
179
+ export interface SteeringTarget {
180
+ position: Vector3
181
+ velocity?: Vector3
182
+ orientation?: number
183
+ }
184
+
185
+ /** A steering behavior's result: the entity's new movement, or the entity itself when it has arrived. */
186
+ export type Steering<TEntity, TMovement> = TEntity | TMovement
187
+
188
+ export type KinematicMovement = {
189
+ velocity: Vector3
190
+ position: Vector3
191
+ orientation: number
192
+ }
193
+
194
+ export type DynamicMovement = KinematicMovement & { acceleration?: Vector3 }
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,20 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ /** Accelerates, capped at `maxAcceleration` and `maxSpeed`, and faces where it goes. */
4
+ export function modernMove(
5
+ entity: {
6
+ position: Vector3
7
+ velocity?: Vector3
8
+ acceleration?: Vector3
9
+ maxAcceleration: number
10
+ maxSpeed: number
11
+ friction?: number
12
+ orientation?: number
13
+ },
14
+ dt: number,
15
+ ): {
16
+ acceleration: Vector3
17
+ velocity: Vector3
18
+ position: Vector3
19
+ orientation: number
20
+ }
@@ -0,0 +1,15 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ /** Accelerates with the acceleration turned to the orientation, with friction. */
4
+ export function tankMove(
5
+ entity: {
6
+ position: Vector3
7
+ velocity?: Vector3
8
+ acceleration?: Vector3
9
+ maxAcceleration?: number
10
+ maxSpeed?: number
11
+ friction?: number
12
+ orientation?: number
13
+ },
14
+ dt: number,
15
+ ): { velocity: Vector3; position: Vector3; orientation: number }
@@ -0,0 +1,12 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ /** Moves with the velocity, capped at `maxSpeed`, and faces where it goes. */
4
+ export function modernMove(
5
+ entity: {
6
+ position: Vector3
7
+ velocity?: Vector3
8
+ maxSpeed: number
9
+ orientation?: number
10
+ },
11
+ dt: number,
12
+ ): { velocity: Vector3; position: Vector3; orientation: number }
@@ -0,0 +1,12 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ /** Moves with the velocity turned to the orientation, capped at `maxSpeed`. */
4
+ export function tankMove(
5
+ entity: {
6
+ position: Vector3
7
+ velocity?: Vector3
8
+ maxSpeed?: number
9
+ orientation?: number
10
+ },
11
+ dt: number,
12
+ ): { velocity: Vector3; position: Vector3; orientation: number }
@@ -0,0 +1,37 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Anchor, Collision, Entity } from "../index"
4
+
5
+ export const CENTER: Vector3
6
+ export const DEFAULT_ANCHOR: Vector3
7
+ export const TOP_LEFT: Vector3
8
+ export const TOP_CENTER: Vector3
9
+ export const TOP_RIGHT: Vector3
10
+ export const LEFT: Vector3
11
+ export const RIGHT: Vector3
12
+ export const BOTTOM_LEFT: Vector3
13
+ export const BOTTOM_CENTER: Vector3
14
+ export const BOTTOM_RIGHT: Vector3
15
+
16
+ /** The entity's anchor, its image's, or the centre. */
17
+ export function entityAnchor(
18
+ entity: Pick<Entity, "anchor"> & { image?: { anchor?: Anchor } },
19
+ ): Anchor
20
+
21
+ /** A collision shape's anchor, or its entity's. */
22
+ export function shapeAnchor(
23
+ entity: Pick<Entity, "anchor"> & { image?: { anchor?: Anchor } },
24
+ collision: Pick<Collision, "anchor">,
25
+ ): Anchor
26
+
27
+ /** How far a box's centre is from the point it hangs from. */
28
+ export function anchorOffset(
29
+ anchor: Anchor | undefined,
30
+ size: readonly number[] | undefined,
31
+ ): Vector3
32
+
33
+ /** The room a box takes before and after the point it hangs from, on each axis. */
34
+ export function anchorMargins(
35
+ anchor: Anchor | undefined,
36
+ size: readonly number[] | undefined,
37
+ ): { before: Vector3; after: Vector3 }
@@ -0,0 +1,27 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Entity } from "../index"
4
+
5
+ /** The world's width and depth. */
6
+ type Bounds = readonly number[]
7
+
8
+ /** Moves at `maxSpeed` along the orientation, turning back at the bounds. */
9
+ export function bounce(
10
+ entity: Entity & { position: Vector3; maxSpeed: number; orientation: number },
11
+ dt: number,
12
+ bounds: Bounds,
13
+ ): { velocity: Vector3; position: Vector3; orientation: number }
14
+
15
+ /** The position that keeps the entity's shape in `collisionGroup` inside the bounds. */
16
+ export function clampToBounds(
17
+ entity: Entity & { position: Vector3 },
18
+ bounds: Bounds,
19
+ collisionGroup?: string,
20
+ depthAxis?: "y" | "z",
21
+ ): Vector3
22
+
23
+ /** Turns the entity back, and stops it, when its `bounds` shape leaves the bounds. */
24
+ export function flip(
25
+ entity: Entity & { position: Vector3; orientation: number },
26
+ bounds: Bounds,
27
+ ): void
@@ -0,0 +1,8 @@
1
+ import type { Entity } from "../index"
2
+
3
+ /** The height an entity's position must be at to stand on top of `target`. */
4
+ export function calculateLandingPosition(
5
+ entity: Entity,
6
+ target: Entity,
7
+ collisionGroup?: string,
8
+ ): number
@@ -0,0 +1,4 @@
1
+ import type { GameSystem } from "../index"
2
+
3
+ /** Plays the animation of every entity with a `sprite` that has a `state`. */
4
+ export function spriteAnimationSystem(): GameSystem
@@ -0,0 +1,12 @@
1
+ import type { GameSystem } from "../index"
2
+
3
+ /**
4
+ * Notifies `event` with the elapsed seconds `fps` times a second, for a world
5
+ * that turns at its own rate. Stops on `pause`, starts again on `resume`.
6
+ */
7
+ export function ticks(options?: {
8
+ /** Defaults to `Infinity`, every frame. */
9
+ fps?: number
10
+ /** Defaults to `"tick"`. */
11
+ event?: string
12
+ }): GameSystem
@@ -0,0 +1,36 @@
1
+ import type { BaseEntity, EntitiesState, Stored } from "@inglorious/store"
2
+
3
+ import type { Entity, EntityConfig, GameConfig } from "./index"
4
+
5
+ /** A game driven frame by frame, for tests. */
6
+ export interface TestGame {
7
+ /** The names of the sounds played so far. */
8
+ playedSounds: string[]
9
+ /** Asserts `condition`, failing with `description`. */
10
+ check(condition: boolean, description: string): void
11
+ /** One entity, by id, typed as `TEntity`. */
12
+ entity<TEntity extends BaseEntity = Entity>(id: string): Stored<TEntity>
13
+ state(): EntitiesState
14
+ /** The pooled entities in play, of one type or all. */
15
+ pooled<TEntity extends BaseEntity = Entity>(
16
+ typeName?: string,
17
+ ): Stored<TEntity>[]
18
+ notify(event: string, payload?: unknown): this
19
+ /** Runs frames. Defaults to one. */
20
+ step(frames?: number): this
21
+ /** Runs the game for `seconds`. */
22
+ advance(seconds: number): this
23
+ /** Taps a key, by `code`, and lets a couple of frames run. */
24
+ press(code: string): this
25
+ /** Holds a key down for `frames` frames, 30 by default, then lets it up. */
26
+ hold(code: string, frames?: number): this
27
+ }
28
+
29
+ /**
30
+ * Makes a game to drive in a test, with its sounds watched. `extendWith` adds
31
+ * the game's own moves, which can call the rest with `this`.
32
+ */
33
+ export function createGame<TExtra extends Record<string, unknown> = {}>(
34
+ gameConfig: GameConfig & { entities: { game: BaseEntity | EntityConfig } },
35
+ extendWith?: TExtra & ThisType<TestGame & TExtra>,
36
+ ): TestGame & TExtra