woofjs-engine 0.1.1 → 0.3.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 (49) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +16 -92
  3. package/THIRD_PARTY_NOTICES.md +7 -0
  4. package/dist/canvas.js +4 -31
  5. package/dist/collision.js +10 -0
  6. package/dist/game.js +4 -71
  7. package/dist/index-1jykaz1m.js +142 -0
  8. package/dist/index-9mz0qg9d.js +39 -0
  9. package/dist/index-b9a70qgn.js +34 -0
  10. package/dist/index-dp75gf4w.js +57 -0
  11. package/dist/index-f63yb8zf.js +85 -0
  12. package/dist/index-gypg8d50.js +31 -0
  13. package/dist/index-pw0zq53c.js +2533 -0
  14. package/dist/index.js +134 -188
  15. package/dist/input.js +6 -0
  16. package/dist/play.js +296 -0
  17. package/dist/sound.js +4 -56
  18. package/dist/store.js +4 -30
  19. package/dist/types/canvas.d.ts +20 -2
  20. package/dist/types/collision.d.ts +71 -0
  21. package/dist/types/game.d.ts +21 -1
  22. package/dist/types/global.d.ts +4 -0
  23. package/dist/types/helpers.d.ts +92 -0
  24. package/dist/types/index.d.ts +23 -8
  25. package/dist/types/input.d.ts +11 -0
  26. package/dist/types/play.d.ts +159 -0
  27. package/dist/types/sound.d.ts +10 -3
  28. package/dist/types/sprites/custom.d.ts +9 -0
  29. package/dist/types/sprites/easing.d.ts +9 -0
  30. package/dist/types/sprites/geometry.d.ts +22 -0
  31. package/dist/types/sprites/options.d.ts +98 -0
  32. package/dist/types/sprites/render.d.ts +2 -0
  33. package/dist/types/sprites/sprite.d.ts +283 -0
  34. package/dist/types/store.d.ts +4 -0
  35. package/dist/types/ui.d.ts +65 -0
  36. package/dist/types/woof.d.ts +386 -0
  37. package/dist/types/world/audio.d.ts +118 -0
  38. package/dist/types/world/globals.d.ts +3 -0
  39. package/dist/types/world/keys.d.ts +2 -0
  40. package/dist/types/world/options.d.ts +44 -0
  41. package/dist/types/world/render.d.ts +13 -0
  42. package/dist/types/world/scheduler.d.ts +65 -0
  43. package/dist/ui.js +235 -0
  44. package/dist/woof.global.js +3577 -0
  45. package/docs/api.json +5359 -0
  46. package/docs/woof-parity.md +231 -0
  47. package/package.json +33 -8
  48. package/docs/architecture.md +0 -37
  49. package/docs/planning.md +0 -32
@@ -0,0 +1,98 @@
1
+ import type { Sprite } from "./sprite.js";
2
+ import type { Project } from "../woof.js";
3
+ export type SpriteType = "circle" | "rectangle" | "oval" | "polygon" | "line" | "text" | "image" | "bundle" | "sprite";
4
+ export interface StaticSpriteOptions {
5
+ /** Project that owns the sprite. Defaults to the beginner project. Example: `new Sprite({ project: game })`. */
6
+ project?: Project;
7
+ /** Horizontal world coordinate, increasing to the right. Example: `new Sprite({ x: 10 })`. */
8
+ x?: number;
9
+ /** Vertical world coordinate, increasing upwards. Example: `new Sprite({ y: 20 })`. */
10
+ y?: number;
11
+ /** Direction in degrees, counter-clockwise from the right. Example: `new Sprite({ angle: 90 })`. */
12
+ angle?: number;
13
+ /** Controls whether the sprite rotates or flips with its angle. Example: `new Sprite({ rotationStyle: 'NO ROTATE' })`. */
14
+ rotationStyle?: string;
15
+ /** Whether this sprite is drawn. Example: `new Sprite({ showing: false })`. */
16
+ showing?: boolean;
17
+ /** Start drawing a trail as this sprite moves. Example: `new Sprite({ penDown: true })`. */
18
+ penDown?: boolean;
19
+ /** Color used when drawing a trail. Example: `new Sprite({ penColor: 'blue' })`. */
20
+ penColor?: string;
21
+ /** Width of the trail in world units. Example: `new Sprite({ penWidth: 2 })`. */
22
+ penWidth?: number;
23
+ /** Whether to draw the collision outline. Example: `new Sprite({ showCollider: true })`. */
24
+ showCollider?: boolean;
25
+ /** Rendering brightness as a percentage. Example: `new Sprite({ brightness: 80 })`. */
26
+ brightness?: number;
27
+ /** Sprite width in world units. Example: `new Sprite({ width: 20 })`. */
28
+ width?: number;
29
+ /** Sprite height in world units. Example: `new Sprite({ height: 30 })`. */
30
+ height?: number;
31
+ /** Circle radius in world units. Example: `new Sprite({ radius: 10 })`. */
32
+ radius?: number;
33
+ /** Number of sides in a regular polygon. Example: `new Sprite({ sides: 5 })`. */
34
+ sides?: number;
35
+ /** Distance from the centre to a regular polygon's vertices. Example: `new Sprite({ length: 30 })`. */
36
+ length?: number;
37
+ /** World x coordinate of the end of a line. Example: `new Sprite({ x1: 10 })`. */
38
+ x1?: number;
39
+ /** World y coordinate of the end of a line. Example: `new Sprite({ y1: 20 })`. */
40
+ y1?: number;
41
+ /** Fill or stroke color used to render the sprite. Example: `new Sprite({ color: 'red' })`. */
42
+ color?: string;
43
+ /** Text size in pixels. Example: `new Sprite({ size: 24 })`. */
44
+ size?: number;
45
+ /** Text to display, or a function evaluated when rendered. Example: `new Sprite({ text: 'Score' })`. */
46
+ text?: string | (() => unknown);
47
+ /** Horizontal alignment of text. Example: `new Sprite({ textAlign: 'left' })`. */
48
+ textAlign?: CanvasTextAlign;
49
+ /** Font family used for text. Example: `new Sprite({ fontFamily: 'sans-serif' })`. */
50
+ fontFamily?: string;
51
+ /** Image URL used by this sprite. Example: `new Sprite({ url: 'player.png' })`. */
52
+ url?: string;
53
+ /** Rendering opacity from zero to one. Example: `new Sprite({ opacity: 0.5 })`. */
54
+ opacity?: number;
55
+ /** Uniform rendering and collision scale. Example: `new Sprite({ scale: 2 })`. */
56
+ scale?: number;
57
+ /** Horizontal velocity in world units per second. Example: `new Sprite({ vx: 10 })`. */
58
+ vx?: number;
59
+ /** Vertical velocity in world units per second. Example: `new Sprite({ vy: 20 })`. */
60
+ vy?: number;
61
+ /** Vertical acceleration in world units per second squared. Example: `new Sprite({ gravity: -9 })`. */
62
+ gravity?: number;
63
+ /** Whether the sprite bounces off world edges. Example: `new Sprite({ bounce: true })`. */
64
+ bounce?: boolean;
65
+ /** Whether the sprite wraps around world edges. Example: `new Sprite({ wrap: true })`. */
66
+ wrap?: boolean;
67
+ /** Labels used to find sprites and subscribe to touches. Example: `new Sprite({ tags: ['enemy'] })`. */
68
+ tags?: string[];
69
+ /** Application-defined data associated with the sprite. Example: `new Sprite({ data: { score: 0 } })`. */
70
+ data?: Record<string, unknown>;
71
+ /** Drawing order; higher layers are drawn later. Example: `new Sprite({ layer: 2 })`. */
72
+ layer?: number;
73
+ /** Custom drawing callback for a sprite. Example: `new Sprite({ draw: ctx => ctx.fillRect(0, 0, 5, 5) })`. */
74
+ draw?: (ctx: CanvasRenderingContext2D) => void;
75
+ /** Callback run with the sprite and elapsed seconds each frame. Example: `new Sprite({ update: (sprite, dt) => sprite.move(dt * 10) })`. */
76
+ update?: (sprite: Sprite, dt: number) => void;
77
+ /** Image URLs available to the sprite. Example: `new Sprite({ costumes: ['idle.png', 'jump.png'] })`. */
78
+ costumes?: string[];
79
+ /** Whether to flip the image horizontally. Example: `new Sprite({ flipX: true })`. */
80
+ flipX?: boolean;
81
+ /** Whether to flip the image vertically. Example: `new Sprite({ flipY: true })`. */
82
+ flipY?: boolean;
83
+ /** Dimensions and count of image sheet frames. Example: `new Sprite({ frames: { width: 16, height: 16, count: 4 } })`. */
84
+ frames?: {
85
+ /** Width of each frame in pixels. Example: `frames: { width: 16, height: 16, count: 4 }`. */
86
+ width: number;
87
+ /** Height of each frame in pixels. Example: `frames: { width: 16, height: 16, count: 4 }`. */
88
+ height: number;
89
+ /** Number of frames in the sheet. Example: `frames: { width: 16, height: 16, count: 4 }`. */
90
+ count: number;
91
+ };
92
+ /** Sprites contained in this bundle. Example: `new Sprite({ components: [player] })`. */
93
+ components?: Sprite[];
94
+ }
95
+ /** Sprite settings can be live functions; draw and update remain callbacks. */
96
+ export type SpriteOptions = {
97
+ [K in keyof StaticSpriteOptions]: K extends "draw" | "update" | "text" | "project" ? StaticSpriteOptions[K] : StaticSpriteOptions[K] | (() => Exclude<StaticSpriteOptions[K], undefined>);
98
+ };
@@ -0,0 +1,2 @@
1
+ import type { Sprite } from "./sprite.js";
2
+ export declare function renderSprite(sprite: Sprite, ctx: CanvasRenderingContext2D): void;
@@ -0,0 +1,283 @@
1
+ import type { Project } from "../woof.js";
2
+ import type { Collider } from "../collision.js";
3
+ import type { StaticSpriteOptions, SpriteOptions, SpriteType } from "./options.js";
4
+ export type { SpriteOptions, SpriteType } from "./options.js";
5
+ import { type EasingName } from "./easing.js";
6
+ type Callback = (x: number, y: number) => void;
7
+ /** A drawable object in a project. Example: `project.circle({ radius: 20 }).move(10)`. */
8
+ export declare class Sprite {
9
+ /** The project that owns and renders this sprite. Example: `sprite.project`. */
10
+ readonly project: Project;
11
+ /** The kind of sprite used for rendering and collision. Example: `sprite.type`. */
12
+ readonly type: SpriteType;
13
+ private px;
14
+ private py;
15
+ private lastPenX;
16
+ private lastPenY;
17
+ private spriteAngle;
18
+ private spriteScale;
19
+ /** Controls whether the sprite rotates or flips with its angle. Example: `sprite.rotationStyle`. */
20
+ rotationStyle: string;
21
+ private spriteShowing;
22
+ /** Whether this sprite has been removed from its project. Example: `sprite.deleted`. */
23
+ deleted: boolean;
24
+ /** Color used when drawing a trail. Example: `sprite.penColor`. */
25
+ penColor: string;
26
+ /** Width of the trail in world units. Example: `sprite.penWidth`. */
27
+ penWidth: number;
28
+ private penActive;
29
+ /** Whether to draw the collision outline. Example: `sprite.showCollider`. */
30
+ showCollider: boolean;
31
+ private spriteBrightness;
32
+ private spriteWidth;
33
+ private spriteHeight;
34
+ /** Circle radius in world units. Example: `sprite.radius`. */
35
+ radius: number;
36
+ /** Number of sides in a regular polygon. Example: `sprite.sides`. */
37
+ sides: number;
38
+ /** Distance from the centre to a regular polygon's vertices. Example: `sprite.length`. */
39
+ length: number;
40
+ /** World x coordinate of the end of a line. Example: `sprite.x1`. */
41
+ x1: number;
42
+ /** World y coordinate of the end of a line. Example: `sprite.y1`. */
43
+ y1: number;
44
+ /** Fill or stroke color used to render the sprite. Example: `sprite.color`. */
45
+ color: string;
46
+ /** Text size in pixels. Example: `sprite.size`. */
47
+ size: number;
48
+ /** Text to display, or a function evaluated when rendered. Example: `sprite.text`. */
49
+ text: string | (() => unknown);
50
+ /** Horizontal alignment of text. Example: `sprite.textAlign`. */
51
+ textAlign: CanvasTextAlign;
52
+ /** Font family used for text. Example: `sprite.fontFamily`. */
53
+ fontFamily: string;
54
+ private imageUrl;
55
+ /** Image URLs available to the sprite. Example: `sprite.costumes`. */
56
+ costumes: string[];
57
+ private costumeIndex;
58
+ /** Rendering opacity from zero to one. Example: `sprite.opacity`. */
59
+ opacity: number;
60
+ /** Horizontal velocity in world units per second. Example: `sprite.vx`. */
61
+ vx: number;
62
+ /** Vertical velocity in world units per second. Example: `sprite.vy`. */
63
+ vy: number;
64
+ /** Vertical acceleration in world units per second squared. Example: `sprite.gravity`. */
65
+ gravity: number;
66
+ /** Whether the sprite bounces off world edges. Example: `sprite.bounce`. */
67
+ bounce: boolean;
68
+ /** Whether the sprite wraps around world edges. Example: `sprite.wrap`. */
69
+ wrap: boolean;
70
+ /** Labels used to find sprites and subscribe to touches. Example: `sprite.tags`. */
71
+ tags: string[];
72
+ /** Application-defined data associated with the sprite. Example: `sprite.data`. */
73
+ data: Record<string, unknown>;
74
+ /** Drawing order; higher layers are drawn later. Example: `sprite.layer`. */
75
+ layer: number;
76
+ /** Custom drawing callback for a sprite. Example: `sprite.draw`. */
77
+ draw?: (ctx: CanvasRenderingContext2D) => void;
78
+ /** Callback run with the sprite and elapsed seconds each frame. Example: `sprite.update`. */
79
+ update?: (sprite: Sprite, dt: number) => void;
80
+ /** Whether to flip the image horizontally. Example: `sprite.flipX`. */
81
+ flipX: boolean;
82
+ /** Whether to flip the image vertically. Example: `sprite.flipY`. */
83
+ flipY: boolean;
84
+ /** Dimensions and count of image sheet frames. Example: `sprite.frames`. */
85
+ frames?: StaticSpriteOptions["frames"];
86
+ /** Current image sheet frame index. Example: `sprite.frame`. */
87
+ frame: number;
88
+ /** Sprites contained in this bundle. Example: `sprite.components`. */
89
+ components: Sprite[];
90
+ private imageElement?;
91
+ /** Horizontal position relative to the parent bundle. Example: `sprite.offsetX`. */
92
+ offsetX: number;
93
+ /** Vertical position relative to the parent bundle. Example: `sprite.offsetY`. */
94
+ offsetY: number;
95
+ /** Angle relative to the parent bundle. Example: `sprite.offsetAngle`. */
96
+ offsetAngle: number;
97
+ private parentBundle?;
98
+ private ownedTasks;
99
+ private pressed;
100
+ private downs;
101
+ private ups;
102
+ private clicks;
103
+ private touches;
104
+ private animation?;
105
+ constructor(project: Project, type: SpriteType, options?: SpriteOptions);
106
+ /** Whether this sprite is drawn. Example: `sprite.showing`. */
107
+ get showing(): boolean;
108
+ set showing(value: boolean);
109
+ /** Rendering brightness as a percentage. Example: `sprite.brightness`. */
110
+ get brightness(): number;
111
+ set brightness(value: number);
112
+ /** Direction in degrees, counter-clockwise from the right. Example: `sprite.angle`. */
113
+ get angle(): number;
114
+ set angle(value: number);
115
+ /** Uniform rendering and collision scale. Example: `sprite.scale`. */
116
+ get scale(): number;
117
+ set scale(value: number);
118
+ /** Loaded image element, when this is an image sprite. Example: `sprite.image`. */
119
+ get image(): HTMLImageElement | undefined;
120
+ /** Index of the current costume. Example: `sprite.costume`. */
121
+ get costume(): number;
122
+ set costume(value: number);
123
+ /** Stroke width of a line. Example: `sprite.lineWidth`. */
124
+ get lineWidth(): number;
125
+ set lineWidth(value: number);
126
+ /** Return a short description containing the sprite type and position. Example: `sprite.toString()`. */
127
+ toString(): string;
128
+ /** Rotate a world point around the sprite. Example: `sprite.rotatedVector(10, 20)`. */
129
+ rotatedVector(x: number, y: number): {
130
+ x: number;
131
+ y: number;
132
+ };
133
+ /** Translate a point by the sprite position. Example: `sprite.translatedVector({ x: 0, y: 0 }, { x: 3, y: 4 })`. */
134
+ translatedVector(pos: {
135
+ x: number;
136
+ y: number;
137
+ }, v: {
138
+ x: number;
139
+ y: number;
140
+ }): {
141
+ x: number;
142
+ y: number;
143
+ };
144
+ /** Image URL used by this sprite. Example: `sprite.url`. */
145
+ get url(): string;
146
+ set url(value: string);
147
+ /** Sprite width in world units. Example: `sprite.width`. */
148
+ get width(): number;
149
+ set width(value: number);
150
+ /** Sprite height in world units. Example: `sprite.height`. */
151
+ get height(): number;
152
+ set height(value: number);
153
+ /** Horizontal world coordinate, increasing to the right. Example: `sprite.x`. */
154
+ get x(): number;
155
+ set x(v: number);
156
+ /** Vertical world coordinate, increasing upwards. Example: `sprite.y`. */
157
+ get y(): number;
158
+ set y(v: number);
159
+ /** Move to world coordinates and draw a pen trail if enabled. Example: `sprite.setPosition(10, 20)`. */
160
+ setPosition(x: number, y: number): this;
161
+ /** Add offsets to the current position. Example: `sprite.changePosition(3, -2)`. */
162
+ changePosition(x: number, y: number): this;
163
+ /** Move forward in the current direction by the given distance. Example: `sprite.move(10)`. */
164
+ move(n?: number): this;
165
+ /** Return the current angle in radians. Example: `sprite.radians()`. */
166
+ radians(): number;
167
+ /** Turn counter-clockwise by the given degrees. Example: `sprite.turnLeft(90)`. */
168
+ turnLeft(n?: number): this;
169
+ /** Turn clockwise by the given degrees. Example: `sprite.turnRight(90)`. */
170
+ turnRight(n?: number): this;
171
+ /** Point toward another sprite or a pair of world coordinates. Example: `sprite.pointTowards(other)`. */
172
+ pointTowards(x: number | {
173
+ x: number;
174
+ y: number;
175
+ }, y?: number): this;
176
+ /** Measure distance to a sprite or world coordinates. Example: `sprite.distanceTo(other)`. */
177
+ distanceTo(x: number | {
178
+ x: number;
179
+ y: number;
180
+ }, y?: number): number;
181
+ /** Change how the sprite is rotated when drawn. Example: `sprite.setRotationStyle('NO ROTATE')`. */
182
+ setRotationStyle(style: string): this;
183
+ /** Return the geometry used for collision detection. Example: `sprite.collider()`. */
184
+ collider(): Collider;
185
+ /** Return the axis-aligned bounds in world coordinates. Example: `sprite.bounds()`. */
186
+ bounds(): {
187
+ left: number;
188
+ right: number;
189
+ top: number;
190
+ bottom: number;
191
+ };
192
+ /** Test whether this sprite intersects another sprite. Example: `sprite.touching(other)`. */
193
+ touching(other: Sprite, precise?: boolean): boolean;
194
+ /** Test whether this sprite overlaps the supplied bounds. Example: `sprite.overlap({ left: 0, right: 10, top: 10, bottom: 0 })`. */
195
+ overlap(b: {
196
+ left: number;
197
+ right: number;
198
+ top: number;
199
+ bottom: number;
200
+ }): boolean;
201
+ /** Test whether a world coordinate lies inside the sprite. Example: `sprite.over(10, 20)`. */
202
+ over(x: number, y: number): boolean;
203
+ /** Whether the pointer is currently over the sprite. Example: `sprite.mouseOver`. */
204
+ get mouseOver(): boolean;
205
+ /** Whether a pointer is pressed on this sprite. Example: `sprite.mouseDown`. */
206
+ get mouseDown(): boolean;
207
+ /** Register a callback for a pointer press on this sprite. Example: `sprite.onMouseDown(() => sprite.hide())`. */
208
+ onMouseDown(fn: Callback): this;
209
+ /** Register a callback for a pointer release on this sprite. Example: `sprite.onMouseUp(() => sprite.show())`. */
210
+ onMouseUp(fn: Callback): this;
211
+ /** Register a callback for a completed click on this sprite. Example: `sprite.onClick(() => sprite.hide())`. */
212
+ onClick(fn: Callback): this;
213
+ /** Call a callback once when contact with a sprite or tag begins. Example: `sprite.onTouch('enemy', other => other.hide())`. */
214
+ onTouch(target: Sprite | string, fn: (other: Sprite) => void): this;
215
+ /** Dispatch a pointer action to this sprite. Example: `sprite.pointer('down', 0, 0)`. */
216
+ pointer(kind: "down" | "up", x: number, y: number, id?: number, cancelled?: boolean): void;
217
+ /** Record any movement since the last pen position. Example: `sprite.trackPen()`. */
218
+ trackPen(): void;
219
+ /** Remove this sprite's pointer callbacks. Example: `sprite.detachListeners()`. */
220
+ detachListeners(): void;
221
+ /** Apply component offsets to this bundle. Example: `bundle.arrangeComponents()`. */
222
+ arrangeComponents(): void;
223
+ /** Advance sprite motion, animation and touch handlers by elapsed seconds. Example: `sprite.advance(0.016)`. */
224
+ advance(dt: number): void;
225
+ /** Draw this sprite onto a canvas context. Example: `sprite.render(context)`. */
226
+ render(ctx: CanvasRenderingContext2D): void;
227
+ /** Evaluate the text value and return it as a string. Example: `sprite.textEval()`. */
228
+ textEval(): string;
229
+ /** Convert the sprite x coordinate to canvas coordinates. Example: `sprite.canvasX()`. */
230
+ canvasX(): number;
231
+ /** Convert the sprite y coordinate to canvas coordinates. Example: `sprite.canvasY()`. */
232
+ canvasY(): number;
233
+ /** Load an image from a URL. Example: `sprite.setImageURL('player.png')`. */
234
+ setImageURL(url: string): this;
235
+ /** Add an image URL to the costume list. Example: `sprite.addCostume('jump.png')`. */
236
+ addCostume(url: string): this;
237
+ /** Switch to a costume by index or URL. Example: `sprite.switchCostume(0)`. */
238
+ switchCostume(costume: number | string): this;
239
+ /** Advance to the next costume. Example: `sprite.nextCostume()`. */
240
+ nextCostume(): this;
241
+ /** Animate frames from an image sheet. Example: `sprite.animate({ fps: 8, frames: [0, 1, 2] })`. */
242
+ animate(o: {
243
+ /** Frames advanced per second. Example: `sprite.animate({ fps: 8 })`. */
244
+ fps: number;
245
+ /** Frame indices to play in order. Example: `sprite.animate({ fps: 8, frames: [0, 1] })`. */
246
+ frames?: number[];
247
+ /** Whether to restart after the last frame. Example: `sprite.animate({ fps: 8, loop: false })`. */
248
+ loop?: boolean;
249
+ }): this;
250
+ /** Start drawing a trail as this sprite moves. Example: `sprite.penDown()`. */
251
+ penDown(): this;
252
+ /** Stop drawing a trail as this sprite moves. Example: `sprite.penUp()`. */
253
+ penUp(): this;
254
+ /** Make this sprite visible. Example: `sprite.show()`. */
255
+ show(): this;
256
+ /** Hide this sprite without deleting it. Example: `sprite.hide()`. */
257
+ hide(): this;
258
+ /** Draw this sprite above its siblings. Example: `sprite.sendToFront()`. */
259
+ sendToFront(): this;
260
+ /** Draw this sprite behind its siblings. Example: `sprite.sendToBack()`. */
261
+ sendToBack(): this;
262
+ /** Remove this sprite and its owned tasks from the project. Example: `sprite.delete()`. */
263
+ delete(): this;
264
+ /** Remove this sprite from its project. Example: `sprite.remove()`. */
265
+ remove(): this;
266
+ /** Add a sprite to this bundle with an optional offset. Example: `bundle.addComponent(sprite)`. */
267
+ addComponent(sprite: Sprite, offsetX?: number, offsetY?: number, offsetAngle?: number): this;
268
+ private containsComponent;
269
+ /** Remove a sprite from this bundle. Example: `bundle.removeComponent(sprite)`. */
270
+ removeComponent(sprite: Sprite): this;
271
+ /** Assign several sprite properties at once. Example: `sprite.set({ x: 5, color: 'red' })`. */
272
+ set(props: Partial<SpriteOptions>): this;
273
+ /** Return serializable sprite options. Example: `sprite.toJSON()`. */
274
+ toJSON(): SpriteOptions;
275
+ /** Create a copy of this sprite in the same project, with optional changes. Example: `sprite.clone({ x: 40 })`. */
276
+ clone(changes?: Partial<SpriteOptions>): Sprite;
277
+ /** Animate numeric properties over game time. Example: `await sprite.tween({ angle: 90 }, 1)`. */
278
+ tween(props: Partial<Record<"x" | "y" | "angle" | "opacity" | "scale", number>>, seconds: number, easing?: EasingName | ((t: number) => number)): Promise<void>;
279
+ /** Move to a point over game time and resolve on arrival. Example: `await sprite.glideTo(20, 30, 1)`. */
280
+ glideTo(x: number, y: number, seconds: number): Promise<void>;
281
+ /** Show a temporary speech bubble above this sprite. Example: `sprite.say('Hello', 2)`. */
282
+ say(text: string, seconds?: number): this;
283
+ }
@@ -1,7 +1,11 @@
1
1
  export interface Store<State> {
2
+ /** Return the current state. Example: `store.getState()`. */
2
3
  getState(): State;
4
+ /** Replace the state or compute its next value. Example: `store.setState({ score: 1 })`. */
3
5
  setState(nextState: State | ((current: State) => State)): State;
6
+ /** Compute the next state from the current state. Example: `store.update(state => ({ ...state, score: state.score + 1 }))`. */
4
7
  update(mutator: (current: State) => State): State;
8
+ /** Listen for changes and return an unsubscribe function. Example: `store.subscribe(state => console.log(state))`. */
5
9
  subscribe(listener: (state: State) => void): () => boolean;
6
10
  }
7
11
  /** A small observable store that can be shared by any number of components. */
@@ -0,0 +1,65 @@
1
+ import { type Project, type ProjectOptions } from "./woof.js";
2
+ /** Canvas-ready colours read from the page's shadcn or Optimus UI theme. */
3
+ export interface GameTheme {
4
+ /** Page background. Example: `theme.background`. */
5
+ background: string;
6
+ /** Text on the background. Example: `theme.foreground`. */
7
+ foreground: string;
8
+ /** Main accent colour. Example: `theme.primary`. */
9
+ primary: string;
10
+ /** Text on the main accent. Example: `theme.primaryForeground`. */
11
+ primaryForeground: string;
12
+ /** Quiet surface colour. Example: `theme.muted`. */
13
+ muted: string;
14
+ /** Quiet text colour. Example: `theme.mutedForeground`. */
15
+ mutedForeground: string;
16
+ /** Colour for danger and errors. Example: `theme.destructive`. */
17
+ destructive: string;
18
+ /** Border colour. Example: `theme.border`. */
19
+ border: string;
20
+ }
21
+ /** What a mounted game is doing. */
22
+ export interface GameState {
23
+ /** Whether the game loop is running. Example: `state.running`. */
24
+ readonly running: boolean;
25
+ /** Whether the game is muted. Example: `state.muted`. */
26
+ readonly muted: boolean;
27
+ /** The first error since the last restart. Example: `state.error`. */
28
+ readonly error: unknown;
29
+ }
30
+ /** Build the game. Return a function to clean up anything the engine does not own. */
31
+ export type GameSetup = (game: Project, theme: Readonly<GameTheme>) => void | (() => void);
32
+ /** Options for `mountGame`. Accepts every `woof` option except `parent`, `global` and `fullScreen`. */
33
+ export interface MountOptions extends Omit<ProjectOptions, "parent" | "global" | "fullScreen"> {
34
+ /** Accessible name of the canvas. Example: `mountGame(el, setup, { label: 'Pong' })`. */
35
+ label?: string;
36
+ /** Start muted. Example: `mountGame(el, setup, { muted: true })`. */
37
+ muted?: boolean;
38
+ }
39
+ /** Controls a mounted game. It is also a Svelte store of its state. */
40
+ export interface GameController {
41
+ /** The current project; `restart` replaces it. Example: `controller.game?.circle()`. */
42
+ readonly game: Project | undefined;
43
+ /** Current state, the same object until something changes. Example: `controller.getState().running`. */
44
+ getState(): GameState;
45
+ /** Receive the state now and after each change; returns an unsubscribe function. Example: `controller.subscribe(console.log)`. */
46
+ subscribe(listener: (state: GameState) => void): () => void;
47
+ /** Start or continue and focus the game. Example: `controller.play()`. */
48
+ play(): void;
49
+ /** Pause the game and its sound. Example: `controller.pause()`. */
50
+ pause(): void;
51
+ /** Play when paused, pause when playing. Example: `controller.toggle()`. */
52
+ toggle(): void;
53
+ /** Run the setup again with a new project. Example: `controller.restart()`. */
54
+ restart(): void;
55
+ /** Mute or unmute. Example: `controller.setMuted(true)`. */
56
+ setMuted(muted: boolean): void;
57
+ /** Remove the game and every listener. Example: `controller.destroy()`. */
58
+ destroy(): void;
59
+ }
60
+ /** Read the theme in force at an element. Example: `readTheme(document.body).primary`. */
61
+ export declare function readTheme(element: Element): GameTheme;
62
+ /** Call back when the page's theme changes; returns a stop function. Example: `watchTheme(element, draw)`. */
63
+ export declare function watchTheme(element: Element, listener: (theme: GameTheme) => void): () => void;
64
+ /** Put a themed game inside an element. Example: `mountGame(element, (game, theme) => { game.circle({ color: () => theme.primary }); })`. */
65
+ export declare function mountGame(element: HTMLElement, setup: GameSetup, options?: MountOptions): GameController;