@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,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
+ >
@@ -0,0 +1,40 @@
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 TankParams {
7
+ /** Defaults to 250. */
8
+ maxSpeed?: number
9
+ /** Radians a second. Defaults to 10. */
10
+ maxAngularSpeed?: number
11
+ /** Defaults to 500. */
12
+ maxAcceleration?: number
13
+ }
14
+
15
+ export interface TankEntity 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 TankActions =
26
+ | "turnLeft"
27
+ | "turnRight"
28
+ | "moveForward"
29
+ | "moveBackward"
30
+ | "strafe"
31
+ | "move"
32
+ | "turn"
33
+
34
+ /** Turns left and right, and accelerates forward and backward from where it faces. */
35
+ export function tankControls(params?: TankParams): Decorator<
36
+ MovementHandlers<TankActions> & {
37
+ create: Handler<TankEntity, unknown>
38
+ update: UpdateHandler<TankEntity>
39
+ }
40
+ >
@@ -0,0 +1,20 @@
1
+ import type { Entity, Handler } from "../../index"
2
+
3
+ /** What each movement action is: `true` while held, or a stick's value from -1 to 1. */
4
+ export type Movement = Partial<Record<string, boolean | number>>
5
+
6
+ export interface MovingEntity extends Entity {
7
+ movement?: Movement
8
+ }
9
+
10
+ /** The handlers `createMovementEventHandlers` makes for `TName`, and their `…End`. */
11
+ export type MovementHandlers<TName extends string> = {
12
+ [K in TName]: Handler<MovingEntity, { value: number } | undefined>
13
+ } & {
14
+ [K in TName as `${K}End`]: Handler<MovingEntity>
15
+ }
16
+
17
+ /** Records each action on the entity's `movement` while it is held. */
18
+ export function createMovementEventHandlers<TName extends string>(
19
+ events: TName[],
20
+ ): MovementHandlers<TName>
@@ -0,0 +1,37 @@
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 ModernVelocityParams {
7
+ /** Defaults to 250. */
8
+ maxSpeed?: number
9
+ }
10
+
11
+ export interface ModernVelocityEntity extends MovingEntity {
12
+ position: Vector3
13
+ maxSpeed?: number
14
+ }
15
+
16
+ type ModernActions =
17
+ | "moveLeft"
18
+ | "moveRight"
19
+ | "moveUp"
20
+ | "moveDown"
21
+ | "moveLeftRight"
22
+ | "moveUpDown"
23
+
24
+ export type ModernVelocityHandlers = MovementHandlers<ModernActions> & {
25
+ create: Handler<ModernVelocityEntity, unknown>
26
+ update: UpdateHandler<ModernVelocityEntity>
27
+ }
28
+
29
+ /** Sets the velocity from the arrows or a stick, in screen directions. */
30
+ export function modernVelocity(
31
+ params?: ModernVelocityParams,
32
+ ): Decorator<ModernVelocityHandlers>
33
+
34
+ /** Moves with the velocity `modernVelocity` sets. */
35
+ export function modernControls(
36
+ params?: ModernVelocityParams,
37
+ ): Decorator<ModernVelocityHandlers>
@@ -0,0 +1,29 @@
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
+ }
12
+
13
+ export interface ShooterEntity extends MovingEntity {
14
+ position: Vector3
15
+ orientation?: number
16
+ maxSpeed?: number
17
+ maxAngularSpeed?: number
18
+ }
19
+
20
+ type ShooterActions =
21
+ "moveLeft" | "moveRight" | "moveUp" | "moveDown" | "move" | "strafe" | "turn"
22
+
23
+ /** Moves relative to where it faces, and faces the `mouse` entity. */
24
+ export function shooterControls(params?: ShooterParams): Decorator<
25
+ MovementHandlers<ShooterActions> & {
26
+ create: Handler<ShooterEntity, unknown>
27
+ update: UpdateHandler<ShooterEntity>
28
+ }
29
+ >
@@ -0,0 +1,35 @@
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 TankParams {
7
+ /** Defaults to 250. */
8
+ maxSpeed?: number
9
+ /** Radians a second. Defaults to 10. */
10
+ maxAngularSpeed?: number
11
+ }
12
+
13
+ export interface TankEntity extends MovingEntity {
14
+ position: Vector3
15
+ orientation?: number
16
+ maxSpeed?: number
17
+ maxAngularSpeed?: number
18
+ }
19
+
20
+ type TankActions =
21
+ | "turnLeft"
22
+ | "turnRight"
23
+ | "moveForward"
24
+ | "moveBackward"
25
+ | "strafe"
26
+ | "move"
27
+ | "turn"
28
+
29
+ /** Turns left and right, and moves forward and backward from where it faces. */
30
+ export function tankControls(params?: TankParams): Decorator<
31
+ MovementHandlers<TankActions> & {
32
+ create: Handler<TankEntity, unknown>
33
+ update: UpdateHandler<TankEntity>
34
+ }
35
+ >
@@ -0,0 +1,7 @@
1
+ import type { Decorator, Entity, Render, Shape } from "../../index"
2
+
3
+ /** Draws the entity's collision shapes over it while the game's `debug` is on. */
4
+ export function collisionGizmos(params?: {
5
+ /** How to draw each shape. */
6
+ shapes?: Partial<Record<Shape, Render>>
7
+ }): Decorator<{ render: Render<Entity> }>
@@ -0,0 +1,21 @@
1
+ import type { Entity, Handler, UpdateHandler } from "../index"
2
+
3
+ export interface FpsParams {
4
+ /** Decimals shown. Defaults to 1. */
5
+ accuracy?: number
6
+ size?: number
7
+ /** Seconds between readings. Defaults to 1. */
8
+ speed?: number
9
+ defaultValue?: number
10
+ }
11
+
12
+ export interface FpsEntity extends Entity {
13
+ /** The last frame's seconds, as `value`, read every `speed` seconds. */
14
+ dt?: FpsParams & { value?: number }
15
+ }
16
+
17
+ /** Reads the frame time, for `renderFps` to show. */
18
+ export function fps(params?: FpsParams): {
19
+ create: Handler<FpsEntity>
20
+ update: UpdateHandler<FpsEntity>
21
+ }
@@ -0,0 +1,9 @@
1
+ import type { GameEntity, Handler } from "../index"
2
+
3
+ /** The `game` entity's own handlers: pause, resume, quit, and `C` and `D` for debug and dev mode. */
4
+ export function game(): {
5
+ pause: Handler<GameEntity>
6
+ resume: Handler<GameEntity>
7
+ quit: Handler<GameEntity>
8
+ keyboardKeyUp: Handler<GameEntity, string>
9
+ }
@@ -0,0 +1,21 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+
3
+ /** An image to load, by id. */
4
+ export interface ImageSource {
5
+ url: string
6
+ }
7
+
8
+ export interface ImagesEntity extends BaseEntity {
9
+ images: Record<string, ImageSource>
10
+ }
11
+
12
+ /** A loaded image, with the id it was loaded under. */
13
+ export type LoadedImage = HTMLImageElement & { id: string }
14
+
15
+ /** Loads images up front with `init`, or on demand with `load`, and hands them out by id. */
16
+ export function images(): {
17
+ init: (entity: ImagesEntity) => Promise<void>
18
+ load: (id: string, src: string) => Promise<LoadedImage>
19
+ get: (id: string) => LoadedImage | undefined
20
+ stop: () => void
21
+ }
@@ -0,0 +1,15 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Entity, UpdateHandler } from "../index"
4
+
5
+ export interface InfiniteScrollEntity extends Entity {
6
+ position: Vector3
7
+ velocity: Vector3
8
+ /** `loop` is where the image repeats. */
9
+ image: { loop: Vector3 }
10
+ }
11
+
12
+ /** Scrolls with its velocity, wrapping around at the image's `loop`, unless the game is paused. */
13
+ export function infiniteScroll(): {
14
+ update: UpdateHandler<InfiniteScrollEntity>
15
+ }
@@ -0,0 +1,29 @@
1
+ import type { GamepadEntity, gamepad, gamepadsPoller } from "./gamepad"
2
+ import type { input, InputEntity } from "./input"
3
+ import type { InputMapping, keyboard, KeyboardEntity } from "./keyboard"
4
+ import type { pointer, PointerEntity } from "./pointer"
5
+
6
+ /** The types every control needs, spread under `types`. */
7
+ export function controlTypes(): {
8
+ Keyboard: ReturnType<typeof keyboard>
9
+ Pointer: ReturnType<typeof pointer>
10
+ GamepadsPoller: ReturnType<typeof gamepadsPoller>
11
+ Gamepad: ReturnType<typeof gamepad>
12
+ Input: ReturnType<typeof input>
13
+ }
14
+
15
+ /**
16
+ * The entities that listen for input, spread under `entities`. `mapping`
17
+ * turns keys, axes and buttons into actions; a click or a touch presses
18
+ * `pointerActions`.
19
+ */
20
+ export function createControlEntities(
21
+ mapping?: InputMapping,
22
+ pointerActions?: string[],
23
+ ): {
24
+ gamepads: { type: "GamepadsPoller" }
25
+ keyboard: KeyboardEntity
26
+ gamepad: GamepadEntity
27
+ input: InputEntity
28
+ pointer: PointerEntity
29
+ }
@@ -0,0 +1,29 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+
3
+ import type { Handler, UpdateHandler } from "../../index"
4
+ import type { InputMapping } from "./keyboard"
5
+
6
+ export interface GamepadsPollerEntity extends BaseEntity {
7
+ gamepadStateCache?: Record<number, { axes: number[]; buttons: boolean[] }>
8
+ }
9
+
10
+ export interface GamepadEntity extends BaseEntity {
11
+ type: "Gamepad"
12
+ mapping: InputMapping
13
+ }
14
+
15
+ /** Polls the gamepads every frame, and notifies `gamepadAxis`, `gamepadPress` and `gamepadRelease`. */
16
+ export function gamepadsPoller(): {
17
+ create: Handler<GamepadsPollerEntity>
18
+ update: UpdateHandler<GamepadsPollerEntity>
19
+ }
20
+
21
+ /** Turns gamepad axes and buttons into `inputAxis`, `inputPress` and `inputRelease`. */
22
+ export function gamepad(): {
23
+ mappingChange: Handler<GamepadEntity, InputMapping>
24
+ gamepadAxis: Handler<GamepadEntity, { axis: string; value: number }>
25
+ gamepadPress: Handler<GamepadEntity, { button: string }>
26
+ gamepadRelease: Handler<GamepadEntity, { button: string }>
27
+ }
28
+
29
+ export function createGamepadEntity(mapping?: InputMapping): GamepadEntity
@@ -0,0 +1,15 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Decorator, Handler } from "../../index"
4
+ import type { MouseEntity } from "./mouse"
5
+
6
+ /** Turns the mouse into a hand over anything with a hitbox. */
7
+ export function hover(cursors?: {
8
+ /** The cursor away from anything pressable. Defaults to `"arrow"`. */
9
+ off?: string
10
+ }): Decorator<{
11
+ create: Handler<MouseEntity, unknown>
12
+ mouseMove: Handler<MouseEntity, Vector3>
13
+ mouseDown: Handler<MouseEntity, Vector3>
14
+ mouseUp: Handler<MouseEntity, Vector3>
15
+ }>
@@ -0,0 +1,19 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+
3
+ import type { Handler } from "../../index"
4
+
5
+ export interface InputEntity extends BaseEntity {
6
+ type: "Input"
7
+ }
8
+
9
+ /**
10
+ * Notifies each action by name: `moveLeft` with `{ value }` for an axis, with
11
+ * nothing for a press, and `moveLeftEnd` on release.
12
+ */
13
+ export function input(): {
14
+ inputAxis: Handler<InputEntity, { action: string; value: number }>
15
+ inputPress: Handler<InputEntity, { action: string }>
16
+ inputRelease: Handler<InputEntity, { action: string }>
17
+ }
18
+
19
+ export function createInputEntity(): InputEntity
@@ -0,0 +1,26 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+
3
+ import type { Handler } from "../../index"
4
+
5
+ /** Maps keys (by `code`), gamepad axes (`Axis0`…) and buttons (`Btn0`…) to actions. */
6
+ export type InputMapping = Record<string, string>
7
+
8
+ export interface KeyboardEntity extends BaseEntity {
9
+ type: "Keyboard"
10
+ mapping: InputMapping
11
+ }
12
+
13
+ /**
14
+ * Turns key presses into `inputPress` and `inputRelease` of their actions,
15
+ * and notifies `keyboardKeyDown`, `keyboardKeyUp` and `keyboardChar`.
16
+ */
17
+ export function keyboard(): {
18
+ create: Handler<KeyboardEntity, unknown>
19
+ mappingChange: Handler<KeyboardEntity, InputMapping>
20
+ stop: () => void
21
+ destroy: () => void
22
+ keyboardKeyDown: Handler<KeyboardEntity, string>
23
+ keyboardKeyUp: Handler<KeyboardEntity, string>
24
+ }
25
+
26
+ export function createKeyboardEntity(mapping?: InputMapping): KeyboardEntity
@@ -0,0 +1,16 @@
1
+ import type { StateChangePayload } from "@inglorious/store/fsm"
2
+
3
+ import type { Decorator, Entity, Handler } from "../../index"
4
+ import type { InputMapping } from "./keyboard"
5
+
6
+ export interface MappedEntity extends Entity {
7
+ state?: string
8
+ }
9
+
10
+ /** Notifies `mappingChange` with what each state of the entity's machine answers to. */
11
+ export function mappings(
12
+ mappingsByState: Record<string, InputMapping>,
13
+ ): Decorator<{
14
+ create: Handler<MappedEntity, unknown>
15
+ stateChange: Handler<MappedEntity, StateChangePayload>
16
+ }>
@@ -0,0 +1,44 @@
1
+ import type { Vector3 } from "@inglorious/utils/vector.js"
2
+
3
+ import type { Entity, GameApi, Handler } from "../../index"
4
+
5
+ export interface MouseEntity extends Entity {
6
+ position: Vector3
7
+ /** The id of the entity pressed, until it is released. */
8
+ pressedId?: string
9
+ /** What `renderMouse` draws: `"crosshair"`, `"arrow"` or `"hand"`. */
10
+ cursor?: string
11
+ }
12
+
13
+ /**
14
+ * Follows the pointer, and notifies `entityPress`, `entityRelease` and
15
+ * `entityClick` with the id of the entity under it, or `scenePress`,
16
+ * `sceneRelease` and `sceneClick` with the position.
17
+ */
18
+ export function mouse(): {
19
+ create: Handler<MouseEntity>
20
+ mouseMove: Handler<MouseEntity, Vector3>
21
+ mouseDown: Handler<MouseEntity, Vector3>
22
+ mouseUp: Handler<MouseEntity, Vector3>
23
+ mouseClick: Handler<MouseEntity, Vector3>
24
+ }
25
+
26
+ /** Turns a point on the page into a position in the game. */
27
+ export type ToGamePosition = (clientX: number, clientY: number) => Vector3
28
+
29
+ /** DOM listeners that notify the mouse's events, in game positions. */
30
+ export function trackMouse(
31
+ parent: HTMLElement | null,
32
+ api: GameApi,
33
+ toGamePosition: ToGamePosition,
34
+ ): {
35
+ onMouseMove: (event: MouseEvent) => void
36
+ onClick: (event: MouseEvent) => void
37
+ onMouseDown: (event: MouseEvent) => void
38
+ onMouseUp: (event: MouseEvent) => void
39
+ onMouseWheel: (event: WheelEvent) => void
40
+ }
41
+
42
+ export function createMouseEntity<TOverrides extends Partial<MouseEntity>>(
43
+ overrides?: TOverrides,
44
+ ): MouseEntity & { type: "Mouse"; layer: number } & TOverrides
@@ -0,0 +1,19 @@
1
+ import type { BaseEntity } from "@inglorious/store"
2
+ import type { Vector3 } from "@inglorious/utils/vector.js"
3
+
4
+ import type { Handler } from "../../index"
5
+
6
+ export interface PointerEntity extends BaseEntity {
7
+ type: "Pointer"
8
+ /** The actions a click or a touch presses. */
9
+ actions: string[]
10
+ }
11
+
12
+ /** Presses the entity's actions on a click, or for as long as a touch lasts. */
13
+ export function pointer(): {
14
+ mouseClick: Handler<PointerEntity, Vector3>
15
+ touchStart: Handler<PointerEntity, { index: number; position: Vector3 }>
16
+ touchEnd: Handler<PointerEntity, { index: number; position: Vector3 }>
17
+ }
18
+
19
+ export function createPointerEntity(actions?: string[]): PointerEntity