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.
- package/CHANGELOG.md +13 -0
- package/README.md +16 -92
- package/THIRD_PARTY_NOTICES.md +7 -0
- package/dist/canvas.js +4 -31
- package/dist/collision.js +10 -0
- package/dist/game.js +4 -71
- 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 -188
- package/dist/input.js +6 -0
- 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 +71 -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 -8
- package/dist/types/input.d.ts +11 -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 +33 -8
- package/docs/architecture.md +0 -37
- package/docs/planning.md +0 -32
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { ProjectSound, type ProjectSoundOptions } from "./world/audio.js";
|
|
2
|
+
import { Repeat, RepeatUntil, type TaskHandle, type RepeatHandle, type Time } from "./world/scheduler.js";
|
|
3
|
+
import * as helpers from "./helpers.js";
|
|
4
|
+
import { Sprite, type SpriteOptions, type SpriteType } from "./sprites/sprite.js";
|
|
5
|
+
import type { Camera, ProjectOptions } from "./world/options.js";
|
|
6
|
+
export type { Camera, ProjectOptions } from "./world/options.js";
|
|
7
|
+
type StorageValue<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
|
|
8
|
+
/** An independent canvas and deterministic game-time scheduler. */
|
|
9
|
+
export declare class Project {
|
|
10
|
+
/** Canvas element used to render this project. Example: `project.canvas`. */
|
|
11
|
+
readonly canvas: HTMLCanvasElement;
|
|
12
|
+
/** Canvas drawing context for this project. Example: `project.context`. */
|
|
13
|
+
readonly context: CanvasRenderingContext2D;
|
|
14
|
+
/** Standalone utility functions available to the project. Example: `project.helpers`. */
|
|
15
|
+
readonly helpers: typeof helpers;
|
|
16
|
+
/** Choose a random RGB color. Example: `project.randomColor`. */
|
|
17
|
+
readonly randomColor: typeof helpers.randomColor;
|
|
18
|
+
/** Create a range of numbers. Example: `project.range`. */
|
|
19
|
+
readonly range: typeof helpers.range;
|
|
20
|
+
/** Test whether a number lies strictly between two bounds. Example: `project.between`. */
|
|
21
|
+
readonly between: typeof helpers.between;
|
|
22
|
+
/** Remove matching items from an array. Example: `project.remove`. */
|
|
23
|
+
readonly remove: typeof helpers.remove;
|
|
24
|
+
/** Format an RGB color string. Example: `project.rgb`. */
|
|
25
|
+
readonly rgb: typeof helpers.rgb;
|
|
26
|
+
/** Choose an item from an array. Example: `project.pick`. */
|
|
27
|
+
readonly pick: typeof helpers.pick;
|
|
28
|
+
/** Return a shuffled copy of an array. Example: `project.shuffle`. */
|
|
29
|
+
readonly shuffle: typeof helpers.shuffle;
|
|
30
|
+
/** Restrict a number to an inclusive interval. Example: `project.clamp`. */
|
|
31
|
+
readonly clamp: typeof helpers.clamp;
|
|
32
|
+
/** Linearly interpolate between two numbers. Example: `project.lerp`. */
|
|
33
|
+
readonly lerp: typeof helpers.lerp;
|
|
34
|
+
/** Measure distance between two points. Example: `project.distance`. */
|
|
35
|
+
readonly distance: typeof helpers.distance;
|
|
36
|
+
/** Direction pointing right, in degrees. Example: `project.RIGHT`. */
|
|
37
|
+
readonly RIGHT = 0;
|
|
38
|
+
/** Direction pointing left, in degrees. Example: `project.LEFT`. */
|
|
39
|
+
readonly LEFT = 180;
|
|
40
|
+
/** Direction pointing up, in degrees. Example: `project.UP`. */
|
|
41
|
+
readonly UP = 90;
|
|
42
|
+
/** Direction pointing down, in degrees. Example: `project.DOWN`. */
|
|
43
|
+
readonly DOWN = 270;
|
|
44
|
+
/** Calculate the sine of an angle in degrees. Example: `project.sin`. */
|
|
45
|
+
readonly sin: typeof helpers.sin;
|
|
46
|
+
/** Calculate the cosine of an angle in degrees. Example: `project.cos`. */
|
|
47
|
+
readonly cos: typeof helpers.cos;
|
|
48
|
+
/** Calculate the tangent of an angle in degrees. Example: `project.tan`. */
|
|
49
|
+
readonly tan: typeof helpers.tan;
|
|
50
|
+
/** Calculate an inverse sine in degrees. Example: `project.asin`. */
|
|
51
|
+
readonly asin: typeof helpers.asin;
|
|
52
|
+
/** Calculate an inverse cosine in degrees. Example: `project.acos`. */
|
|
53
|
+
readonly acos: typeof helpers.acos;
|
|
54
|
+
/** Calculate an inverse tangent in degrees. Example: `project.atan`. */
|
|
55
|
+
readonly atan: typeof helpers.atan;
|
|
56
|
+
/** Calculate a square root. Example: `project.sqrt`. */
|
|
57
|
+
readonly sqrt: (x: number) => number;
|
|
58
|
+
/** Calculate an absolute value. Example: `project.abs`. */
|
|
59
|
+
readonly abs: (x: number) => number;
|
|
60
|
+
/** Round down to an integer. Example: `project.floor`. */
|
|
61
|
+
readonly floor: (x: number) => number;
|
|
62
|
+
/** Round up to an integer. Example: `project.ceiling`. */
|
|
63
|
+
readonly ceiling: (x: number) => number;
|
|
64
|
+
/** Calculate a natural logarithm. Example: `project.ln`. */
|
|
65
|
+
readonly ln: (x: number) => number;
|
|
66
|
+
/** Calculate a base-ten logarithm. Example: `project.log`. */
|
|
67
|
+
readonly log: (x: number) => number;
|
|
68
|
+
/** Raise a number to a power. Example: `project.pow`. */
|
|
69
|
+
readonly pow: (x: number, y: number) => number;
|
|
70
|
+
/** Current local hour in twelve-hour time. Example: `project.hour`. */
|
|
71
|
+
readonly hour: typeof helpers.hour;
|
|
72
|
+
/** Current local hour in twenty-four-hour time. Example: `project.hourMilitary`. */
|
|
73
|
+
readonly hourMilitary: typeof helpers.hourMilitary;
|
|
74
|
+
/** Current local minute. Example: `project.minute`. */
|
|
75
|
+
readonly minute: typeof helpers.minute;
|
|
76
|
+
/** Current local second. Example: `project.second`. */
|
|
77
|
+
readonly second: typeof helpers.second;
|
|
78
|
+
/** Current local day of the month. Example: `project.dayOfMonth`. */
|
|
79
|
+
readonly dayOfMonth: typeof helpers.dayOfMonth;
|
|
80
|
+
/** Current local weekday name. Example: `project.dayOfWeek`. */
|
|
81
|
+
readonly dayOfWeek: typeof helpers.dayOfWeek;
|
|
82
|
+
/** Current local month name. Example: `project.month`. */
|
|
83
|
+
readonly month: typeof helpers.month;
|
|
84
|
+
/** Current local year. Example: `project.year`. */
|
|
85
|
+
readonly year: typeof helpers.year;
|
|
86
|
+
/** Test whether the user agent resembles a mobile device. Example: `project.mobile`. */
|
|
87
|
+
readonly mobile: typeof helpers.mobile;
|
|
88
|
+
/** Load external scripts in order and return a promise. Example: `project.importCodeURL`. */
|
|
89
|
+
readonly importCodeURL: typeof helpers.importCodeURL;
|
|
90
|
+
/** Construct a manually advanced repetition. Example: `new project.Repeat(3, fn)`. */
|
|
91
|
+
readonly Repeat: typeof Repeat;
|
|
92
|
+
/** Construct a conditional repetition. Example: `new project.RepeatUntil(done, fn)`. */
|
|
93
|
+
readonly RepeatUntil: typeof RepeatUntil;
|
|
94
|
+
/** Convert a key code. Example: `project.keyCodeToString(38)`. */
|
|
95
|
+
readonly keyCodeToString: typeof helpers.keyCodeToString;
|
|
96
|
+
/** Convert a duration to milliseconds. Example: `project.unitsToMiliseconds(2, 'seconds')`. */
|
|
97
|
+
readonly unitsToMiliseconds: typeof helpers.unitsToMiliseconds;
|
|
98
|
+
/** Month names from January to December. Example: `project.months[0]`. */
|
|
99
|
+
readonly months: readonly string[];
|
|
100
|
+
/** Limit callback frequency. Example: `project.throttle(fn, 500)`. */
|
|
101
|
+
readonly throttle: typeof helpers.throttle;
|
|
102
|
+
/** Fetch JSON. Example: `await project.getData('/scores.json')`. */
|
|
103
|
+
readonly getData: typeof helpers.getData;
|
|
104
|
+
/** Copy enumerable properties. Example: `project.extend({}, { x: 1 })`. */
|
|
105
|
+
readonly extend: typeof helpers.extend;
|
|
106
|
+
/** Width of the world in canvas pixels. Example: `project.width`. */
|
|
107
|
+
width: number;
|
|
108
|
+
/** Height of the world in canvas pixels. Example: `project.height`. */
|
|
109
|
+
height: number;
|
|
110
|
+
/** Whether the project fills its browser window. Example: `project.fullScreen`. */
|
|
111
|
+
fullScreen: boolean;
|
|
112
|
+
/** Whether this project installed its beginner API on its window. Example: `project.global`. */
|
|
113
|
+
readonly global: boolean;
|
|
114
|
+
private restoreGlobals;
|
|
115
|
+
/** Current backdrop settings. Example: `project.backdrop.color`. */
|
|
116
|
+
readonly backdrop: {
|
|
117
|
+
/** Current solid color, if one has been set. Example: `project.backdrop.color`. */
|
|
118
|
+
color: string | null;
|
|
119
|
+
/** Kind of backdrop currently displayed. Example: `project.backdrop.type`. */
|
|
120
|
+
type: "color" | "url" | null;
|
|
121
|
+
/** Current image URL, if one has been set. Example: `project.backdrop.url`. */
|
|
122
|
+
url: string | null;
|
|
123
|
+
/** CSS background size. Example: `project.backdrop.size`. */
|
|
124
|
+
size: string;
|
|
125
|
+
/** CSS background repeat mode. Example: `project.backdrop.repeat`. */
|
|
126
|
+
repeat: string;
|
|
127
|
+
};
|
|
128
|
+
/** Horizontal world coordinate at the centre of the view. Example: `project.cameraX`. */
|
|
129
|
+
cameraX: number;
|
|
130
|
+
/** Vertical world coordinate at the centre of the view. Example: `project.cameraY`. */
|
|
131
|
+
cameraY: number;
|
|
132
|
+
/** Sprites currently owned by this project. Example: `project.sprites`. */
|
|
133
|
+
readonly sprites: Sprite[];
|
|
134
|
+
/** Pointer x coordinate in world units. Example: `project.mouseX`. */
|
|
135
|
+
mouseX: number;
|
|
136
|
+
/** Pointer y coordinate in world units. Example: `project.mouseY`. */
|
|
137
|
+
mouseY: number;
|
|
138
|
+
/** Pointer x coordinate on the previous frame. Example: `project.pMouseX`. */
|
|
139
|
+
pMouseX: number;
|
|
140
|
+
/** Pointer y coordinate on the previous frame. Example: `project.pMouseY`. */
|
|
141
|
+
pMouseY: number;
|
|
142
|
+
/** Pointer horizontal displacement since the previous frame. Example: `project.mouseSpeedX`. */
|
|
143
|
+
mouseSpeedX: number;
|
|
144
|
+
/** Pointer vertical displacement since the previous frame. Example: `project.mouseSpeedY`. */
|
|
145
|
+
mouseSpeedY: number;
|
|
146
|
+
/** Whether a pointer is currently pressed. Example: `project.mouseDown`. */
|
|
147
|
+
mouseDown: boolean;
|
|
148
|
+
/** Set of currently held normalized key names. Example: `project.keysDown`. */
|
|
149
|
+
readonly keysDown: Set<string> & {
|
|
150
|
+
/** Check a held key using WoofJS names and aliases. Example: `project.keysDown.includes('UP')`. */
|
|
151
|
+
includes: (key: string) => boolean;
|
|
152
|
+
};
|
|
153
|
+
private pendingKeys;
|
|
154
|
+
private frameKeys;
|
|
155
|
+
private listeners;
|
|
156
|
+
private keyDownHandlers;
|
|
157
|
+
private keyUpHandlers;
|
|
158
|
+
private pointerDownHandlers;
|
|
159
|
+
private pointerUpHandlers;
|
|
160
|
+
private pointerMoveHandlers;
|
|
161
|
+
private pen;
|
|
162
|
+
private readonly doc;
|
|
163
|
+
private readonly win?;
|
|
164
|
+
private readonly reportError;
|
|
165
|
+
private readonly scheduler;
|
|
166
|
+
private readonly requestFrame?;
|
|
167
|
+
private readonly cancelFrame?;
|
|
168
|
+
private readonly maxDeltaMs;
|
|
169
|
+
private frame;
|
|
170
|
+
private previous;
|
|
171
|
+
private elapsed;
|
|
172
|
+
private timerEpoch;
|
|
173
|
+
private running;
|
|
174
|
+
private frozen;
|
|
175
|
+
private freezePending;
|
|
176
|
+
private destroyed;
|
|
177
|
+
private scenes;
|
|
178
|
+
private sounds;
|
|
179
|
+
private audio?;
|
|
180
|
+
private master?;
|
|
181
|
+
private volume;
|
|
182
|
+
private muted;
|
|
183
|
+
private audioFactory?;
|
|
184
|
+
private fetcher;
|
|
185
|
+
private storage?;
|
|
186
|
+
private randomState?;
|
|
187
|
+
private following?;
|
|
188
|
+
private shakeTime;
|
|
189
|
+
private shakeStrength;
|
|
190
|
+
/** Control camera following and shake. Example: `project.camera`. */
|
|
191
|
+
readonly camera: Camera;
|
|
192
|
+
constructor(options?: ProjectOptions);
|
|
193
|
+
/** Left edge of the visible world. Example: `project.minX`. */
|
|
194
|
+
get minX(): number;
|
|
195
|
+
/** Right edge of the visible world. Example: `project.maxX`. */
|
|
196
|
+
get maxX(): number;
|
|
197
|
+
/** Bottom edge of the visible world. Example: `project.minY`. */
|
|
198
|
+
get minY(): number;
|
|
199
|
+
/** Top edge of the visible world. Example: `project.maxY`. */
|
|
200
|
+
get maxY(): number;
|
|
201
|
+
/** Return the visible world edges. Example: `project.bounds()`. */
|
|
202
|
+
bounds(): {
|
|
203
|
+
left: number;
|
|
204
|
+
right: number;
|
|
205
|
+
top: number;
|
|
206
|
+
bottom: number;
|
|
207
|
+
};
|
|
208
|
+
/** Return a fractional random value. Example: `project.randomFloat(0, 1)`. */
|
|
209
|
+
randomFloat(a: number, b: number): number;
|
|
210
|
+
/** Return a whole number between inclusive bounds. Example: `project.random(1, 6)`. */
|
|
211
|
+
random(a: number, b: number): number;
|
|
212
|
+
/** Choose an integer x coordinate within the visible world. Example: `project.randomX()`. */
|
|
213
|
+
randomX(): number;
|
|
214
|
+
/** Choose an integer y coordinate within the visible world. Example: `project.randomY()`. */
|
|
215
|
+
randomY(): number;
|
|
216
|
+
/** Freeze completes synchronously, so no delayed freeze is pending. Example: `project.freezing`. */
|
|
217
|
+
get freezing(): boolean;
|
|
218
|
+
/** The wall-clock epoch corresponding to the last timer reset. Example: `project.woofEpoch`. */
|
|
219
|
+
get woofEpoch(): Date;
|
|
220
|
+
/** The currently requested animation frame, if one is pending. Example: `project.renderInterval`. */
|
|
221
|
+
get renderInterval(): number | undefined;
|
|
222
|
+
/** True when animation is stopped or game time is frozen. Example: `project.stopped`. */
|
|
223
|
+
get stopped(): boolean;
|
|
224
|
+
/** Run after project initialization, which is synchronous. Example: `project.ready(startGame)`. */
|
|
225
|
+
ready(fn: () => void): void;
|
|
226
|
+
/** Convert world coordinates to canvas coordinates. Example: `project.translateToCanvas(0, 0)`. */
|
|
227
|
+
translateToCanvas(x: number, y: number): [number, number];
|
|
228
|
+
/** Convert canvas coordinates to world coordinates. Example: `project.translateToCenter(100, 100)`. */
|
|
229
|
+
translateToCenter(x: number, y: number): [number, number];
|
|
230
|
+
/** Return the visible world bounds as a polygon collider. Example: `project.collider()`. */
|
|
231
|
+
collider(): import("./collision.js").Collider;
|
|
232
|
+
/** Set the backdrop to a CSS color. Example: `project.setBackdropColor('blue')`. */
|
|
233
|
+
setBackdropColor(color: string): this;
|
|
234
|
+
/** Set the backdrop to an image URL. Example: `project.setBackdropURL('sky.png')`. */
|
|
235
|
+
setBackdropURL(url: string): this;
|
|
236
|
+
/** Resize the canvas or set CSS background dimensions. Example: `project.setBackdropSize(400, 300)`. */
|
|
237
|
+
setBackdropSize(width: number | string, height?: number | string): this;
|
|
238
|
+
/** Set the CSS background size. Example: `project.setBackdropStyle('cover')`. */
|
|
239
|
+
setBackdropStyle(style: string): this;
|
|
240
|
+
/** Set how the backdrop image repeats. Example: `project.setBackdropRepeat('repeat')`. */
|
|
241
|
+
setBackdropRepeat(repeat: string): this;
|
|
242
|
+
/** Register a named scene setup callback. Example: `project.scene('menu', showMenu)`. */
|
|
243
|
+
scene<T = unknown>(name: string, setup: (data: T) => void): this;
|
|
244
|
+
/** Clean up the current scene and run a named scene. Example: `project.go('menu')`. */
|
|
245
|
+
go(name: string, data?: unknown): this;
|
|
246
|
+
/** Save JSON data in namespaced browser storage. Example: `project.save('score', 10)`. */
|
|
247
|
+
save(key: string, value: unknown): this;
|
|
248
|
+
/** Load a saved value, using the fallback if storage is unavailable. Example: `project.load('score', 0)`. */
|
|
249
|
+
load<T>(key: string, fallback: T): StorageValue<T>;
|
|
250
|
+
private audioContext;
|
|
251
|
+
private audioDestination;
|
|
252
|
+
/** Volume multiplier for all project audio. Example: `project.masterVolume`. */
|
|
253
|
+
get masterVolume(): number;
|
|
254
|
+
set masterVolume(value: number);
|
|
255
|
+
/** Mute project audio. Example: `project.mute()`. */
|
|
256
|
+
mute(): this;
|
|
257
|
+
/** Restore project audio volume. Example: `project.unmute()`. */
|
|
258
|
+
unmute(): this;
|
|
259
|
+
/** Create a sound owned by this project. Example: `project.sound({ url: 'bell.wav' })`. */
|
|
260
|
+
sound(options?: ProjectSoundOptions): ProjectSound;
|
|
261
|
+
/** Play an oscillator at a frequency for a duration in seconds. Example: `project.tone(440, 0.2)`. */
|
|
262
|
+
tone(freq: number, seconds: number): this;
|
|
263
|
+
/** Play a short 440 Hz tone. Example: `project.beep()`. */
|
|
264
|
+
beep(): this;
|
|
265
|
+
/** Run a callback for a fixed number of frames. Example: `project.repeat(3, () => player.move(1))`. */
|
|
266
|
+
repeat(times: number, fn: (iteration: number) => void, after?: (count: number) => void): RepeatHandle;
|
|
267
|
+
/** Run a callback each frame until a condition becomes true. Example: `project.repeatUntil(() => done, tick)`. */
|
|
268
|
+
repeatUntil(condition: () => boolean, fn: (iteration: number) => void, after?: (count: number) => void): TaskHandle;
|
|
269
|
+
/** Create moving sprites that expire after a given lifetime. Example: `project.particles({ count: 5 })`. */
|
|
270
|
+
particles(options?: {
|
|
271
|
+
/** Horizontal origin in world units. Example: `particles({ x: 20 })`. */
|
|
272
|
+
x?: number;
|
|
273
|
+
/** Vertical origin in world units. Example: `particles({ y: 20 })`. */
|
|
274
|
+
y?: number;
|
|
275
|
+
/** Number of particles to create. Example: `particles({ count: 5 })`. */
|
|
276
|
+
count?: number;
|
|
277
|
+
/** Initial speed in world units per second. Example: `particles({ speed: 50 })`. */
|
|
278
|
+
speed?: number;
|
|
279
|
+
/** Lifetime in seconds. Example: `particles({ life: 2 })`. */
|
|
280
|
+
life?: number;
|
|
281
|
+
/** Fill color. Example: `particles({ color: 'orange' })`. */
|
|
282
|
+
color?: string;
|
|
283
|
+
/** Radius in world units. Example: `particles({ radius: 3 })`. */
|
|
284
|
+
radius?: number;
|
|
285
|
+
}): Sprite[];
|
|
286
|
+
/** Return elapsed game time in seconds. Example: `project.timer()`. */
|
|
287
|
+
timer(): number;
|
|
288
|
+
/** Reset the game-time timer to zero. Example: `project.resetTimer()`. */
|
|
289
|
+
resetTimer(): this;
|
|
290
|
+
/** Run a callback after a game-time delay. Example: `project.after(2, 'seconds', spawn)`. */
|
|
291
|
+
after(time: Time, fn: () => void): TaskHandle;
|
|
292
|
+
/** Run a callback after a game-time delay. Example: `project.after(2, 'seconds', spawn)`. */
|
|
293
|
+
after(time: Time, units: string, fn: () => void): TaskHandle;
|
|
294
|
+
/** Run a callback immediately and at each game-time interval. Example: `project.every(2, 'seconds', spawn)`. */
|
|
295
|
+
every(time: Time, fn: () => void): TaskHandle;
|
|
296
|
+
/** Run a callback immediately and at each game-time interval. Example: `project.every(2, 'seconds', spawn)`. */
|
|
297
|
+
every(time: Time, units: string, fn: () => void): TaskHandle;
|
|
298
|
+
/** Run a callback once per frame until stopped. Example: `project.forever(() => player.move(1))`. */
|
|
299
|
+
forever(fn: () => void): TaskHandle;
|
|
300
|
+
/** Run a callback on every frame while a condition is true. Example: `project.when(() => player.x > 10, win)`. */
|
|
301
|
+
when(condition: () => boolean, fn: () => void): TaskHandle;
|
|
302
|
+
/** Run a callback once when a condition becomes true. Example: `project.once(() => done, finish)`. */
|
|
303
|
+
once(condition: () => boolean, fn: () => void): TaskHandle;
|
|
304
|
+
/** Return a promise resolved after game-time seconds. Example: `await project.wait(1)`. */
|
|
305
|
+
wait(seconds: number): Promise<void>;
|
|
306
|
+
/** Pause automatic game-time advancement. Example: `project.freeze()`. */
|
|
307
|
+
freeze(): this;
|
|
308
|
+
/** Resume automatic game-time advancement. Example: `project.defrost()`. */
|
|
309
|
+
defrost(): this;
|
|
310
|
+
/** Advance game time explicitly, including while frozen. Example: `project.step(16)`. */
|
|
311
|
+
step(ms: number): this;
|
|
312
|
+
private tick;
|
|
313
|
+
/** Start requesting animation frames. Example: `project.start()`. */
|
|
314
|
+
start(): this;
|
|
315
|
+
/** Stop requesting animation frames. Example: `project.stop()`. */
|
|
316
|
+
stop(): this;
|
|
317
|
+
/** Run a callback and report any exception to the project error handler. Example: `project.invoke(callback)`. */
|
|
318
|
+
invoke(fn: () => void): void;
|
|
319
|
+
/** Create an image element using the project document. Example: `project.createImage()`. */
|
|
320
|
+
createImage(): HTMLImageElement | undefined;
|
|
321
|
+
/** Create and register a sprite of the given type. Example: `project.createSprite('circle', { radius: 10 })`. */
|
|
322
|
+
createSprite(type: SpriteType, options?: SpriteOptions): Sprite;
|
|
323
|
+
/** Define a custom sprite constructor. Example: `const Star = project.customSprite({ render() {} });`. */
|
|
324
|
+
customSprite<T extends {
|
|
325
|
+
render: (ctx: CanvasRenderingContext2D) => void;
|
|
326
|
+
}>(definition: T & ThisType<Sprite & T>): {
|
|
327
|
+
(options?: SpriteOptions): Sprite & T;
|
|
328
|
+
new (options?: SpriteOptions): Sprite & T;
|
|
329
|
+
};
|
|
330
|
+
/** Create a circle sprite in this project. Example: `project.circle({ radius: 10 })`. */
|
|
331
|
+
circle(o?: SpriteOptions): Sprite;
|
|
332
|
+
/** Create a rectangle sprite in this project. Example: `project.rectangle({ width: 20, height: 10 })`. */
|
|
333
|
+
rectangle(o?: SpriteOptions): Sprite;
|
|
334
|
+
/** Create an oval sprite in this project. Example: `project.oval({ width: 20, height: 40 })`. */
|
|
335
|
+
oval(o?: SpriteOptions): Sprite;
|
|
336
|
+
/** Create a regular polygon sprite in this project. Example: `project.polygon({ sides: 5, length: 20 })`. */
|
|
337
|
+
polygon(o?: SpriteOptions): Sprite;
|
|
338
|
+
/** Create a line sprite in this project. Example: `project.line({ x1: 10, y1: 20 })`. */
|
|
339
|
+
line(o?: SpriteOptions): Sprite;
|
|
340
|
+
/** Create a text sprite in this project. Example: `project.text({ text: 'Score' })`. */
|
|
341
|
+
text(o?: SpriteOptions): Sprite;
|
|
342
|
+
/** Create an image sprite in this project. Example: `project.image({ url: 'player.png' })`. */
|
|
343
|
+
image(o?: SpriteOptions): Sprite;
|
|
344
|
+
/** Create a group of sprites in this project. Example: `project.bundle({ components: [player] })`. */
|
|
345
|
+
bundle(o?: SpriteOptions): Sprite;
|
|
346
|
+
/** Create a sprite with a custom draw callback. Example: `project.sprite({ draw: ctx => ctx.fillRect(0, 0, 5, 5) })`. */
|
|
347
|
+
sprite(o?: SpriteOptions): Sprite;
|
|
348
|
+
/** Remove a sprite from the project registry. Example: `project.removeSprite(player)`. */
|
|
349
|
+
removeSprite(sprite: Sprite): void;
|
|
350
|
+
/** Find all sprites carrying a tag. Example: `project.find('enemy')`. */
|
|
351
|
+
find(tag: string): Sprite[];
|
|
352
|
+
/** Register a callback receiving elapsed seconds each frame. Example: `project.onUpdate(dt => player.move(dt * 10))`. */
|
|
353
|
+
onUpdate(fn: (dt: number) => void): TaskHandle;
|
|
354
|
+
/** Test pixel overlap between two sprites using a canvas. Example: `project.preciseCollision(player, enemy)`. */
|
|
355
|
+
preciseCollision(a: Sprite, b: Sprite): boolean;
|
|
356
|
+
/** Record a pen segment when the pen is active. Example: `project.trackPen(0, 0, 1, 1, true, 'black', 1)`. */
|
|
357
|
+
trackPen(x1: number, y1: number, x2: number, y2: number, active: boolean, color: string, width: number): void;
|
|
358
|
+
/** Erase all recorded pen strokes. Example: `project.clearPen()`. */
|
|
359
|
+
clearPen(): this;
|
|
360
|
+
private render;
|
|
361
|
+
/** Test whether a named key is currently held. Example: `project.key('UP')`. */
|
|
362
|
+
key(key: string): boolean;
|
|
363
|
+
/** Test whether a key was pressed during this frame. Example: `project.keyPressed('SPACE')`. */
|
|
364
|
+
keyPressed(key: string): boolean;
|
|
365
|
+
/** Register a key-down callback and return an unsubscribe function. Example: `project.onKeyDown(key => console.log(key))`. */
|
|
366
|
+
onKeyDown(fn: (key: string) => void): () => void;
|
|
367
|
+
/** Register a key-up callback and return an unsubscribe function. Example: `project.onKeyUp(key => console.log(key))`. */
|
|
368
|
+
onKeyUp(fn: (key: string) => void): () => void;
|
|
369
|
+
/** Register a pointer-down callback and return an unsubscribe function. Example: `project.onMouseDown((x, y) => console.log(x, y))`. */
|
|
370
|
+
onMouseDown(fn: (x: number, y: number) => void): () => void;
|
|
371
|
+
/** Register a pointer-up callback and return an unsubscribe function. Example: `project.onMouseUp((x, y) => console.log(x, y))`. */
|
|
372
|
+
onMouseUp(fn: (x: number, y: number) => void): () => void;
|
|
373
|
+
/** Register a pointer-move callback and return an unsubscribe function. Example: `project.onMouseMove((x, y) => player.setPosition(x, y))`. */
|
|
374
|
+
onMouseMove(fn: (x: number, y: number) => void): () => void;
|
|
375
|
+
/** Horizontal pointer displacement since the previous frame. Example: `project.mouseXSpeed`. */
|
|
376
|
+
get mouseXSpeed(): number;
|
|
377
|
+
/** Vertical pointer displacement since the previous frame. Example: `project.mouseYSpeed`. */
|
|
378
|
+
get mouseYSpeed(): number;
|
|
379
|
+
/** Return the gamepad at an index, if connected. Example: `project.gamepad(0)`. */
|
|
380
|
+
gamepad(index?: number): Gamepad | null;
|
|
381
|
+
private installInput;
|
|
382
|
+
/** Remove the canvas, sprites, scheduled tasks and listeners. Example: `project.destroy()`. */
|
|
383
|
+
destroy(): void;
|
|
384
|
+
}
|
|
385
|
+
/** Create an independent project. Example: `const game = woof({ autoStart: false })`. */
|
|
386
|
+
export declare function woof(options?: ProjectOptions): Project;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { TaskHandle } from "./scheduler.js";
|
|
2
|
+
export interface ProjectSoundOptions {
|
|
3
|
+
/** URL of the sound to load on first playback. Example: `project.sound({ url: 'bell.wav' })`. */
|
|
4
|
+
url?: string;
|
|
5
|
+
/** Decoded audio buffer to play without fetching. Example: `project.sound({ buffer: audioBuffer })`. */
|
|
6
|
+
buffer?: AudioBuffer;
|
|
7
|
+
/** Whether playback repeats. Example: `project.sound({ loop: true })`. */
|
|
8
|
+
loop?: boolean;
|
|
9
|
+
/** Base volume from zero to one, or a named preset. Example: `project.sound({ volume: 0.5 })`. */
|
|
10
|
+
volume?: number | "mute" | "low" | "normal";
|
|
11
|
+
/** Playback rate multiplier, or a named preset. Example: `project.sound({ speed: 'fast' })`. */
|
|
12
|
+
speed?: number | "slow" | "normal" | "fast";
|
|
13
|
+
/** Maximum amplified gain. Example: `project.sound({ maxGain: 3 })`. */
|
|
14
|
+
maxGain?: number;
|
|
15
|
+
/** Whether to use a compressor when available. Example: `project.sound({ limiter: true })`. */
|
|
16
|
+
limiter?: boolean;
|
|
17
|
+
}
|
|
18
|
+
declare const volumes: {
|
|
19
|
+
mute: number;
|
|
20
|
+
low: number;
|
|
21
|
+
normal: number;
|
|
22
|
+
};
|
|
23
|
+
declare const speeds: {
|
|
24
|
+
slow: number;
|
|
25
|
+
normal: number;
|
|
26
|
+
fast: number;
|
|
27
|
+
};
|
|
28
|
+
/** A reusable project sound. Example: `project.sound({ url: 'bell.wav' }).startPlaying()`. */
|
|
29
|
+
export declare class ProjectSound {
|
|
30
|
+
private readonly context;
|
|
31
|
+
private readonly destination;
|
|
32
|
+
private readonly fetcher;
|
|
33
|
+
private readonly onUpdate?;
|
|
34
|
+
/** Maximum amplified gain. Example: `sound.maxGain`. */
|
|
35
|
+
readonly maxGain: number;
|
|
36
|
+
/** Whether to use a compressor when available. Example: `sound.limiter`. */
|
|
37
|
+
readonly limiter: boolean;
|
|
38
|
+
private loopValue;
|
|
39
|
+
private urlValue?;
|
|
40
|
+
private baseVolume;
|
|
41
|
+
private rate;
|
|
42
|
+
private multiplier;
|
|
43
|
+
private source?;
|
|
44
|
+
private gain?;
|
|
45
|
+
private panNode?;
|
|
46
|
+
private buffer?;
|
|
47
|
+
private loading?;
|
|
48
|
+
private offset;
|
|
49
|
+
private startedAt;
|
|
50
|
+
private played;
|
|
51
|
+
private generation;
|
|
52
|
+
private panValue;
|
|
53
|
+
private fade?;
|
|
54
|
+
/** Preset names accepted by the sound's speed and volume setters. Example: `sound.audioSettings`. */
|
|
55
|
+
readonly audioSettings: {
|
|
56
|
+
/** Named playback-rate presets. */
|
|
57
|
+
speed: {
|
|
58
|
+
/** Half-speed playback. Example: `sound.audioSettings.speed.slow`. */
|
|
59
|
+
slow: number;
|
|
60
|
+
/** Unchanged playback rate. Example: `sound.audioSettings.speed.normal`. */
|
|
61
|
+
normal: number;
|
|
62
|
+
/** Double-speed playback. Example: `sound.audioSettings.speed.fast`. */
|
|
63
|
+
fast: number;
|
|
64
|
+
};
|
|
65
|
+
/** Named volume presets. */
|
|
66
|
+
volume: {
|
|
67
|
+
/** Silent playback. Example: `sound.audioSettings.volume.mute`. */
|
|
68
|
+
mute: number;
|
|
69
|
+
/** Half-volume playback. Example: `sound.audioSettings.volume.low`. */
|
|
70
|
+
low: number;
|
|
71
|
+
/** Full base volume. Example: `sound.audioSettings.volume.normal`. */
|
|
72
|
+
normal: number;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
/** The current Web Audio source, when playback has started. Example: `sound.audio`. */
|
|
76
|
+
get audio(): AudioBufferSourceNode | undefined;
|
|
77
|
+
/** Whether the sound is currently playing. Example: `sound.isPlaying`. */
|
|
78
|
+
isPlaying: boolean;
|
|
79
|
+
constructor(context: () => AudioContext, destination: (ctx: AudioContext) => AudioNode, fetcher: typeof fetch, options?: ProjectSoundOptions, onUpdate?: ((fn: (dt: number) => void) => TaskHandle) | undefined);
|
|
80
|
+
/** Whether playback repeats. Changing this during playback updates the active source. Example: `sound.loop`. */
|
|
81
|
+
get loop(): boolean;
|
|
82
|
+
set loop(value: boolean);
|
|
83
|
+
/** The sound URL. Assigning a new URL resets playback and loads it on the next start. Example: `sound.url`. */
|
|
84
|
+
get url(): string | undefined;
|
|
85
|
+
set url(value: string | undefined);
|
|
86
|
+
/** Base volume from zero to one, or a named preset. Example: `sound.volume`. */
|
|
87
|
+
get volume(): number;
|
|
88
|
+
set volume(v: number | keyof typeof volumes);
|
|
89
|
+
/** Playback rate multiplier, or a named preset. Example: `sound.speed`. */
|
|
90
|
+
get speed(): number;
|
|
91
|
+
set speed(v: number | keyof typeof speeds);
|
|
92
|
+
/** Duration of the decoded sound in seconds. Example: `sound.duration`. */
|
|
93
|
+
get duration(): number;
|
|
94
|
+
/** Playback position in seconds. Example: `sound.currentTime`. */
|
|
95
|
+
get currentTime(): number;
|
|
96
|
+
set currentTime(v: number);
|
|
97
|
+
/** Return whether playback has never started. Example: `sound.neverPlayed()`. */
|
|
98
|
+
neverPlayed(): boolean;
|
|
99
|
+
private applyGain;
|
|
100
|
+
/** Multiply the base volume, limited by maxGain. Example: `sound.amplify(2)`. */
|
|
101
|
+
amplify(n: number | "max"): this;
|
|
102
|
+
private load;
|
|
103
|
+
/** Start or resume playback. Example: `sound.startPlaying()`. */
|
|
104
|
+
startPlaying(): this;
|
|
105
|
+
/** Pause playback and retain the current position. Example: `sound.pausePlaying()`. */
|
|
106
|
+
pausePlaying(): this;
|
|
107
|
+
/** Stop playback and reset the position to zero. Example: `sound.stopPlaying()`. */
|
|
108
|
+
stopPlaying(): this;
|
|
109
|
+
/** Set stereo pan from -1 to 1. Example: `sound.pan(-0.5)`. */
|
|
110
|
+
pan(value: number): this;
|
|
111
|
+
private cancelFade;
|
|
112
|
+
private beginFade;
|
|
113
|
+
/** Fade in over game-time seconds. Example: `sound.fadeIn(1)`. */
|
|
114
|
+
fadeIn(seconds: number): this;
|
|
115
|
+
/** Fade out over game-time seconds. Example: `sound.fadeOut(1)`. */
|
|
116
|
+
fadeOut(seconds: number): this;
|
|
117
|
+
}
|
|
118
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Sprite } from "../sprites/sprite.js";
|
|
2
|
+
/** Camera controls for following sprites and adding a temporary shake. */
|
|
3
|
+
export type Camera = {
|
|
4
|
+
/** Follow a sprite, optionally smoothing the movement. Example: `project.camera.follow(player)`. */
|
|
5
|
+
follow(sprite: Sprite, smoothing?: number): Camera;
|
|
6
|
+
/** Shake the view with a strength and duration in seconds. Example: `project.camera.shake(5, 1)`. */
|
|
7
|
+
shake(strength: number, seconds: number): Camera;
|
|
8
|
+
};
|
|
9
|
+
export interface ProjectOptions {
|
|
10
|
+
/** Width of the world in canvas pixels. Example: `woof({ width: 400 })`. */
|
|
11
|
+
width?: number;
|
|
12
|
+
/** Height of the world in canvas pixels. Example: `woof({ height: 300 })`. */
|
|
13
|
+
height?: number;
|
|
14
|
+
/** Element or selector to contain the project canvas. Example: `woof({ parent: '#game' })`. */
|
|
15
|
+
parent?: Element | string;
|
|
16
|
+
/** Whether the project fills its browser window. Example: `woof({ fullScreen: true })`. */
|
|
17
|
+
fullScreen?: boolean;
|
|
18
|
+
/** Initial CSS color or image URL for the backdrop. Example: `woof({ backdrop: 'blue' })`. */
|
|
19
|
+
backdrop?: string;
|
|
20
|
+
/** Install this project's beginner API on its window. Example: `woof({ global: true })`. */
|
|
21
|
+
global?: boolean;
|
|
22
|
+
/** Whether to start the animation loop immediately. Example: `woof({ autoStart: false })`. */
|
|
23
|
+
autoStart?: boolean;
|
|
24
|
+
/** Document used to create the canvas and image elements. Example: `woof({ document: document })`. */
|
|
25
|
+
document?: Document;
|
|
26
|
+
/** Window used for input and animation events. Example: `woof({ window: window })`. */
|
|
27
|
+
window?: Window;
|
|
28
|
+
/** Replacement animation-frame scheduler, useful for testing. Example: `woof({ requestFrame: requestAnimationFrame })`. */
|
|
29
|
+
requestFrame?: (callback: FrameRequestCallback) => number;
|
|
30
|
+
/** Cancel a previously requested animation frame. Example: `woof({ cancelFrame: cancelAnimationFrame })`. */
|
|
31
|
+
cancelFrame?: (id: number) => void;
|
|
32
|
+
/** Handle exceptions thrown by user callbacks. Example: `woof({ onError: error => console.error(error) })`. */
|
|
33
|
+
onError?: (error: unknown) => void;
|
|
34
|
+
/** Maximum automatic frame delta in milliseconds. Example: `woof({ maxDeltaMs: 50 })`. */
|
|
35
|
+
maxDeltaMs?: number;
|
|
36
|
+
/** Seed for the project random number generator. Example: `woof({ seed: 123 })`. */
|
|
37
|
+
seed?: number;
|
|
38
|
+
/** Audio context or factory used for project sounds. Example: `woof({ audioContext: audioContext })`. */
|
|
39
|
+
audioContext?: AudioContext | (() => AudioContext);
|
|
40
|
+
/** Fetch implementation used to load audio. Example: `woof({ fetch: fetch })`. */
|
|
41
|
+
fetch?: typeof fetch;
|
|
42
|
+
/** Storage implementation used by save and load. Example: `woof({ storage: localStorage })`. */
|
|
43
|
+
storage?: Storage;
|
|
44
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Sprite } from "../sprites/sprite.js";
|
|
2
|
+
import type { Project } from "../woof.js";
|
|
3
|
+
export interface PenSegment {
|
|
4
|
+
x1: number;
|
|
5
|
+
y1: number;
|
|
6
|
+
x2: number;
|
|
7
|
+
y2: number;
|
|
8
|
+
color: string;
|
|
9
|
+
width: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function renderOne(sprite: Sprite, context: CanvasRenderingContext2D): void;
|
|
12
|
+
export declare function renderWorld(project: Project, pen: readonly PenSegment[], shake: number | undefined): void;
|
|
13
|
+
export declare function preciseCollision(project: Project, document: Document, a: Sprite, b: Sprite): boolean;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** A cancellable game-time task. */
|
|
2
|
+
export interface TaskHandle {
|
|
3
|
+
/** Cancel this scheduled task. Example: `handle.stop()`. */
|
|
4
|
+
stop(): void;
|
|
5
|
+
}
|
|
6
|
+
/** A repeat task with a completion callback. */
|
|
7
|
+
export interface RepeatHandle extends TaskHandle {
|
|
8
|
+
/** Run a callback after all repetitions complete. Example: `repeat(3, tick).then(() => console.log("done"))`. */
|
|
9
|
+
then(callback: (count: number) => void): RepeatHandle;
|
|
10
|
+
}
|
|
11
|
+
export type Time = number | string | (() => number | string);
|
|
12
|
+
/** Convert a duration to seconds. For example, `duration('250ms')` is 0.25. */
|
|
13
|
+
export declare function duration(value: Time, units?: string): number;
|
|
14
|
+
/** Deterministic task scheduler; advance it with `step(seconds)`. */
|
|
15
|
+
export declare class Scheduler {
|
|
16
|
+
private readonly onError;
|
|
17
|
+
private tasks;
|
|
18
|
+
constructor(onError?: (error: unknown) => void);
|
|
19
|
+
private add;
|
|
20
|
+
private call;
|
|
21
|
+
/** Run once after game time passes. Example: `after(1, () => {})`. */
|
|
22
|
+
after(time: Time, fn: () => void): TaskHandle;
|
|
23
|
+
/** Run immediately and then at intervals. Example: `every(1, () => {})`. */
|
|
24
|
+
every(time: Time, fn: () => void): TaskHandle;
|
|
25
|
+
/** Run each frame. Example: `forever(() => {})`. */
|
|
26
|
+
forever(fn: () => void): TaskHandle;
|
|
27
|
+
/** Run each frame while the condition is true. Example: `scheduler.when(() => ready, start)`. */
|
|
28
|
+
when(condition: () => boolean, fn: () => void): TaskHandle;
|
|
29
|
+
/** Run once when the condition becomes true. Example: `scheduler.once(() => ready, start)`. */
|
|
30
|
+
once(condition: () => boolean, fn: () => void): TaskHandle;
|
|
31
|
+
/** Advance all tasks once, in seconds. Example: `scheduler.step(0.016)`. */
|
|
32
|
+
step(dt: number): void;
|
|
33
|
+
/** Cancel all pending tasks. Example: `scheduler.clear()`. */
|
|
34
|
+
clear(): void;
|
|
35
|
+
}
|
|
36
|
+
/** A manually advanced repetition. Example: `new Repeat(3, n => console.log(n)).next()`. */
|
|
37
|
+
export declare class Repeat {
|
|
38
|
+
private readonly after?;
|
|
39
|
+
/** Number of repetitions already performed. Example: `repeat.curTimes`. */
|
|
40
|
+
curTimes: number;
|
|
41
|
+
/** Whether the repetition has completed. Example: `repeat.done`. */
|
|
42
|
+
done: boolean;
|
|
43
|
+
/** Number of repetitions to perform. Example: `repeat.times`. */
|
|
44
|
+
readonly times: number;
|
|
45
|
+
/** Callback invoked for each iteration. Example: `repeat.func(1)`. */
|
|
46
|
+
readonly func: (iteration: number) => void;
|
|
47
|
+
constructor(times: number, func: (iteration: number) => void, after?: ((count: number) => void) | undefined);
|
|
48
|
+
/** Advance one iteration or finish. Example: `repeat.next()`. */
|
|
49
|
+
next(): void;
|
|
50
|
+
}
|
|
51
|
+
/** A manually advanced conditional repetition. Example: `new RepeatUntil(done, tick).next()`. */
|
|
52
|
+
export declare class RepeatUntil {
|
|
53
|
+
private readonly after?;
|
|
54
|
+
/** Number of repetitions already performed. Example: `repeat.curTimes`. */
|
|
55
|
+
curTimes: number;
|
|
56
|
+
/** Whether the repetition has completed. Example: `repeat.done`. */
|
|
57
|
+
done: boolean;
|
|
58
|
+
/** Condition that ends the repetition. Example: `repeat.condition()`. */
|
|
59
|
+
readonly condition: () => boolean;
|
|
60
|
+
/** Callback invoked for each iteration. Example: `repeat.func(1)`. */
|
|
61
|
+
readonly func: (iteration: number) => void;
|
|
62
|
+
constructor(condition: () => boolean, func: (iteration: number) => void, after?: ((count: number) => void) | undefined);
|
|
63
|
+
/** Advance one iteration or finish. Example: `repeat.next()`. */
|
|
64
|
+
next(): void;
|
|
65
|
+
}
|