@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.
Files changed (86) hide show
  1. package/README.md +43 -10
  2. package/package.json +18 -8
  3. package/src/animation/due.js +6 -20
  4. package/src/behaviors/at-fps.js +6 -17
  5. package/src/behaviors/controls/event-handlers.js +4 -3
  6. package/src/behaviors/physics/bouncy.js +1 -1
  7. package/src/core/engine.js +4 -7
  8. package/src/core/loops/index.js +6 -15
  9. package/src/core/types.js +2 -14
  10. package/src/physics/bounds.js +1 -1
  11. package/src/systems/ticks.js +7 -20
  12. package/types/ai/movement/dynamic/align.d.ts +19 -0
  13. package/types/ai/movement/dynamic/arrive.d.ts +22 -0
  14. package/types/ai/movement/dynamic/evade.d.ts +16 -0
  15. package/types/ai/movement/dynamic/face.d.ts +10 -0
  16. package/types/ai/movement/dynamic/flee.d.ts +13 -0
  17. package/types/ai/movement/dynamic/look-where-youre-going.d.ts +11 -0
  18. package/types/ai/movement/dynamic/match-velocity.d.ts +11 -0
  19. package/types/ai/movement/dynamic/pursue.d.ts +16 -0
  20. package/types/ai/movement/dynamic/seek.d.ts +13 -0
  21. package/types/ai/movement/dynamic/wander.d.ts +11 -0
  22. package/types/ai/movement/kinematic/align.d.ts +12 -0
  23. package/types/ai/movement/kinematic/arrive.d.ts +17 -0
  24. package/types/ai/movement/kinematic/face.d.ts +9 -0
  25. package/types/ai/movement/kinematic/flee.d.ts +13 -0
  26. package/types/ai/movement/kinematic/seek.d.ts +13 -0
  27. package/types/ai/movement/kinematic/wander-as-seek.d.ts +10 -0
  28. package/types/ai/movement/kinematic/wander.d.ts +4 -0
  29. package/types/animation/due.d.ts +8 -0
  30. package/types/animation/sprite.d.ts +31 -0
  31. package/types/animation/ticker.d.ts +25 -0
  32. package/types/behaviors/at-fps.d.ts +15 -0
  33. package/types/behaviors/audio.d.ts +25 -0
  34. package/types/behaviors/camera.d.ts +29 -0
  35. package/types/behaviors/controls/dynamic/modern.d.ts +39 -0
  36. package/types/behaviors/controls/dynamic/shooter.d.ts +34 -0
  37. package/types/behaviors/controls/dynamic/tank.d.ts +40 -0
  38. package/types/behaviors/controls/event-handlers.d.ts +20 -0
  39. package/types/behaviors/controls/kinematic/modern.d.ts +37 -0
  40. package/types/behaviors/controls/kinematic/shooter.d.ts +29 -0
  41. package/types/behaviors/controls/kinematic/tank.d.ts +35 -0
  42. package/types/behaviors/debug/collision.d.ts +7 -0
  43. package/types/behaviors/fps.d.ts +21 -0
  44. package/types/behaviors/game.d.ts +9 -0
  45. package/types/behaviors/images.d.ts +21 -0
  46. package/types/behaviors/infinite-scroll.d.ts +15 -0
  47. package/types/behaviors/input/controls.d.ts +29 -0
  48. package/types/behaviors/input/gamepad.d.ts +29 -0
  49. package/types/behaviors/input/hover.d.ts +15 -0
  50. package/types/behaviors/input/input.d.ts +19 -0
  51. package/types/behaviors/input/keyboard.d.ts +26 -0
  52. package/types/behaviors/input/mappings.d.ts +16 -0
  53. package/types/behaviors/input/mouse.d.ts +44 -0
  54. package/types/behaviors/input/pointer.d.ts +19 -0
  55. package/types/behaviors/input/touch.d.ts +46 -0
  56. package/types/behaviors/particles.d.ts +44 -0
  57. package/types/behaviors/physics/bouncy.d.ts +20 -0
  58. package/types/behaviors/physics/clamped.d.ts +20 -0
  59. package/types/behaviors/physics/jumpable.d.ts +34 -0
  60. package/types/behaviors/ui/button.d.ts +8 -0
  61. package/types/behaviors/ui/slider.d.ts +26 -0
  62. package/types/collision/detection.d.ts +31 -0
  63. package/types/core/core-events.d.ts +2 -0
  64. package/types/core/engine.d.ts +28 -0
  65. package/types/core/loops/animation-frame.d.ts +7 -0
  66. package/types/core/loops/fixed.d.ts +7 -0
  67. package/types/core/loops/index.d.ts +16 -0
  68. package/types/core/middlewares/entity-pool/entity-pool-middleware.d.ts +4 -0
  69. package/types/core/middlewares/entity-pool/entity-pool.d.ts +9 -0
  70. package/types/core/middlewares/entity-pool/entity-pools.d.ts +12 -0
  71. package/types/core/types.d.ts +4 -0
  72. package/types/index.d.ts +194 -0
  73. package/types/main.d.ts +1 -0
  74. package/types/movement/dynamic/modern.d.ts +20 -0
  75. package/types/movement/dynamic/tank.d.ts +15 -0
  76. package/types/movement/kinematic/modern.d.ts +12 -0
  77. package/types/movement/kinematic/tank.d.ts +12 -0
  78. package/types/physics/anchor.d.ts +37 -0
  79. package/types/physics/bounds.d.ts +27 -0
  80. package/types/physics/position.d.ts +8 -0
  81. package/types/systems/sprite-animation.d.ts +4 -0
  82. package/types/systems/ticks.d.ts +12 -0
  83. package/types/test.d.ts +36 -0
  84. package/src/core/loops/elapsed.js +0 -22
  85. package/src/core/loops/flash.js +0 -13
  86. package/src/core/loops/lag.js +0 -26
@@ -0,0 +1,46 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Collision, Entity, GameApi, Handler } from "../../index"
4
+ import type { ToGamePosition } from "./mouse"
5
+
6
+ export interface TouchEntity extends Entity {
7
+ /** Each finger's position, by its index. */
8
+ positions: (Vector3 | undefined)[]
9
+ /** The entity each finger is on, by its index. */
10
+ targetIds: (string | undefined)[]
11
+ isSwiping?: boolean
12
+ collisions: Record<string, Collision>
13
+ }
14
+
15
+ /** What a touch event carries: which finger, and where. */
16
+ export interface TouchPayload {
17
+ index: number
18
+ position: Vector3
19
+ }
20
+
21
+ /**
22
+ * Notifies `entityTouchStart`, `entityTouchMove` and `entityTouchEnd` for the
23
+ * entity under a finger, or `sceneTouchStart`, `sceneTouchEnd` with the position.
24
+ */
25
+ export function touch(): {
26
+ touchStart: Handler<TouchEntity, TouchPayload>
27
+ touchMove: Handler<TouchEntity, TouchPayload>
28
+ touchEnd: Handler<TouchEntity, Pick<TouchPayload, "index">>
29
+ }
30
+
31
+ /** DOM listeners that notify the touch events, in game positions. */
32
+ export function trackTouch(
33
+ parent: HTMLElement | null,
34
+ api: GameApi,
35
+ toGamePosition: ToGamePosition,
36
+ ): {
37
+ onTouchStart: (event: TouchEvent) => void
38
+ onTouchMove: (event: TouchEvent) => void
39
+ onTouchCancel: (event: TouchEvent) => void
40
+ onTouchEnd: (event: TouchEvent) => void
41
+ }
42
+
43
+ export function createTouchEntity(): TouchEntity & {
44
+ type: "Touch"
45
+ layer: number
46
+ }
@@ -0,0 +1,44 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Entity, GameApi, UpdateHandler } from "../index"
4
+
5
+ export interface ParticleEntity extends Entity {
6
+ position: Vector3
7
+ velocity: Vector3
8
+ acceleration: Vector3
9
+ /** Seconds since it was emitted. */
10
+ age: number
11
+ /** Seconds it lasts. */
12
+ life: number
13
+ startOpacity: number
14
+ opacity: number
15
+ }
16
+
17
+ /** Drifts and fades over its life, then despawns. */
18
+ export function particle(): {
19
+ update: UpdateHandler<ParticleEntity>
20
+ }
21
+
22
+ export interface BurstParams {
23
+ /** Defaults to 1. */
24
+ count?: number
25
+ /** Defaults to `"Particle"`. */
26
+ type?: string
27
+ size?: Vector3
28
+ /** The shortest and longest a particle lasts, in seconds. Defaults to 1. */
29
+ lifetime?: readonly [number, number]
30
+ /** Each particle's acceleration is drawn between these, axis by axis. */
31
+ acceleration?: readonly [Vector3, Vector3]
32
+ /** The half-extent of the box particles are emitted in. */
33
+ spread?: Vector3
34
+ velocity?: Vector3
35
+ /** Defaults to 1. */
36
+ opacity?: number
37
+ layer?: number
38
+ image?: unknown
39
+ tint?: string
40
+ position?: Vector3
41
+ }
42
+
43
+ /** Spawns a burst of pooled particles. */
44
+ export function emitBurst(api: GameApi, params: BurstParams): void
@@ -0,0 +1,20 @@
1
+ import type { Decorator, Entity, Handler } from "../../index"
2
+
3
+ export interface BouncyParams {
4
+ /** How much of a full jump it bounces back up. Defaults to 1. */
5
+ bounciness?: number
6
+ }
7
+
8
+ export interface BouncyEntity extends Entity, BouncyParams {
9
+ maxJump?: number
10
+ maxLeap?: number
11
+ maxSpeed?: number
12
+ vy?: number
13
+ groundObject?: Entity
14
+ }
15
+
16
+ /** Bounces back up when `jumpable` says the entity landed. */
17
+ export function bouncy(params?: BouncyParams): Decorator<{
18
+ create: Handler<BouncyEntity, unknown>
19
+ land: Handler<BouncyEntity, { entityId: string; targetId: string }>
20
+ }>
@@ -0,0 +1,20 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Decorator, Entity, Handler, UpdateHandler } from "../../index"
4
+
5
+ export interface ClampedParams {
6
+ /** The collision group whose shape is kept inside. Defaults to `"bounds"`. */
7
+ collisionGroup?: string
8
+ /** The axis a circle's depth is on. Defaults to `"y"`. */
9
+ depthAxis?: "y" | "z"
10
+ }
11
+
12
+ export interface ClampedEntity extends Entity {
13
+ position: Vector3
14
+ }
15
+
16
+ /** Keeps the entity inside the game's size. */
17
+ export function clamped(params?: ClampedParams): Decorator<{
18
+ create: Handler<ClampedEntity, unknown>
19
+ update: UpdateHandler<ClampedEntity>
20
+ }>
@@ -0,0 +1,34 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Decorator, Entity, Handler, UpdateHandler } from "../../index"
4
+
5
+ export interface JumpableParams {
6
+ /** Defaults to 250. */
7
+ maxSpeed?: number
8
+ /** How high a jump goes. Defaults to 100. */
9
+ maxJump?: number
10
+ /** How far a jump goes. Defaults to 100. */
11
+ maxLeap?: number
12
+ /** Jumps before it has to land again. Defaults to 1. */
13
+ maxJumps?: number
14
+ }
15
+
16
+ export interface JumpableEntity extends Entity, JumpableParams {
17
+ position: Vector3
18
+ size: Vector3
19
+ velocity?: Vector3
20
+ vy?: number
21
+ jumpsLeft?: number
22
+ /** What it stands on. */
23
+ groundObject?: Entity
24
+ }
25
+
26
+ /**
27
+ * Jumps on `jump`, falls, and stands on anything in the `platform` collision
28
+ * group, notifying `land` when it does.
29
+ */
30
+ export function jumpable(params?: JumpableParams): Decorator<{
31
+ create: Handler<JumpableEntity, unknown>
32
+ jump: Handler<JumpableEntity, unknown>
33
+ update: UpdateHandler<JumpableEntity>
34
+ }>
@@ -0,0 +1,8 @@
1
+ import type { Decorator, Entity, GameType } from "../../index"
2
+
3
+ export interface ButtonEntity extends Entity {
4
+ state?: "default" | "pressed"
5
+ }
6
+
7
+ /** Looks pressed from `entityPress` to `entityRelease`, and announces it through `stateChange`. */
8
+ export function button(): Decorator<GameType<ButtonEntity>>
@@ -0,0 +1,26 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Decorator, Entity, Handler } from "../../index"
4
+
5
+ export interface SliderBounds {
6
+ /** Defaults to 0. */
7
+ min?: number
8
+ /** Defaults to 1. */
9
+ max?: number
10
+ /** Defaults to 0, any value. */
11
+ step?: number
12
+ }
13
+
14
+ export interface SliderEntity extends Entity, SliderBounds {
15
+ position: Vector3
16
+ value?: number
17
+ isDragging?: boolean
18
+ }
19
+
20
+ /** Follows the pointer while pressed, and notifies `valueChange` with `{ entityId, value }`. */
21
+ export function slider(bounds?: SliderBounds): Decorator<{
22
+ create: Handler<SliderEntity, unknown>
23
+ entityPress: Handler<SliderEntity, string>
24
+ mouseMove: Handler<SliderEntity, Vector3>
25
+ entityRelease: Handler<SliderEntity, string>
26
+ }>
@@ -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