woofjs-engine 0.2.0 → 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.
- package/CHANGELOG.md +13 -0
- package/README.md +16 -106
- package/THIRD_PARTY_NOTICES.md +7 -0
- package/dist/canvas.js +4 -31
- package/dist/collision.js +8 -5
- package/dist/game.js +4 -84
- package/dist/index-1jykaz1m.js +142 -0
- package/dist/index-9mz0qg9d.js +39 -0
- package/dist/index-b9a70qgn.js +34 -0
- package/dist/index-dp75gf4w.js +57 -0
- package/dist/index-f63yb8zf.js +85 -0
- package/dist/index-gypg8d50.js +31 -0
- package/dist/index-pw0zq53c.js +2533 -0
- package/dist/index.js +134 -239
- package/dist/input.js +4 -33
- package/dist/play.js +296 -0
- package/dist/sound.js +4 -56
- package/dist/store.js +4 -30
- package/dist/types/canvas.d.ts +20 -2
- package/dist/types/collision.d.ts +62 -0
- package/dist/types/game.d.ts +21 -1
- package/dist/types/global.d.ts +4 -0
- package/dist/types/helpers.d.ts +92 -0
- package/dist/types/index.d.ts +23 -12
- package/dist/types/input.d.ts +4 -0
- package/dist/types/play.d.ts +159 -0
- package/dist/types/sound.d.ts +10 -3
- package/dist/types/sprites/custom.d.ts +9 -0
- package/dist/types/sprites/easing.d.ts +9 -0
- package/dist/types/sprites/geometry.d.ts +22 -0
- package/dist/types/sprites/options.d.ts +98 -0
- package/dist/types/sprites/render.d.ts +2 -0
- package/dist/types/sprites/sprite.d.ts +283 -0
- package/dist/types/store.d.ts +4 -0
- package/dist/types/ui.d.ts +65 -0
- package/dist/types/woof.d.ts +386 -0
- package/dist/types/world/audio.d.ts +118 -0
- package/dist/types/world/globals.d.ts +3 -0
- package/dist/types/world/keys.d.ts +2 -0
- package/dist/types/world/options.d.ts +44 -0
- package/dist/types/world/render.d.ts +13 -0
- package/dist/types/world/scheduler.d.ts +65 -0
- package/dist/ui.js +235 -0
- package/dist/woof.global.js +3577 -0
- package/docs/api.json +5359 -0
- package/docs/woof-parity.md +231 -0
- package/package.json +25 -8
- package/docs/architecture.md +0 -37
- package/docs/planning.md +0 -33
package/dist/sound.js
CHANGED
|
@@ -1,58 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
throw new TypeError("buffer is required");
|
|
5
|
-
if (!audioContext || typeof audioContext.createBufferSource !== "function" || typeof audioContext.createGain !== "function") {
|
|
6
|
-
throw new TypeError("audioContext must provide createBufferSource() and createGain()");
|
|
7
|
-
}
|
|
8
|
-
if (!Number.isFinite(maxGain) || maxGain < 0)
|
|
9
|
-
throw new RangeError("maxGain must be a finite, non-negative number");
|
|
10
|
-
if (!Number.isFinite(volume) || volume < 0)
|
|
11
|
-
throw new RangeError("volume must be a finite, non-negative number");
|
|
12
|
-
return {
|
|
13
|
-
startPlaying({ loop = false, playbackRate = 1 } = {}) {
|
|
14
|
-
const source = audioContext.createBufferSource();
|
|
15
|
-
const gainNode = audioContext.createGain();
|
|
16
|
-
source.buffer = buffer;
|
|
17
|
-
source.loop = Boolean(loop);
|
|
18
|
-
if (source.playbackRate)
|
|
19
|
-
source.playbackRate.value = playbackRate;
|
|
20
|
-
const baseGain = clamp(volume, maxGain);
|
|
21
|
-
gainNode.gain.value = baseGain;
|
|
22
|
-
source.connect(gainNode);
|
|
23
|
-
gainNode.connect(audioContext.destination);
|
|
24
|
-
source.start();
|
|
25
|
-
let stopped = false;
|
|
26
|
-
return {
|
|
27
|
-
source,
|
|
28
|
-
amplify(multiplier) {
|
|
29
|
-
if (stopped)
|
|
30
|
-
return this;
|
|
31
|
-
if (multiplier === "max")
|
|
32
|
-
gainNode.gain.value = maxGain;
|
|
33
|
-
else if (typeof multiplier === "number" && Number.isFinite(multiplier)) {
|
|
34
|
-
gainNode.gain.value = clamp(baseGain * multiplier, maxGain);
|
|
35
|
-
} else
|
|
36
|
-
throw new TypeError("amplify expects a finite multiplier or 'max'");
|
|
37
|
-
return this;
|
|
38
|
-
},
|
|
39
|
-
stop() {
|
|
40
|
-
if (!stopped) {
|
|
41
|
-
stopped = true;
|
|
42
|
-
source.stop();
|
|
43
|
-
}
|
|
44
|
-
return this;
|
|
45
|
-
},
|
|
46
|
-
get stopped() {
|
|
47
|
-
return stopped;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
function clamp(value, maximum) {
|
|
54
|
-
return Math.min(maximum, Math.max(0, value));
|
|
55
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
createSound2
|
|
3
|
+
} from "./index-dp75gf4w.js";
|
|
56
4
|
export {
|
|
57
|
-
createSound
|
|
5
|
+
createSound2 as createSound
|
|
58
6
|
};
|
package/dist/store.js
CHANGED
|
@@ -1,32 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const listeners = new Set;
|
|
5
|
-
function setState(nextState) {
|
|
6
|
-
const next = typeof nextState === "function" ? nextState(state) : nextState;
|
|
7
|
-
if (Object.is(next, state))
|
|
8
|
-
return state;
|
|
9
|
-
state = next;
|
|
10
|
-
for (const listener of [...listeners])
|
|
11
|
-
listener(state);
|
|
12
|
-
return state;
|
|
13
|
-
}
|
|
14
|
-
return {
|
|
15
|
-
getState: () => state,
|
|
16
|
-
setState,
|
|
17
|
-
update(mutator) {
|
|
18
|
-
if (typeof mutator !== "function")
|
|
19
|
-
throw new TypeError("mutator must be a function");
|
|
20
|
-
return setState(mutator(state));
|
|
21
|
-
},
|
|
22
|
-
subscribe(listener) {
|
|
23
|
-
if (typeof listener !== "function")
|
|
24
|
-
throw new TypeError("listener must be a function");
|
|
25
|
-
listeners.add(listener);
|
|
26
|
-
return () => listeners.delete(listener);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
createStore2
|
|
3
|
+
} from "./index-gypg8d50.js";
|
|
30
4
|
export {
|
|
31
|
-
createStore
|
|
5
|
+
createStore2 as createStore
|
|
32
6
|
};
|
package/dist/types/canvas.d.ts
CHANGED
|
@@ -1,16 +1,34 @@
|
|
|
1
1
|
export interface CanvasOptions {
|
|
2
|
+
/** Document used to create the canvas element. Example: `createCanvas({ document })`. */
|
|
2
3
|
document?: Document;
|
|
4
|
+
/** Optional element to which the canvas is appended. Example: `createCanvas({ parent: container })`. */
|
|
3
5
|
parent?: Element;
|
|
6
|
+
/** Initial canvas width in pixels. Example: `createCanvas({ width: 400 })`. */
|
|
4
7
|
width?: number;
|
|
8
|
+
/** Initial canvas height in pixels. Example: `createCanvas({ height: 300 })`. */
|
|
5
9
|
height?: number;
|
|
6
|
-
context
|
|
10
|
+
/** Canvas context kind to create. Example: `createCanvas({ context: '2d' })`. */
|
|
11
|
+
context?: "2d";
|
|
12
|
+
/** Options passed to the canvas context constructor. Example: `createCanvas({ contextOptions: { alpha: false } })`. */
|
|
7
13
|
contextOptions?: CanvasRenderingContext2DSettings;
|
|
8
14
|
}
|
|
9
15
|
export interface CanvasSurface {
|
|
16
|
+
/** Canvas element owned by this surface. Example: `surface.element`. */
|
|
10
17
|
element: HTMLCanvasElement;
|
|
18
|
+
/** Canvas context kind to create. Example: `surface.context.fillRect(0, 0, 10, 10)`. */
|
|
11
19
|
context: CanvasRenderingContext2D;
|
|
20
|
+
/** Resize the canvas in pixels. Example: `surface.resize(400, 300)`. */
|
|
12
21
|
resize(width: number, height: number): this;
|
|
22
|
+
/** Remove the canvas from its parent. Example: `surface.destroy()`. */
|
|
13
23
|
destroy(): void;
|
|
14
24
|
}
|
|
15
25
|
/** Create an independent canvas surface. DOM access happens only when called. */
|
|
16
|
-
export declare function createCanvas({
|
|
26
|
+
export declare function createCanvas({
|
|
27
|
+
/** Document used to create the canvas element. */
|
|
28
|
+
document: doc, parent,
|
|
29
|
+
/** Initial canvas width in pixels. */
|
|
30
|
+
width,
|
|
31
|
+
/** Initial canvas height in pixels. */
|
|
32
|
+
height,
|
|
33
|
+
/** Canvas context kind to create. */
|
|
34
|
+
context, contextOptions, }?: CanvasOptions): CanvasSurface;
|
|
@@ -1,9 +1,71 @@
|
|
|
1
1
|
/** Axis-aligned rectangle in world coordinates. */
|
|
2
2
|
export interface Rectangle {
|
|
3
|
+
/** Horizontal coordinate of the rectangle. Example: `{ x: 0, y: 0, width: 10, height: 20 }`. */
|
|
3
4
|
x: number;
|
|
5
|
+
/** Vertical coordinate of the rectangle. Example: `{ x: 0, y: 0, width: 10, height: 20 }`. */
|
|
4
6
|
y: number;
|
|
7
|
+
/** Rectangle width in world units. Example: `{ x: 0, y: 0, width: 10, height: 20 }`. */
|
|
5
8
|
width: number;
|
|
9
|
+
/** Rectangle height in world units. Example: `{ x: 0, y: 0, width: 10, height: 20 }`. */
|
|
6
10
|
height: number;
|
|
7
11
|
}
|
|
8
12
|
/** True when two positive-area rectangles overlap. Touching edges do not overlap. */
|
|
9
13
|
export declare function overlaps(a: Rectangle, b: Rectangle): boolean;
|
|
14
|
+
export type Collider = {
|
|
15
|
+
/** Circle collider discriminator. */
|
|
16
|
+
kind: "circle";
|
|
17
|
+
/** Horizontal centre coordinate. */
|
|
18
|
+
x: number;
|
|
19
|
+
/** Vertical centre coordinate. */
|
|
20
|
+
y: number;
|
|
21
|
+
/** Circle radius. */
|
|
22
|
+
radius: number;
|
|
23
|
+
} | {
|
|
24
|
+
/** Ellipse collider discriminator. */
|
|
25
|
+
kind: "ellipse";
|
|
26
|
+
/** Horizontal centre coordinate. */
|
|
27
|
+
x: number;
|
|
28
|
+
/** Vertical centre coordinate. */
|
|
29
|
+
y: number;
|
|
30
|
+
/** Horizontal radius before rotation. */
|
|
31
|
+
radiusX: number;
|
|
32
|
+
/** Vertical radius before rotation. */
|
|
33
|
+
radiusY: number;
|
|
34
|
+
/** Rotation angle in radians. */
|
|
35
|
+
angle: number;
|
|
36
|
+
} | {
|
|
37
|
+
/** Polygon collider discriminator. */
|
|
38
|
+
kind: "polygon";
|
|
39
|
+
/** World-coordinate polygon vertices. */
|
|
40
|
+
points: {
|
|
41
|
+
x: number;
|
|
42
|
+
y: number;
|
|
43
|
+
}[];
|
|
44
|
+
};
|
|
45
|
+
/** Test convex colliders with a bounding-box prefilter and separating axes. */
|
|
46
|
+
export declare function intersects(a: Collider, b: Collider): boolean;
|
|
47
|
+
/** A circle from the SAT.js API used by WoofJS. */
|
|
48
|
+
export interface SATCircle {
|
|
49
|
+
/** Circle centre in world coordinates. Example: `{ pos: { x: 0, y: 0 }, r: 5 }`. */
|
|
50
|
+
pos: {
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
};
|
|
54
|
+
/** Circle radius in world units. Example: `{ pos: { x: 0, y: 0 }, r: 5 }`. */
|
|
55
|
+
r: number;
|
|
56
|
+
}
|
|
57
|
+
/** A SAT.js polygon, with its transformed points relative to its position. */
|
|
58
|
+
export interface SATPolygon {
|
|
59
|
+
/** Origin of the polygon. Example: `{ pos: { x: 0, y: 0 }, calcPoints: [{ x: 0, y: 0 }, { x: 5, y: 0 }, { x: 0, y: 5 }] }`. */
|
|
60
|
+
pos: {
|
|
61
|
+
x: number;
|
|
62
|
+
y: number;
|
|
63
|
+
};
|
|
64
|
+
/** Transformed vertices relative to the polygon origin. Example: `{ pos: { x: 0, y: 0 }, calcPoints: [{ x: 0, y: 0 }, { x: 5, y: 0 }, { x: 0, y: 5 }] }`. */
|
|
65
|
+
calcPoints: ReadonlyArray<{
|
|
66
|
+
x: number;
|
|
67
|
+
y: number;
|
|
68
|
+
}>;
|
|
69
|
+
}
|
|
70
|
+
/** Test engine colliders or SAT.js circles and polygons. Example: `detectCollision(a.collider(), b.collider())`. */
|
|
71
|
+
export declare function detectCollision(a: Collider | SATCircle | SATPolygon, b: Collider | SATCircle | SATPolygon): boolean;
|
package/dist/types/game.d.ts
CHANGED
|
@@ -1,24 +1,44 @@
|
|
|
1
1
|
export interface FrameInfo {
|
|
2
|
+
/** Elapsed frame time in milliseconds. Example: `frame.deltaMs`. */
|
|
2
3
|
deltaMs: number;
|
|
4
|
+
/** Animation-frame timestamp in milliseconds. Example: `frame.timestamp`. */
|
|
3
5
|
timestamp: number;
|
|
6
|
+
/** Zero-based frame number. Example: `frame.frame`. */
|
|
4
7
|
frame: number;
|
|
5
8
|
}
|
|
6
9
|
export interface GameOptions<State = unknown> {
|
|
10
|
+
/** Initial state passed to the game loop. Example: `createGame({ state: { score: 0 } })`. */
|
|
7
11
|
state?: State;
|
|
12
|
+
/** Callback run before rendering, with elapsed seconds. Example: `createGame({ update: (dt, state) => { state.x += dt; }, state: { x: 0 } })`. */
|
|
8
13
|
update?: (deltaSeconds: number, state: State, frame: FrameInfo) => void;
|
|
14
|
+
/** Callback used to draw the current frame. Example: `createGame({ render: (state, frame) => console.log(frame.frame) })`. */
|
|
9
15
|
render?: (state: State, frame: FrameInfo & {
|
|
10
16
|
deltaSeconds: number;
|
|
11
17
|
}) => void;
|
|
18
|
+
/** Animation-frame scheduler to use for automatic updates. Example: `createGame({ requestFrame: requestAnimationFrame })`. */
|
|
12
19
|
requestFrame?: (callback: FrameRequestCallback) => number;
|
|
20
|
+
/** Cancel a requested animation frame. Example: `createGame({ cancelFrame: cancelAnimationFrame })`. */
|
|
13
21
|
cancelFrame?: (id: number) => void;
|
|
14
22
|
}
|
|
15
23
|
export interface GameLoop<State = unknown> {
|
|
24
|
+
/** Advance one frame by a duration in milliseconds. Example: `game.step(16)`. */
|
|
16
25
|
step(deltaMs: number, timestamp?: number): State;
|
|
26
|
+
/** Return the current game state. Example: `game.getState()`. */
|
|
17
27
|
getState(): State;
|
|
28
|
+
/** Replace the current game state. Example: `game.setState({ score: 1 })`. */
|
|
18
29
|
setState(nextState: State): State;
|
|
30
|
+
/** Start the automatic animation loop. Example: `game.start()`. */
|
|
19
31
|
start(): this;
|
|
32
|
+
/** Stop the automatic animation loop. Example: `game.stop()`. */
|
|
20
33
|
stop(): this;
|
|
34
|
+
/** Whether the animation loop is active. Example: `game.running`. */
|
|
21
35
|
readonly running: boolean;
|
|
22
36
|
}
|
|
23
37
|
/** A tiny browser game loop with deterministic, manually-driven `step`. */
|
|
24
|
-
export declare function createGame<State = Record<string, never>>({
|
|
38
|
+
export declare function createGame<State = Record<string, never>>({
|
|
39
|
+
/** Initial state passed to the game loop. */
|
|
40
|
+
state,
|
|
41
|
+
/** Callback run before rendering, with elapsed seconds. */
|
|
42
|
+
update,
|
|
43
|
+
/** Callback used to draw the current frame. */
|
|
44
|
+
render, requestFrame, cancelFrame, }?: GameOptions<State>): GameLoop<State>;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Clamp a value to an inclusive interval. Example: `clamp(12, 0, 10)`. */
|
|
2
|
+
export declare const clamp: (n: number, min: number, max: number) => number;
|
|
3
|
+
/** Linearly interpolate between two values. Example: `lerp(0, 10, 0.5)`. */
|
|
4
|
+
export declare const lerp: (a: number, b: number, t: number) => number;
|
|
5
|
+
/** Distance between two points. Example: `distance(0, 0, 3, 4)`. */
|
|
6
|
+
export declare const distance: (x1: number, y1: number, x2: number, y2: number) => number;
|
|
7
|
+
/** True when a value lies strictly between its bounds. Example: `between(5, 0, 10)`. */
|
|
8
|
+
export declare const between: (n: number, a: number, b: number) => boolean;
|
|
9
|
+
/** Remove all occurrences of an item from an array. Example: `remove(items, item)`. */
|
|
10
|
+
export declare function remove<T>(array: T[], item: T): void;
|
|
11
|
+
/** Return a fractional random number in the half-open interval. Example: `randomFloat(0, 1)`. */
|
|
12
|
+
export declare const randomFloat: (a: number, b: number) => number;
|
|
13
|
+
/** Return a whole number between the inclusive bounds. Example: `random(1, 6)`. */
|
|
14
|
+
export declare function random(a: number, b: number): number;
|
|
15
|
+
/** Pick an item from an array. Example: `pick([1, 2, 3])`. */
|
|
16
|
+
export declare const pick: <T>(array: readonly T[]) => T | undefined;
|
|
17
|
+
/** Return a shuffled copy. Example: `shuffle([1, 2, 3])`. */
|
|
18
|
+
export declare function shuffle<T>(array: readonly T[]): T[];
|
|
19
|
+
/** Generate a half-open range. Example: `range(0, 3)` returns [0, 1, 2]. */
|
|
20
|
+
export declare function range(start: number, end: number, increment?: number): number[];
|
|
21
|
+
/** Format an RGB color. Example: `rgb(255, 0, 0)`. */
|
|
22
|
+
export declare const rgb: (r: number, g: number, b: number) => string;
|
|
23
|
+
/** Choose a random RGB color. Example: `randomColor()`. */
|
|
24
|
+
export declare const randomColor: () => string;
|
|
25
|
+
/** Rightward direction in degrees. Example: `RIGHT`. */
|
|
26
|
+
export declare const RIGHT = 0;
|
|
27
|
+
/** Leftward direction in degrees. Example: `LEFT`. */
|
|
28
|
+
export declare const LEFT = 180;
|
|
29
|
+
/** Upward direction in degrees. Example: `UP`. */
|
|
30
|
+
export declare const UP = 90;
|
|
31
|
+
/** Downward direction in degrees. Example: `DOWN`. */
|
|
32
|
+
export declare const DOWN = 270;
|
|
33
|
+
/** Sine of an angle in degrees. Example: `sin(90)`. */
|
|
34
|
+
export declare const sin: (a: number) => number;
|
|
35
|
+
/** Cosine of an angle in degrees. Example: `cos(0)`. */
|
|
36
|
+
export declare const cos: (a: number) => number;
|
|
37
|
+
/** Tangent of an angle in degrees. Example: `tan(45)`. */
|
|
38
|
+
export declare const tan: (a: number) => number;
|
|
39
|
+
/** Inverse sine, returned in degrees. Example: `asin(1)`. */
|
|
40
|
+
export declare const asin: (a: number) => number;
|
|
41
|
+
/** Inverse cosine, returned in degrees. Example: `acos(1)`. */
|
|
42
|
+
export declare const acos: (a: number) => number;
|
|
43
|
+
/** Inverse tangent, returned in degrees. Example: `atan(1)`. */
|
|
44
|
+
export declare const atan: (a: number) => number;
|
|
45
|
+
/** Square root of a number. Example: `sqrt(9)`. */
|
|
46
|
+
export declare const sqrt: (x: number) => number;
|
|
47
|
+
/** Absolute value of a number. Example: `abs(-2)`. */
|
|
48
|
+
export declare const abs: (x: number) => number;
|
|
49
|
+
/** Round down to an integer. Example: `floor(1.5)`. */
|
|
50
|
+
export declare const floor: (x: number) => number;
|
|
51
|
+
/** Round up to an integer. Example: `ceiling(1.5)`. */
|
|
52
|
+
export declare const ceiling: (x: number) => number;
|
|
53
|
+
/** Natural logarithm. Example: `ln(1)`. */
|
|
54
|
+
export declare const ln: (x: number) => number;
|
|
55
|
+
/** Base-ten logarithm. Example: `log(100)`. */
|
|
56
|
+
export declare const log: (x: number) => number;
|
|
57
|
+
/** Raise a number to a power. Example: `pow(2, 3)`. */
|
|
58
|
+
export declare const pow: (x: number, y: number) => number;
|
|
59
|
+
/** Current local hour in twenty-four-hour time. Example: `hourMilitary()`. */
|
|
60
|
+
export declare const hourMilitary: () => number;
|
|
61
|
+
/** Current local hour in twelve-hour time. Example: `hour()`. */
|
|
62
|
+
export declare const hour: () => number;
|
|
63
|
+
/** Current local minute. Example: `minute()`. */
|
|
64
|
+
export declare const minute: () => number;
|
|
65
|
+
/** Current local second. Example: `second()`. */
|
|
66
|
+
export declare const second: () => number;
|
|
67
|
+
/** Current local day of the month. Example: `dayOfMonth()`. */
|
|
68
|
+
export declare const dayOfMonth: () => number;
|
|
69
|
+
/** Current local weekday name. Example: `dayOfWeek()`. */
|
|
70
|
+
export declare const dayOfWeek: () => string;
|
|
71
|
+
/** Current local month name. Example: `month()`. */
|
|
72
|
+
export declare const month: () => string;
|
|
73
|
+
/** Current local year. Example: `year()`. */
|
|
74
|
+
export declare const year: () => number;
|
|
75
|
+
/** Detect common mobile user agents without accessing browser globals at import time. Example: `mobile()`. */
|
|
76
|
+
export declare const mobile: () => boolean;
|
|
77
|
+
/** Load scripts in order. Example: `await importCodeURL('/library.js')`. */
|
|
78
|
+
export declare function importCodeURL(url: string | string[], callback?: () => void): Promise<void>;
|
|
79
|
+
/** Copy enumerable properties into an object. Example: `extend({x: 1}, {y: 2})`. */
|
|
80
|
+
export declare function extend<A extends object, B extends object>(target: A, source: B): A & B;
|
|
81
|
+
/** Names of the months, starting with January. Example: `months[0]`. */
|
|
82
|
+
export declare const months: readonly string[];
|
|
83
|
+
/** Convert a legacy keyboard key code into a Woof key name. Example: `keyCodeToString(38)`. */
|
|
84
|
+
export declare function keyCodeToString(code: number): string;
|
|
85
|
+
/** Convert a Woof time unit to milliseconds. Example: `unitsToMiliseconds(2, 'seconds')`. */
|
|
86
|
+
export declare function unitsToMiliseconds(time: number, units: string): number;
|
|
87
|
+
/** Limit calls to a callback to once per interval in milliseconds. Example: `throttle(save, 500)`. */
|
|
88
|
+
export declare function throttle<This, Args extends unknown[]>(callback: (this: This, ...args: Args) => void, limit: number): (this: This, ...args: Args) => void;
|
|
89
|
+
/** Fetch JSON and optionally pass the result to a callback. Example: `await getData('/scores.json')`. */
|
|
90
|
+
export declare function getData<T = unknown>(url: string, callback?: (data: T) => void): Promise<T>;
|
|
91
|
+
/** Format a list in brackets without changing Array.prototype. Example: `arrayToString([1, 2])`. */
|
|
92
|
+
export declare function arrayToString(values: readonly unknown[]): string;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
export { createGame } from
|
|
2
|
-
export type { GameOptions, GameLoop, FrameInfo } from
|
|
3
|
-
export { createSound } from
|
|
4
|
-
export type { SoundOptions, Sound, Playback, PlaybackOptions } from
|
|
5
|
-
export { createStore } from
|
|
6
|
-
export type { Store } from
|
|
7
|
-
export { createCanvas } from
|
|
8
|
-
export type { CanvasOptions, CanvasSurface } from
|
|
9
|
-
export { createKeyboard } from
|
|
10
|
-
export type { KeyboardInput } from
|
|
11
|
-
export { overlaps } from
|
|
12
|
-
export type { Rectangle } from
|
|
1
|
+
export { createGame } from "./game.js";
|
|
2
|
+
export type { GameOptions, GameLoop, FrameInfo } from "./game.js";
|
|
3
|
+
export { createSound } from "./sound.js";
|
|
4
|
+
export type { SoundOptions, Sound, Playback, PlaybackOptions, } from "./sound.js";
|
|
5
|
+
export { createStore } from "./store.js";
|
|
6
|
+
export type { Store } from "./store.js";
|
|
7
|
+
export { createCanvas } from "./canvas.js";
|
|
8
|
+
export type { CanvasOptions, CanvasSurface } from "./canvas.js";
|
|
9
|
+
export { createKeyboard } from "./input.js";
|
|
10
|
+
export type { KeyboardInput } from "./input.js";
|
|
11
|
+
export { overlaps, detectCollision } from "./collision.js";
|
|
12
|
+
export type { Rectangle, Collider, SATCircle, SATPolygon, } from "./collision.js";
|
|
13
|
+
export * from "./helpers.js";
|
|
14
|
+
export { woof, Project } from "./woof.js";
|
|
15
|
+
export type { ProjectOptions } from "./woof.js";
|
|
16
|
+
export { Repeat, RepeatUntil } from "./world/scheduler.js";
|
|
17
|
+
export type { TaskHandle, RepeatHandle, Time } from "./world/scheduler.js";
|
|
18
|
+
export { Sprite } from "./sprites/sprite.js";
|
|
19
|
+
export type { SpriteOptions, SpriteType } from "./sprites/sprite.js";
|
|
20
|
+
export { easings } from "./sprites/easing.js";
|
|
21
|
+
export type { EasingName } from "./sprites/easing.js";
|
|
22
|
+
export { ProjectSound } from "./world/audio.js";
|
|
23
|
+
export type { ProjectSoundOptions } from "./world/audio.js";
|
package/dist/types/input.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
/** Observe physical keyboard codes without owning a framework or game loop. */
|
|
2
2
|
export interface KeyboardInput {
|
|
3
|
+
/** Check whether a physical keyboard code is held. Example: `keyboard.isDown('ArrowLeft')`. */
|
|
3
4
|
isDown(code: string): boolean;
|
|
5
|
+
/** Forget all held keys. Example: `keyboard.clear()`. */
|
|
4
6
|
clear(): void;
|
|
7
|
+
/** Remove keyboard listeners and clear held keys. Example: `keyboard.destroy()`. */
|
|
5
8
|
destroy(): void;
|
|
6
9
|
}
|
|
10
|
+
/** Listen for held keyboard codes on an event target. */
|
|
7
11
|
export declare function createKeyboard(target?: EventTarget): KeyboardInput;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { type Project } from "./woof.js";
|
|
2
|
+
import type { Sprite as SpriteInstance, SpriteOptions } from "./sprites/sprite.js";
|
|
3
|
+
import type { ProjectSoundOptions } from "./world/audio.js";
|
|
4
|
+
import type { Time, TaskHandle } from "./world/scheduler.js";
|
|
5
|
+
/** Return the lazily created beginner project. */
|
|
6
|
+
export declare function project(): Project;
|
|
7
|
+
/** Mouse x coordinate in the beginner project's world. Example: `player.x = mouseX`. */
|
|
8
|
+
export declare let mouseX: number;
|
|
9
|
+
/** Mouse y coordinate in the beginner project's world. Example: `player.y = mouseY`. */
|
|
10
|
+
export declare let mouseY: number;
|
|
11
|
+
/** Left edge of the visible world. Example: `player.x = minX`. */
|
|
12
|
+
export declare let minX: number;
|
|
13
|
+
/** Right edge of the visible world. Example: `player.x = maxX`. */
|
|
14
|
+
export declare let maxX: number;
|
|
15
|
+
/** Bottom edge of the visible world. Example: `player.y = minY`. */
|
|
16
|
+
export declare let minY: number;
|
|
17
|
+
/** Top edge of the visible world. Example: `player.y = maxY`. */
|
|
18
|
+
export declare let maxY: number;
|
|
19
|
+
type Shape = {
|
|
20
|
+
(options?: SpriteOptions): SpriteInstance;
|
|
21
|
+
/** Construct a sprite from an options object. */
|
|
22
|
+
new (options?: SpriteOptions): SpriteInstance;
|
|
23
|
+
};
|
|
24
|
+
/** Create a circle, for example `new Circle({ radius: 20 })`. */
|
|
25
|
+
export declare const Circle: Shape;
|
|
26
|
+
/** Create a rectangle, for example `new Rectangle({ width: 30, height: 10 })`. */
|
|
27
|
+
export declare const Rectangle: Shape;
|
|
28
|
+
/** Create an oval, for example `new Oval({ width: 20, height: 40 })`. */
|
|
29
|
+
export declare const Oval: Shape;
|
|
30
|
+
/** Create a regular polygon, for example `new Polygon({ sides: 5, length: 20 })`. */
|
|
31
|
+
export declare const Polygon: Shape;
|
|
32
|
+
/** Create a line, for example `new Line({ x1: 30, y1: 20 })`. */
|
|
33
|
+
export declare const Line: Shape;
|
|
34
|
+
/** Create text, for example `new Text({ text: 'Score', size: 24 })`. */
|
|
35
|
+
export declare const Text: Shape;
|
|
36
|
+
/** Create a text primitive. Example: `new TextPrimative({ text: 'Hello' })`. */
|
|
37
|
+
export declare const TextPrimative: Shape;
|
|
38
|
+
/** Create an image sprite, for example `new Image({ url: 'player.png' })`. */
|
|
39
|
+
export declare const Image: Shape;
|
|
40
|
+
/** Group sprites, for example `new Bundle({ components: [player] })`. */
|
|
41
|
+
export declare const Bundle: Shape;
|
|
42
|
+
/** Create a custom sprite, for example `new Sprite({ draw: ctx => ctx.fillRect(0, 0, 10, 10) })`. */
|
|
43
|
+
export declare const Sprite: Shape;
|
|
44
|
+
/** Define a custom sprite constructor. Example: `const Star = customSprite({ render() {} })`. */
|
|
45
|
+
export declare const customSprite: Project["customSprite"];
|
|
46
|
+
/** Create a sound, for example `new Sound({ url: 'sound.mp3' })`. */
|
|
47
|
+
export declare const Sound: {
|
|
48
|
+
(options?: ProjectSoundOptions): ReturnType<Project["sound"]>;
|
|
49
|
+
/** Construct a sound from an options object. */
|
|
50
|
+
new (options?: ProjectSoundOptions): ReturnType<Project["sound"]>;
|
|
51
|
+
};
|
|
52
|
+
/** Run immediately and again at each interval, such as `every(2, 'seconds', fn)`. */
|
|
53
|
+
export declare function every(time: Time, fn: () => void): TaskHandle;
|
|
54
|
+
/** Run immediately and at an interval. Example: `every(2, 'seconds', spawn)`. */
|
|
55
|
+
export declare function every(time: Time, units: string, fn: () => void): TaskHandle;
|
|
56
|
+
/** Run once after a delay, such as `after(2, 'seconds', fn)`. */
|
|
57
|
+
export declare function after(time: Time, fn: () => void): TaskHandle;
|
|
58
|
+
/** Run after a delay. Example: `after(2, 'seconds', spawn)`. */
|
|
59
|
+
export declare function after(time: Time, units: string, fn: () => void): TaskHandle;
|
|
60
|
+
/** Run a callback each frame, for example `forever(() => player.move(1))`. */
|
|
61
|
+
export declare const forever: Project["forever"];
|
|
62
|
+
/** Run each frame while a condition is true, for example `when(() => pressing.includes('UP'), move)`. */
|
|
63
|
+
export declare const when: Project["when"];
|
|
64
|
+
/** Run once when a condition becomes true. */
|
|
65
|
+
export declare const once: Project["once"];
|
|
66
|
+
/** Run once per frame for a count; chain `.then(...)` to run on completion. */
|
|
67
|
+
export declare const repeat: Project["repeat"];
|
|
68
|
+
/** Run once per frame until the condition becomes true. */
|
|
69
|
+
export declare const repeatUntil: Project["repeatUntil"];
|
|
70
|
+
/** Resolve after the specified number of seconds of game time. */
|
|
71
|
+
export declare const wait: (seconds: number) => Promise<void>;
|
|
72
|
+
/** Run after the beginner project is initialized. Example: `ready(startGame)`. */
|
|
73
|
+
export declare const ready: Project["ready"];
|
|
74
|
+
/** Convert world coordinates to canvas coordinates. Example: `translateToCanvas(0, 0)`. */
|
|
75
|
+
export declare const translateToCanvas: Project["translateToCanvas"];
|
|
76
|
+
/** Convert canvas coordinates to world coordinates. Example: `translateToCenter(100, 100)`. */
|
|
77
|
+
export declare const translateToCenter: Project["translateToCenter"];
|
|
78
|
+
/** Whether a key is held, e.g. `pressing.includes('UP')`. */
|
|
79
|
+
export declare const pressing: {
|
|
80
|
+
/** Test a WoofJS key name. Example: `pressing.includes('UP')`. */
|
|
81
|
+
includes: (key: string) => boolean;
|
|
82
|
+
};
|
|
83
|
+
/** Set the background color of the beginner project. */
|
|
84
|
+
export declare const setBackdropColor: (color: string) => Project;
|
|
85
|
+
/** Set a backdrop image. Example: `setBackdropURL('sky.png')`. */
|
|
86
|
+
export declare const setBackdropURL: Project["setBackdropURL"];
|
|
87
|
+
/** Resize the backdrop. Example: `setBackdropSize(400, 300)`. */
|
|
88
|
+
export declare const setBackdropSize: Project["setBackdropSize"];
|
|
89
|
+
/** Set the CSS backdrop size style. Example: `setBackdropStyle('cover')`. */
|
|
90
|
+
export declare const setBackdropStyle: Project["setBackdropStyle"];
|
|
91
|
+
/** Set backdrop repetition. Example: `setBackdropRepeat('repeat')`. */
|
|
92
|
+
export declare const setBackdropRepeat: Project["setBackdropRepeat"];
|
|
93
|
+
/** Return the visible world bounds. Example: `bounds().left`. */
|
|
94
|
+
export declare const bounds: Project["bounds"];
|
|
95
|
+
/** Pick a whole-number x coordinate in the visible world. Example: `randomX()`. */
|
|
96
|
+
export declare const randomX: Project["randomX"];
|
|
97
|
+
/** Pick a whole-number y coordinate in the visible world. Example: `randomY()`. */
|
|
98
|
+
export declare const randomY: Project["randomY"];
|
|
99
|
+
/** Remove all pen trails. Example: `clearPen()`. */
|
|
100
|
+
export declare const clearPen: Project["clearPen"];
|
|
101
|
+
/** Read elapsed game time in seconds. Example: `timer()`. */
|
|
102
|
+
export declare const timer: Project["timer"];
|
|
103
|
+
/** Reset the game timer. Example: `resetTimer()`. */
|
|
104
|
+
export declare const resetTimer: Project["resetTimer"];
|
|
105
|
+
/** Pause game-time updates. Example: `freeze()`. */
|
|
106
|
+
export declare const freeze: Project["freeze"];
|
|
107
|
+
/** Resume game-time updates. Example: `defrost()`. */
|
|
108
|
+
export declare const defrost: Project["defrost"];
|
|
109
|
+
/** Register a scene setup function. Example: `scene('menu', showMenu)`. */
|
|
110
|
+
export declare const scene: Project["scene"];
|
|
111
|
+
/** Enter a registered scene. Example: `go('menu')`. */
|
|
112
|
+
export declare const go: Project["go"];
|
|
113
|
+
/** Find sprites carrying a tag. Example: `find('enemy')`. */
|
|
114
|
+
export declare const find: Project["find"];
|
|
115
|
+
/** Create a burst of temporary sprites. Example: `particles({ count: 5 })`. */
|
|
116
|
+
export declare const particles: Project["particles"];
|
|
117
|
+
/** Save a JSON value in project storage. Example: `save('score', 10)`. */
|
|
118
|
+
export declare const save: Project["save"];
|
|
119
|
+
/** Load a stored JSON value or use a fallback. Example: `load('score', 0)`. */
|
|
120
|
+
export declare const load: Project["load"];
|
|
121
|
+
/** Run a callback each frame with elapsed seconds. Example: `onUpdate(dt => {})`. */
|
|
122
|
+
export declare const onUpdate: Project["onUpdate"];
|
|
123
|
+
/** Subscribe to a key press. Example: `onKeyDown(key => {})`. */
|
|
124
|
+
export declare const onKeyDown: Project["onKeyDown"];
|
|
125
|
+
/** Subscribe to a key release. Example: `onKeyUp(key => {})`. */
|
|
126
|
+
export declare const onKeyUp: Project["onKeyUp"];
|
|
127
|
+
/** Subscribe to pointer presses. Example: `onMouseDown((x, y) => {})`. */
|
|
128
|
+
export declare const onMouseDown: Project["onMouseDown"];
|
|
129
|
+
/** Subscribe to pointer releases. Example: `onMouseUp((x, y) => {})`. */
|
|
130
|
+
export declare const onMouseUp: Project["onMouseUp"];
|
|
131
|
+
/** Subscribe to pointer movement. Example: `onMouseMove((x, y) => {})`. */
|
|
132
|
+
export declare const onMouseMove: Project["onMouseMove"];
|
|
133
|
+
/** Pointer coordinates and button state for the beginner project. */
|
|
134
|
+
export declare const mouse: {
|
|
135
|
+
/** Pointer x coordinate in world units. Example: `mouse.x`. */
|
|
136
|
+
readonly x: number;
|
|
137
|
+
/** Pointer y coordinate in world units. Example: `mouse.y`. */
|
|
138
|
+
readonly y: number;
|
|
139
|
+
/** Whether a pointer is pressed. Example: `mouse.down`. */
|
|
140
|
+
readonly down: boolean;
|
|
141
|
+
};
|
|
142
|
+
/** Dimensions, bounds and sprites of the beginner project. */
|
|
143
|
+
export declare const world: {
|
|
144
|
+
/** Width of the visible world. Example: `world.width`. */
|
|
145
|
+
readonly width: number;
|
|
146
|
+
/** Height of the visible world. Example: `world.height`. */
|
|
147
|
+
readonly height: number;
|
|
148
|
+
/** Left edge of the visible world. Example: `world.minX`. */
|
|
149
|
+
readonly minX: number;
|
|
150
|
+
/** Right edge of the visible world. Example: `world.maxX`. */
|
|
151
|
+
readonly maxX: number;
|
|
152
|
+
/** Bottom edge of the visible world. Example: `world.minY`. */
|
|
153
|
+
readonly minY: number;
|
|
154
|
+
/** Top edge of the visible world. Example: `world.maxY`. */
|
|
155
|
+
readonly maxY: number;
|
|
156
|
+
/** Sprites currently in the project. Example: `world.sprites`. */
|
|
157
|
+
readonly sprites: SpriteInstance[];
|
|
158
|
+
};
|
|
159
|
+
export * from "./helpers.js";
|
package/dist/types/sound.d.ts
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
export interface SoundOptions {
|
|
2
|
-
/** Initial linear gain, in the Web Audio gain scale. */
|
|
2
|
+
/** Initial linear gain, in the Web Audio gain scale. Example: `createSound(buffer, audioContext, { volume: 0.5 })`. */
|
|
3
3
|
volume?: number;
|
|
4
|
-
/** Upper bound for gain values produced by amplify(); defaults to 4. */
|
|
4
|
+
/** Upper bound for gain values produced by amplify(); defaults to 4. Example: `createSound(buffer, audioContext, { maxGain: 3 })`. */
|
|
5
5
|
maxGain?: number;
|
|
6
6
|
}
|
|
7
7
|
export interface PlaybackOptions {
|
|
8
|
+
/** Whether the playback repeats. Example: `sound.startPlaying({ loop: true })`. */
|
|
8
9
|
loop?: boolean;
|
|
10
|
+
/** Playback rate multiplier. Example: `sound.startPlaying({ playbackRate: 2 })`. */
|
|
9
11
|
playbackRate?: number;
|
|
10
12
|
}
|
|
11
13
|
export interface Playback {
|
|
14
|
+
/** Web Audio source for this playback. */
|
|
12
15
|
readonly source: AudioBufferSourceNode;
|
|
16
|
+
/** Whether this playback has been stopped. */
|
|
13
17
|
readonly stopped: boolean;
|
|
14
|
-
|
|
18
|
+
/** Multiply playback volume up to its configured maximum. */
|
|
19
|
+
amplify(multiplier: number | "max"): this;
|
|
20
|
+
/** Stop this playback. */
|
|
15
21
|
stop(): this;
|
|
16
22
|
}
|
|
17
23
|
export interface Sound {
|
|
24
|
+
/** Start playback and return its controls. */
|
|
18
25
|
startPlaying(options?: PlaybackOptions): Playback;
|
|
19
26
|
}
|
|
20
27
|
/** Create a reusable sound backed by a Web Audio AudioBuffer. */
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Project } from "../woof.js";
|
|
2
|
+
import type { Sprite, SpriteOptions } from "./sprite.js";
|
|
3
|
+
/** Build a project-bound constructor from an upstream-style render definition. */
|
|
4
|
+
export declare function createCustomSprite<T extends {
|
|
5
|
+
render: (ctx: CanvasRenderingContext2D) => void;
|
|
6
|
+
}>(project: Project, definition: T & ThisType<Sprite & T>): {
|
|
7
|
+
(options?: SpriteOptions): Sprite & T;
|
|
8
|
+
new (options?: SpriteOptions): Sprite & T;
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Easing curves that tween accepts by name. Example: `sprite.tween({ x: 10 }, 1, 'easeOut')`. */
|
|
2
|
+
export declare const easings: {
|
|
3
|
+
linear: (t: number) => number;
|
|
4
|
+
easeIn: (t: number) => number;
|
|
5
|
+
easeOut: (t: number) => number;
|
|
6
|
+
easeInOut: (t: number) => number;
|
|
7
|
+
};
|
|
8
|
+
/** The name of a built-in easing curve. Example: `const name: EasingName = 'easeIn'`. */
|
|
9
|
+
export type EasingName = keyof typeof easings;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Collider } from "../collision.js";
|
|
2
|
+
import type { Sprite } from "./sprite.js";
|
|
3
|
+
/** Construct the collision geometry for a sprite in world coordinates. */
|
|
4
|
+
export declare function spriteCollider(sprite: Sprite): Collider;
|
|
5
|
+
/** Compute the axis-aligned world bounds of a sprite. */
|
|
6
|
+
export declare function spriteBounds(sprite: Sprite): {
|
|
7
|
+
left: number;
|
|
8
|
+
right: number;
|
|
9
|
+
top: number;
|
|
10
|
+
bottom: number;
|
|
11
|
+
};
|
|
12
|
+
/** Test visible sprites, descending into bundles when necessary. */
|
|
13
|
+
export declare function spritesTouching(sprite: Sprite, other: Sprite, precise: boolean): boolean;
|
|
14
|
+
/** Test a sprite against a world-space rectangle. */
|
|
15
|
+
export declare function spriteOverlaps(sprite: Sprite, bounds: {
|
|
16
|
+
left: number;
|
|
17
|
+
right: number;
|
|
18
|
+
top: number;
|
|
19
|
+
bottom: number;
|
|
20
|
+
}): boolean;
|
|
21
|
+
/** Test a point in world coordinates against a visible sprite. */
|
|
22
|
+
export declare function spriteOver(sprite: Sprite, x: number, y: number): boolean;
|