woofjs-engine 0.2.0 → 0.4.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-w4yvg8ax.js +2568 -0
- package/dist/index.js +134 -239
- package/dist/input.js +4 -33
- package/dist/play.js +304 -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 +167 -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 +395 -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 +3620 -0
- package/docs/api.json +5439 -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/play.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import"./index-1jykaz1m.js";
|
|
2
|
+
import {
|
|
3
|
+
clamp2,
|
|
4
|
+
lerp2,
|
|
5
|
+
distance2,
|
|
6
|
+
between2,
|
|
7
|
+
remove2,
|
|
8
|
+
randomFloat2,
|
|
9
|
+
random2,
|
|
10
|
+
pick2,
|
|
11
|
+
shuffle2,
|
|
12
|
+
range2,
|
|
13
|
+
rgb2,
|
|
14
|
+
randomColor2,
|
|
15
|
+
RIGHT2,
|
|
16
|
+
LEFT2,
|
|
17
|
+
UP2,
|
|
18
|
+
DOWN2,
|
|
19
|
+
sin2,
|
|
20
|
+
cos2,
|
|
21
|
+
tan2,
|
|
22
|
+
asin2,
|
|
23
|
+
acos2,
|
|
24
|
+
atan2,
|
|
25
|
+
sqrt2,
|
|
26
|
+
abs2,
|
|
27
|
+
floor2,
|
|
28
|
+
ceiling2,
|
|
29
|
+
ln2,
|
|
30
|
+
log2,
|
|
31
|
+
pow2,
|
|
32
|
+
hourMilitary2,
|
|
33
|
+
hour2,
|
|
34
|
+
minute2,
|
|
35
|
+
second2,
|
|
36
|
+
dayOfMonth2,
|
|
37
|
+
dayOfWeek2,
|
|
38
|
+
month2,
|
|
39
|
+
year2,
|
|
40
|
+
mobile2,
|
|
41
|
+
importCodeURL2,
|
|
42
|
+
extend2,
|
|
43
|
+
months2,
|
|
44
|
+
keyCodeToString2,
|
|
45
|
+
unitsToMiliseconds2,
|
|
46
|
+
throttle2,
|
|
47
|
+
getData2,
|
|
48
|
+
arrayToString2,
|
|
49
|
+
woof2
|
|
50
|
+
} from "./index-w4yvg8ax.js";
|
|
51
|
+
|
|
52
|
+
// src/play.ts
|
|
53
|
+
var current;
|
|
54
|
+
function project() {
|
|
55
|
+
if (!current) {
|
|
56
|
+
current = woof2({ fullScreen: true });
|
|
57
|
+
const sync = () => {
|
|
58
|
+
const p = current;
|
|
59
|
+
mouseX = p.mouseX;
|
|
60
|
+
mouseY = p.mouseY;
|
|
61
|
+
minX = p.minX;
|
|
62
|
+
maxX = p.maxX;
|
|
63
|
+
minY = p.minY;
|
|
64
|
+
maxY = p.maxY;
|
|
65
|
+
};
|
|
66
|
+
for (const key of [
|
|
67
|
+
"mouseX",
|
|
68
|
+
"mouseY",
|
|
69
|
+
"cameraX",
|
|
70
|
+
"cameraY",
|
|
71
|
+
"width",
|
|
72
|
+
"height"
|
|
73
|
+
]) {
|
|
74
|
+
let value = current[key];
|
|
75
|
+
Object.defineProperty(current, key, {
|
|
76
|
+
configurable: true,
|
|
77
|
+
enumerable: true,
|
|
78
|
+
get: () => value,
|
|
79
|
+
set: (next) => {
|
|
80
|
+
value = next;
|
|
81
|
+
sync();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
sync();
|
|
86
|
+
}
|
|
87
|
+
return current;
|
|
88
|
+
}
|
|
89
|
+
var mouseX = 0;
|
|
90
|
+
var mouseY = 0;
|
|
91
|
+
var minX = -175;
|
|
92
|
+
var maxX = 175;
|
|
93
|
+
var minY = -250;
|
|
94
|
+
var maxY = 250;
|
|
95
|
+
function shape(type) {
|
|
96
|
+
return function(options = {}) {
|
|
97
|
+
return (options.project ?? project()).createSprite(type, options);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
var Circle = shape("circle");
|
|
101
|
+
var Rectangle = shape("rectangle");
|
|
102
|
+
var Oval = shape("oval");
|
|
103
|
+
var Polygon = shape("polygon");
|
|
104
|
+
var Line = shape("line");
|
|
105
|
+
var Text = shape("text");
|
|
106
|
+
var TextPrimative = shape("text");
|
|
107
|
+
var Image = shape("image");
|
|
108
|
+
var Bundle = shape("bundle");
|
|
109
|
+
var Sprite = shape("sprite");
|
|
110
|
+
var customSprite = (definition) => project().customSprite(definition);
|
|
111
|
+
var Sound = function(options = {}) {
|
|
112
|
+
return project().sound(options);
|
|
113
|
+
};
|
|
114
|
+
function every(time, unitsOrFn, fn) {
|
|
115
|
+
return typeof unitsOrFn === "string" ? project().every(time, unitsOrFn, fn) : project().every(time, unitsOrFn);
|
|
116
|
+
}
|
|
117
|
+
function after(time, unitsOrFn, fn) {
|
|
118
|
+
return typeof unitsOrFn === "string" ? project().after(time, unitsOrFn, fn) : project().after(time, unitsOrFn);
|
|
119
|
+
}
|
|
120
|
+
var forever = (fn) => project().forever(fn);
|
|
121
|
+
var when = (condition, fn) => project().when(condition, fn);
|
|
122
|
+
var once = (condition, fn) => project().once(condition, fn);
|
|
123
|
+
var repeat = (count, fn, after) => project().repeat(count, fn, after);
|
|
124
|
+
var repeatUntil = (condition, fn, after) => project().repeatUntil(condition, fn, after);
|
|
125
|
+
var wait = (seconds) => project().wait(seconds);
|
|
126
|
+
var ready = (fn) => project().ready(fn);
|
|
127
|
+
var translateToCanvas = (x, y) => project().translateToCanvas(x, y);
|
|
128
|
+
var translateToCenter = (x, y) => project().translateToCenter(x, y);
|
|
129
|
+
var pressing = {
|
|
130
|
+
includes: (key) => project().key(key)
|
|
131
|
+
};
|
|
132
|
+
var setBackdropColor = (color) => project().setBackdropColor(color);
|
|
133
|
+
var setBackdropURL = (url) => project().setBackdropURL(url);
|
|
134
|
+
var setBackdropSize = (width, height) => project().setBackdropSize(width, height);
|
|
135
|
+
var setBackdropStyle = (style) => project().setBackdropStyle(style);
|
|
136
|
+
var setBackdropRepeat = (repeat) => project().setBackdropRepeat(repeat);
|
|
137
|
+
var bounds = () => project().bounds();
|
|
138
|
+
var randomX = () => project().randomX();
|
|
139
|
+
var randomY = () => project().randomY();
|
|
140
|
+
var clearPen = () => project().clearPen();
|
|
141
|
+
var timer = () => project().timer();
|
|
142
|
+
var resetTimer = () => project().resetTimer();
|
|
143
|
+
var freeze = () => project().freeze();
|
|
144
|
+
var defrost = () => project().defrost();
|
|
145
|
+
var scene = (name, setup) => project().scene(name, setup);
|
|
146
|
+
var go = (name, data) => project().go(name, data);
|
|
147
|
+
var find = (tag) => project().find(tag);
|
|
148
|
+
var particles = (options) => project().particles(options);
|
|
149
|
+
var save = (key, value) => project().save(key, value);
|
|
150
|
+
var load = (key, fallback) => project().load(key, fallback);
|
|
151
|
+
var onUpdate = (fn) => project().onUpdate(fn);
|
|
152
|
+
var onKeyDown = (fn) => project().onKeyDown(fn);
|
|
153
|
+
var onKeyUp = (fn) => project().onKeyUp(fn);
|
|
154
|
+
var onMouseDown = (fn) => project().onMouseDown(fn);
|
|
155
|
+
var onMouseUp = (fn) => project().onMouseUp(fn);
|
|
156
|
+
var onMouseMove = (fn) => project().onMouseMove(fn);
|
|
157
|
+
var onTouchStart = (fn) => project().onTouchStart(fn);
|
|
158
|
+
var onTouchMove = (fn) => project().onTouchMove(fn);
|
|
159
|
+
var onTouchEnd = (fn) => project().onTouchEnd(fn);
|
|
160
|
+
var onTouchCancel = (fn) => project().onTouchCancel(fn);
|
|
161
|
+
var mouse = {
|
|
162
|
+
get x() {
|
|
163
|
+
return project().mouseX;
|
|
164
|
+
},
|
|
165
|
+
get y() {
|
|
166
|
+
return project().mouseY;
|
|
167
|
+
},
|
|
168
|
+
get down() {
|
|
169
|
+
return project().mouseDown;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
var world = {
|
|
173
|
+
get width() {
|
|
174
|
+
return project().width;
|
|
175
|
+
},
|
|
176
|
+
get height() {
|
|
177
|
+
return project().height;
|
|
178
|
+
},
|
|
179
|
+
get minX() {
|
|
180
|
+
return project().minX;
|
|
181
|
+
},
|
|
182
|
+
get maxX() {
|
|
183
|
+
return project().maxX;
|
|
184
|
+
},
|
|
185
|
+
get minY() {
|
|
186
|
+
return project().minY;
|
|
187
|
+
},
|
|
188
|
+
get maxY() {
|
|
189
|
+
return project().maxY;
|
|
190
|
+
},
|
|
191
|
+
get sprites() {
|
|
192
|
+
return project().sprites;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
export {
|
|
196
|
+
Bundle,
|
|
197
|
+
Circle,
|
|
198
|
+
DOWN2 as DOWN,
|
|
199
|
+
Image,
|
|
200
|
+
LEFT2 as LEFT,
|
|
201
|
+
Line,
|
|
202
|
+
Oval,
|
|
203
|
+
Polygon,
|
|
204
|
+
RIGHT2 as RIGHT,
|
|
205
|
+
Rectangle,
|
|
206
|
+
Sound,
|
|
207
|
+
Sprite,
|
|
208
|
+
Text,
|
|
209
|
+
TextPrimative,
|
|
210
|
+
UP2 as UP,
|
|
211
|
+
abs2 as abs,
|
|
212
|
+
acos2 as acos,
|
|
213
|
+
after,
|
|
214
|
+
arrayToString2 as arrayToString,
|
|
215
|
+
asin2 as asin,
|
|
216
|
+
atan2 as atan,
|
|
217
|
+
between2 as between,
|
|
218
|
+
bounds,
|
|
219
|
+
ceiling2 as ceiling,
|
|
220
|
+
clamp2 as clamp,
|
|
221
|
+
clearPen,
|
|
222
|
+
cos2 as cos,
|
|
223
|
+
customSprite,
|
|
224
|
+
dayOfMonth2 as dayOfMonth,
|
|
225
|
+
dayOfWeek2 as dayOfWeek,
|
|
226
|
+
defrost,
|
|
227
|
+
distance2 as distance,
|
|
228
|
+
every,
|
|
229
|
+
extend2 as extend,
|
|
230
|
+
find,
|
|
231
|
+
floor2 as floor,
|
|
232
|
+
forever,
|
|
233
|
+
freeze,
|
|
234
|
+
getData2 as getData,
|
|
235
|
+
go,
|
|
236
|
+
hour2 as hour,
|
|
237
|
+
hourMilitary2 as hourMilitary,
|
|
238
|
+
importCodeURL2 as importCodeURL,
|
|
239
|
+
keyCodeToString2 as keyCodeToString,
|
|
240
|
+
lerp2 as lerp,
|
|
241
|
+
ln2 as ln,
|
|
242
|
+
load,
|
|
243
|
+
log2 as log,
|
|
244
|
+
maxX,
|
|
245
|
+
maxY,
|
|
246
|
+
minX,
|
|
247
|
+
minY,
|
|
248
|
+
minute2 as minute,
|
|
249
|
+
mobile2 as mobile,
|
|
250
|
+
month2 as month,
|
|
251
|
+
months2 as months,
|
|
252
|
+
mouse,
|
|
253
|
+
mouseX,
|
|
254
|
+
mouseY,
|
|
255
|
+
onKeyDown,
|
|
256
|
+
onKeyUp,
|
|
257
|
+
onMouseDown,
|
|
258
|
+
onMouseMove,
|
|
259
|
+
onMouseUp,
|
|
260
|
+
onTouchCancel,
|
|
261
|
+
onTouchEnd,
|
|
262
|
+
onTouchMove,
|
|
263
|
+
onTouchStart,
|
|
264
|
+
onUpdate,
|
|
265
|
+
once,
|
|
266
|
+
particles,
|
|
267
|
+
pick2 as pick,
|
|
268
|
+
pow2 as pow,
|
|
269
|
+
pressing,
|
|
270
|
+
project,
|
|
271
|
+
random2 as random,
|
|
272
|
+
randomColor2 as randomColor,
|
|
273
|
+
randomFloat2 as randomFloat,
|
|
274
|
+
randomX,
|
|
275
|
+
randomY,
|
|
276
|
+
range2 as range,
|
|
277
|
+
ready,
|
|
278
|
+
remove2 as remove,
|
|
279
|
+
repeat,
|
|
280
|
+
repeatUntil,
|
|
281
|
+
resetTimer,
|
|
282
|
+
rgb2 as rgb,
|
|
283
|
+
save,
|
|
284
|
+
scene,
|
|
285
|
+
second2 as second,
|
|
286
|
+
setBackdropColor,
|
|
287
|
+
setBackdropRepeat,
|
|
288
|
+
setBackdropSize,
|
|
289
|
+
setBackdropStyle,
|
|
290
|
+
setBackdropURL,
|
|
291
|
+
shuffle2 as shuffle,
|
|
292
|
+
sin2 as sin,
|
|
293
|
+
sqrt2 as sqrt,
|
|
294
|
+
tan2 as tan,
|
|
295
|
+
throttle2 as throttle,
|
|
296
|
+
timer,
|
|
297
|
+
translateToCanvas,
|
|
298
|
+
translateToCenter,
|
|
299
|
+
unitsToMiliseconds2 as unitsToMiliseconds,
|
|
300
|
+
wait,
|
|
301
|
+
when,
|
|
302
|
+
world,
|
|
303
|
+
year2 as year
|
|
304
|
+
};
|
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;
|