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,3577 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
function __accessProp(key) {
|
|
7
|
+
return this[key];
|
|
8
|
+
}
|
|
9
|
+
var __toCommonJS = (from) => {
|
|
10
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
11
|
+
if (entry)
|
|
12
|
+
return entry;
|
|
13
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (var key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(entry, key))
|
|
17
|
+
__defProp(entry, key, {
|
|
18
|
+
get: __accessProp.bind(from, key),
|
|
19
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
__moduleCache.set(from, entry);
|
|
23
|
+
return entry;
|
|
24
|
+
};
|
|
25
|
+
var __moduleCache;
|
|
26
|
+
var __returnValue = (v) => v;
|
|
27
|
+
function __exportSetter(name, newValue) {
|
|
28
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
29
|
+
}
|
|
30
|
+
var __export = (target, all) => {
|
|
31
|
+
for (var name in all)
|
|
32
|
+
__defProp(target, name, {
|
|
33
|
+
get: all[name],
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
set: __exportSetter.bind(all, name)
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/global.ts
|
|
41
|
+
var exports_global = {};
|
|
42
|
+
__export(exports_global, {
|
|
43
|
+
DOWN: () => DOWN,
|
|
44
|
+
LEFT: () => LEFT,
|
|
45
|
+
Project: () => Project,
|
|
46
|
+
ProjectSound: () => ProjectSound,
|
|
47
|
+
RIGHT: () => RIGHT,
|
|
48
|
+
Repeat: () => Repeat,
|
|
49
|
+
RepeatUntil: () => RepeatUntil,
|
|
50
|
+
Sprite: () => Sprite,
|
|
51
|
+
UP: () => UP,
|
|
52
|
+
abs: () => abs,
|
|
53
|
+
acos: () => acos,
|
|
54
|
+
arrayToString: () => arrayToString,
|
|
55
|
+
asin: () => asin,
|
|
56
|
+
atan: () => atan,
|
|
57
|
+
between: () => between,
|
|
58
|
+
ceiling: () => ceiling,
|
|
59
|
+
clamp: () => clamp2,
|
|
60
|
+
cos: () => cos,
|
|
61
|
+
createCanvas: () => createCanvas,
|
|
62
|
+
createGame: () => createGame,
|
|
63
|
+
createKeyboard: () => createKeyboard,
|
|
64
|
+
createSound: () => createSound,
|
|
65
|
+
createStore: () => createStore,
|
|
66
|
+
dayOfMonth: () => dayOfMonth,
|
|
67
|
+
dayOfWeek: () => dayOfWeek,
|
|
68
|
+
detectCollision: () => detectCollision,
|
|
69
|
+
distance: () => distance,
|
|
70
|
+
easings: () => easings,
|
|
71
|
+
extend: () => extend,
|
|
72
|
+
floor: () => floor,
|
|
73
|
+
getData: () => getData,
|
|
74
|
+
hour: () => hour,
|
|
75
|
+
hourMilitary: () => hourMilitary,
|
|
76
|
+
importCodeURL: () => importCodeURL,
|
|
77
|
+
keyCodeToString: () => keyCodeToString,
|
|
78
|
+
lerp: () => lerp,
|
|
79
|
+
ln: () => ln,
|
|
80
|
+
log: () => log,
|
|
81
|
+
minute: () => minute,
|
|
82
|
+
mobile: () => mobile,
|
|
83
|
+
month: () => month,
|
|
84
|
+
months: () => months,
|
|
85
|
+
overlaps: () => overlaps,
|
|
86
|
+
pick: () => pick,
|
|
87
|
+
play: () => exports_play,
|
|
88
|
+
pow: () => pow,
|
|
89
|
+
random: () => random,
|
|
90
|
+
randomColor: () => randomColor,
|
|
91
|
+
randomFloat: () => randomFloat,
|
|
92
|
+
range: () => range,
|
|
93
|
+
remove: () => remove,
|
|
94
|
+
rgb: () => rgb,
|
|
95
|
+
second: () => second,
|
|
96
|
+
shuffle: () => shuffle,
|
|
97
|
+
sin: () => sin,
|
|
98
|
+
sqrt: () => sqrt,
|
|
99
|
+
tan: () => tan,
|
|
100
|
+
throttle: () => throttle,
|
|
101
|
+
ui: () => exports_ui,
|
|
102
|
+
unitsToMiliseconds: () => unitsToMiliseconds,
|
|
103
|
+
woof: () => woof,
|
|
104
|
+
year: () => year
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// src/index.ts
|
|
108
|
+
var exports_src = {};
|
|
109
|
+
__export(exports_src, {
|
|
110
|
+
DOWN: () => DOWN,
|
|
111
|
+
LEFT: () => LEFT,
|
|
112
|
+
Project: () => Project,
|
|
113
|
+
ProjectSound: () => ProjectSound,
|
|
114
|
+
RIGHT: () => RIGHT,
|
|
115
|
+
Repeat: () => Repeat,
|
|
116
|
+
RepeatUntil: () => RepeatUntil,
|
|
117
|
+
Sprite: () => Sprite,
|
|
118
|
+
UP: () => UP,
|
|
119
|
+
abs: () => abs,
|
|
120
|
+
acos: () => acos,
|
|
121
|
+
arrayToString: () => arrayToString,
|
|
122
|
+
asin: () => asin,
|
|
123
|
+
atan: () => atan,
|
|
124
|
+
between: () => between,
|
|
125
|
+
ceiling: () => ceiling,
|
|
126
|
+
clamp: () => clamp2,
|
|
127
|
+
cos: () => cos,
|
|
128
|
+
createCanvas: () => createCanvas,
|
|
129
|
+
createGame: () => createGame,
|
|
130
|
+
createKeyboard: () => createKeyboard,
|
|
131
|
+
createSound: () => createSound,
|
|
132
|
+
createStore: () => createStore,
|
|
133
|
+
dayOfMonth: () => dayOfMonth,
|
|
134
|
+
dayOfWeek: () => dayOfWeek,
|
|
135
|
+
detectCollision: () => detectCollision,
|
|
136
|
+
distance: () => distance,
|
|
137
|
+
easings: () => easings,
|
|
138
|
+
extend: () => extend,
|
|
139
|
+
floor: () => floor,
|
|
140
|
+
getData: () => getData,
|
|
141
|
+
hour: () => hour,
|
|
142
|
+
hourMilitary: () => hourMilitary,
|
|
143
|
+
importCodeURL: () => importCodeURL,
|
|
144
|
+
keyCodeToString: () => keyCodeToString,
|
|
145
|
+
lerp: () => lerp,
|
|
146
|
+
ln: () => ln,
|
|
147
|
+
log: () => log,
|
|
148
|
+
minute: () => minute,
|
|
149
|
+
mobile: () => mobile,
|
|
150
|
+
month: () => month,
|
|
151
|
+
months: () => months,
|
|
152
|
+
overlaps: () => overlaps,
|
|
153
|
+
pick: () => pick,
|
|
154
|
+
pow: () => pow,
|
|
155
|
+
random: () => random,
|
|
156
|
+
randomColor: () => randomColor,
|
|
157
|
+
randomFloat: () => randomFloat,
|
|
158
|
+
range: () => range,
|
|
159
|
+
remove: () => remove,
|
|
160
|
+
rgb: () => rgb,
|
|
161
|
+
second: () => second,
|
|
162
|
+
shuffle: () => shuffle,
|
|
163
|
+
sin: () => sin,
|
|
164
|
+
sqrt: () => sqrt,
|
|
165
|
+
tan: () => tan,
|
|
166
|
+
throttle: () => throttle,
|
|
167
|
+
unitsToMiliseconds: () => unitsToMiliseconds,
|
|
168
|
+
woof: () => woof,
|
|
169
|
+
year: () => year
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// src/helpers.ts
|
|
173
|
+
var exports_helpers = {};
|
|
174
|
+
__export(exports_helpers, {
|
|
175
|
+
DOWN: () => DOWN,
|
|
176
|
+
LEFT: () => LEFT,
|
|
177
|
+
RIGHT: () => RIGHT,
|
|
178
|
+
UP: () => UP,
|
|
179
|
+
abs: () => abs,
|
|
180
|
+
acos: () => acos,
|
|
181
|
+
arrayToString: () => arrayToString,
|
|
182
|
+
asin: () => asin,
|
|
183
|
+
atan: () => atan,
|
|
184
|
+
between: () => between,
|
|
185
|
+
ceiling: () => ceiling,
|
|
186
|
+
clamp: () => clamp2,
|
|
187
|
+
cos: () => cos,
|
|
188
|
+
dayOfMonth: () => dayOfMonth,
|
|
189
|
+
dayOfWeek: () => dayOfWeek,
|
|
190
|
+
distance: () => distance,
|
|
191
|
+
extend: () => extend,
|
|
192
|
+
floor: () => floor,
|
|
193
|
+
getData: () => getData,
|
|
194
|
+
hour: () => hour,
|
|
195
|
+
hourMilitary: () => hourMilitary,
|
|
196
|
+
importCodeURL: () => importCodeURL,
|
|
197
|
+
keyCodeToString: () => keyCodeToString,
|
|
198
|
+
lerp: () => lerp,
|
|
199
|
+
ln: () => ln,
|
|
200
|
+
log: () => log,
|
|
201
|
+
minute: () => minute,
|
|
202
|
+
mobile: () => mobile,
|
|
203
|
+
month: () => month,
|
|
204
|
+
months: () => months,
|
|
205
|
+
pick: () => pick,
|
|
206
|
+
pow: () => pow,
|
|
207
|
+
random: () => random,
|
|
208
|
+
randomColor: () => randomColor,
|
|
209
|
+
randomFloat: () => randomFloat,
|
|
210
|
+
range: () => range,
|
|
211
|
+
remove: () => remove,
|
|
212
|
+
rgb: () => rgb,
|
|
213
|
+
second: () => second,
|
|
214
|
+
shuffle: () => shuffle,
|
|
215
|
+
sin: () => sin,
|
|
216
|
+
sqrt: () => sqrt,
|
|
217
|
+
tan: () => tan,
|
|
218
|
+
throttle: () => throttle,
|
|
219
|
+
unitsToMiliseconds: () => unitsToMiliseconds,
|
|
220
|
+
year: () => year
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// src/play.ts
|
|
224
|
+
var exports_play = {};
|
|
225
|
+
__export(exports_play, {
|
|
226
|
+
Bundle: () => Bundle,
|
|
227
|
+
Circle: () => Circle,
|
|
228
|
+
DOWN: () => DOWN,
|
|
229
|
+
Image: () => Image,
|
|
230
|
+
LEFT: () => LEFT,
|
|
231
|
+
Line: () => Line,
|
|
232
|
+
Oval: () => Oval,
|
|
233
|
+
Polygon: () => Polygon,
|
|
234
|
+
RIGHT: () => RIGHT,
|
|
235
|
+
Rectangle: () => Rectangle,
|
|
236
|
+
Sound: () => Sound,
|
|
237
|
+
Sprite: () => Sprite2,
|
|
238
|
+
Text: () => Text,
|
|
239
|
+
TextPrimative: () => TextPrimative,
|
|
240
|
+
UP: () => UP,
|
|
241
|
+
abs: () => abs,
|
|
242
|
+
acos: () => acos,
|
|
243
|
+
after: () => after,
|
|
244
|
+
arrayToString: () => arrayToString,
|
|
245
|
+
asin: () => asin,
|
|
246
|
+
atan: () => atan,
|
|
247
|
+
between: () => between,
|
|
248
|
+
bounds: () => bounds,
|
|
249
|
+
ceiling: () => ceiling,
|
|
250
|
+
clamp: () => clamp2,
|
|
251
|
+
clearPen: () => clearPen,
|
|
252
|
+
cos: () => cos,
|
|
253
|
+
customSprite: () => customSprite,
|
|
254
|
+
dayOfMonth: () => dayOfMonth,
|
|
255
|
+
dayOfWeek: () => dayOfWeek,
|
|
256
|
+
defrost: () => defrost,
|
|
257
|
+
distance: () => distance,
|
|
258
|
+
every: () => every,
|
|
259
|
+
extend: () => extend,
|
|
260
|
+
find: () => find,
|
|
261
|
+
floor: () => floor,
|
|
262
|
+
forever: () => forever,
|
|
263
|
+
freeze: () => freeze,
|
|
264
|
+
getData: () => getData,
|
|
265
|
+
go: () => go,
|
|
266
|
+
hour: () => hour,
|
|
267
|
+
hourMilitary: () => hourMilitary,
|
|
268
|
+
importCodeURL: () => importCodeURL,
|
|
269
|
+
keyCodeToString: () => keyCodeToString,
|
|
270
|
+
lerp: () => lerp,
|
|
271
|
+
ln: () => ln,
|
|
272
|
+
load: () => load,
|
|
273
|
+
log: () => log,
|
|
274
|
+
maxX: () => maxX,
|
|
275
|
+
maxY: () => maxY,
|
|
276
|
+
minX: () => minX,
|
|
277
|
+
minY: () => minY,
|
|
278
|
+
minute: () => minute,
|
|
279
|
+
mobile: () => mobile,
|
|
280
|
+
month: () => month,
|
|
281
|
+
months: () => months,
|
|
282
|
+
mouse: () => mouse,
|
|
283
|
+
mouseX: () => mouseX,
|
|
284
|
+
mouseY: () => mouseY,
|
|
285
|
+
onKeyDown: () => onKeyDown,
|
|
286
|
+
onKeyUp: () => onKeyUp,
|
|
287
|
+
onMouseDown: () => onMouseDown,
|
|
288
|
+
onMouseMove: () => onMouseMove,
|
|
289
|
+
onMouseUp: () => onMouseUp,
|
|
290
|
+
onUpdate: () => onUpdate,
|
|
291
|
+
once: () => once,
|
|
292
|
+
particles: () => particles,
|
|
293
|
+
pick: () => pick,
|
|
294
|
+
pow: () => pow,
|
|
295
|
+
pressing: () => pressing,
|
|
296
|
+
project: () => project2,
|
|
297
|
+
random: () => random,
|
|
298
|
+
randomColor: () => randomColor,
|
|
299
|
+
randomFloat: () => randomFloat,
|
|
300
|
+
randomX: () => randomX,
|
|
301
|
+
randomY: () => randomY,
|
|
302
|
+
range: () => range,
|
|
303
|
+
ready: () => ready,
|
|
304
|
+
remove: () => remove,
|
|
305
|
+
repeat: () => repeat,
|
|
306
|
+
repeatUntil: () => repeatUntil,
|
|
307
|
+
resetTimer: () => resetTimer,
|
|
308
|
+
rgb: () => rgb,
|
|
309
|
+
save: () => save,
|
|
310
|
+
scene: () => scene,
|
|
311
|
+
second: () => second,
|
|
312
|
+
setBackdropColor: () => setBackdropColor,
|
|
313
|
+
setBackdropRepeat: () => setBackdropRepeat,
|
|
314
|
+
setBackdropSize: () => setBackdropSize,
|
|
315
|
+
setBackdropStyle: () => setBackdropStyle,
|
|
316
|
+
setBackdropURL: () => setBackdropURL,
|
|
317
|
+
shuffle: () => shuffle,
|
|
318
|
+
sin: () => sin,
|
|
319
|
+
sqrt: () => sqrt,
|
|
320
|
+
tan: () => tan,
|
|
321
|
+
throttle: () => throttle,
|
|
322
|
+
timer: () => timer,
|
|
323
|
+
translateToCanvas: () => translateToCanvas,
|
|
324
|
+
translateToCenter: () => translateToCenter,
|
|
325
|
+
unitsToMiliseconds: () => unitsToMiliseconds,
|
|
326
|
+
wait: () => wait,
|
|
327
|
+
when: () => when,
|
|
328
|
+
world: () => world,
|
|
329
|
+
year: () => year
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
// src/ui.ts
|
|
333
|
+
var exports_ui = {};
|
|
334
|
+
__export(exports_ui, {
|
|
335
|
+
mountGame: () => mountGame,
|
|
336
|
+
readTheme: () => readTheme,
|
|
337
|
+
watchTheme: () => watchTheme
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// src/game.ts
|
|
341
|
+
function createGame({
|
|
342
|
+
state = {},
|
|
343
|
+
update = () => {},
|
|
344
|
+
render = () => {},
|
|
345
|
+
requestFrame,
|
|
346
|
+
cancelFrame
|
|
347
|
+
} = {}) {
|
|
348
|
+
let running = false;
|
|
349
|
+
let frameId = null;
|
|
350
|
+
let previousTime = null;
|
|
351
|
+
let frame = 0;
|
|
352
|
+
let currentState = state;
|
|
353
|
+
function step(deltaMs, timestamp = 0) {
|
|
354
|
+
if (!Number.isFinite(deltaMs) || deltaMs < 0)
|
|
355
|
+
throw new RangeError("deltaMs must be a finite, non-negative number");
|
|
356
|
+
const deltaSeconds = deltaMs / 1000;
|
|
357
|
+
const info = { deltaMs, timestamp, frame };
|
|
358
|
+
update(deltaSeconds, currentState, info);
|
|
359
|
+
render(currentState, { ...info, deltaSeconds });
|
|
360
|
+
frame += 1;
|
|
361
|
+
return currentState;
|
|
362
|
+
}
|
|
363
|
+
function getRequestFrame() {
|
|
364
|
+
const fn = requestFrame ?? globalThis.requestAnimationFrame;
|
|
365
|
+
if (typeof fn !== "function")
|
|
366
|
+
throw new Error("requestAnimationFrame is unavailable; provide requestFrame");
|
|
367
|
+
return fn;
|
|
368
|
+
}
|
|
369
|
+
function tick(timestamp) {
|
|
370
|
+
if (!running)
|
|
371
|
+
return;
|
|
372
|
+
const delta = previousTime === null ? 0 : Math.max(0, timestamp - previousTime);
|
|
373
|
+
previousTime = timestamp;
|
|
374
|
+
frameId = null;
|
|
375
|
+
try {
|
|
376
|
+
step(delta, timestamp);
|
|
377
|
+
if (running)
|
|
378
|
+
frameId = getRequestFrame()(tick);
|
|
379
|
+
} catch (error) {
|
|
380
|
+
running = false;
|
|
381
|
+
previousTime = null;
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const loop = {
|
|
386
|
+
step,
|
|
387
|
+
getState: () => currentState,
|
|
388
|
+
setState(nextState) {
|
|
389
|
+
currentState = nextState;
|
|
390
|
+
return currentState;
|
|
391
|
+
},
|
|
392
|
+
start() {
|
|
393
|
+
if (running)
|
|
394
|
+
return this;
|
|
395
|
+
const schedule = getRequestFrame();
|
|
396
|
+
previousTime = null;
|
|
397
|
+
running = true;
|
|
398
|
+
try {
|
|
399
|
+
frameId = schedule(tick);
|
|
400
|
+
} catch (error) {
|
|
401
|
+
running = false;
|
|
402
|
+
throw error;
|
|
403
|
+
}
|
|
404
|
+
return this;
|
|
405
|
+
},
|
|
406
|
+
stop() {
|
|
407
|
+
if (!running)
|
|
408
|
+
return this;
|
|
409
|
+
running = false;
|
|
410
|
+
const cancel = cancelFrame ?? globalThis.cancelAnimationFrame;
|
|
411
|
+
if (frameId !== null && typeof cancel === "function")
|
|
412
|
+
cancel(frameId);
|
|
413
|
+
frameId = null;
|
|
414
|
+
previousTime = null;
|
|
415
|
+
return this;
|
|
416
|
+
},
|
|
417
|
+
get running() {
|
|
418
|
+
return running;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
return loop;
|
|
422
|
+
}
|
|
423
|
+
// src/sound.ts
|
|
424
|
+
function createSound(buffer, audioContext, { volume = 1, maxGain = 4 } = {}) {
|
|
425
|
+
if (!buffer)
|
|
426
|
+
throw new TypeError("buffer is required");
|
|
427
|
+
if (!audioContext || typeof audioContext.createBufferSource !== "function" || typeof audioContext.createGain !== "function") {
|
|
428
|
+
throw new TypeError("audioContext must provide createBufferSource() and createGain()");
|
|
429
|
+
}
|
|
430
|
+
if (!Number.isFinite(maxGain) || maxGain < 0)
|
|
431
|
+
throw new RangeError("maxGain must be a finite, non-negative number");
|
|
432
|
+
if (!Number.isFinite(volume) || volume < 0)
|
|
433
|
+
throw new RangeError("volume must be a finite, non-negative number");
|
|
434
|
+
return {
|
|
435
|
+
startPlaying({ loop = false, playbackRate = 1 } = {}) {
|
|
436
|
+
const source = audioContext.createBufferSource();
|
|
437
|
+
const gainNode = audioContext.createGain();
|
|
438
|
+
source.buffer = buffer;
|
|
439
|
+
source.loop = Boolean(loop);
|
|
440
|
+
if (source.playbackRate)
|
|
441
|
+
source.playbackRate.value = playbackRate;
|
|
442
|
+
const baseGain = clamp(volume, maxGain);
|
|
443
|
+
gainNode.gain.value = baseGain;
|
|
444
|
+
source.connect(gainNode);
|
|
445
|
+
gainNode.connect(audioContext.destination);
|
|
446
|
+
source.start();
|
|
447
|
+
let stopped = false;
|
|
448
|
+
return {
|
|
449
|
+
source,
|
|
450
|
+
amplify(multiplier) {
|
|
451
|
+
if (stopped)
|
|
452
|
+
return this;
|
|
453
|
+
if (multiplier === "max")
|
|
454
|
+
gainNode.gain.value = maxGain;
|
|
455
|
+
else if (typeof multiplier === "number" && Number.isFinite(multiplier)) {
|
|
456
|
+
gainNode.gain.value = clamp(baseGain * multiplier, maxGain);
|
|
457
|
+
} else
|
|
458
|
+
throw new TypeError("amplify expects a finite multiplier or 'max'");
|
|
459
|
+
return this;
|
|
460
|
+
},
|
|
461
|
+
stop() {
|
|
462
|
+
if (!stopped) {
|
|
463
|
+
stopped = true;
|
|
464
|
+
source.stop();
|
|
465
|
+
}
|
|
466
|
+
return this;
|
|
467
|
+
},
|
|
468
|
+
get stopped() {
|
|
469
|
+
return stopped;
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
function clamp(value, maximum) {
|
|
476
|
+
return Math.min(maximum, Math.max(0, value));
|
|
477
|
+
}
|
|
478
|
+
// src/store.ts
|
|
479
|
+
function createStore(initialState = {}) {
|
|
480
|
+
let state = initialState;
|
|
481
|
+
const listeners = new Set;
|
|
482
|
+
function setState(nextState) {
|
|
483
|
+
const next = typeof nextState === "function" ? nextState(state) : nextState;
|
|
484
|
+
if (Object.is(next, state))
|
|
485
|
+
return state;
|
|
486
|
+
state = next;
|
|
487
|
+
for (const listener of [...listeners])
|
|
488
|
+
listener(state);
|
|
489
|
+
return state;
|
|
490
|
+
}
|
|
491
|
+
return {
|
|
492
|
+
getState: () => state,
|
|
493
|
+
setState,
|
|
494
|
+
update(mutator) {
|
|
495
|
+
if (typeof mutator !== "function")
|
|
496
|
+
throw new TypeError("mutator must be a function");
|
|
497
|
+
return setState(mutator(state));
|
|
498
|
+
},
|
|
499
|
+
subscribe(listener) {
|
|
500
|
+
if (typeof listener !== "function")
|
|
501
|
+
throw new TypeError("listener must be a function");
|
|
502
|
+
listeners.add(listener);
|
|
503
|
+
return () => listeners.delete(listener);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
// src/canvas.ts
|
|
508
|
+
function createCanvas({
|
|
509
|
+
document: doc = globalThis.document,
|
|
510
|
+
parent,
|
|
511
|
+
width = 300,
|
|
512
|
+
height = 150,
|
|
513
|
+
context = "2d",
|
|
514
|
+
contextOptions
|
|
515
|
+
} = {}) {
|
|
516
|
+
if (!doc || typeof doc.createElement !== "function")
|
|
517
|
+
throw new Error("A browser document is required; pass document explicitly");
|
|
518
|
+
const element = doc.createElement("canvas");
|
|
519
|
+
element.width = width;
|
|
520
|
+
element.height = height;
|
|
521
|
+
const drawingContext = element.getContext(context, contextOptions);
|
|
522
|
+
if (!drawingContext)
|
|
523
|
+
throw new Error(`Unable to create ${context} canvas context`);
|
|
524
|
+
parent?.appendChild(element);
|
|
525
|
+
let removed = false;
|
|
526
|
+
return {
|
|
527
|
+
element,
|
|
528
|
+
context: drawingContext,
|
|
529
|
+
resize(nextWidth, nextHeight) {
|
|
530
|
+
if (removed)
|
|
531
|
+
throw new Error("Cannot resize a removed canvas");
|
|
532
|
+
element.width = nextWidth;
|
|
533
|
+
element.height = nextHeight;
|
|
534
|
+
return this;
|
|
535
|
+
},
|
|
536
|
+
destroy() {
|
|
537
|
+
if (!removed) {
|
|
538
|
+
removed = true;
|
|
539
|
+
element.remove();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
// src/input.ts
|
|
545
|
+
function createKeyboard(target = globalThis.window) {
|
|
546
|
+
if (!target || typeof target.addEventListener !== "function")
|
|
547
|
+
throw new Error("A keyboard event target is required");
|
|
548
|
+
const pressed = new Set;
|
|
549
|
+
const down = (event) => {
|
|
550
|
+
const code = event.code;
|
|
551
|
+
if (code)
|
|
552
|
+
pressed.add(code);
|
|
553
|
+
};
|
|
554
|
+
const up = (event) => {
|
|
555
|
+
pressed.delete(event.code);
|
|
556
|
+
};
|
|
557
|
+
const clear = () => pressed.clear();
|
|
558
|
+
target.addEventListener("keydown", down);
|
|
559
|
+
target.addEventListener("keyup", up);
|
|
560
|
+
target.addEventListener("blur", clear);
|
|
561
|
+
let destroyed = false;
|
|
562
|
+
return {
|
|
563
|
+
isDown: (code) => pressed.has(code),
|
|
564
|
+
clear,
|
|
565
|
+
destroy() {
|
|
566
|
+
if (destroyed)
|
|
567
|
+
return;
|
|
568
|
+
destroyed = true;
|
|
569
|
+
target.removeEventListener("keydown", down);
|
|
570
|
+
target.removeEventListener("keyup", up);
|
|
571
|
+
target.removeEventListener("blur", clear);
|
|
572
|
+
clear();
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
// src/collision.ts
|
|
577
|
+
function overlaps(a, b) {
|
|
578
|
+
return a.width > 0 && a.height > 0 && b.width > 0 && b.height > 0 && a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
|
|
579
|
+
}
|
|
580
|
+
function project(c, x, y) {
|
|
581
|
+
if (c.kind === "polygon") {
|
|
582
|
+
const values = c.points.map((point) => point.x * x + point.y * y);
|
|
583
|
+
return [Math.min(...values), Math.max(...values)];
|
|
584
|
+
}
|
|
585
|
+
const center = c.x * x + c.y * y;
|
|
586
|
+
if (c.kind === "circle")
|
|
587
|
+
return [center - c.radius, center + c.radius];
|
|
588
|
+
const cos = Math.cos(c.angle);
|
|
589
|
+
const sin = Math.sin(c.angle);
|
|
590
|
+
const radius = Math.hypot(c.radiusX * (x * cos + y * sin), c.radiusY * (y * cos - x * sin));
|
|
591
|
+
return [center - radius, center + radius];
|
|
592
|
+
}
|
|
593
|
+
function ellipseIntersects(ellipse, other) {
|
|
594
|
+
const cos = Math.cos(ellipse.angle);
|
|
595
|
+
const sin = Math.sin(ellipse.angle);
|
|
596
|
+
const rx = Math.abs(ellipse.radiusX);
|
|
597
|
+
const ry = Math.abs(ellipse.radiusY);
|
|
598
|
+
if (rx === 0 || ry === 0) {
|
|
599
|
+
const x = rx * cos - ry * sin;
|
|
600
|
+
const y = rx * sin + ry * cos;
|
|
601
|
+
return intersects({
|
|
602
|
+
kind: "polygon",
|
|
603
|
+
points: [
|
|
604
|
+
{ x: ellipse.x - x, y: ellipse.y - y },
|
|
605
|
+
{ x: ellipse.x + x, y: ellipse.y + y }
|
|
606
|
+
]
|
|
607
|
+
}, other);
|
|
608
|
+
}
|
|
609
|
+
const transform = (x, y) => ({
|
|
610
|
+
x: (x * cos + y * sin) / rx,
|
|
611
|
+
y: (y * cos - x * sin) / ry
|
|
612
|
+
});
|
|
613
|
+
if (other.kind === "polygon") {
|
|
614
|
+
return intersects({ kind: "circle", x: 0, y: 0, radius: 1 }, {
|
|
615
|
+
kind: "polygon",
|
|
616
|
+
points: other.points.map((point) => transform(point.x - ellipse.x, point.y - ellipse.y))
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
const center = transform(other.x - ellipse.x, other.y - ellipse.y);
|
|
620
|
+
const angle = other.kind === "circle" ? 0 : other.angle;
|
|
621
|
+
const otherX = Math.abs(other.kind === "circle" ? other.radius : other.radiusX);
|
|
622
|
+
const otherY = Math.abs(other.kind === "circle" ? other.radius : other.radiusY);
|
|
623
|
+
const u = transform(otherX * Math.cos(angle), otherX * Math.sin(angle));
|
|
624
|
+
const v = transform(-otherY * Math.sin(angle), otherY * Math.cos(angle));
|
|
625
|
+
const xx = u.x * u.x + v.x * v.x;
|
|
626
|
+
const xy = u.x * u.y + v.x * v.y;
|
|
627
|
+
const yy = u.y * u.y + v.y * v.y;
|
|
628
|
+
const axis = Math.atan2(2 * xy, xx - yy) / 2;
|
|
629
|
+
const c = Math.cos(axis);
|
|
630
|
+
const s = Math.sin(axis);
|
|
631
|
+
const major = Math.max(0, (xx + yy + Math.hypot(xx - yy, 2 * xy)) / 2);
|
|
632
|
+
const determinant = u.x * v.y - u.y * v.x;
|
|
633
|
+
const minor = major === 0 ? 0 : determinant / major * determinant;
|
|
634
|
+
const cx = center.x * c + center.y * s;
|
|
635
|
+
const cy = -center.x * s + center.y * c;
|
|
636
|
+
const inside = (major > 0 ? cx * cx / major : cx === 0 ? 0 : Infinity) + (minor > 0 ? cy * cy / minor : cy === 0 ? 0 : Infinity);
|
|
637
|
+
if (inside <= 1)
|
|
638
|
+
return true;
|
|
639
|
+
const equation = (lambda) => {
|
|
640
|
+
const a = major > 0 ? Math.sqrt(major) * cx / (major + lambda) : 0;
|
|
641
|
+
const b = minor > 0 ? Math.sqrt(minor) * cy / (minor + lambda) : 0;
|
|
642
|
+
return a * a + b * b;
|
|
643
|
+
};
|
|
644
|
+
let low = 0;
|
|
645
|
+
let high = Math.max(1, Math.sqrt(major) * Math.hypot(cx, cy));
|
|
646
|
+
while (equation(high) > 1)
|
|
647
|
+
high *= 2;
|
|
648
|
+
for (let i = 0;i < 100; i++) {
|
|
649
|
+
const mid = (low + high) / 2;
|
|
650
|
+
if (equation(mid) > 1)
|
|
651
|
+
low = mid;
|
|
652
|
+
else
|
|
653
|
+
high = mid;
|
|
654
|
+
}
|
|
655
|
+
const dx = major > 0 ? cx * high / (major + high) : cx;
|
|
656
|
+
const dy = minor > 0 ? cy * high / (minor + high) : cy;
|
|
657
|
+
return Math.hypot(dx, dy) <= 1 + 0.000000000001;
|
|
658
|
+
}
|
|
659
|
+
function intersects(a, b) {
|
|
660
|
+
const bounds = (c) => {
|
|
661
|
+
const x = project(c, 1, 0);
|
|
662
|
+
const y = project(c, 0, 1);
|
|
663
|
+
return [x[0], x[1], y[0], y[1]];
|
|
664
|
+
};
|
|
665
|
+
const aa = bounds(a), bb = bounds(b);
|
|
666
|
+
if (aa[1] < bb[0] || bb[1] < aa[0] || aa[3] < bb[2] || bb[3] < aa[2])
|
|
667
|
+
return false;
|
|
668
|
+
if (a.kind === "ellipse")
|
|
669
|
+
return ellipseIntersects(a, b);
|
|
670
|
+
if (b.kind === "ellipse")
|
|
671
|
+
return ellipseIntersects(b, a);
|
|
672
|
+
if (a.kind === "circle" && b.kind === "circle")
|
|
673
|
+
return Math.hypot(a.x - b.x, a.y - b.y) <= a.radius + b.radius;
|
|
674
|
+
const axes = [];
|
|
675
|
+
for (const c of [a, b])
|
|
676
|
+
if (c.kind === "polygon")
|
|
677
|
+
for (let i = 0;i < c.points.length; i++) {
|
|
678
|
+
const p = c.points[i], q = c.points[(i + 1) % c.points.length], x = -(q.y - p.y), y = q.x - p.x, d = Math.hypot(x, y);
|
|
679
|
+
if (d)
|
|
680
|
+
axes.push({ x: x / d, y: y / d });
|
|
681
|
+
}
|
|
682
|
+
const circle = a.kind === "circle" ? a : b.kind === "circle" ? b : undefined, poly = a.kind === "polygon" ? a : b.kind === "polygon" ? b : undefined;
|
|
683
|
+
if (circle && poly && poly.points.length) {
|
|
684
|
+
const p = poly.points.reduce((best, v) => Math.hypot(v.x - circle.x, v.y - circle.y) < Math.hypot(best.x - circle.x, best.y - circle.y) ? v : best);
|
|
685
|
+
const x = p.x - circle.x, y = p.y - circle.y, d = Math.hypot(x, y);
|
|
686
|
+
if (d)
|
|
687
|
+
axes.push({ x: x / d, y: y / d });
|
|
688
|
+
}
|
|
689
|
+
const projection = (c, axis) => project(c, axis.x, axis.y);
|
|
690
|
+
return axes.every((axis) => {
|
|
691
|
+
const x = projection(a, axis), y = projection(b, axis);
|
|
692
|
+
return x[1] >= y[0] && y[1] >= x[0];
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
function detectCollision(a, b) {
|
|
696
|
+
const normalize = (shape) => {
|
|
697
|
+
if ("kind" in shape)
|
|
698
|
+
return shape;
|
|
699
|
+
if ("r" in shape)
|
|
700
|
+
return {
|
|
701
|
+
kind: "circle",
|
|
702
|
+
x: shape.pos.x,
|
|
703
|
+
y: shape.pos.y,
|
|
704
|
+
radius: shape.r
|
|
705
|
+
};
|
|
706
|
+
return {
|
|
707
|
+
kind: "polygon",
|
|
708
|
+
points: shape.calcPoints.map((point) => ({
|
|
709
|
+
x: shape.pos.x + point.x,
|
|
710
|
+
y: shape.pos.y + point.y
|
|
711
|
+
}))
|
|
712
|
+
};
|
|
713
|
+
};
|
|
714
|
+
return intersects(normalize(a), normalize(b));
|
|
715
|
+
}
|
|
716
|
+
// src/helpers.ts
|
|
717
|
+
var clamp2 = (n, min, max) => Math.min(max, Math.max(min, n));
|
|
718
|
+
var lerp = (a, b, t) => a + (b - a) * t;
|
|
719
|
+
var distance = (x1, y1, x2, y2) => Math.hypot(x2 - x1, y2 - y1);
|
|
720
|
+
var between = (n, a, b) => n > Math.min(a, b) && n < Math.max(a, b);
|
|
721
|
+
function remove(array, item) {
|
|
722
|
+
for (let i = array.length - 1;i >= 0; i--)
|
|
723
|
+
if (array[i] === item)
|
|
724
|
+
array.splice(i, 1);
|
|
725
|
+
}
|
|
726
|
+
var randomFloat = (a, b) => Math.random() * Math.abs(b - a) + Math.min(a, b);
|
|
727
|
+
function random(a, b) {
|
|
728
|
+
const min = Math.ceil(Math.min(a, b));
|
|
729
|
+
const max = Math.floor(Math.max(a, b));
|
|
730
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min > max)
|
|
731
|
+
throw new RangeError("random needs bounds containing a whole number; try random(1, 6).");
|
|
732
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
733
|
+
}
|
|
734
|
+
var pick = (array) => array[Math.floor(Math.random() * array.length)];
|
|
735
|
+
function shuffle(array) {
|
|
736
|
+
const result = [...array];
|
|
737
|
+
for (let i = result.length - 1;i > 0; i--) {
|
|
738
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
739
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
740
|
+
}
|
|
741
|
+
return result;
|
|
742
|
+
}
|
|
743
|
+
function range(start, end, increment = 1) {
|
|
744
|
+
if (!Number.isFinite(increment) || increment === 0)
|
|
745
|
+
throw new RangeError("range increment must be nonzero; try range(0, 10, 1).");
|
|
746
|
+
const result = [];
|
|
747
|
+
const step = Math.abs(increment) * (start < end ? 1 : -1);
|
|
748
|
+
for (let i = start;step > 0 ? i < end : i > end; i += step)
|
|
749
|
+
result.push(i);
|
|
750
|
+
return result;
|
|
751
|
+
}
|
|
752
|
+
var rgb = (r, g, b) => `rgb(${r}, ${g}, ${b})`;
|
|
753
|
+
var randomColor = () => rgb(random(0, 255), random(0, 255), random(0, 255));
|
|
754
|
+
var RIGHT = 0;
|
|
755
|
+
var LEFT = 180;
|
|
756
|
+
var UP = 90;
|
|
757
|
+
var DOWN = 270;
|
|
758
|
+
var sin = (a) => Math.sin(a * Math.PI / 180);
|
|
759
|
+
var cos = (a) => Math.cos(a * Math.PI / 180);
|
|
760
|
+
var tan = (a) => Math.tan(a * Math.PI / 180);
|
|
761
|
+
var asin = (a) => Math.asin(a) * 180 / Math.PI;
|
|
762
|
+
var acos = (a) => Math.acos(a) * 180 / Math.PI;
|
|
763
|
+
var atan = (a) => Math.atan(a) * 180 / Math.PI;
|
|
764
|
+
var sqrt = Math.sqrt;
|
|
765
|
+
var abs = Math.abs;
|
|
766
|
+
var floor = Math.floor;
|
|
767
|
+
var ceiling = Math.ceil;
|
|
768
|
+
var ln = Math.log;
|
|
769
|
+
var log = Math.log10;
|
|
770
|
+
var pow = Math.pow;
|
|
771
|
+
var hourMilitary = () => new Date().getHours();
|
|
772
|
+
var hour = () => hourMilitary() % 12 || 12;
|
|
773
|
+
var minute = () => new Date().getMinutes();
|
|
774
|
+
var second = () => new Date().getSeconds();
|
|
775
|
+
var dayOfMonth = () => new Date().getDate();
|
|
776
|
+
var dayOfWeek = () => [
|
|
777
|
+
"Sunday",
|
|
778
|
+
"Monday",
|
|
779
|
+
"Tuesday",
|
|
780
|
+
"Wednesday",
|
|
781
|
+
"Thursday",
|
|
782
|
+
"Friday",
|
|
783
|
+
"Saturday"
|
|
784
|
+
][new Date().getDay()];
|
|
785
|
+
var month = () => months[new Date().getMonth()];
|
|
786
|
+
var year = () => new Date().getFullYear();
|
|
787
|
+
var mobile = () => typeof navigator !== "undefined" && /Android|iPhone|iPad|IEMobile/i.test(navigator.userAgent);
|
|
788
|
+
async function importCodeURL(url, callback) {
|
|
789
|
+
if (typeof document === "undefined")
|
|
790
|
+
throw new Error("importCodeURL requires a document.");
|
|
791
|
+
for (const src of typeof url === "string" ? [url] : url)
|
|
792
|
+
await new Promise((resolve, reject) => {
|
|
793
|
+
const script = document.createElement("script");
|
|
794
|
+
script.src = src;
|
|
795
|
+
script.onload = () => resolve();
|
|
796
|
+
script.onerror = () => reject(new Error(`Could not load ${src}`));
|
|
797
|
+
document.body.appendChild(script);
|
|
798
|
+
});
|
|
799
|
+
callback?.();
|
|
800
|
+
}
|
|
801
|
+
function extend(target, source) {
|
|
802
|
+
return Object.assign(target, source);
|
|
803
|
+
}
|
|
804
|
+
var months = [
|
|
805
|
+
"January",
|
|
806
|
+
"February",
|
|
807
|
+
"March",
|
|
808
|
+
"April",
|
|
809
|
+
"May",
|
|
810
|
+
"June",
|
|
811
|
+
"July",
|
|
812
|
+
"August",
|
|
813
|
+
"September",
|
|
814
|
+
"October",
|
|
815
|
+
"November",
|
|
816
|
+
"December"
|
|
817
|
+
];
|
|
818
|
+
function keyCodeToString(code) {
|
|
819
|
+
const names = {
|
|
820
|
+
38: "UP",
|
|
821
|
+
37: "LEFT",
|
|
822
|
+
39: "RIGHT",
|
|
823
|
+
40: "DOWN",
|
|
824
|
+
9: "TAB",
|
|
825
|
+
13: "ENTER",
|
|
826
|
+
16: "SHIFT",
|
|
827
|
+
17: "CTRL",
|
|
828
|
+
18: "ALT",
|
|
829
|
+
27: "ESCAPE",
|
|
830
|
+
32: "SPACE",
|
|
831
|
+
192: "`",
|
|
832
|
+
186: ";",
|
|
833
|
+
222: "'",
|
|
834
|
+
189: "-",
|
|
835
|
+
187: "=",
|
|
836
|
+
219: "[",
|
|
837
|
+
220: "\\",
|
|
838
|
+
221: "]",
|
|
839
|
+
191: "/",
|
|
840
|
+
190: ".",
|
|
841
|
+
188: ",",
|
|
842
|
+
20: "CAPS LOCK",
|
|
843
|
+
8: "DELETE",
|
|
844
|
+
91: "COMMAND"
|
|
845
|
+
};
|
|
846
|
+
return names[code] ?? (code >= 65 && code <= 90 || code >= 48 && code <= 57 ? String.fromCharCode(code) : String(code));
|
|
847
|
+
}
|
|
848
|
+
function unitsToMiliseconds(time, units) {
|
|
849
|
+
if (/^mil(?:l)?iseconds?$/.test(units))
|
|
850
|
+
return time;
|
|
851
|
+
if (/^seconds?$/.test(units))
|
|
852
|
+
return time * 1000;
|
|
853
|
+
if (/^minutes?$/.test(units))
|
|
854
|
+
return time * 60000;
|
|
855
|
+
throw new TypeError("Unknown time unit; try unitsToMiliseconds(2, 'seconds').");
|
|
856
|
+
}
|
|
857
|
+
function throttle(callback, limit) {
|
|
858
|
+
if (typeof callback !== "function" || !Number.isFinite(limit) || limit < 0) {
|
|
859
|
+
throw new TypeError("throttle requires a callback and non-negative milliseconds; try throttle(save, 500).");
|
|
860
|
+
}
|
|
861
|
+
let last = -Infinity;
|
|
862
|
+
return function(...args) {
|
|
863
|
+
const now = Date.now();
|
|
864
|
+
if (now - last >= limit) {
|
|
865
|
+
last = now;
|
|
866
|
+
callback.apply(this, args);
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
async function getData(url, callback) {
|
|
871
|
+
const response = await fetch(url);
|
|
872
|
+
if (!response.ok)
|
|
873
|
+
throw new Error(`Could not fetch ${url}: ${response.status}`);
|
|
874
|
+
const data = await response.json();
|
|
875
|
+
callback?.(data);
|
|
876
|
+
return data;
|
|
877
|
+
}
|
|
878
|
+
function arrayToString(values) {
|
|
879
|
+
return `[${values.join(",")}]`;
|
|
880
|
+
}
|
|
881
|
+
// src/world/keys.ts
|
|
882
|
+
function normalizeKey(key) {
|
|
883
|
+
const map = {
|
|
884
|
+
ArrowLeft: "LEFT",
|
|
885
|
+
ArrowRight: "RIGHT",
|
|
886
|
+
ArrowUp: "UP",
|
|
887
|
+
ArrowDown: "DOWN",
|
|
888
|
+
Space: "SPACE",
|
|
889
|
+
" ": "SPACE",
|
|
890
|
+
ControlLeft: "CTRL",
|
|
891
|
+
ControlRight: "CTRL",
|
|
892
|
+
ShiftLeft: "SHIFT",
|
|
893
|
+
ShiftRight: "SHIFT",
|
|
894
|
+
AltLeft: "ALT",
|
|
895
|
+
AltRight: "ALT",
|
|
896
|
+
Backspace: "DELETE",
|
|
897
|
+
CapsLock: "CAPS LOCK",
|
|
898
|
+
MetaLeft: "COMMAND",
|
|
899
|
+
MetaRight: "COMMAND",
|
|
900
|
+
Backquote: "`",
|
|
901
|
+
Semicolon: ";",
|
|
902
|
+
Quote: "'",
|
|
903
|
+
Minus: "-",
|
|
904
|
+
Equal: "=",
|
|
905
|
+
BracketLeft: "[",
|
|
906
|
+
Backslash: "\\",
|
|
907
|
+
BracketRight: "]",
|
|
908
|
+
Slash: "/",
|
|
909
|
+
Period: ".",
|
|
910
|
+
Comma: ","
|
|
911
|
+
};
|
|
912
|
+
const aliases = {
|
|
913
|
+
"UP ARROW": "UP",
|
|
914
|
+
"DOWN ARROW": "DOWN",
|
|
915
|
+
"LEFT ARROW": "LEFT",
|
|
916
|
+
"RIGHT ARROW": "RIGHT",
|
|
917
|
+
RETURN: "ENTER",
|
|
918
|
+
CONTROL: "CTRL",
|
|
919
|
+
OPTION: "ALT",
|
|
920
|
+
ESC: "ESCAPE",
|
|
921
|
+
"SPACE BAR": "SPACE",
|
|
922
|
+
SPACEBAR: "SPACE",
|
|
923
|
+
CAPS: "CAPS LOCK",
|
|
924
|
+
DEL: "DELETE",
|
|
925
|
+
BACKSPACE: "DELETE",
|
|
926
|
+
CMD: "COMMAND",
|
|
927
|
+
WINDOWS: "COMMAND",
|
|
928
|
+
SEARCH: "COMMAND",
|
|
929
|
+
METALEFT: "COMMAND",
|
|
930
|
+
METARIGHT: "COMMAND"
|
|
931
|
+
};
|
|
932
|
+
const upper = key.toUpperCase();
|
|
933
|
+
if (aliases[upper])
|
|
934
|
+
return aliases[upper];
|
|
935
|
+
return map[key] ?? (key.startsWith("Key") ? key.slice(3).toUpperCase() : key.startsWith("Digit") ? key.slice(5) : key.toUpperCase());
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// src/world/globals.ts
|
|
939
|
+
function installProjectGlobals(project, win, restoreGlobals) {
|
|
940
|
+
if (!win)
|
|
941
|
+
throw new Error("global mode requires a window; pass { window } or use a browser.");
|
|
942
|
+
const target = win;
|
|
943
|
+
const install = (name, descriptor, replace = false) => {
|
|
944
|
+
if (!replace && name in target)
|
|
945
|
+
return;
|
|
946
|
+
const previous = Object.getOwnPropertyDescriptor(target, name);
|
|
947
|
+
if (previous && !previous.configurable)
|
|
948
|
+
return;
|
|
949
|
+
Object.defineProperty(target, name, {
|
|
950
|
+
configurable: true,
|
|
951
|
+
enumerable: true,
|
|
952
|
+
...descriptor
|
|
953
|
+
});
|
|
954
|
+
const installed = Object.getOwnPropertyDescriptor(target, name);
|
|
955
|
+
restoreGlobals.push(() => {
|
|
956
|
+
const current = Object.getOwnPropertyDescriptor(target, name);
|
|
957
|
+
if (!current || current.value !== installed?.value || current.get !== installed?.get)
|
|
958
|
+
return;
|
|
959
|
+
if (previous)
|
|
960
|
+
Object.defineProperty(target, name, previous);
|
|
961
|
+
else
|
|
962
|
+
delete target[name];
|
|
963
|
+
});
|
|
964
|
+
};
|
|
965
|
+
const value = (name, item, replace = false) => install(name, { value: item, writable: true }, replace);
|
|
966
|
+
const shapeTypes = {
|
|
967
|
+
Circle: "circle",
|
|
968
|
+
Rectangle: "rectangle",
|
|
969
|
+
Oval: "oval",
|
|
970
|
+
Polygon: "polygon",
|
|
971
|
+
Line: "line",
|
|
972
|
+
Text: "text",
|
|
973
|
+
TextPrimative: "text",
|
|
974
|
+
Image: "image",
|
|
975
|
+
Bundle: "bundle",
|
|
976
|
+
Sprite: "sprite"
|
|
977
|
+
};
|
|
978
|
+
for (const [name, type] of Object.entries(shapeTypes)) {
|
|
979
|
+
value(name, function(options = {}) {
|
|
980
|
+
return (options.project ?? project).createSprite(type, options);
|
|
981
|
+
}, name === "Image" || name === "Text");
|
|
982
|
+
}
|
|
983
|
+
value("Sound", function(options = {}) {
|
|
984
|
+
return project.sound(options);
|
|
985
|
+
});
|
|
986
|
+
for (const name of [
|
|
987
|
+
"after",
|
|
988
|
+
"every",
|
|
989
|
+
"forever",
|
|
990
|
+
"when",
|
|
991
|
+
"once",
|
|
992
|
+
"repeat",
|
|
993
|
+
"repeatUntil",
|
|
994
|
+
"wait",
|
|
995
|
+
"setBackdropColor",
|
|
996
|
+
"setBackdropURL",
|
|
997
|
+
"setBackdropSize",
|
|
998
|
+
"setBackdropStyle",
|
|
999
|
+
"setBackdropRepeat",
|
|
1000
|
+
"random",
|
|
1001
|
+
"randomFloat",
|
|
1002
|
+
"randomX",
|
|
1003
|
+
"randomY",
|
|
1004
|
+
"customSprite",
|
|
1005
|
+
"freeze",
|
|
1006
|
+
"defrost",
|
|
1007
|
+
"ready",
|
|
1008
|
+
"translateToCanvas",
|
|
1009
|
+
"translateToCenter",
|
|
1010
|
+
"bounds",
|
|
1011
|
+
"clearPen",
|
|
1012
|
+
"timer",
|
|
1013
|
+
"resetTimer",
|
|
1014
|
+
"onKeyDown",
|
|
1015
|
+
"onKeyUp",
|
|
1016
|
+
"onMouseDown",
|
|
1017
|
+
"onMouseUp",
|
|
1018
|
+
"onMouseMove",
|
|
1019
|
+
"onUpdate",
|
|
1020
|
+
"scene",
|
|
1021
|
+
"go",
|
|
1022
|
+
"find",
|
|
1023
|
+
"particles",
|
|
1024
|
+
"save",
|
|
1025
|
+
"load"
|
|
1026
|
+
])
|
|
1027
|
+
value(name, project[name].bind(project));
|
|
1028
|
+
for (const [name, item] of Object.entries(exports_helpers))
|
|
1029
|
+
value(name, item);
|
|
1030
|
+
value("pressing", { includes: (key) => project.key(key) });
|
|
1031
|
+
value("global", true);
|
|
1032
|
+
for (const name of [
|
|
1033
|
+
"mouseX",
|
|
1034
|
+
"mouseY",
|
|
1035
|
+
"pMouseX",
|
|
1036
|
+
"pMouseY",
|
|
1037
|
+
"mouseXSpeed",
|
|
1038
|
+
"mouseYSpeed",
|
|
1039
|
+
"mouseDown",
|
|
1040
|
+
"keysDown",
|
|
1041
|
+
"width",
|
|
1042
|
+
"height",
|
|
1043
|
+
"minX",
|
|
1044
|
+
"maxX",
|
|
1045
|
+
"minY",
|
|
1046
|
+
"maxY",
|
|
1047
|
+
"sprites",
|
|
1048
|
+
"stopped",
|
|
1049
|
+
"backdrop"
|
|
1050
|
+
])
|
|
1051
|
+
install(name, { get: () => project[name] });
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// src/sprites/custom.ts
|
|
1055
|
+
function createCustomSprite(project, definition) {
|
|
1056
|
+
if (typeof definition.render !== "function")
|
|
1057
|
+
throw new TypeError("customSprite requires a render function; try customSprite({ render(ctx) {} }).");
|
|
1058
|
+
return function(options = {}) {
|
|
1059
|
+
const sprite = Object.assign((options.project ?? project).sprite(options), definition);
|
|
1060
|
+
sprite.draw = (ctx) => sprite.render(ctx);
|
|
1061
|
+
return sprite;
|
|
1062
|
+
};
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
// src/sprites/geometry.ts
|
|
1066
|
+
function spriteCollider(sprite) {
|
|
1067
|
+
if (sprite.type === "circle")
|
|
1068
|
+
return {
|
|
1069
|
+
kind: "circle",
|
|
1070
|
+
x: sprite.x,
|
|
1071
|
+
y: sprite.y,
|
|
1072
|
+
radius: Math.abs(sprite.radius * sprite.scale)
|
|
1073
|
+
};
|
|
1074
|
+
if (sprite.type === "oval")
|
|
1075
|
+
return {
|
|
1076
|
+
kind: "ellipse",
|
|
1077
|
+
x: sprite.x,
|
|
1078
|
+
y: sprite.y,
|
|
1079
|
+
radiusX: Math.abs(sprite.width * sprite.scale / 2),
|
|
1080
|
+
radiusY: Math.abs(sprite.height * sprite.scale / 2),
|
|
1081
|
+
angle: sprite.rotationStyle === "ROTATE" ? sprite.radians() : 0
|
|
1082
|
+
};
|
|
1083
|
+
let points;
|
|
1084
|
+
if (sprite.type === "line") {
|
|
1085
|
+
const dx = sprite.x1 - sprite.x, dy = sprite.y1 - sprite.y, d = Math.hypot(dx, dy) || 1, w = sprite.width * sprite.scale / 2;
|
|
1086
|
+
return {
|
|
1087
|
+
kind: "polygon",
|
|
1088
|
+
points: [
|
|
1089
|
+
{ x: sprite.x - dy / d * w, y: sprite.y + dx / d * w },
|
|
1090
|
+
{ x: sprite.x + dy / d * w, y: sprite.y - dx / d * w },
|
|
1091
|
+
{ x: sprite.x1 + dy / d * w, y: sprite.y1 - dx / d * w },
|
|
1092
|
+
{ x: sprite.x1 - dy / d * w, y: sprite.y1 + dx / d * w }
|
|
1093
|
+
]
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
if (sprite.type === "polygon")
|
|
1097
|
+
points = Array.from({ length: Math.max(3, Math.floor(sprite.sides)) }, (_, i) => ({
|
|
1098
|
+
x: sprite.length * Math.cos(i * 2 * Math.PI / sprite.sides),
|
|
1099
|
+
y: sprite.length * Math.sin(i * 2 * Math.PI / sprite.sides)
|
|
1100
|
+
}));
|
|
1101
|
+
else {
|
|
1102
|
+
const w = sprite.type === "text" ? sprite.textEval().length * sprite.size * 0.6 : sprite.width, h = sprite.type === "text" ? sprite.size : sprite.height;
|
|
1103
|
+
points = [
|
|
1104
|
+
{ x: -w / 2, y: -h / 2 },
|
|
1105
|
+
{ x: w / 2, y: -h / 2 },
|
|
1106
|
+
{ x: w / 2, y: h / 2 },
|
|
1107
|
+
{ x: -w / 2, y: h / 2 }
|
|
1108
|
+
];
|
|
1109
|
+
}
|
|
1110
|
+
const a = sprite.rotationStyle === "ROTATE" ? sprite.radians() : 0, c = Math.cos(a), s = Math.sin(a);
|
|
1111
|
+
return {
|
|
1112
|
+
kind: "polygon",
|
|
1113
|
+
points: points.map((p) => ({
|
|
1114
|
+
x: sprite.x + sprite.scale * (p.x * c - p.y * s),
|
|
1115
|
+
y: sprite.y + sprite.scale * (p.x * s + p.y * c)
|
|
1116
|
+
}))
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
function spriteBounds(sprite) {
|
|
1120
|
+
if (sprite.type === "bundle") {
|
|
1121
|
+
const bounds = sprite.components.map((child) => child.bounds());
|
|
1122
|
+
if (!bounds.length)
|
|
1123
|
+
return { left: sprite.x, right: sprite.x, top: sprite.y, bottom: sprite.y };
|
|
1124
|
+
return {
|
|
1125
|
+
left: Math.min(...bounds.map((b) => b.left)),
|
|
1126
|
+
right: Math.max(...bounds.map((b) => b.right)),
|
|
1127
|
+
top: Math.max(...bounds.map((b) => b.top)),
|
|
1128
|
+
bottom: Math.min(...bounds.map((b) => b.bottom))
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
const c = sprite.collider();
|
|
1132
|
+
if (c.kind === "circle")
|
|
1133
|
+
return {
|
|
1134
|
+
left: c.x - c.radius,
|
|
1135
|
+
right: c.x + c.radius,
|
|
1136
|
+
top: c.y + c.radius,
|
|
1137
|
+
bottom: c.y - c.radius
|
|
1138
|
+
};
|
|
1139
|
+
if (c.kind === "ellipse") {
|
|
1140
|
+
const x = Math.hypot(c.radiusX * Math.cos(c.angle), c.radiusY * Math.sin(c.angle));
|
|
1141
|
+
const y = Math.hypot(c.radiusX * Math.sin(c.angle), c.radiusY * Math.cos(c.angle));
|
|
1142
|
+
return { left: c.x - x, right: c.x + x, top: c.y + y, bottom: c.y - y };
|
|
1143
|
+
}
|
|
1144
|
+
return {
|
|
1145
|
+
left: Math.min(...c.points.map((p) => p.x)),
|
|
1146
|
+
right: Math.max(...c.points.map((p) => p.x)),
|
|
1147
|
+
top: Math.max(...c.points.map((p) => p.y)),
|
|
1148
|
+
bottom: Math.min(...c.points.map((p) => p.y))
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
function spritesTouching(sprite, other, precise) {
|
|
1152
|
+
if (!sprite.showing || sprite.deleted || !other.showing || other.deleted || sprite === other) {
|
|
1153
|
+
return false;
|
|
1154
|
+
}
|
|
1155
|
+
if (sprite.type === "bundle") {
|
|
1156
|
+
return sprite.components.some((child) => child.touching(other, precise));
|
|
1157
|
+
}
|
|
1158
|
+
if (other.type === "bundle") {
|
|
1159
|
+
return other.components.some((child) => sprite.touching(child, precise));
|
|
1160
|
+
}
|
|
1161
|
+
return intersects(sprite.collider(), other.collider()) && (!precise || sprite.project.preciseCollision(sprite, other));
|
|
1162
|
+
}
|
|
1163
|
+
function spriteOverlaps(sprite, bounds) {
|
|
1164
|
+
if (sprite.deleted || !sprite.showing)
|
|
1165
|
+
return false;
|
|
1166
|
+
if (sprite.type === "bundle") {
|
|
1167
|
+
return sprite.components.some((child) => child.overlap(bounds));
|
|
1168
|
+
}
|
|
1169
|
+
return intersects(sprite.collider(), {
|
|
1170
|
+
kind: "polygon",
|
|
1171
|
+
points: [
|
|
1172
|
+
{ x: bounds.left, y: bounds.bottom },
|
|
1173
|
+
{ x: bounds.right, y: bounds.bottom },
|
|
1174
|
+
{ x: bounds.right, y: bounds.top },
|
|
1175
|
+
{ x: bounds.left, y: bounds.top }
|
|
1176
|
+
]
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
function spriteOver(sprite, x, y) {
|
|
1180
|
+
if (!sprite.showing || sprite.deleted)
|
|
1181
|
+
return false;
|
|
1182
|
+
if (sprite.type === "bundle")
|
|
1183
|
+
return sprite.components.some((child) => child.over(x, y));
|
|
1184
|
+
return intersects(sprite.collider(), { kind: "circle", x, y, radius: 0 });
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
// src/sprites/render.ts
|
|
1188
|
+
function renderSprite(sprite, ctx) {
|
|
1189
|
+
if (!sprite.showing || sprite.deleted || sprite.type === "bundle")
|
|
1190
|
+
return;
|
|
1191
|
+
ctx.save();
|
|
1192
|
+
ctx.translate(sprite.canvasX(), sprite.canvasY());
|
|
1193
|
+
if (sprite.rotationStyle === "ROTATE" && sprite.type !== "line")
|
|
1194
|
+
ctx.rotate(-sprite.radians());
|
|
1195
|
+
else if (sprite.rotationStyle === "ROTATE LEFT RIGHT" && Math.cos(sprite.radians()) < 0)
|
|
1196
|
+
ctx.scale(-1, 1);
|
|
1197
|
+
ctx.scale(sprite.scale * (sprite.flipX ? -1 : 1), sprite.scale * (sprite.flipY ? -1 : 1));
|
|
1198
|
+
ctx.globalAlpha = sprite.opacity * sprite.brightness / 100;
|
|
1199
|
+
ctx.fillStyle = sprite.color;
|
|
1200
|
+
if (sprite.type === "circle") {
|
|
1201
|
+
ctx.beginPath();
|
|
1202
|
+
ctx.arc(0, 0, sprite.radius, 0, Math.PI * 2);
|
|
1203
|
+
ctx.fill();
|
|
1204
|
+
} else if (sprite.type === "oval") {
|
|
1205
|
+
ctx.beginPath();
|
|
1206
|
+
ctx.ellipse(0, 0, sprite.width / 2, sprite.height / 2, 0, 0, Math.PI * 2);
|
|
1207
|
+
ctx.fill();
|
|
1208
|
+
} else if (sprite.type === "polygon") {
|
|
1209
|
+
ctx.beginPath();
|
|
1210
|
+
for (let i = 0;i < sprite.sides; i++) {
|
|
1211
|
+
const a = -i * 2 * Math.PI / sprite.sides;
|
|
1212
|
+
if (i === 0)
|
|
1213
|
+
ctx.moveTo(sprite.length, 0);
|
|
1214
|
+
else
|
|
1215
|
+
ctx.lineTo(sprite.length * Math.cos(a), sprite.length * Math.sin(a));
|
|
1216
|
+
}
|
|
1217
|
+
ctx.closePath();
|
|
1218
|
+
ctx.fill();
|
|
1219
|
+
} else if (sprite.type === "line") {
|
|
1220
|
+
ctx.beginPath();
|
|
1221
|
+
ctx.moveTo(0, 0);
|
|
1222
|
+
ctx.lineTo(sprite.x1 - sprite.x, -(sprite.y1 - sprite.y));
|
|
1223
|
+
ctx.strokeStyle = sprite.color;
|
|
1224
|
+
ctx.lineWidth = sprite.width;
|
|
1225
|
+
ctx.stroke();
|
|
1226
|
+
} else if (sprite.type === "text") {
|
|
1227
|
+
ctx.font = `${sprite.size}px ${sprite.fontFamily}`;
|
|
1228
|
+
ctx.textAlign = sprite.textAlign;
|
|
1229
|
+
ctx.textBaseline = "middle";
|
|
1230
|
+
sprite.textEval().split(`
|
|
1231
|
+
`).forEach((line, i, all) => ctx.fillText(line, 0, (i - (all.length - 1) / 2) * sprite.size));
|
|
1232
|
+
} else if (sprite.type === "image" && sprite.image?.complete) {
|
|
1233
|
+
if (sprite.frames) {
|
|
1234
|
+
const f = sprite.frames, cols = Math.max(1, Math.floor(sprite.image.width / f.width));
|
|
1235
|
+
ctx.drawImage(sprite.image, sprite.frame % cols * f.width, Math.floor(sprite.frame / cols) * f.height, f.width, f.height, -sprite.width / 2, -sprite.height / 2, sprite.width, sprite.height);
|
|
1236
|
+
} else
|
|
1237
|
+
ctx.drawImage(sprite.image, -sprite.width / 2, -sprite.height / 2, sprite.width, sprite.height);
|
|
1238
|
+
} else if (sprite.type === "sprite")
|
|
1239
|
+
sprite.draw?.(ctx);
|
|
1240
|
+
else
|
|
1241
|
+
ctx.fillRect(-sprite.width / 2, -sprite.height / 2, sprite.width, sprite.height);
|
|
1242
|
+
ctx.restore();
|
|
1243
|
+
if (sprite.data.say !== undefined) {
|
|
1244
|
+
ctx.save();
|
|
1245
|
+
ctx.font = "14px sans-serif";
|
|
1246
|
+
ctx.textAlign = "center";
|
|
1247
|
+
ctx.fillStyle = "black";
|
|
1248
|
+
ctx.fillText(String(sprite.data.say), sprite.canvasX(), sprite.canvasY() - sprite.height * Math.abs(sprite.scale) / 2 - 10);
|
|
1249
|
+
ctx.restore();
|
|
1250
|
+
}
|
|
1251
|
+
if (sprite.showCollider) {
|
|
1252
|
+
const shape = sprite.collider();
|
|
1253
|
+
ctx.save();
|
|
1254
|
+
ctx.strokeStyle = "green";
|
|
1255
|
+
ctx.lineWidth = 1;
|
|
1256
|
+
ctx.beginPath();
|
|
1257
|
+
if (shape.kind === "circle")
|
|
1258
|
+
ctx.arc(shape.x - sprite.project.cameraX + sprite.project.width / 2, sprite.project.height / 2 - (shape.y - sprite.project.cameraY), shape.radius, 0, Math.PI * 2);
|
|
1259
|
+
else if (shape.kind === "ellipse") {
|
|
1260
|
+
ctx.ellipse(shape.x - sprite.project.cameraX + sprite.project.width / 2, sprite.project.height / 2 - (shape.y - sprite.project.cameraY), shape.radiusX, shape.radiusY, -shape.angle, 0, Math.PI * 2);
|
|
1261
|
+
} else
|
|
1262
|
+
shape.points.forEach((p, i) => {
|
|
1263
|
+
const x = p.x - sprite.project.cameraX + sprite.project.width / 2, y = sprite.project.height / 2 - (p.y - sprite.project.cameraY);
|
|
1264
|
+
if (i === 0)
|
|
1265
|
+
ctx.moveTo(x, y);
|
|
1266
|
+
else
|
|
1267
|
+
ctx.lineTo(x, y);
|
|
1268
|
+
});
|
|
1269
|
+
ctx.closePath();
|
|
1270
|
+
ctx.stroke();
|
|
1271
|
+
ctx.restore();
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
// src/sprites/easing.ts
|
|
1276
|
+
var easings = {
|
|
1277
|
+
linear: (t) => t,
|
|
1278
|
+
easeIn: (t) => t * t,
|
|
1279
|
+
easeOut: (t) => 1 - (1 - t) * (1 - t),
|
|
1280
|
+
easeInOut: (t) => t < 0.5 ? 2 * t * t : 1 - 2 * (1 - t) * (1 - t)
|
|
1281
|
+
};
|
|
1282
|
+
|
|
1283
|
+
// src/sprites/sprite.ts
|
|
1284
|
+
class Sprite {
|
|
1285
|
+
project;
|
|
1286
|
+
type;
|
|
1287
|
+
px;
|
|
1288
|
+
py;
|
|
1289
|
+
lastPenX = 0;
|
|
1290
|
+
lastPenY = 0;
|
|
1291
|
+
spriteAngle = 0;
|
|
1292
|
+
spriteScale = 1;
|
|
1293
|
+
rotationStyle;
|
|
1294
|
+
spriteShowing = true;
|
|
1295
|
+
deleted = false;
|
|
1296
|
+
penColor;
|
|
1297
|
+
penWidth;
|
|
1298
|
+
penActive;
|
|
1299
|
+
showCollider;
|
|
1300
|
+
spriteBrightness = 100;
|
|
1301
|
+
spriteWidth = 10;
|
|
1302
|
+
spriteHeight = 10;
|
|
1303
|
+
radius;
|
|
1304
|
+
sides;
|
|
1305
|
+
length;
|
|
1306
|
+
x1;
|
|
1307
|
+
y1;
|
|
1308
|
+
color;
|
|
1309
|
+
size;
|
|
1310
|
+
text;
|
|
1311
|
+
textAlign;
|
|
1312
|
+
fontFamily;
|
|
1313
|
+
imageUrl = "";
|
|
1314
|
+
costumes;
|
|
1315
|
+
costumeIndex = 0;
|
|
1316
|
+
opacity;
|
|
1317
|
+
vx;
|
|
1318
|
+
vy;
|
|
1319
|
+
gravity;
|
|
1320
|
+
bounce;
|
|
1321
|
+
wrap;
|
|
1322
|
+
tags;
|
|
1323
|
+
data;
|
|
1324
|
+
layer;
|
|
1325
|
+
draw;
|
|
1326
|
+
update;
|
|
1327
|
+
flipX;
|
|
1328
|
+
flipY;
|
|
1329
|
+
frames;
|
|
1330
|
+
frame = 0;
|
|
1331
|
+
components = [];
|
|
1332
|
+
imageElement;
|
|
1333
|
+
offsetX = 0;
|
|
1334
|
+
offsetY = 0;
|
|
1335
|
+
offsetAngle = 0;
|
|
1336
|
+
parentBundle;
|
|
1337
|
+
ownedTasks = new Set;
|
|
1338
|
+
pressed = new Set;
|
|
1339
|
+
downs = [];
|
|
1340
|
+
ups = [];
|
|
1341
|
+
clicks = [];
|
|
1342
|
+
touches = [];
|
|
1343
|
+
animation;
|
|
1344
|
+
constructor(project, type, options = {}) {
|
|
1345
|
+
const live = Object.entries(options).filter(([key, value]) => typeof value === "function" && !["draw", "update", "text", "project"].includes(key));
|
|
1346
|
+
const o = { ...options };
|
|
1347
|
+
for (const [key, value] of live)
|
|
1348
|
+
o[key] = value();
|
|
1349
|
+
const resolved = o;
|
|
1350
|
+
this.project = project;
|
|
1351
|
+
this.type = type;
|
|
1352
|
+
this.px = resolved.x ?? 0;
|
|
1353
|
+
this.py = resolved.y ?? 0;
|
|
1354
|
+
this.lastPenX = this.px;
|
|
1355
|
+
this.lastPenY = this.py;
|
|
1356
|
+
this.angle = resolved.angle ?? 0;
|
|
1357
|
+
this.rotationStyle = resolved.rotationStyle ?? "ROTATE";
|
|
1358
|
+
this.showing = resolved.showing ?? true;
|
|
1359
|
+
this.penActive = resolved.penDown ?? false;
|
|
1360
|
+
this.penColor = resolved.penColor ?? "black";
|
|
1361
|
+
this.penWidth = resolved.penWidth ?? 1;
|
|
1362
|
+
this.showCollider = resolved.showCollider ?? false;
|
|
1363
|
+
this.brightness = resolved.brightness ?? 100;
|
|
1364
|
+
this.radius = Math.abs(resolved.radius ?? 10);
|
|
1365
|
+
this.sides = resolved.sides ?? 3;
|
|
1366
|
+
this.length = resolved.length ?? 100;
|
|
1367
|
+
this.width = resolved.width ?? (type === "oval" ? 20 : type === "line" ? 1 : 10);
|
|
1368
|
+
this.height = resolved.height ?? (type === "oval" ? 50 : 10);
|
|
1369
|
+
this.x1 = resolved.x1 ?? 10;
|
|
1370
|
+
this.y1 = resolved.y1 ?? 10;
|
|
1371
|
+
this.costumes = [...resolved.costumes ?? []];
|
|
1372
|
+
this.color = resolved.color ?? (type === "oval" ? "green" : "black");
|
|
1373
|
+
this.size = resolved.size ?? 12;
|
|
1374
|
+
this.text = resolved.text ?? "Text";
|
|
1375
|
+
this.textAlign = resolved.textAlign ?? "center";
|
|
1376
|
+
this.fontFamily = resolved.fontFamily ?? "arial";
|
|
1377
|
+
this.imageUrl = resolved.url ?? "";
|
|
1378
|
+
this.opacity = resolved.opacity ?? 1;
|
|
1379
|
+
this.scale = resolved.scale ?? 1;
|
|
1380
|
+
this.vx = resolved.vx ?? 0;
|
|
1381
|
+
this.vy = resolved.vy ?? 0;
|
|
1382
|
+
this.gravity = resolved.gravity ?? 0;
|
|
1383
|
+
this.bounce = resolved.bounce ?? false;
|
|
1384
|
+
this.wrap = resolved.wrap ?? false;
|
|
1385
|
+
this.tags = [...resolved.tags ?? []];
|
|
1386
|
+
this.data = { ...resolved.data };
|
|
1387
|
+
this.layer = resolved.layer ?? 0;
|
|
1388
|
+
this.draw = resolved.draw;
|
|
1389
|
+
this.update = resolved.update;
|
|
1390
|
+
this.flipX = resolved.flipX ?? false;
|
|
1391
|
+
this.flipY = resolved.flipY ?? false;
|
|
1392
|
+
this.frames = resolved.frames;
|
|
1393
|
+
if (type === "image" && (this.imageUrl || this.costumes.length))
|
|
1394
|
+
this.setImageURL(this.imageUrl || this.costumes[0]);
|
|
1395
|
+
for (const component of resolved.components ?? [])
|
|
1396
|
+
this.addComponent(component);
|
|
1397
|
+
for (const [key, value] of live) {
|
|
1398
|
+
const descriptor = Object.getOwnPropertyDescriptor(this, key) ?? Object.getOwnPropertyDescriptor(Sprite.prototype, key);
|
|
1399
|
+
if (!descriptor)
|
|
1400
|
+
continue;
|
|
1401
|
+
let stored = descriptor.value;
|
|
1402
|
+
let source = value;
|
|
1403
|
+
Object.defineProperty(this, key, {
|
|
1404
|
+
configurable: true,
|
|
1405
|
+
enumerable: true,
|
|
1406
|
+
get: () => source ? source() : descriptor.get ? descriptor.get.call(this) : stored,
|
|
1407
|
+
set: (next) => {
|
|
1408
|
+
source = undefined;
|
|
1409
|
+
if (descriptor.set)
|
|
1410
|
+
descriptor.set.call(this, next);
|
|
1411
|
+
else
|
|
1412
|
+
stored = next;
|
|
1413
|
+
}
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
get showing() {
|
|
1418
|
+
return this.spriteShowing;
|
|
1419
|
+
}
|
|
1420
|
+
set showing(value) {
|
|
1421
|
+
this.spriteShowing = value;
|
|
1422
|
+
for (const child of this.components ?? [])
|
|
1423
|
+
child.showing = value;
|
|
1424
|
+
}
|
|
1425
|
+
get brightness() {
|
|
1426
|
+
return this.spriteBrightness;
|
|
1427
|
+
}
|
|
1428
|
+
set brightness(value) {
|
|
1429
|
+
this.spriteBrightness = value;
|
|
1430
|
+
for (const child of this.components ?? [])
|
|
1431
|
+
child.brightness = value;
|
|
1432
|
+
}
|
|
1433
|
+
get angle() {
|
|
1434
|
+
return this.spriteAngle;
|
|
1435
|
+
}
|
|
1436
|
+
set angle(value) {
|
|
1437
|
+
const delta = (value - this.spriteAngle) * Math.PI / 180;
|
|
1438
|
+
this.spriteAngle = value;
|
|
1439
|
+
if (!delta || !this.components)
|
|
1440
|
+
return;
|
|
1441
|
+
const cos = Math.cos(delta), sin = Math.sin(delta);
|
|
1442
|
+
for (const child of this.components) {
|
|
1443
|
+
const x = child.x - this.x, y = child.y - this.y;
|
|
1444
|
+
child.setPosition(this.x + x * cos - y * sin, this.y + x * sin + y * cos);
|
|
1445
|
+
child.angle += delta * 180 / Math.PI;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
get scale() {
|
|
1449
|
+
return this.spriteScale;
|
|
1450
|
+
}
|
|
1451
|
+
set scale(value) {
|
|
1452
|
+
const previous = this.spriteScale;
|
|
1453
|
+
if (this.components?.length && previous === 0 && value !== previous)
|
|
1454
|
+
throw new RangeError("Cannot rescale a bundle from zero.");
|
|
1455
|
+
this.spriteScale = value;
|
|
1456
|
+
if (!this.components || previous === value)
|
|
1457
|
+
return;
|
|
1458
|
+
if (previous === 0)
|
|
1459
|
+
throw new RangeError("Cannot rescale a bundle from zero.");
|
|
1460
|
+
const ratio = value / previous;
|
|
1461
|
+
for (const child of this.components) {
|
|
1462
|
+
child.setPosition(this.x + (child.x - this.x) * ratio, this.y + (child.y - this.y) * ratio);
|
|
1463
|
+
child.scale *= ratio;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
get image() {
|
|
1467
|
+
return this.imageElement;
|
|
1468
|
+
}
|
|
1469
|
+
get costume() {
|
|
1470
|
+
return this.costumeIndex;
|
|
1471
|
+
}
|
|
1472
|
+
set costume(value) {
|
|
1473
|
+
this.switchCostume(value);
|
|
1474
|
+
}
|
|
1475
|
+
get lineWidth() {
|
|
1476
|
+
return this.width;
|
|
1477
|
+
}
|
|
1478
|
+
set lineWidth(value) {
|
|
1479
|
+
this.width = value;
|
|
1480
|
+
}
|
|
1481
|
+
toString() {
|
|
1482
|
+
return `${this.type}@(${this.x},${this.y})`;
|
|
1483
|
+
}
|
|
1484
|
+
rotatedVector(x, y) {
|
|
1485
|
+
const dx = x - this.x, dy = y - this.y + (this.type === "line" ? this.height / 2 : 0), c = Math.cos(this.radians()), s = Math.sin(this.radians());
|
|
1486
|
+
return { x: c * dx - s * dy + this.x, y: s * dx + c * dy + this.y };
|
|
1487
|
+
}
|
|
1488
|
+
translatedVector(pos, v) {
|
|
1489
|
+
return { x: v.x - pos.x, y: v.y - pos.y };
|
|
1490
|
+
}
|
|
1491
|
+
get url() {
|
|
1492
|
+
return this.imageUrl;
|
|
1493
|
+
}
|
|
1494
|
+
set url(value) {
|
|
1495
|
+
this.setImageURL(value);
|
|
1496
|
+
}
|
|
1497
|
+
get width() {
|
|
1498
|
+
if (this.type === "circle")
|
|
1499
|
+
return this.radius * 2;
|
|
1500
|
+
if (this.type === "polygon")
|
|
1501
|
+
return this.length * 2;
|
|
1502
|
+
if (this.type === "text")
|
|
1503
|
+
return Math.max(0, ...this.textEval().split(`
|
|
1504
|
+
`).map((line) => line.length * this.size * 0.6));
|
|
1505
|
+
return this.spriteWidth;
|
|
1506
|
+
}
|
|
1507
|
+
set width(value) {
|
|
1508
|
+
this.spriteWidth = value;
|
|
1509
|
+
}
|
|
1510
|
+
get height() {
|
|
1511
|
+
if (this.type === "circle")
|
|
1512
|
+
return this.radius * 2;
|
|
1513
|
+
if (this.type === "polygon")
|
|
1514
|
+
return this.length * 2;
|
|
1515
|
+
if (this.type === "line")
|
|
1516
|
+
return Math.hypot(this.x1 - this.x, this.y1 - this.y);
|
|
1517
|
+
if (this.type === "text")
|
|
1518
|
+
return this.textEval().split(`
|
|
1519
|
+
`).length * this.size;
|
|
1520
|
+
return this.spriteHeight;
|
|
1521
|
+
}
|
|
1522
|
+
set height(value) {
|
|
1523
|
+
this.spriteHeight = value;
|
|
1524
|
+
}
|
|
1525
|
+
get x() {
|
|
1526
|
+
return this.px;
|
|
1527
|
+
}
|
|
1528
|
+
set x(v) {
|
|
1529
|
+
this.setPosition(v, this.py);
|
|
1530
|
+
}
|
|
1531
|
+
get y() {
|
|
1532
|
+
return this.py;
|
|
1533
|
+
}
|
|
1534
|
+
set y(v) {
|
|
1535
|
+
this.setPosition(this.px, v);
|
|
1536
|
+
}
|
|
1537
|
+
setPosition(x, y) {
|
|
1538
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
1539
|
+
throw new TypeError("setPosition requires two numbers; try setPosition(10, 20).");
|
|
1540
|
+
const dx = x - this.px, dy = y - this.py;
|
|
1541
|
+
this.project.trackPen(this.px, this.py, x, y, this.penActive, this.penColor, this.penWidth);
|
|
1542
|
+
this.px = x;
|
|
1543
|
+
this.py = y;
|
|
1544
|
+
this.lastPenX = x;
|
|
1545
|
+
this.lastPenY = y;
|
|
1546
|
+
for (const c of this.components)
|
|
1547
|
+
c.changePosition(dx, dy);
|
|
1548
|
+
return this;
|
|
1549
|
+
}
|
|
1550
|
+
changePosition(x, y) {
|
|
1551
|
+
return this.setPosition(this.x + x, this.y + y);
|
|
1552
|
+
}
|
|
1553
|
+
move(n = 1) {
|
|
1554
|
+
return this.changePosition(n * Math.cos(this.radians()), n * Math.sin(this.radians()));
|
|
1555
|
+
}
|
|
1556
|
+
radians() {
|
|
1557
|
+
return this.angle * Math.PI / 180;
|
|
1558
|
+
}
|
|
1559
|
+
turnLeft(n = 1) {
|
|
1560
|
+
this.angle = ((this.angle + n) % 360 + 360) % 360;
|
|
1561
|
+
return this;
|
|
1562
|
+
}
|
|
1563
|
+
turnRight(n = 1) {
|
|
1564
|
+
return this.turnLeft(-n);
|
|
1565
|
+
}
|
|
1566
|
+
pointTowards(x, y) {
|
|
1567
|
+
const p = typeof x === "number" ? { x, y: y ?? 0 } : x;
|
|
1568
|
+
this.angle = (Math.atan2(p.y - this.y, p.x - this.x) * 180 / Math.PI + 360) % 360;
|
|
1569
|
+
return this;
|
|
1570
|
+
}
|
|
1571
|
+
distanceTo(x, y) {
|
|
1572
|
+
const p = typeof x === "number" ? { x, y: y ?? 0 } : x;
|
|
1573
|
+
return Math.hypot(this.x - p.x, this.y - p.y);
|
|
1574
|
+
}
|
|
1575
|
+
setRotationStyle(style) {
|
|
1576
|
+
if (!["ROTATE", "NO ROTATE", "ROTATE LEFT RIGHT"].includes(style))
|
|
1577
|
+
throw new TypeError("Use setRotationStyle('ROTATE'), 'NO ROTATE', or 'ROTATE LEFT RIGHT'.");
|
|
1578
|
+
this.rotationStyle = style;
|
|
1579
|
+
return this;
|
|
1580
|
+
}
|
|
1581
|
+
collider() {
|
|
1582
|
+
return spriteCollider(this);
|
|
1583
|
+
}
|
|
1584
|
+
bounds() {
|
|
1585
|
+
return spriteBounds(this);
|
|
1586
|
+
}
|
|
1587
|
+
touching(other, precise = false) {
|
|
1588
|
+
return spritesTouching(this, other, precise);
|
|
1589
|
+
}
|
|
1590
|
+
overlap(b) {
|
|
1591
|
+
return spriteOverlaps(this, b);
|
|
1592
|
+
}
|
|
1593
|
+
over(x, y) {
|
|
1594
|
+
return spriteOver(this, x, y);
|
|
1595
|
+
}
|
|
1596
|
+
get mouseOver() {
|
|
1597
|
+
return this.over(this.project.mouseX, this.project.mouseY);
|
|
1598
|
+
}
|
|
1599
|
+
get mouseDown() {
|
|
1600
|
+
return this.mouseOver && this.project.mouseDown;
|
|
1601
|
+
}
|
|
1602
|
+
onMouseDown(fn) {
|
|
1603
|
+
this.downs.push(fn);
|
|
1604
|
+
return this;
|
|
1605
|
+
}
|
|
1606
|
+
onMouseUp(fn) {
|
|
1607
|
+
this.ups.push(fn);
|
|
1608
|
+
return this;
|
|
1609
|
+
}
|
|
1610
|
+
onClick(fn) {
|
|
1611
|
+
this.clicks.push(fn);
|
|
1612
|
+
return this;
|
|
1613
|
+
}
|
|
1614
|
+
onTouch(target, fn) {
|
|
1615
|
+
this.touches.push({ target, fn, active: new Set });
|
|
1616
|
+
return this;
|
|
1617
|
+
}
|
|
1618
|
+
pointer(kind, x, y, id = 0, cancelled = false) {
|
|
1619
|
+
const over = this.over(x, y);
|
|
1620
|
+
if (kind === "down" && over)
|
|
1621
|
+
this.pressed.add(id);
|
|
1622
|
+
const wasPressed = this.pressed.has(id);
|
|
1623
|
+
if (kind === "up")
|
|
1624
|
+
this.pressed.delete(id);
|
|
1625
|
+
const callbacks = kind === "down" ? over ? this.downs : [] : over && !cancelled ? [...this.ups, ...wasPressed ? this.clicks : []] : [];
|
|
1626
|
+
for (const fn of callbacks)
|
|
1627
|
+
this.project.invoke(() => fn(x, y));
|
|
1628
|
+
}
|
|
1629
|
+
trackPen() {
|
|
1630
|
+
this.project.trackPen(this.lastPenX, this.lastPenY, this.x, this.y, this.penActive, this.penColor, this.penWidth);
|
|
1631
|
+
this.lastPenX = this.x;
|
|
1632
|
+
this.lastPenY = this.y;
|
|
1633
|
+
}
|
|
1634
|
+
detachListeners() {
|
|
1635
|
+
this.downs = [];
|
|
1636
|
+
this.ups = [];
|
|
1637
|
+
this.clicks = [];
|
|
1638
|
+
this.pressed.clear();
|
|
1639
|
+
}
|
|
1640
|
+
arrangeComponents() {
|
|
1641
|
+
const cosine = Math.cos(this.radians());
|
|
1642
|
+
const sine = Math.sin(this.radians());
|
|
1643
|
+
for (const child of this.components) {
|
|
1644
|
+
child.setPosition(this.x + this.scale * (child.offsetX * cosine - child.offsetY * sine), this.y + this.scale * (child.offsetX * sine + child.offsetY * cosine));
|
|
1645
|
+
child.angle = this.angle + child.offsetAngle;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
advance(dt) {
|
|
1649
|
+
this.vy += this.gravity * dt;
|
|
1650
|
+
if (this.vx || this.vy)
|
|
1651
|
+
this.changePosition(this.vx * dt, this.vy * dt);
|
|
1652
|
+
const b = this.bounds(), p = this.project;
|
|
1653
|
+
if (this.bounce) {
|
|
1654
|
+
if (b.left < p.minX && this.vx < 0 || b.right > p.maxX && this.vx > 0)
|
|
1655
|
+
this.vx = -this.vx;
|
|
1656
|
+
if (b.bottom < p.minY && this.vy < 0 || b.top > p.maxY && this.vy > 0)
|
|
1657
|
+
this.vy = -this.vy;
|
|
1658
|
+
}
|
|
1659
|
+
if (this.wrap) {
|
|
1660
|
+
if (this.x < p.minX)
|
|
1661
|
+
this.x = p.maxX;
|
|
1662
|
+
else if (this.x > p.maxX)
|
|
1663
|
+
this.x = p.minX;
|
|
1664
|
+
if (this.y < p.minY)
|
|
1665
|
+
this.y = p.maxY;
|
|
1666
|
+
else if (this.y > p.maxY)
|
|
1667
|
+
this.y = p.minY;
|
|
1668
|
+
}
|
|
1669
|
+
if (this.animation && this.animation.fps > 0) {
|
|
1670
|
+
const a = this.animation;
|
|
1671
|
+
a.elapsed += dt;
|
|
1672
|
+
while (a.elapsed >= 1 / a.fps) {
|
|
1673
|
+
a.elapsed -= 1 / a.fps;
|
|
1674
|
+
if (a.index < a.frames.length - 1)
|
|
1675
|
+
a.index++;
|
|
1676
|
+
else if (a.loop)
|
|
1677
|
+
a.index = 0;
|
|
1678
|
+
else {
|
|
1679
|
+
this.animation = undefined;
|
|
1680
|
+
break;
|
|
1681
|
+
}
|
|
1682
|
+
this.frame = a.frames[a.index];
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
if (this.update)
|
|
1686
|
+
p.invoke(() => this.update(this, dt));
|
|
1687
|
+
for (const t of this.touches) {
|
|
1688
|
+
const candidates = typeof t.target === "string" ? p.sprites.filter((s) => s.tags.includes(t.target)) : [t.target];
|
|
1689
|
+
const now = new Set(candidates.filter((s) => this.touching(s)));
|
|
1690
|
+
for (const s of now)
|
|
1691
|
+
if (!t.active.has(s))
|
|
1692
|
+
p.invoke(() => t.fn(s));
|
|
1693
|
+
t.active = now;
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
render(ctx) {
|
|
1697
|
+
renderSprite(this, ctx);
|
|
1698
|
+
}
|
|
1699
|
+
textEval() {
|
|
1700
|
+
const v = typeof this.text === "function" ? this.text() : this.text;
|
|
1701
|
+
return String(v ?? "");
|
|
1702
|
+
}
|
|
1703
|
+
canvasX() {
|
|
1704
|
+
return this.x - this.project.cameraX + this.project.width / 2;
|
|
1705
|
+
}
|
|
1706
|
+
canvasY() {
|
|
1707
|
+
return this.project.height / 2 - (this.y - this.project.cameraY);
|
|
1708
|
+
}
|
|
1709
|
+
setImageURL(url) {
|
|
1710
|
+
this.imageUrl = url;
|
|
1711
|
+
const image = this.project.createImage();
|
|
1712
|
+
if (image) {
|
|
1713
|
+
image.src = url;
|
|
1714
|
+
this.imageElement = image;
|
|
1715
|
+
}
|
|
1716
|
+
return this;
|
|
1717
|
+
}
|
|
1718
|
+
addCostume(url) {
|
|
1719
|
+
this.costumes.push(url);
|
|
1720
|
+
return this;
|
|
1721
|
+
}
|
|
1722
|
+
switchCostume(costume) {
|
|
1723
|
+
const index = typeof costume === "number" ? costume : this.costumes.indexOf(costume);
|
|
1724
|
+
if (!Number.isInteger(index) || index < 0 || index >= this.costumes.length)
|
|
1725
|
+
throw new RangeError("Costume was not found; try switchCostume(0).");
|
|
1726
|
+
this.costumeIndex = index;
|
|
1727
|
+
return this.setImageURL(this.costumes[index]);
|
|
1728
|
+
}
|
|
1729
|
+
nextCostume() {
|
|
1730
|
+
if (this.costumes.length)
|
|
1731
|
+
this.switchCostume((this.costume + 1) % this.costumes.length);
|
|
1732
|
+
return this;
|
|
1733
|
+
}
|
|
1734
|
+
animate(o) {
|
|
1735
|
+
const frames = o.frames ?? Array.from({ length: this.frames?.count ?? 1 }, (_, i) => i);
|
|
1736
|
+
if (!frames.length || o.fps <= 0)
|
|
1737
|
+
throw new RangeError("animate requires positive fps and frames; try animate({ fps: 10 }).");
|
|
1738
|
+
this.animation = {
|
|
1739
|
+
fps: o.fps,
|
|
1740
|
+
frames,
|
|
1741
|
+
loop: o.loop ?? true,
|
|
1742
|
+
elapsed: 0,
|
|
1743
|
+
index: 0
|
|
1744
|
+
};
|
|
1745
|
+
this.frame = frames[0];
|
|
1746
|
+
return this;
|
|
1747
|
+
}
|
|
1748
|
+
penDown() {
|
|
1749
|
+
this.penActive = true;
|
|
1750
|
+
return this;
|
|
1751
|
+
}
|
|
1752
|
+
penUp() {
|
|
1753
|
+
this.penActive = false;
|
|
1754
|
+
return this;
|
|
1755
|
+
}
|
|
1756
|
+
show() {
|
|
1757
|
+
this.showing = true;
|
|
1758
|
+
for (const child of this.components)
|
|
1759
|
+
child.show();
|
|
1760
|
+
return this;
|
|
1761
|
+
}
|
|
1762
|
+
hide() {
|
|
1763
|
+
this.showing = false;
|
|
1764
|
+
for (const child of this.components)
|
|
1765
|
+
child.hide();
|
|
1766
|
+
return this;
|
|
1767
|
+
}
|
|
1768
|
+
sendToFront() {
|
|
1769
|
+
this.layer = Math.max(0, ...this.project.sprites.map((s) => s.layer)) + 1;
|
|
1770
|
+
for (const child of this.components)
|
|
1771
|
+
child.sendToFront();
|
|
1772
|
+
return this;
|
|
1773
|
+
}
|
|
1774
|
+
sendToBack() {
|
|
1775
|
+
this.layer = Math.min(0, ...this.project.sprites.map((s) => s.layer)) - 1;
|
|
1776
|
+
for (const child of [...this.components].reverse())
|
|
1777
|
+
child.sendToBack();
|
|
1778
|
+
return this;
|
|
1779
|
+
}
|
|
1780
|
+
delete() {
|
|
1781
|
+
if (!this.deleted) {
|
|
1782
|
+
this.deleted = true;
|
|
1783
|
+
this.showing = false;
|
|
1784
|
+
this.detachListeners();
|
|
1785
|
+
this.touches = [];
|
|
1786
|
+
this.animation = undefined;
|
|
1787
|
+
for (const task of this.ownedTasks)
|
|
1788
|
+
task.stop();
|
|
1789
|
+
this.ownedTasks.clear();
|
|
1790
|
+
this.parentBundle?.removeComponent(this);
|
|
1791
|
+
for (const c of [...this.components])
|
|
1792
|
+
c.delete();
|
|
1793
|
+
this.components = [];
|
|
1794
|
+
this.project.removeSprite(this);
|
|
1795
|
+
}
|
|
1796
|
+
return this;
|
|
1797
|
+
}
|
|
1798
|
+
remove() {
|
|
1799
|
+
return this.delete();
|
|
1800
|
+
}
|
|
1801
|
+
addComponent(sprite, offsetX, offsetY, offsetAngle) {
|
|
1802
|
+
if (sprite.deleted || this.deleted || sprite === this || sprite.project !== this.project || sprite.type === "bundle" && sprite.containsComponent(this)) {
|
|
1803
|
+
throw new Error("Invalid bundle component.");
|
|
1804
|
+
}
|
|
1805
|
+
if (this.components.includes(sprite))
|
|
1806
|
+
return this;
|
|
1807
|
+
if (this.scale === 0)
|
|
1808
|
+
throw new RangeError("Cannot add a component to a bundle with zero scale.");
|
|
1809
|
+
sprite.parentBundle?.removeComponent(sprite);
|
|
1810
|
+
sprite.parentBundle = this;
|
|
1811
|
+
const cosine = Math.cos(this.radians());
|
|
1812
|
+
const sine = Math.sin(this.radians());
|
|
1813
|
+
const dx = sprite.x - this.x;
|
|
1814
|
+
const dy = sprite.y - this.y;
|
|
1815
|
+
sprite.offsetX = offsetX ?? (dx * cosine + dy * sine) / this.scale;
|
|
1816
|
+
sprite.offsetY = offsetY ?? (dy * cosine - dx * sine) / this.scale;
|
|
1817
|
+
sprite.offsetAngle = offsetAngle ?? sprite.angle - this.angle;
|
|
1818
|
+
this.components.push(sprite);
|
|
1819
|
+
if (!this.showing)
|
|
1820
|
+
sprite.hide();
|
|
1821
|
+
sprite.brightness = this.brightness;
|
|
1822
|
+
if (offsetX !== undefined || offsetY !== undefined || offsetAngle !== undefined)
|
|
1823
|
+
this.arrangeComponents();
|
|
1824
|
+
return this;
|
|
1825
|
+
}
|
|
1826
|
+
containsComponent(sprite) {
|
|
1827
|
+
return this.components.some((child) => child === sprite || child.containsComponent(sprite));
|
|
1828
|
+
}
|
|
1829
|
+
removeComponent(sprite) {
|
|
1830
|
+
this.components = this.components.filter((s) => s !== sprite);
|
|
1831
|
+
if (sprite.parentBundle === this)
|
|
1832
|
+
sprite.parentBundle = undefined;
|
|
1833
|
+
return this;
|
|
1834
|
+
}
|
|
1835
|
+
set(props) {
|
|
1836
|
+
Object.assign(this, props);
|
|
1837
|
+
return this;
|
|
1838
|
+
}
|
|
1839
|
+
toJSON() {
|
|
1840
|
+
return {
|
|
1841
|
+
x: this.x,
|
|
1842
|
+
y: this.y,
|
|
1843
|
+
angle: this.angle,
|
|
1844
|
+
rotationStyle: this.rotationStyle,
|
|
1845
|
+
showing: this.showing,
|
|
1846
|
+
penDown: this.penActive,
|
|
1847
|
+
penColor: this.penColor,
|
|
1848
|
+
penWidth: this.penWidth,
|
|
1849
|
+
width: this.width,
|
|
1850
|
+
height: this.height,
|
|
1851
|
+
radius: this.radius,
|
|
1852
|
+
sides: this.sides,
|
|
1853
|
+
length: this.length,
|
|
1854
|
+
x1: this.x1,
|
|
1855
|
+
y1: this.y1,
|
|
1856
|
+
color: this.color,
|
|
1857
|
+
size: this.size,
|
|
1858
|
+
text: this.text,
|
|
1859
|
+
textAlign: this.textAlign,
|
|
1860
|
+
fontFamily: this.fontFamily,
|
|
1861
|
+
url: this.url,
|
|
1862
|
+
opacity: this.opacity,
|
|
1863
|
+
scale: this.scale,
|
|
1864
|
+
vx: this.vx,
|
|
1865
|
+
vy: this.vy,
|
|
1866
|
+
gravity: this.gravity,
|
|
1867
|
+
bounce: this.bounce,
|
|
1868
|
+
wrap: this.wrap,
|
|
1869
|
+
tags: [...this.tags],
|
|
1870
|
+
data: { ...this.data },
|
|
1871
|
+
layer: this.layer,
|
|
1872
|
+
draw: this.draw,
|
|
1873
|
+
update: this.update,
|
|
1874
|
+
flipX: this.flipX,
|
|
1875
|
+
flipY: this.flipY,
|
|
1876
|
+
frames: this.frames,
|
|
1877
|
+
costumes: [...this.costumes]
|
|
1878
|
+
};
|
|
1879
|
+
}
|
|
1880
|
+
clone(changes = {}) {
|
|
1881
|
+
const copy = this.project.createSprite(this.type, this.toJSON());
|
|
1882
|
+
if (this.costumes.length && this.url === this.costumes[this.costumeIndex])
|
|
1883
|
+
copy.switchCostume(this.costumeIndex);
|
|
1884
|
+
for (const child of this.components)
|
|
1885
|
+
copy.addComponent(child.clone());
|
|
1886
|
+
return copy.set(changes);
|
|
1887
|
+
}
|
|
1888
|
+
tween(props, seconds, easing = "linear") {
|
|
1889
|
+
const curve = typeof easing === "function" ? easing : easings[easing];
|
|
1890
|
+
if (!curve)
|
|
1891
|
+
throw new RangeError(`Unknown easing: ${String(easing)}; try ${Object.keys(easings).join(", ")}.`);
|
|
1892
|
+
const start = Object.fromEntries(Object.keys(props).map((k) => [
|
|
1893
|
+
k,
|
|
1894
|
+
this[k]
|
|
1895
|
+
]));
|
|
1896
|
+
let elapsed = 0;
|
|
1897
|
+
return new Promise((resolve) => {
|
|
1898
|
+
if (seconds <= 0) {
|
|
1899
|
+
Object.assign(this, props);
|
|
1900
|
+
resolve();
|
|
1901
|
+
return;
|
|
1902
|
+
}
|
|
1903
|
+
const h = this.project.onUpdate((dt) => {
|
|
1904
|
+
elapsed += dt;
|
|
1905
|
+
const t = curve(Math.min(1, elapsed / seconds));
|
|
1906
|
+
for (const [k, v] of Object.entries(props))
|
|
1907
|
+
this[k] = start[k] + (v - start[k]) * t;
|
|
1908
|
+
if (elapsed >= seconds) {
|
|
1909
|
+
task.stop();
|
|
1910
|
+
}
|
|
1911
|
+
});
|
|
1912
|
+
const task = {
|
|
1913
|
+
stop: () => {
|
|
1914
|
+
h.stop();
|
|
1915
|
+
this.ownedTasks.delete(task);
|
|
1916
|
+
resolve();
|
|
1917
|
+
}
|
|
1918
|
+
};
|
|
1919
|
+
this.ownedTasks.add(task);
|
|
1920
|
+
});
|
|
1921
|
+
}
|
|
1922
|
+
glideTo(x, y, seconds) {
|
|
1923
|
+
return this.tween({ x, y }, seconds);
|
|
1924
|
+
}
|
|
1925
|
+
say(text, seconds) {
|
|
1926
|
+
this.data.say = text;
|
|
1927
|
+
if (seconds !== undefined) {
|
|
1928
|
+
const task = this.project.after(seconds, () => {
|
|
1929
|
+
this.ownedTasks.delete(task);
|
|
1930
|
+
if (this.data.say === text)
|
|
1931
|
+
delete this.data.say;
|
|
1932
|
+
});
|
|
1933
|
+
this.ownedTasks.add(task);
|
|
1934
|
+
}
|
|
1935
|
+
return this;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
// src/world/render.ts
|
|
1940
|
+
function renderOne(sprite, context) {
|
|
1941
|
+
if (sprite.type === "sprite" && sprite.render !== Sprite.prototype.render) {
|
|
1942
|
+
renderSprite(sprite, context);
|
|
1943
|
+
} else {
|
|
1944
|
+
sprite.render(context);
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
function renderWorld(project, pen, shake) {
|
|
1948
|
+
const context = project.context;
|
|
1949
|
+
if (typeof context.clearRect !== "function")
|
|
1950
|
+
return;
|
|
1951
|
+
context.clearRect(0, 0, project.width, project.height);
|
|
1952
|
+
if (shake !== undefined) {
|
|
1953
|
+
context.save();
|
|
1954
|
+
context.translate(project.randomFloat(-shake, shake), project.randomFloat(-shake, shake));
|
|
1955
|
+
}
|
|
1956
|
+
for (const segment of pen) {
|
|
1957
|
+
context.beginPath();
|
|
1958
|
+
context.moveTo(segment.x1 - project.cameraX + project.width / 2, project.height / 2 - (segment.y1 - project.cameraY));
|
|
1959
|
+
context.lineTo(segment.x2 - project.cameraX + project.width / 2, project.height / 2 - (segment.y2 - project.cameraY));
|
|
1960
|
+
context.strokeStyle = segment.color;
|
|
1961
|
+
context.lineWidth = segment.width;
|
|
1962
|
+
context.lineCap = "round";
|
|
1963
|
+
context.stroke();
|
|
1964
|
+
}
|
|
1965
|
+
for (const sprite of [...project.sprites].sort((a, b) => a.layer - b.layer)) {
|
|
1966
|
+
project.invoke(() => renderOne(sprite, context));
|
|
1967
|
+
}
|
|
1968
|
+
if (shake !== undefined)
|
|
1969
|
+
context.restore();
|
|
1970
|
+
}
|
|
1971
|
+
function preciseCollision(project, document2, a, b) {
|
|
1972
|
+
const canvas = document2.createElement("canvas");
|
|
1973
|
+
canvas.width = project.width;
|
|
1974
|
+
canvas.height = project.height;
|
|
1975
|
+
const context = canvas.getContext("2d");
|
|
1976
|
+
if (!context || typeof context.getImageData !== "function") {
|
|
1977
|
+
throw new Error("Precise collision requires readable canvas pixels.");
|
|
1978
|
+
}
|
|
1979
|
+
try {
|
|
1980
|
+
renderOne(a, context);
|
|
1981
|
+
context.globalCompositeOperation = "source-in";
|
|
1982
|
+
renderOne(b, context);
|
|
1983
|
+
const pixels = context.getImageData(0, 0, project.width, project.height).data;
|
|
1984
|
+
for (let i = 3;i < pixels.length; i += 4) {
|
|
1985
|
+
if (pixels[i])
|
|
1986
|
+
return true;
|
|
1987
|
+
}
|
|
1988
|
+
return false;
|
|
1989
|
+
} catch (error) {
|
|
1990
|
+
throw new Error("Precise collision could not read canvas pixels; an image may be cross-origin or tainted.", { cause: error });
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
// src/world/audio.ts
|
|
1995
|
+
var volumes = { mute: 0, low: 0.5, normal: 1 };
|
|
1996
|
+
var speeds = { slow: 0.5, normal: 1, fast: 2 };
|
|
1997
|
+
|
|
1998
|
+
class ProjectSound {
|
|
1999
|
+
context;
|
|
2000
|
+
destination;
|
|
2001
|
+
fetcher;
|
|
2002
|
+
onUpdate;
|
|
2003
|
+
maxGain;
|
|
2004
|
+
limiter;
|
|
2005
|
+
loopValue = false;
|
|
2006
|
+
urlValue;
|
|
2007
|
+
baseVolume;
|
|
2008
|
+
rate;
|
|
2009
|
+
multiplier = 1;
|
|
2010
|
+
source;
|
|
2011
|
+
gain;
|
|
2012
|
+
panNode;
|
|
2013
|
+
buffer;
|
|
2014
|
+
loading;
|
|
2015
|
+
offset = 0;
|
|
2016
|
+
startedAt = 0;
|
|
2017
|
+
played = false;
|
|
2018
|
+
generation = 0;
|
|
2019
|
+
panValue = 0;
|
|
2020
|
+
fade;
|
|
2021
|
+
audioSettings = { speed: { ...speeds }, volume: { ...volumes } };
|
|
2022
|
+
get audio() {
|
|
2023
|
+
return this.source;
|
|
2024
|
+
}
|
|
2025
|
+
isPlaying = false;
|
|
2026
|
+
constructor(context, destination, fetcher, options = {}, onUpdate) {
|
|
2027
|
+
this.context = context;
|
|
2028
|
+
this.destination = destination;
|
|
2029
|
+
this.fetcher = fetcher;
|
|
2030
|
+
this.onUpdate = onUpdate;
|
|
2031
|
+
this.urlValue = options.url;
|
|
2032
|
+
this.buffer = options.buffer;
|
|
2033
|
+
this.loop = options.loop ?? false;
|
|
2034
|
+
this.maxGain = options.maxGain ?? 4;
|
|
2035
|
+
this.limiter = options.limiter ?? true;
|
|
2036
|
+
if (!Number.isFinite(this.maxGain) || this.maxGain < 0)
|
|
2037
|
+
throw new RangeError("maxGain must be non-negative; try maxGain: 4.");
|
|
2038
|
+
this.baseVolume = 1;
|
|
2039
|
+
this.rate = 1;
|
|
2040
|
+
this.volume = options.volume ?? 1;
|
|
2041
|
+
this.speed = options.speed ?? 1;
|
|
2042
|
+
}
|
|
2043
|
+
get loop() {
|
|
2044
|
+
return this.loopValue;
|
|
2045
|
+
}
|
|
2046
|
+
set loop(value) {
|
|
2047
|
+
if (typeof value !== "boolean")
|
|
2048
|
+
throw new TypeError("loop must be true or false; try sound.loop = true.");
|
|
2049
|
+
this.loopValue = value;
|
|
2050
|
+
if (this.source)
|
|
2051
|
+
this.source.loop = value;
|
|
2052
|
+
}
|
|
2053
|
+
get url() {
|
|
2054
|
+
return this.urlValue;
|
|
2055
|
+
}
|
|
2056
|
+
set url(value) {
|
|
2057
|
+
if (value !== undefined && typeof value !== "string")
|
|
2058
|
+
throw new TypeError("url must be a string; try sound.url = 'bell.wav'.");
|
|
2059
|
+
if (value === this.urlValue)
|
|
2060
|
+
return;
|
|
2061
|
+
this.stopPlaying();
|
|
2062
|
+
this.urlValue = value;
|
|
2063
|
+
this.buffer = undefined;
|
|
2064
|
+
this.loading = undefined;
|
|
2065
|
+
this.played = false;
|
|
2066
|
+
}
|
|
2067
|
+
get volume() {
|
|
2068
|
+
return this.baseVolume;
|
|
2069
|
+
}
|
|
2070
|
+
set volume(v) {
|
|
2071
|
+
const n = typeof v === "string" ? volumes[v] : v;
|
|
2072
|
+
if (!Number.isFinite(n) || n < 0 || n > 1)
|
|
2073
|
+
throw new RangeError("volume must be between 0 and 1; try volume: 0.5.");
|
|
2074
|
+
this.baseVolume = n;
|
|
2075
|
+
this.applyGain();
|
|
2076
|
+
}
|
|
2077
|
+
get speed() {
|
|
2078
|
+
return this.rate;
|
|
2079
|
+
}
|
|
2080
|
+
set speed(v) {
|
|
2081
|
+
const n = typeof v === "string" ? speeds[v] : v;
|
|
2082
|
+
if (!Number.isFinite(n) || n <= 0)
|
|
2083
|
+
throw new RangeError("speed must be positive; try speed: 1.");
|
|
2084
|
+
if (this.isPlaying) {
|
|
2085
|
+
this.offset = this.currentTime;
|
|
2086
|
+
this.startedAt = this.context().currentTime;
|
|
2087
|
+
}
|
|
2088
|
+
this.rate = n;
|
|
2089
|
+
if (this.source)
|
|
2090
|
+
this.source.playbackRate.value = n;
|
|
2091
|
+
}
|
|
2092
|
+
get duration() {
|
|
2093
|
+
return this.buffer?.duration ?? 0;
|
|
2094
|
+
}
|
|
2095
|
+
get currentTime() {
|
|
2096
|
+
const t = this.offset + (this.isPlaying ? (this.context().currentTime - this.startedAt) * this.rate : 0);
|
|
2097
|
+
return this.loop && this.duration ? t % this.duration : Math.min(this.duration || t, t);
|
|
2098
|
+
}
|
|
2099
|
+
set currentTime(v) {
|
|
2100
|
+
if (!Number.isFinite(v) || v < 0 || this.duration && v > this.duration)
|
|
2101
|
+
throw new RangeError("currentTime must be within the sound duration.");
|
|
2102
|
+
const playing = this.isPlaying;
|
|
2103
|
+
if (playing)
|
|
2104
|
+
this.pausePlaying();
|
|
2105
|
+
this.offset = v;
|
|
2106
|
+
if (playing)
|
|
2107
|
+
this.startPlaying();
|
|
2108
|
+
}
|
|
2109
|
+
neverPlayed() {
|
|
2110
|
+
return !this.played;
|
|
2111
|
+
}
|
|
2112
|
+
applyGain() {
|
|
2113
|
+
if (this.gain)
|
|
2114
|
+
this.gain.gain.value = this.multiplier === "max" ? this.maxGain : Math.min(this.maxGain, Math.max(0, this.baseVolume * this.multiplier));
|
|
2115
|
+
}
|
|
2116
|
+
amplify(n) {
|
|
2117
|
+
if (n !== "max" && (!Number.isFinite(n) || n < 0))
|
|
2118
|
+
throw new RangeError("amplify requires a non-negative number or 'max'.");
|
|
2119
|
+
this.multiplier = n;
|
|
2120
|
+
this.applyGain();
|
|
2121
|
+
return this;
|
|
2122
|
+
}
|
|
2123
|
+
load() {
|
|
2124
|
+
if (this.buffer)
|
|
2125
|
+
return Promise.resolve(this.buffer);
|
|
2126
|
+
if (!this.url)
|
|
2127
|
+
return Promise.reject(new Error("sound requires a url or buffer."));
|
|
2128
|
+
if (this.loading)
|
|
2129
|
+
return this.loading;
|
|
2130
|
+
const url = this.url;
|
|
2131
|
+
const loading = this.fetcher(url).then((response) => {
|
|
2132
|
+
if (!response.ok)
|
|
2133
|
+
throw new Error(`Could not load sound: ${url}`);
|
|
2134
|
+
return response.arrayBuffer();
|
|
2135
|
+
}).then((data) => this.context().decodeAudioData(data)).then((buffer) => {
|
|
2136
|
+
if (this.loading === loading)
|
|
2137
|
+
this.buffer = buffer;
|
|
2138
|
+
return buffer;
|
|
2139
|
+
}).catch((error) => {
|
|
2140
|
+
if (this.loading === loading)
|
|
2141
|
+
this.loading = undefined;
|
|
2142
|
+
throw error;
|
|
2143
|
+
});
|
|
2144
|
+
this.loading = loading;
|
|
2145
|
+
return loading;
|
|
2146
|
+
}
|
|
2147
|
+
startPlaying() {
|
|
2148
|
+
if (this.isPlaying)
|
|
2149
|
+
return this;
|
|
2150
|
+
this.isPlaying = true;
|
|
2151
|
+
this.played = true;
|
|
2152
|
+
const generation = ++this.generation;
|
|
2153
|
+
const start = (buffer) => {
|
|
2154
|
+
if (!this.isPlaying || generation !== this.generation)
|
|
2155
|
+
return;
|
|
2156
|
+
const ctx = this.context(), source = ctx.createBufferSource(), gain = ctx.createGain();
|
|
2157
|
+
this.source = source;
|
|
2158
|
+
this.gain = gain;
|
|
2159
|
+
source.buffer = buffer;
|
|
2160
|
+
source.loop = this.loop;
|
|
2161
|
+
source.playbackRate.value = this.rate;
|
|
2162
|
+
source.connect(gain);
|
|
2163
|
+
let node = gain;
|
|
2164
|
+
if (typeof ctx.createStereoPanner === "function") {
|
|
2165
|
+
this.panNode = ctx.createStereoPanner();
|
|
2166
|
+
this.panNode.pan.value = this.panValue;
|
|
2167
|
+
node.connect(this.panNode);
|
|
2168
|
+
node = this.panNode;
|
|
2169
|
+
}
|
|
2170
|
+
if (this.limiter && typeof ctx.createDynamicsCompressor === "function") {
|
|
2171
|
+
const compressor = ctx.createDynamicsCompressor();
|
|
2172
|
+
node.connect(compressor);
|
|
2173
|
+
node = compressor;
|
|
2174
|
+
}
|
|
2175
|
+
node.connect(this.destination(ctx));
|
|
2176
|
+
this.applyGain();
|
|
2177
|
+
this.beginFade();
|
|
2178
|
+
this.startedAt = ctx.currentTime;
|
|
2179
|
+
source.onended = () => {
|
|
2180
|
+
if (this.source === source) {
|
|
2181
|
+
this.isPlaying = false;
|
|
2182
|
+
this.offset = 0;
|
|
2183
|
+
this.source = undefined;
|
|
2184
|
+
}
|
|
2185
|
+
};
|
|
2186
|
+
source.start(0, this.offset);
|
|
2187
|
+
};
|
|
2188
|
+
if (this.buffer)
|
|
2189
|
+
start(this.buffer);
|
|
2190
|
+
else
|
|
2191
|
+
this.load().then(start).catch((error) => {
|
|
2192
|
+
if (generation === this.generation)
|
|
2193
|
+
this.isPlaying = false;
|
|
2194
|
+
console.error(error);
|
|
2195
|
+
});
|
|
2196
|
+
return this;
|
|
2197
|
+
}
|
|
2198
|
+
pausePlaying() {
|
|
2199
|
+
this.cancelFade();
|
|
2200
|
+
if (this.isPlaying) {
|
|
2201
|
+
if (this.source)
|
|
2202
|
+
this.offset = this.currentTime;
|
|
2203
|
+
this.isPlaying = false;
|
|
2204
|
+
this.generation++;
|
|
2205
|
+
const s = this.source;
|
|
2206
|
+
this.source = undefined;
|
|
2207
|
+
s?.stop();
|
|
2208
|
+
}
|
|
2209
|
+
return this;
|
|
2210
|
+
}
|
|
2211
|
+
stopPlaying() {
|
|
2212
|
+
this.pausePlaying();
|
|
2213
|
+
this.offset = 0;
|
|
2214
|
+
return this;
|
|
2215
|
+
}
|
|
2216
|
+
pan(value) {
|
|
2217
|
+
if (!Number.isFinite(value))
|
|
2218
|
+
throw new TypeError("pan requires a number; try pan(-1).");
|
|
2219
|
+
this.panValue = Math.max(-1, Math.min(1, value));
|
|
2220
|
+
if (this.panNode)
|
|
2221
|
+
this.panNode.pan.value = this.panValue;
|
|
2222
|
+
return this;
|
|
2223
|
+
}
|
|
2224
|
+
cancelFade() {
|
|
2225
|
+
this.fade?.task?.stop();
|
|
2226
|
+
this.fade = undefined;
|
|
2227
|
+
}
|
|
2228
|
+
beginFade() {
|
|
2229
|
+
const fade = this.fade;
|
|
2230
|
+
if (!fade || !this.gain || fade.task)
|
|
2231
|
+
return;
|
|
2232
|
+
const gain = this.gain.gain;
|
|
2233
|
+
const target = this.multiplier === "max" ? this.maxGain : Math.min(this.maxGain, this.baseVolume * this.multiplier);
|
|
2234
|
+
const start = fade.direction === "in" ? 0 : gain.value;
|
|
2235
|
+
const end = fade.direction === "in" ? target : 0;
|
|
2236
|
+
gain.value = start;
|
|
2237
|
+
const complete = () => {
|
|
2238
|
+
gain.value = end;
|
|
2239
|
+
this.cancelFade();
|
|
2240
|
+
if (fade.direction === "out")
|
|
2241
|
+
this.stopPlaying();
|
|
2242
|
+
};
|
|
2243
|
+
if (fade.seconds <= 0) {
|
|
2244
|
+
complete();
|
|
2245
|
+
return;
|
|
2246
|
+
}
|
|
2247
|
+
if (!this.onUpdate)
|
|
2248
|
+
return;
|
|
2249
|
+
let elapsed = 0;
|
|
2250
|
+
fade.task = this.onUpdate((dt) => {
|
|
2251
|
+
elapsed += dt;
|
|
2252
|
+
gain.value = start + (end - start) * Math.min(1, elapsed / fade.seconds);
|
|
2253
|
+
if (elapsed >= fade.seconds)
|
|
2254
|
+
complete();
|
|
2255
|
+
});
|
|
2256
|
+
}
|
|
2257
|
+
fadeIn(seconds) {
|
|
2258
|
+
if (!Number.isFinite(seconds) || seconds < 0)
|
|
2259
|
+
throw new RangeError("Fade duration must be non-negative.");
|
|
2260
|
+
this.cancelFade();
|
|
2261
|
+
this.fade = { direction: "in", seconds };
|
|
2262
|
+
this.startPlaying();
|
|
2263
|
+
this.beginFade();
|
|
2264
|
+
return this;
|
|
2265
|
+
}
|
|
2266
|
+
fadeOut(seconds) {
|
|
2267
|
+
if (!Number.isFinite(seconds) || seconds < 0)
|
|
2268
|
+
throw new RangeError("Fade duration must be non-negative.");
|
|
2269
|
+
this.cancelFade();
|
|
2270
|
+
if (this.isPlaying) {
|
|
2271
|
+
this.fade = { direction: "out", seconds };
|
|
2272
|
+
this.beginFade();
|
|
2273
|
+
}
|
|
2274
|
+
return this;
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
// src/world/scheduler.ts
|
|
2279
|
+
function duration(value, units = "seconds") {
|
|
2280
|
+
const resolved = typeof value === "function" ? value() : value;
|
|
2281
|
+
let amount;
|
|
2282
|
+
let unit = units;
|
|
2283
|
+
if (typeof resolved === "string") {
|
|
2284
|
+
const match = /^\s*(\d+(?:\.\d+)?)\s*(ms|milliseconds?|seconds?|s|minutes?|m)\s*$/i.exec(resolved);
|
|
2285
|
+
if (!match)
|
|
2286
|
+
throw new TypeError("Invalid duration; try '250ms' or 2 seconds.");
|
|
2287
|
+
amount = Number(match[1]);
|
|
2288
|
+
unit = match[2].toLowerCase();
|
|
2289
|
+
} else
|
|
2290
|
+
amount = resolved;
|
|
2291
|
+
if (!Number.isFinite(amount) || amount < 0)
|
|
2292
|
+
throw new RangeError("Duration must be non-negative; try 2 seconds.");
|
|
2293
|
+
if (/^(ms|milliseconds?|miliseconds?)$/.test(unit))
|
|
2294
|
+
return amount / 1000;
|
|
2295
|
+
if (/^(m|minutes?)$/.test(unit))
|
|
2296
|
+
return amount * 60;
|
|
2297
|
+
if (/^(s|seconds?)$/.test(unit))
|
|
2298
|
+
return amount;
|
|
2299
|
+
throw new TypeError("Unknown time unit; try 'seconds'.");
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
class Scheduler {
|
|
2303
|
+
onError;
|
|
2304
|
+
tasks = new Set;
|
|
2305
|
+
constructor(onError = console.error) {
|
|
2306
|
+
this.onError = onError;
|
|
2307
|
+
}
|
|
2308
|
+
add(tick) {
|
|
2309
|
+
const task = {
|
|
2310
|
+
active: true,
|
|
2311
|
+
stop: () => {
|
|
2312
|
+
task.active = false;
|
|
2313
|
+
this.tasks.delete(task);
|
|
2314
|
+
},
|
|
2315
|
+
tick: (dt) => tick(dt, task)
|
|
2316
|
+
};
|
|
2317
|
+
this.tasks.add(task);
|
|
2318
|
+
return task;
|
|
2319
|
+
}
|
|
2320
|
+
call(fn) {
|
|
2321
|
+
try {
|
|
2322
|
+
fn();
|
|
2323
|
+
} catch (error) {
|
|
2324
|
+
this.onError(error);
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
after(time, fn) {
|
|
2328
|
+
let remaining = duration(time);
|
|
2329
|
+
return this.add((dt, task) => {
|
|
2330
|
+
remaining -= dt;
|
|
2331
|
+
if (remaining <= 0) {
|
|
2332
|
+
task.stop();
|
|
2333
|
+
this.call(fn);
|
|
2334
|
+
}
|
|
2335
|
+
});
|
|
2336
|
+
}
|
|
2337
|
+
every(time, fn) {
|
|
2338
|
+
let remaining = duration(time);
|
|
2339
|
+
if (remaining <= 0)
|
|
2340
|
+
throw new RangeError("every interval must be positive; try every(1, fn).");
|
|
2341
|
+
this.call(fn);
|
|
2342
|
+
return this.add((dt, task) => {
|
|
2343
|
+
remaining -= dt;
|
|
2344
|
+
while (task.active && remaining <= 0) {
|
|
2345
|
+
this.call(fn);
|
|
2346
|
+
if (!task.active)
|
|
2347
|
+
break;
|
|
2348
|
+
const next = duration(time);
|
|
2349
|
+
if (next <= 0) {
|
|
2350
|
+
this.onError(new RangeError("every interval must be positive"));
|
|
2351
|
+
break;
|
|
2352
|
+
}
|
|
2353
|
+
remaining += next;
|
|
2354
|
+
}
|
|
2355
|
+
});
|
|
2356
|
+
}
|
|
2357
|
+
forever(fn) {
|
|
2358
|
+
return this.add(() => this.call(fn));
|
|
2359
|
+
}
|
|
2360
|
+
when(condition, fn) {
|
|
2361
|
+
return this.add(() => this.call(() => {
|
|
2362
|
+
if (condition())
|
|
2363
|
+
fn();
|
|
2364
|
+
}));
|
|
2365
|
+
}
|
|
2366
|
+
once(condition, fn) {
|
|
2367
|
+
return this.add((_dt, task) => this.call(() => {
|
|
2368
|
+
if (condition()) {
|
|
2369
|
+
task.stop();
|
|
2370
|
+
fn();
|
|
2371
|
+
}
|
|
2372
|
+
}));
|
|
2373
|
+
}
|
|
2374
|
+
step(dt) {
|
|
2375
|
+
for (const task of [...this.tasks])
|
|
2376
|
+
if (task.active)
|
|
2377
|
+
this.call(() => task.tick(dt));
|
|
2378
|
+
}
|
|
2379
|
+
clear() {
|
|
2380
|
+
for (const task of [...this.tasks])
|
|
2381
|
+
task.stop();
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
|
|
2385
|
+
class Repeat {
|
|
2386
|
+
after;
|
|
2387
|
+
curTimes = 0;
|
|
2388
|
+
done = false;
|
|
2389
|
+
times;
|
|
2390
|
+
func;
|
|
2391
|
+
constructor(times, func, after) {
|
|
2392
|
+
this.after = after;
|
|
2393
|
+
this.times = Math.floor(times);
|
|
2394
|
+
this.func = func;
|
|
2395
|
+
}
|
|
2396
|
+
next() {
|
|
2397
|
+
if (this.done)
|
|
2398
|
+
return;
|
|
2399
|
+
if (this.curTimes >= this.times) {
|
|
2400
|
+
this.done = true;
|
|
2401
|
+
this.after?.(this.curTimes);
|
|
2402
|
+
return;
|
|
2403
|
+
}
|
|
2404
|
+
this.func(++this.curTimes);
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
class RepeatUntil {
|
|
2409
|
+
after;
|
|
2410
|
+
curTimes = 0;
|
|
2411
|
+
done = false;
|
|
2412
|
+
condition;
|
|
2413
|
+
func;
|
|
2414
|
+
constructor(condition, func, after) {
|
|
2415
|
+
this.after = after;
|
|
2416
|
+
this.condition = condition;
|
|
2417
|
+
this.func = func;
|
|
2418
|
+
}
|
|
2419
|
+
next() {
|
|
2420
|
+
if (this.done)
|
|
2421
|
+
return;
|
|
2422
|
+
if (this.condition()) {
|
|
2423
|
+
this.done = true;
|
|
2424
|
+
this.after?.(this.curTimes);
|
|
2425
|
+
return;
|
|
2426
|
+
}
|
|
2427
|
+
this.func(++this.curTimes);
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
// src/woof.ts
|
|
2432
|
+
class Project {
|
|
2433
|
+
canvas;
|
|
2434
|
+
context;
|
|
2435
|
+
helpers = exports_helpers;
|
|
2436
|
+
randomColor = randomColor;
|
|
2437
|
+
range = range;
|
|
2438
|
+
between = between;
|
|
2439
|
+
remove = remove;
|
|
2440
|
+
rgb = rgb;
|
|
2441
|
+
pick = pick;
|
|
2442
|
+
shuffle = shuffle;
|
|
2443
|
+
clamp = clamp2;
|
|
2444
|
+
lerp = lerp;
|
|
2445
|
+
distance = distance;
|
|
2446
|
+
RIGHT = RIGHT;
|
|
2447
|
+
LEFT = LEFT;
|
|
2448
|
+
UP = UP;
|
|
2449
|
+
DOWN = DOWN;
|
|
2450
|
+
sin = sin;
|
|
2451
|
+
cos = cos;
|
|
2452
|
+
tan = tan;
|
|
2453
|
+
asin = asin;
|
|
2454
|
+
acos = acos;
|
|
2455
|
+
atan = atan;
|
|
2456
|
+
sqrt = sqrt;
|
|
2457
|
+
abs = abs;
|
|
2458
|
+
floor = floor;
|
|
2459
|
+
ceiling = ceiling;
|
|
2460
|
+
ln = ln;
|
|
2461
|
+
log = log;
|
|
2462
|
+
pow = pow;
|
|
2463
|
+
hour = hour;
|
|
2464
|
+
hourMilitary = hourMilitary;
|
|
2465
|
+
minute = minute;
|
|
2466
|
+
second = second;
|
|
2467
|
+
dayOfMonth = dayOfMonth;
|
|
2468
|
+
dayOfWeek = dayOfWeek;
|
|
2469
|
+
month = month;
|
|
2470
|
+
year = year;
|
|
2471
|
+
mobile = mobile;
|
|
2472
|
+
importCodeURL = importCodeURL;
|
|
2473
|
+
Repeat = Repeat;
|
|
2474
|
+
RepeatUntil = RepeatUntil;
|
|
2475
|
+
keyCodeToString = keyCodeToString;
|
|
2476
|
+
unitsToMiliseconds = unitsToMiliseconds;
|
|
2477
|
+
months = months;
|
|
2478
|
+
throttle = throttle;
|
|
2479
|
+
getData = getData;
|
|
2480
|
+
extend = extend;
|
|
2481
|
+
width;
|
|
2482
|
+
height;
|
|
2483
|
+
fullScreen;
|
|
2484
|
+
global;
|
|
2485
|
+
restoreGlobals = [];
|
|
2486
|
+
backdrop = {
|
|
2487
|
+
color: null,
|
|
2488
|
+
type: null,
|
|
2489
|
+
url: null,
|
|
2490
|
+
size: "100% 100%",
|
|
2491
|
+
repeat: "no-repeat"
|
|
2492
|
+
};
|
|
2493
|
+
cameraX = 0;
|
|
2494
|
+
cameraY = 0;
|
|
2495
|
+
sprites = [];
|
|
2496
|
+
mouseX = 0;
|
|
2497
|
+
mouseY = 0;
|
|
2498
|
+
pMouseX = 0;
|
|
2499
|
+
pMouseY = 0;
|
|
2500
|
+
mouseSpeedX = 0;
|
|
2501
|
+
mouseSpeedY = 0;
|
|
2502
|
+
mouseDown = false;
|
|
2503
|
+
keysDown = Object.assign(new Set, {
|
|
2504
|
+
includes: (key) => this.key(key)
|
|
2505
|
+
});
|
|
2506
|
+
pendingKeys = new Set;
|
|
2507
|
+
frameKeys = new Set;
|
|
2508
|
+
listeners = [];
|
|
2509
|
+
keyDownHandlers = new Set;
|
|
2510
|
+
keyUpHandlers = new Set;
|
|
2511
|
+
pointerDownHandlers = new Set;
|
|
2512
|
+
pointerUpHandlers = new Set;
|
|
2513
|
+
pointerMoveHandlers = new Set;
|
|
2514
|
+
pen = [];
|
|
2515
|
+
doc;
|
|
2516
|
+
win;
|
|
2517
|
+
reportError;
|
|
2518
|
+
scheduler;
|
|
2519
|
+
requestFrame;
|
|
2520
|
+
cancelFrame;
|
|
2521
|
+
maxDeltaMs;
|
|
2522
|
+
frame;
|
|
2523
|
+
previous;
|
|
2524
|
+
elapsed = 0;
|
|
2525
|
+
timerEpoch = Date.now();
|
|
2526
|
+
running = false;
|
|
2527
|
+
frozen = false;
|
|
2528
|
+
freezePending = false;
|
|
2529
|
+
destroyed = false;
|
|
2530
|
+
scenes = new Map;
|
|
2531
|
+
sounds = new Set;
|
|
2532
|
+
audio;
|
|
2533
|
+
master;
|
|
2534
|
+
volume = 1;
|
|
2535
|
+
muted = false;
|
|
2536
|
+
audioFactory;
|
|
2537
|
+
fetcher;
|
|
2538
|
+
storage;
|
|
2539
|
+
randomState;
|
|
2540
|
+
following;
|
|
2541
|
+
shakeTime = 0;
|
|
2542
|
+
shakeStrength = 0;
|
|
2543
|
+
camera = {
|
|
2544
|
+
follow: (sprite, smoothing = 1) => {
|
|
2545
|
+
this.following = { sprite, smoothing };
|
|
2546
|
+
return this.camera;
|
|
2547
|
+
},
|
|
2548
|
+
shake: (strength, seconds) => {
|
|
2549
|
+
this.shakeStrength = strength;
|
|
2550
|
+
this.shakeTime = seconds;
|
|
2551
|
+
return this.camera;
|
|
2552
|
+
}
|
|
2553
|
+
};
|
|
2554
|
+
constructor(options = {}) {
|
|
2555
|
+
const doc = options.document ?? globalThis.document;
|
|
2556
|
+
if (!doc)
|
|
2557
|
+
throw new Error("woof() requires a document; pass { document } in tests.");
|
|
2558
|
+
const win = options.window ?? doc.defaultView ?? undefined;
|
|
2559
|
+
this.audioFactory = options.audioContext;
|
|
2560
|
+
this.fetcher = options.fetch ?? globalThis.fetch;
|
|
2561
|
+
this.storage = options.storage;
|
|
2562
|
+
this.randomState = options.seed;
|
|
2563
|
+
this.doc = doc;
|
|
2564
|
+
this.win = win;
|
|
2565
|
+
this.reportError = options.onError ?? ((error) => console.error(error));
|
|
2566
|
+
this.fullScreen = options.fullScreen ?? false;
|
|
2567
|
+
this.global = options.global ?? false;
|
|
2568
|
+
this.width = this.fullScreen ? win?.innerWidth ?? options.width ?? 350 : options.width ?? 350;
|
|
2569
|
+
this.height = this.fullScreen ? win?.innerHeight ?? options.height ?? 500 : options.height ?? 500;
|
|
2570
|
+
const parent = typeof options.parent === "string" ? doc.querySelector(options.parent) : options.parent ?? doc.body;
|
|
2571
|
+
if (!parent)
|
|
2572
|
+
throw new Error('Parent element was not found; try woof({ parent: "#game" }).');
|
|
2573
|
+
this.canvas = doc.createElement("canvas");
|
|
2574
|
+
this.canvas.width = this.width;
|
|
2575
|
+
this.canvas.height = this.height;
|
|
2576
|
+
this.canvas.style.backgroundSize = this.backdrop.size;
|
|
2577
|
+
this.canvas.style.backgroundRepeat = this.backdrop.repeat;
|
|
2578
|
+
const context = this.canvas.getContext("2d");
|
|
2579
|
+
if (!context)
|
|
2580
|
+
throw new Error("A 2D canvas context is required.");
|
|
2581
|
+
this.context = context;
|
|
2582
|
+
parent.appendChild(this.canvas);
|
|
2583
|
+
if (options.backdrop) {
|
|
2584
|
+
if (/^(?:https?:|data:|blob:|\/|\.\.?\/)|\.(?:png|jpe?g|gif|webp|svg)(?:[?#]|$)/i.test(options.backdrop))
|
|
2585
|
+
this.setBackdropURL(options.backdrop);
|
|
2586
|
+
else
|
|
2587
|
+
this.setBackdropColor(options.backdrop);
|
|
2588
|
+
}
|
|
2589
|
+
this.scheduler = new Scheduler(options.onError);
|
|
2590
|
+
this.requestFrame = options.requestFrame ?? win?.requestAnimationFrame?.bind(win);
|
|
2591
|
+
this.cancelFrame = options.cancelFrame ?? win?.cancelAnimationFrame?.bind(win);
|
|
2592
|
+
this.maxDeltaMs = options.maxDeltaMs ?? 100;
|
|
2593
|
+
this.installInput();
|
|
2594
|
+
if (this.global)
|
|
2595
|
+
installProjectGlobals(this, this.win, this.restoreGlobals);
|
|
2596
|
+
if (options.autoStart !== false)
|
|
2597
|
+
this.start();
|
|
2598
|
+
}
|
|
2599
|
+
get minX() {
|
|
2600
|
+
return this.cameraX - this.width / 2;
|
|
2601
|
+
}
|
|
2602
|
+
get maxX() {
|
|
2603
|
+
return this.cameraX + this.width / 2;
|
|
2604
|
+
}
|
|
2605
|
+
get minY() {
|
|
2606
|
+
return this.cameraY - this.height / 2;
|
|
2607
|
+
}
|
|
2608
|
+
get maxY() {
|
|
2609
|
+
return this.cameraY + this.height / 2;
|
|
2610
|
+
}
|
|
2611
|
+
bounds() {
|
|
2612
|
+
return {
|
|
2613
|
+
left: this.minX,
|
|
2614
|
+
right: this.maxX,
|
|
2615
|
+
top: this.maxY,
|
|
2616
|
+
bottom: this.minY
|
|
2617
|
+
};
|
|
2618
|
+
}
|
|
2619
|
+
randomFloat(a, b) {
|
|
2620
|
+
let r;
|
|
2621
|
+
if (this.randomState === undefined)
|
|
2622
|
+
r = Math.random();
|
|
2623
|
+
else {
|
|
2624
|
+
this.randomState = Math.imul(this.randomState, 1664525) + 1013904223 >>> 0;
|
|
2625
|
+
r = this.randomState / 4294967296;
|
|
2626
|
+
}
|
|
2627
|
+
return Math.min(a, b) + r * Math.abs(b - a);
|
|
2628
|
+
}
|
|
2629
|
+
random(a, b) {
|
|
2630
|
+
const min = Math.ceil(Math.min(a, b)), max = Math.floor(Math.max(a, b));
|
|
2631
|
+
if (!Number.isFinite(min) || !Number.isFinite(max) || min > max)
|
|
2632
|
+
throw new RangeError("random needs bounds containing a whole number; try random(1, 6).");
|
|
2633
|
+
return min + Math.floor(this.randomFloat(0, max - min + 1));
|
|
2634
|
+
}
|
|
2635
|
+
randomX() {
|
|
2636
|
+
return this.random(this.minX, this.maxX);
|
|
2637
|
+
}
|
|
2638
|
+
randomY() {
|
|
2639
|
+
return this.random(this.minY, this.maxY);
|
|
2640
|
+
}
|
|
2641
|
+
get freezing() {
|
|
2642
|
+
return this.freezePending;
|
|
2643
|
+
}
|
|
2644
|
+
get woofEpoch() {
|
|
2645
|
+
return new Date(this.timerEpoch);
|
|
2646
|
+
}
|
|
2647
|
+
get renderInterval() {
|
|
2648
|
+
return this.frame;
|
|
2649
|
+
}
|
|
2650
|
+
get stopped() {
|
|
2651
|
+
return !this.running || this.frozen;
|
|
2652
|
+
}
|
|
2653
|
+
ready(fn) {
|
|
2654
|
+
if (typeof fn !== "function")
|
|
2655
|
+
throw new TypeError("ready requires a callback; try ready(() => {}).");
|
|
2656
|
+
this.invoke(fn);
|
|
2657
|
+
}
|
|
2658
|
+
translateToCanvas(x, y) {
|
|
2659
|
+
return [
|
|
2660
|
+
x - this.cameraX + this.width / 2,
|
|
2661
|
+
this.height / 2 - (y - this.cameraY)
|
|
2662
|
+
];
|
|
2663
|
+
}
|
|
2664
|
+
translateToCenter(x, y) {
|
|
2665
|
+
return [
|
|
2666
|
+
x - this.width / 2 + this.cameraX,
|
|
2667
|
+
this.height / 2 - y + this.cameraY
|
|
2668
|
+
];
|
|
2669
|
+
}
|
|
2670
|
+
collider() {
|
|
2671
|
+
const b = this.bounds();
|
|
2672
|
+
return {
|
|
2673
|
+
kind: "polygon",
|
|
2674
|
+
points: [
|
|
2675
|
+
{ x: b.left, y: b.top },
|
|
2676
|
+
{ x: b.left, y: b.bottom },
|
|
2677
|
+
{ x: b.right, y: b.bottom },
|
|
2678
|
+
{ x: b.right, y: b.top }
|
|
2679
|
+
]
|
|
2680
|
+
};
|
|
2681
|
+
}
|
|
2682
|
+
setBackdropColor(color) {
|
|
2683
|
+
this.backdrop.color = color;
|
|
2684
|
+
this.backdrop.type = "color";
|
|
2685
|
+
this.canvas.style.backgroundImage = "none";
|
|
2686
|
+
this.canvas.style.backgroundColor = color;
|
|
2687
|
+
return this;
|
|
2688
|
+
}
|
|
2689
|
+
setBackdropURL(url) {
|
|
2690
|
+
this.backdrop.url = url;
|
|
2691
|
+
this.backdrop.type = "url";
|
|
2692
|
+
this.canvas.style.backgroundColor = "";
|
|
2693
|
+
this.canvas.style.backgroundImage = `url(${JSON.stringify(url)})`;
|
|
2694
|
+
return this;
|
|
2695
|
+
}
|
|
2696
|
+
setBackdropSize(width, height) {
|
|
2697
|
+
if (typeof width === "number" && typeof height === "number") {
|
|
2698
|
+
this.width = width;
|
|
2699
|
+
this.height = height;
|
|
2700
|
+
this.canvas.width = width;
|
|
2701
|
+
this.canvas.height = height;
|
|
2702
|
+
} else {
|
|
2703
|
+
this.backdrop.size = height === undefined ? String(width) : `${width} ${height}`;
|
|
2704
|
+
this.canvas.style.backgroundSize = this.backdrop.size;
|
|
2705
|
+
}
|
|
2706
|
+
return this;
|
|
2707
|
+
}
|
|
2708
|
+
setBackdropStyle(style) {
|
|
2709
|
+
this.backdrop.size = style;
|
|
2710
|
+
this.canvas.style.backgroundSize = style;
|
|
2711
|
+
return this;
|
|
2712
|
+
}
|
|
2713
|
+
setBackdropRepeat(repeat) {
|
|
2714
|
+
this.backdrop.repeat = repeat;
|
|
2715
|
+
this.canvas.style.backgroundRepeat = repeat;
|
|
2716
|
+
return this;
|
|
2717
|
+
}
|
|
2718
|
+
scene(name, setup) {
|
|
2719
|
+
this.scenes.set(name, setup);
|
|
2720
|
+
return this;
|
|
2721
|
+
}
|
|
2722
|
+
go(name, data) {
|
|
2723
|
+
const setup = this.scenes.get(name);
|
|
2724
|
+
if (!setup)
|
|
2725
|
+
throw new Error(`Unknown scene: ${name}`);
|
|
2726
|
+
this.scheduler.clear();
|
|
2727
|
+
for (const sound of this.sounds)
|
|
2728
|
+
sound.stopPlaying();
|
|
2729
|
+
this.sounds.clear();
|
|
2730
|
+
for (const s of [...this.sprites])
|
|
2731
|
+
s.delete();
|
|
2732
|
+
this.keyDownHandlers.clear();
|
|
2733
|
+
this.keyUpHandlers.clear();
|
|
2734
|
+
this.pointerDownHandlers.clear();
|
|
2735
|
+
this.pointerUpHandlers.clear();
|
|
2736
|
+
this.pointerMoveHandlers.clear();
|
|
2737
|
+
this.following = undefined;
|
|
2738
|
+
this.clearPen();
|
|
2739
|
+
this.invoke(() => setup(data));
|
|
2740
|
+
return this;
|
|
2741
|
+
}
|
|
2742
|
+
save(key, value) {
|
|
2743
|
+
try {
|
|
2744
|
+
(this.storage ?? this.win?.localStorage)?.setItem(`woof:${key}`, JSON.stringify(value));
|
|
2745
|
+
} catch {}
|
|
2746
|
+
return this;
|
|
2747
|
+
}
|
|
2748
|
+
load(key, fallback) {
|
|
2749
|
+
try {
|
|
2750
|
+
const v = (this.storage ?? this.win?.localStorage)?.getItem(`woof:${key}`);
|
|
2751
|
+
return v == null ? fallback : JSON.parse(v);
|
|
2752
|
+
} catch {
|
|
2753
|
+
return fallback;
|
|
2754
|
+
}
|
|
2755
|
+
}
|
|
2756
|
+
audioContext() {
|
|
2757
|
+
if (!this.audio) {
|
|
2758
|
+
const factory = this.audioFactory;
|
|
2759
|
+
if (factory)
|
|
2760
|
+
this.audio = typeof factory === "function" ? factory() : factory;
|
|
2761
|
+
else {
|
|
2762
|
+
const Ctor = this.win?.AudioContext ?? globalThis.AudioContext;
|
|
2763
|
+
if (!Ctor)
|
|
2764
|
+
throw new Error("AudioContext is unavailable.");
|
|
2765
|
+
this.audio = new Ctor;
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
return this.audio;
|
|
2769
|
+
}
|
|
2770
|
+
audioDestination(ctx) {
|
|
2771
|
+
if (!this.master) {
|
|
2772
|
+
this.master = ctx.createGain();
|
|
2773
|
+
this.master.gain.value = this.muted ? 0 : this.volume;
|
|
2774
|
+
this.master.connect(ctx.destination);
|
|
2775
|
+
}
|
|
2776
|
+
return this.master;
|
|
2777
|
+
}
|
|
2778
|
+
get masterVolume() {
|
|
2779
|
+
return this.volume;
|
|
2780
|
+
}
|
|
2781
|
+
set masterVolume(value) {
|
|
2782
|
+
if (!Number.isFinite(value) || value < 0)
|
|
2783
|
+
throw new RangeError("masterVolume must be non-negative.");
|
|
2784
|
+
this.volume = value;
|
|
2785
|
+
if (this.master)
|
|
2786
|
+
this.master.gain.value = this.muted ? 0 : value;
|
|
2787
|
+
}
|
|
2788
|
+
mute() {
|
|
2789
|
+
this.muted = true;
|
|
2790
|
+
if (this.master)
|
|
2791
|
+
this.master.gain.value = 0;
|
|
2792
|
+
return this;
|
|
2793
|
+
}
|
|
2794
|
+
unmute() {
|
|
2795
|
+
this.muted = false;
|
|
2796
|
+
if (this.master)
|
|
2797
|
+
this.master.gain.value = this.volume;
|
|
2798
|
+
return this;
|
|
2799
|
+
}
|
|
2800
|
+
sound(options = {}) {
|
|
2801
|
+
const sound = new ProjectSound(() => this.audioContext(), (ctx) => this.audioDestination(ctx), this.fetcher, options, (fn) => this.onUpdate(fn));
|
|
2802
|
+
this.sounds.add(sound);
|
|
2803
|
+
return sound;
|
|
2804
|
+
}
|
|
2805
|
+
tone(freq, seconds) {
|
|
2806
|
+
const ctx = this.audioContext(), osc = ctx.createOscillator();
|
|
2807
|
+
osc.frequency.value = freq;
|
|
2808
|
+
osc.connect(this.audioDestination(ctx));
|
|
2809
|
+
osc.start();
|
|
2810
|
+
osc.stop(ctx.currentTime + seconds);
|
|
2811
|
+
return this;
|
|
2812
|
+
}
|
|
2813
|
+
beep() {
|
|
2814
|
+
return this.tone(440, 0.1);
|
|
2815
|
+
}
|
|
2816
|
+
repeat(times, fn, after) {
|
|
2817
|
+
let count = 0, completed = false;
|
|
2818
|
+
const callbacks = after ? [after] : [];
|
|
2819
|
+
const task = this.forever(() => {
|
|
2820
|
+
if (count < Math.floor(times))
|
|
2821
|
+
fn(++count);
|
|
2822
|
+
else {
|
|
2823
|
+
task.stop();
|
|
2824
|
+
completed = true;
|
|
2825
|
+
for (const callback of callbacks.splice(0))
|
|
2826
|
+
this.invoke(() => callback(count));
|
|
2827
|
+
}
|
|
2828
|
+
});
|
|
2829
|
+
const handle = {
|
|
2830
|
+
stop: () => task.stop(),
|
|
2831
|
+
then: (callback) => {
|
|
2832
|
+
if (completed)
|
|
2833
|
+
this.invoke(() => callback(count));
|
|
2834
|
+
else
|
|
2835
|
+
callbacks.push(callback);
|
|
2836
|
+
return handle;
|
|
2837
|
+
}
|
|
2838
|
+
};
|
|
2839
|
+
return handle;
|
|
2840
|
+
}
|
|
2841
|
+
repeatUntil(condition, fn, after) {
|
|
2842
|
+
let count = 0;
|
|
2843
|
+
const h = this.forever(() => {
|
|
2844
|
+
if (condition()) {
|
|
2845
|
+
h.stop();
|
|
2846
|
+
after?.(count);
|
|
2847
|
+
} else
|
|
2848
|
+
fn(++count);
|
|
2849
|
+
});
|
|
2850
|
+
return h;
|
|
2851
|
+
}
|
|
2852
|
+
particles(options = {}) {
|
|
2853
|
+
const out = [];
|
|
2854
|
+
for (let i = 0;i < (options.count ?? 10); i++) {
|
|
2855
|
+
const angle = this.randomFloat(0, Math.PI * 2), speed = options.speed ?? 50;
|
|
2856
|
+
const s = this.circle({
|
|
2857
|
+
x: options.x,
|
|
2858
|
+
y: options.y,
|
|
2859
|
+
radius: options.radius ?? 2,
|
|
2860
|
+
color: options.color,
|
|
2861
|
+
vx: Math.cos(angle) * speed,
|
|
2862
|
+
vy: Math.sin(angle) * speed
|
|
2863
|
+
});
|
|
2864
|
+
out.push(s);
|
|
2865
|
+
this.after(options.life ?? 1, () => s.delete());
|
|
2866
|
+
}
|
|
2867
|
+
return out;
|
|
2868
|
+
}
|
|
2869
|
+
timer() {
|
|
2870
|
+
return this.elapsed;
|
|
2871
|
+
}
|
|
2872
|
+
resetTimer() {
|
|
2873
|
+
this.elapsed = 0;
|
|
2874
|
+
this.timerEpoch = Date.now();
|
|
2875
|
+
return this;
|
|
2876
|
+
}
|
|
2877
|
+
after(time, unitsOrFn, fn) {
|
|
2878
|
+
return this.scheduler.after(typeof unitsOrFn === "string" ? () => duration(time, unitsOrFn) : time, typeof unitsOrFn === "function" ? unitsOrFn : fn);
|
|
2879
|
+
}
|
|
2880
|
+
every(time, unitsOrFn, fn) {
|
|
2881
|
+
return this.scheduler.every(typeof unitsOrFn === "string" ? () => duration(time, unitsOrFn) : time, typeof unitsOrFn === "function" ? unitsOrFn : fn);
|
|
2882
|
+
}
|
|
2883
|
+
forever(fn) {
|
|
2884
|
+
return this.scheduler.forever(fn);
|
|
2885
|
+
}
|
|
2886
|
+
when(condition, fn) {
|
|
2887
|
+
return this.scheduler.when(condition, fn);
|
|
2888
|
+
}
|
|
2889
|
+
once(condition, fn) {
|
|
2890
|
+
return this.scheduler.once(condition, fn);
|
|
2891
|
+
}
|
|
2892
|
+
wait(seconds) {
|
|
2893
|
+
return new Promise((resolve) => {
|
|
2894
|
+
this.after(seconds, resolve);
|
|
2895
|
+
});
|
|
2896
|
+
}
|
|
2897
|
+
freeze() {
|
|
2898
|
+
if (!this.frozen) {
|
|
2899
|
+
this.freezePending = true;
|
|
2900
|
+
try {
|
|
2901
|
+
this.frozen = true;
|
|
2902
|
+
} finally {
|
|
2903
|
+
this.freezePending = false;
|
|
2904
|
+
}
|
|
2905
|
+
}
|
|
2906
|
+
return this;
|
|
2907
|
+
}
|
|
2908
|
+
defrost() {
|
|
2909
|
+
this.frozen = false;
|
|
2910
|
+
this.previous = undefined;
|
|
2911
|
+
return this;
|
|
2912
|
+
}
|
|
2913
|
+
step(ms) {
|
|
2914
|
+
if (!Number.isFinite(ms) || ms < 0)
|
|
2915
|
+
throw new RangeError("step requires non-negative milliseconds; try step(16).");
|
|
2916
|
+
this.elapsed += ms / 1000;
|
|
2917
|
+
this.frameKeys = this.pendingKeys;
|
|
2918
|
+
this.pendingKeys = new Set;
|
|
2919
|
+
this.mouseSpeedX = this.mouseX - this.pMouseX;
|
|
2920
|
+
this.mouseSpeedY = this.mouseY - this.pMouseY;
|
|
2921
|
+
this.scheduler.step(ms / 1000);
|
|
2922
|
+
if (this.following && !this.following.sprite.deleted) {
|
|
2923
|
+
const { sprite, smoothing } = this.following;
|
|
2924
|
+
const t = Math.max(0, Math.min(1, smoothing));
|
|
2925
|
+
this.cameraX += (sprite.x - this.cameraX) * t;
|
|
2926
|
+
this.cameraY += (sprite.y - this.cameraY) * t;
|
|
2927
|
+
}
|
|
2928
|
+
if (this.shakeTime > 0)
|
|
2929
|
+
this.shakeTime = Math.max(0, this.shakeTime - ms / 1000);
|
|
2930
|
+
for (const sprite of [...this.sprites])
|
|
2931
|
+
if (!sprite.deleted)
|
|
2932
|
+
sprite.advance(ms / 1000);
|
|
2933
|
+
this.render();
|
|
2934
|
+
this.pMouseX = this.mouseX;
|
|
2935
|
+
this.pMouseY = this.mouseY;
|
|
2936
|
+
this.frameKeys.clear();
|
|
2937
|
+
return this;
|
|
2938
|
+
}
|
|
2939
|
+
tick = (timestamp) => {
|
|
2940
|
+
if (!this.running)
|
|
2941
|
+
return;
|
|
2942
|
+
const delta = this.previous === undefined ? 0 : Math.min(this.maxDeltaMs, Math.max(0, timestamp - this.previous));
|
|
2943
|
+
this.previous = timestamp;
|
|
2944
|
+
if (!this.frozen)
|
|
2945
|
+
this.step(delta);
|
|
2946
|
+
if (this.running)
|
|
2947
|
+
this.frame = this.requestFrame?.(this.tick);
|
|
2948
|
+
};
|
|
2949
|
+
start() {
|
|
2950
|
+
if (this.destroyed)
|
|
2951
|
+
throw new Error("Cannot start a destroyed project.");
|
|
2952
|
+
if (!this.running) {
|
|
2953
|
+
if (!this.requestFrame)
|
|
2954
|
+
throw new Error("requestAnimationFrame is unavailable; provide requestFrame or autoStart: false.");
|
|
2955
|
+
this.running = true;
|
|
2956
|
+
this.previous = undefined;
|
|
2957
|
+
this.frame = this.requestFrame(this.tick);
|
|
2958
|
+
}
|
|
2959
|
+
return this;
|
|
2960
|
+
}
|
|
2961
|
+
stop() {
|
|
2962
|
+
this.running = false;
|
|
2963
|
+
if (this.frame !== undefined)
|
|
2964
|
+
this.cancelFrame?.(this.frame);
|
|
2965
|
+
this.frame = undefined;
|
|
2966
|
+
this.previous = undefined;
|
|
2967
|
+
return this;
|
|
2968
|
+
}
|
|
2969
|
+
invoke(fn) {
|
|
2970
|
+
try {
|
|
2971
|
+
fn();
|
|
2972
|
+
} catch (error) {
|
|
2973
|
+
this.reportError(error);
|
|
2974
|
+
}
|
|
2975
|
+
}
|
|
2976
|
+
createImage() {
|
|
2977
|
+
return this.doc.createElement("img");
|
|
2978
|
+
}
|
|
2979
|
+
createSprite(type, options = {}) {
|
|
2980
|
+
if (this.destroyed)
|
|
2981
|
+
throw new Error("Cannot create a sprite in a destroyed project.");
|
|
2982
|
+
const s = new Sprite(this, type, options);
|
|
2983
|
+
this.sprites.push(s);
|
|
2984
|
+
return s;
|
|
2985
|
+
}
|
|
2986
|
+
customSprite(definition) {
|
|
2987
|
+
return createCustomSprite(this, definition);
|
|
2988
|
+
}
|
|
2989
|
+
circle(o = {}) {
|
|
2990
|
+
return this.createSprite("circle", o);
|
|
2991
|
+
}
|
|
2992
|
+
rectangle(o = {}) {
|
|
2993
|
+
return this.createSprite("rectangle", o);
|
|
2994
|
+
}
|
|
2995
|
+
oval(o = {}) {
|
|
2996
|
+
return this.createSprite("oval", o);
|
|
2997
|
+
}
|
|
2998
|
+
polygon(o = {}) {
|
|
2999
|
+
return this.createSprite("polygon", o);
|
|
3000
|
+
}
|
|
3001
|
+
line(o = {}) {
|
|
3002
|
+
return this.createSprite("line", o);
|
|
3003
|
+
}
|
|
3004
|
+
text(o = {}) {
|
|
3005
|
+
return this.createSprite("text", o);
|
|
3006
|
+
}
|
|
3007
|
+
image(o = {}) {
|
|
3008
|
+
return this.createSprite("image", o);
|
|
3009
|
+
}
|
|
3010
|
+
bundle(o = {}) {
|
|
3011
|
+
return this.createSprite("bundle", o);
|
|
3012
|
+
}
|
|
3013
|
+
sprite(o = {}) {
|
|
3014
|
+
return this.createSprite("sprite", o);
|
|
3015
|
+
}
|
|
3016
|
+
removeSprite(sprite) {
|
|
3017
|
+
const i = this.sprites.indexOf(sprite);
|
|
3018
|
+
if (i >= 0)
|
|
3019
|
+
this.sprites.splice(i, 1);
|
|
3020
|
+
}
|
|
3021
|
+
find(tag) {
|
|
3022
|
+
return this.sprites.filter((s) => s.tags.includes(tag));
|
|
3023
|
+
}
|
|
3024
|
+
onUpdate(fn) {
|
|
3025
|
+
let previous = this.elapsed;
|
|
3026
|
+
return this.forever(() => {
|
|
3027
|
+
const dt = this.elapsed - previous;
|
|
3028
|
+
previous = this.elapsed;
|
|
3029
|
+
fn(dt);
|
|
3030
|
+
});
|
|
3031
|
+
}
|
|
3032
|
+
preciseCollision(a, b) {
|
|
3033
|
+
return preciseCollision(this, this.doc, a, b);
|
|
3034
|
+
}
|
|
3035
|
+
trackPen(x1, y1, x2, y2, active, color, width) {
|
|
3036
|
+
if (active && (x1 !== x2 || y1 !== y2))
|
|
3037
|
+
this.pen.push({ x1, y1, x2, y2, color, width });
|
|
3038
|
+
}
|
|
3039
|
+
clearPen() {
|
|
3040
|
+
this.pen = [];
|
|
3041
|
+
return this;
|
|
3042
|
+
}
|
|
3043
|
+
render() {
|
|
3044
|
+
renderWorld(this, this.pen, this.shakeTime > 0 ? this.shakeStrength : undefined);
|
|
3045
|
+
}
|
|
3046
|
+
key(key) {
|
|
3047
|
+
return this.keysDown.has(normalizeKey(key));
|
|
3048
|
+
}
|
|
3049
|
+
keyPressed(key) {
|
|
3050
|
+
const k = normalizeKey(key);
|
|
3051
|
+
return this.frameKeys.has(k) || this.pendingKeys.has(k);
|
|
3052
|
+
}
|
|
3053
|
+
onKeyDown(fn) {
|
|
3054
|
+
this.keyDownHandlers.add(fn);
|
|
3055
|
+
return () => this.keyDownHandlers.delete(fn);
|
|
3056
|
+
}
|
|
3057
|
+
onKeyUp(fn) {
|
|
3058
|
+
this.keyUpHandlers.add(fn);
|
|
3059
|
+
return () => this.keyUpHandlers.delete(fn);
|
|
3060
|
+
}
|
|
3061
|
+
onMouseDown(fn) {
|
|
3062
|
+
this.pointerDownHandlers.add(fn);
|
|
3063
|
+
return () => this.pointerDownHandlers.delete(fn);
|
|
3064
|
+
}
|
|
3065
|
+
onMouseUp(fn) {
|
|
3066
|
+
this.pointerUpHandlers.add(fn);
|
|
3067
|
+
return () => this.pointerUpHandlers.delete(fn);
|
|
3068
|
+
}
|
|
3069
|
+
onMouseMove(fn) {
|
|
3070
|
+
this.pointerMoveHandlers.add(fn);
|
|
3071
|
+
return () => this.pointerMoveHandlers.delete(fn);
|
|
3072
|
+
}
|
|
3073
|
+
get mouseXSpeed() {
|
|
3074
|
+
return this.mouseSpeedX;
|
|
3075
|
+
}
|
|
3076
|
+
get mouseYSpeed() {
|
|
3077
|
+
return this.mouseSpeedY;
|
|
3078
|
+
}
|
|
3079
|
+
gamepad(index = 0) {
|
|
3080
|
+
return this.win?.navigator?.getGamepads?.()[index] ?? null;
|
|
3081
|
+
}
|
|
3082
|
+
installInput() {
|
|
3083
|
+
const listen = (target, type, fn) => {
|
|
3084
|
+
if (!target?.addEventListener)
|
|
3085
|
+
return;
|
|
3086
|
+
target.addEventListener(type, fn);
|
|
3087
|
+
this.listeners.push(() => target.removeEventListener(type, fn));
|
|
3088
|
+
};
|
|
3089
|
+
listen(this.win, "keydown", (event) => {
|
|
3090
|
+
const k = normalizeKey(event.code || event.key);
|
|
3091
|
+
if (!this.keysDown.has(k)) {
|
|
3092
|
+
this.keysDown.add(k);
|
|
3093
|
+
this.pendingKeys.add(k);
|
|
3094
|
+
for (const fn of this.keyDownHandlers)
|
|
3095
|
+
this.invoke(() => fn(k));
|
|
3096
|
+
}
|
|
3097
|
+
});
|
|
3098
|
+
listen(this.win, "keyup", (event) => {
|
|
3099
|
+
const k = normalizeKey(event.code || event.key);
|
|
3100
|
+
this.keysDown.delete(k);
|
|
3101
|
+
for (const fn of this.keyUpHandlers)
|
|
3102
|
+
this.invoke(() => fn(k));
|
|
3103
|
+
});
|
|
3104
|
+
listen(this.win, "blur", () => {
|
|
3105
|
+
this.keysDown.clear();
|
|
3106
|
+
this.pendingKeys.clear();
|
|
3107
|
+
this.frameKeys.clear();
|
|
3108
|
+
this.mouseDown = false;
|
|
3109
|
+
});
|
|
3110
|
+
const activePointers = new Set;
|
|
3111
|
+
const pointer = (kind) => (event) => {
|
|
3112
|
+
const id = event.pointerId ?? 0;
|
|
3113
|
+
if ((kind === "up" || kind === "cancel") && !activePointers.has(id))
|
|
3114
|
+
return;
|
|
3115
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
3116
|
+
if (!rect.width || !rect.height)
|
|
3117
|
+
return;
|
|
3118
|
+
this.mouseX = (event.clientX - rect.left) * this.width / rect.width - this.width / 2 + this.cameraX;
|
|
3119
|
+
this.mouseY = this.height / 2 - (event.clientY - rect.top) * this.height / rect.height + this.cameraY;
|
|
3120
|
+
if (kind === "down") {
|
|
3121
|
+
activePointers.add(id);
|
|
3122
|
+
this.canvas.setPointerCapture?.(id);
|
|
3123
|
+
}
|
|
3124
|
+
if (kind === "up" || kind === "cancel")
|
|
3125
|
+
activePointers.delete(id);
|
|
3126
|
+
this.mouseDown = activePointers.size > 0;
|
|
3127
|
+
const handlers = kind === "down" ? this.pointerDownHandlers : kind === "move" ? this.pointerMoveHandlers : this.pointerUpHandlers;
|
|
3128
|
+
for (const fn of handlers)
|
|
3129
|
+
this.invoke(() => fn(this.mouseX, this.mouseY));
|
|
3130
|
+
if (kind !== "move")
|
|
3131
|
+
for (const sprite of [...this.sprites])
|
|
3132
|
+
sprite.pointer(kind === "down" ? "down" : "up", this.mouseX, this.mouseY, id, kind === "cancel");
|
|
3133
|
+
};
|
|
3134
|
+
listen(this.canvas, "pointerdown", pointer("down"));
|
|
3135
|
+
listen(this.canvas, "pointermove", pointer("move"));
|
|
3136
|
+
listen(this.win ?? this.canvas, "pointerup", pointer("up"));
|
|
3137
|
+
listen(this.win ?? this.canvas, "pointercancel", pointer("cancel"));
|
|
3138
|
+
if (this.win) {
|
|
3139
|
+
listen(this.canvas, "pointerup", pointer("up"));
|
|
3140
|
+
listen(this.canvas, "pointercancel", pointer("cancel"));
|
|
3141
|
+
}
|
|
3142
|
+
}
|
|
3143
|
+
destroy() {
|
|
3144
|
+
if (this.destroyed)
|
|
3145
|
+
return;
|
|
3146
|
+
this.stop();
|
|
3147
|
+
for (const sound of this.sounds)
|
|
3148
|
+
sound.stopPlaying();
|
|
3149
|
+
this.sounds.clear();
|
|
3150
|
+
this.audio?.close();
|
|
3151
|
+
this.scheduler.clear();
|
|
3152
|
+
for (const off of this.listeners)
|
|
3153
|
+
off();
|
|
3154
|
+
this.listeners = [];
|
|
3155
|
+
for (const sprite of [...this.sprites])
|
|
3156
|
+
sprite.delete();
|
|
3157
|
+
this.keysDown.clear();
|
|
3158
|
+
this.pendingKeys.clear();
|
|
3159
|
+
this.frameKeys.clear();
|
|
3160
|
+
this.mouseDown = false;
|
|
3161
|
+
this.keyDownHandlers.clear();
|
|
3162
|
+
this.keyUpHandlers.clear();
|
|
3163
|
+
this.pointerDownHandlers.clear();
|
|
3164
|
+
this.pointerUpHandlers.clear();
|
|
3165
|
+
this.pointerMoveHandlers.clear();
|
|
3166
|
+
this.scenes.clear();
|
|
3167
|
+
this.following = undefined;
|
|
3168
|
+
this.clearPen();
|
|
3169
|
+
this.canvas.remove();
|
|
3170
|
+
for (const restore of this.restoreGlobals.reverse())
|
|
3171
|
+
restore();
|
|
3172
|
+
this.restoreGlobals = [];
|
|
3173
|
+
this.destroyed = true;
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
function woof(options = {}) {
|
|
3177
|
+
return new Project(options);
|
|
3178
|
+
}
|
|
3179
|
+
// src/play.ts
|
|
3180
|
+
var current;
|
|
3181
|
+
function project2() {
|
|
3182
|
+
if (!current) {
|
|
3183
|
+
current = woof({ fullScreen: true });
|
|
3184
|
+
const sync = () => {
|
|
3185
|
+
const p = current;
|
|
3186
|
+
mouseX = p.mouseX;
|
|
3187
|
+
mouseY = p.mouseY;
|
|
3188
|
+
minX = p.minX;
|
|
3189
|
+
maxX = p.maxX;
|
|
3190
|
+
minY = p.minY;
|
|
3191
|
+
maxY = p.maxY;
|
|
3192
|
+
};
|
|
3193
|
+
for (const key of [
|
|
3194
|
+
"mouseX",
|
|
3195
|
+
"mouseY",
|
|
3196
|
+
"cameraX",
|
|
3197
|
+
"cameraY",
|
|
3198
|
+
"width",
|
|
3199
|
+
"height"
|
|
3200
|
+
]) {
|
|
3201
|
+
let value = current[key];
|
|
3202
|
+
Object.defineProperty(current, key, {
|
|
3203
|
+
configurable: true,
|
|
3204
|
+
enumerable: true,
|
|
3205
|
+
get: () => value,
|
|
3206
|
+
set: (next) => {
|
|
3207
|
+
value = next;
|
|
3208
|
+
sync();
|
|
3209
|
+
}
|
|
3210
|
+
});
|
|
3211
|
+
}
|
|
3212
|
+
sync();
|
|
3213
|
+
}
|
|
3214
|
+
return current;
|
|
3215
|
+
}
|
|
3216
|
+
var mouseX = 0;
|
|
3217
|
+
var mouseY = 0;
|
|
3218
|
+
var minX = -175;
|
|
3219
|
+
var maxX = 175;
|
|
3220
|
+
var minY = -250;
|
|
3221
|
+
var maxY = 250;
|
|
3222
|
+
function shape(type) {
|
|
3223
|
+
return function(options = {}) {
|
|
3224
|
+
return (options.project ?? project2()).createSprite(type, options);
|
|
3225
|
+
};
|
|
3226
|
+
}
|
|
3227
|
+
var Circle = shape("circle");
|
|
3228
|
+
var Rectangle = shape("rectangle");
|
|
3229
|
+
var Oval = shape("oval");
|
|
3230
|
+
var Polygon = shape("polygon");
|
|
3231
|
+
var Line = shape("line");
|
|
3232
|
+
var Text = shape("text");
|
|
3233
|
+
var TextPrimative = shape("text");
|
|
3234
|
+
var Image = shape("image");
|
|
3235
|
+
var Bundle = shape("bundle");
|
|
3236
|
+
var Sprite2 = shape("sprite");
|
|
3237
|
+
var customSprite = (definition) => project2().customSprite(definition);
|
|
3238
|
+
var Sound = function(options = {}) {
|
|
3239
|
+
return project2().sound(options);
|
|
3240
|
+
};
|
|
3241
|
+
function every(time, unitsOrFn, fn) {
|
|
3242
|
+
return typeof unitsOrFn === "string" ? project2().every(time, unitsOrFn, fn) : project2().every(time, unitsOrFn);
|
|
3243
|
+
}
|
|
3244
|
+
function after(time, unitsOrFn, fn) {
|
|
3245
|
+
return typeof unitsOrFn === "string" ? project2().after(time, unitsOrFn, fn) : project2().after(time, unitsOrFn);
|
|
3246
|
+
}
|
|
3247
|
+
var forever = (fn) => project2().forever(fn);
|
|
3248
|
+
var when = (condition, fn) => project2().when(condition, fn);
|
|
3249
|
+
var once = (condition, fn) => project2().once(condition, fn);
|
|
3250
|
+
var repeat = (count, fn, after2) => project2().repeat(count, fn, after2);
|
|
3251
|
+
var repeatUntil = (condition, fn, after2) => project2().repeatUntil(condition, fn, after2);
|
|
3252
|
+
var wait = (seconds) => project2().wait(seconds);
|
|
3253
|
+
var ready = (fn) => project2().ready(fn);
|
|
3254
|
+
var translateToCanvas = (x, y) => project2().translateToCanvas(x, y);
|
|
3255
|
+
var translateToCenter = (x, y) => project2().translateToCenter(x, y);
|
|
3256
|
+
var pressing = {
|
|
3257
|
+
includes: (key) => project2().key(key)
|
|
3258
|
+
};
|
|
3259
|
+
var setBackdropColor = (color) => project2().setBackdropColor(color);
|
|
3260
|
+
var setBackdropURL = (url) => project2().setBackdropURL(url);
|
|
3261
|
+
var setBackdropSize = (width, height) => project2().setBackdropSize(width, height);
|
|
3262
|
+
var setBackdropStyle = (style) => project2().setBackdropStyle(style);
|
|
3263
|
+
var setBackdropRepeat = (repeat2) => project2().setBackdropRepeat(repeat2);
|
|
3264
|
+
var bounds = () => project2().bounds();
|
|
3265
|
+
var randomX = () => project2().randomX();
|
|
3266
|
+
var randomY = () => project2().randomY();
|
|
3267
|
+
var clearPen = () => project2().clearPen();
|
|
3268
|
+
var timer = () => project2().timer();
|
|
3269
|
+
var resetTimer = () => project2().resetTimer();
|
|
3270
|
+
var freeze = () => project2().freeze();
|
|
3271
|
+
var defrost = () => project2().defrost();
|
|
3272
|
+
var scene = (name, setup) => project2().scene(name, setup);
|
|
3273
|
+
var go = (name, data) => project2().go(name, data);
|
|
3274
|
+
var find = (tag) => project2().find(tag);
|
|
3275
|
+
var particles = (options) => project2().particles(options);
|
|
3276
|
+
var save = (key, value) => project2().save(key, value);
|
|
3277
|
+
var load = (key, fallback) => project2().load(key, fallback);
|
|
3278
|
+
var onUpdate = (fn) => project2().onUpdate(fn);
|
|
3279
|
+
var onKeyDown = (fn) => project2().onKeyDown(fn);
|
|
3280
|
+
var onKeyUp = (fn) => project2().onKeyUp(fn);
|
|
3281
|
+
var onMouseDown = (fn) => project2().onMouseDown(fn);
|
|
3282
|
+
var onMouseUp = (fn) => project2().onMouseUp(fn);
|
|
3283
|
+
var onMouseMove = (fn) => project2().onMouseMove(fn);
|
|
3284
|
+
var mouse = {
|
|
3285
|
+
get x() {
|
|
3286
|
+
return project2().mouseX;
|
|
3287
|
+
},
|
|
3288
|
+
get y() {
|
|
3289
|
+
return project2().mouseY;
|
|
3290
|
+
},
|
|
3291
|
+
get down() {
|
|
3292
|
+
return project2().mouseDown;
|
|
3293
|
+
}
|
|
3294
|
+
};
|
|
3295
|
+
var world = {
|
|
3296
|
+
get width() {
|
|
3297
|
+
return project2().width;
|
|
3298
|
+
},
|
|
3299
|
+
get height() {
|
|
3300
|
+
return project2().height;
|
|
3301
|
+
},
|
|
3302
|
+
get minX() {
|
|
3303
|
+
return project2().minX;
|
|
3304
|
+
},
|
|
3305
|
+
get maxX() {
|
|
3306
|
+
return project2().maxX;
|
|
3307
|
+
},
|
|
3308
|
+
get minY() {
|
|
3309
|
+
return project2().minY;
|
|
3310
|
+
},
|
|
3311
|
+
get maxY() {
|
|
3312
|
+
return project2().maxY;
|
|
3313
|
+
},
|
|
3314
|
+
get sprites() {
|
|
3315
|
+
return project2().sprites;
|
|
3316
|
+
}
|
|
3317
|
+
};
|
|
3318
|
+
|
|
3319
|
+
// src/ui.ts
|
|
3320
|
+
var tokens = {
|
|
3321
|
+
background: ["--background", "--p-content-background", "Canvas"],
|
|
3322
|
+
foreground: ["--foreground", "--p-text-color", "CanvasText"],
|
|
3323
|
+
primary: ["--primary", "--p-primary-color", "LinkText"],
|
|
3324
|
+
primaryForeground: [
|
|
3325
|
+
"--primary-foreground",
|
|
3326
|
+
"--p-primary-contrast-color",
|
|
3327
|
+
"Canvas"
|
|
3328
|
+
],
|
|
3329
|
+
muted: ["--muted", "--p-content-hover-background", "ButtonFace"],
|
|
3330
|
+
mutedForeground: ["--muted-foreground", "--p-text-muted-color", "GrayText"],
|
|
3331
|
+
destructive: ["--destructive", "--p-red-500", "red"],
|
|
3332
|
+
border: ["--border", "--p-content-border-color", "GrayText"]
|
|
3333
|
+
};
|
|
3334
|
+
function readTheme(element) {
|
|
3335
|
+
const doc = element.ownerDocument;
|
|
3336
|
+
const win = doc.defaultView;
|
|
3337
|
+
const style = win.getComputedStyle(element);
|
|
3338
|
+
const probe = doc.createElement("span");
|
|
3339
|
+
probe.style.display = "none";
|
|
3340
|
+
element.appendChild(probe);
|
|
3341
|
+
const theme = {};
|
|
3342
|
+
try {
|
|
3343
|
+
for (const name of Object.keys(tokens)) {
|
|
3344
|
+
const [shadcn, optimus, system] = tokens[name];
|
|
3345
|
+
const value = style.getPropertyValue(shadcn).trim() || style.getPropertyValue(optimus).trim() || system;
|
|
3346
|
+
probe.style.color = system;
|
|
3347
|
+
probe.style.color = /^[\d.]+(deg)?\s+[\d.]+%\s+[\d.]+%/.test(value) ? `hsl(${value})` : value;
|
|
3348
|
+
theme[name] = win.getComputedStyle(probe).color || value;
|
|
3349
|
+
}
|
|
3350
|
+
} finally {
|
|
3351
|
+
probe.remove();
|
|
3352
|
+
}
|
|
3353
|
+
return theme;
|
|
3354
|
+
}
|
|
3355
|
+
function watchTheme(element, listener) {
|
|
3356
|
+
const doc = element.ownerDocument;
|
|
3357
|
+
const win = doc.defaultView;
|
|
3358
|
+
let previous = JSON.stringify(readTheme(element));
|
|
3359
|
+
const check = () => {
|
|
3360
|
+
const theme = readTheme(element);
|
|
3361
|
+
const next = JSON.stringify(theme);
|
|
3362
|
+
if (next === previous)
|
|
3363
|
+
return;
|
|
3364
|
+
previous = next;
|
|
3365
|
+
listener(theme);
|
|
3366
|
+
};
|
|
3367
|
+
const observer = new win.MutationObserver(check);
|
|
3368
|
+
for (let node = element;node; node = node.parentElement)
|
|
3369
|
+
observer.observe(node, {
|
|
3370
|
+
attributes: true,
|
|
3371
|
+
attributeFilter: ["class", "style", "data-theme", "data-mode", "data-palette"]
|
|
3372
|
+
});
|
|
3373
|
+
observer.observe(doc.head, {
|
|
3374
|
+
childList: true,
|
|
3375
|
+
subtree: true,
|
|
3376
|
+
characterData: true
|
|
3377
|
+
});
|
|
3378
|
+
const preference = win.matchMedia("(prefers-color-scheme: dark)");
|
|
3379
|
+
preference.addEventListener("change", check);
|
|
3380
|
+
return () => {
|
|
3381
|
+
observer.disconnect();
|
|
3382
|
+
preference.removeEventListener("change", check);
|
|
3383
|
+
};
|
|
3384
|
+
}
|
|
3385
|
+
var scrollKeys = new Set([
|
|
3386
|
+
" ",
|
|
3387
|
+
"ArrowUp",
|
|
3388
|
+
"ArrowDown",
|
|
3389
|
+
"ArrowLeft",
|
|
3390
|
+
"ArrowRight"
|
|
3391
|
+
]);
|
|
3392
|
+
function mountGame(element, setup, options = {}) {
|
|
3393
|
+
const {
|
|
3394
|
+
label = "Game",
|
|
3395
|
+
muted = false,
|
|
3396
|
+
autoStart = true,
|
|
3397
|
+
onError,
|
|
3398
|
+
audioContext,
|
|
3399
|
+
backdrop,
|
|
3400
|
+
...projectOptions
|
|
3401
|
+
} = options;
|
|
3402
|
+
const win = element.ownerDocument.defaultView;
|
|
3403
|
+
const theme = readTheme(element);
|
|
3404
|
+
const listeners = new Set;
|
|
3405
|
+
let state = { running: false, muted, error: undefined };
|
|
3406
|
+
let game;
|
|
3407
|
+
let cleanup;
|
|
3408
|
+
let audio;
|
|
3409
|
+
let destroyed = false;
|
|
3410
|
+
const set = (change) => {
|
|
3411
|
+
const next = { ...state, ...change };
|
|
3412
|
+
if (next.running === state.running && next.muted === state.muted && next.error === state.error)
|
|
3413
|
+
return;
|
|
3414
|
+
state = next;
|
|
3415
|
+
for (const listener of [...listeners])
|
|
3416
|
+
listener(state);
|
|
3417
|
+
};
|
|
3418
|
+
const fail = (error) => {
|
|
3419
|
+
onError?.(error);
|
|
3420
|
+
if (state.error === undefined)
|
|
3421
|
+
set({ error });
|
|
3422
|
+
};
|
|
3423
|
+
const close = () => {
|
|
3424
|
+
try {
|
|
3425
|
+
cleanup?.();
|
|
3426
|
+
} catch (error) {
|
|
3427
|
+
fail(error);
|
|
3428
|
+
}
|
|
3429
|
+
cleanup = undefined;
|
|
3430
|
+
game?.destroy();
|
|
3431
|
+
game = undefined;
|
|
3432
|
+
audio = undefined;
|
|
3433
|
+
};
|
|
3434
|
+
const build = () => {
|
|
3435
|
+
const project = woof({
|
|
3436
|
+
document: element.ownerDocument,
|
|
3437
|
+
window: win,
|
|
3438
|
+
...projectOptions,
|
|
3439
|
+
backdrop,
|
|
3440
|
+
parent: element,
|
|
3441
|
+
autoStart: false,
|
|
3442
|
+
onError: fail,
|
|
3443
|
+
audioContext: audioContext ?? (() => audio ??= new win.AudioContext)
|
|
3444
|
+
});
|
|
3445
|
+
game = project;
|
|
3446
|
+
const canvas = project.canvas;
|
|
3447
|
+
canvas.style.maxWidth = "100%";
|
|
3448
|
+
canvas.style.height = "auto";
|
|
3449
|
+
canvas.tabIndex = 0;
|
|
3450
|
+
canvas.setAttribute("role", "application");
|
|
3451
|
+
canvas.setAttribute("aria-label", label);
|
|
3452
|
+
canvas.addEventListener("keydown", (event) => {
|
|
3453
|
+
if (scrollKeys.has(event.key))
|
|
3454
|
+
event.preventDefault();
|
|
3455
|
+
});
|
|
3456
|
+
if (backdrop === undefined)
|
|
3457
|
+
project.setBackdropColor(theme.background);
|
|
3458
|
+
if (state.muted)
|
|
3459
|
+
project.mute();
|
|
3460
|
+
try {
|
|
3461
|
+
cleanup = setup(project, theme);
|
|
3462
|
+
} catch (error) {
|
|
3463
|
+
fail(error);
|
|
3464
|
+
}
|
|
3465
|
+
project.step(0);
|
|
3466
|
+
};
|
|
3467
|
+
const run = () => {
|
|
3468
|
+
game?.start();
|
|
3469
|
+
if (audio?.state === "suspended")
|
|
3470
|
+
audio.resume();
|
|
3471
|
+
set({ running: true });
|
|
3472
|
+
};
|
|
3473
|
+
const stopTheme = watchTheme(element, (next) => {
|
|
3474
|
+
Object.assign(theme, next);
|
|
3475
|
+
if (backdrop === undefined)
|
|
3476
|
+
game?.setBackdropColor(theme.background);
|
|
3477
|
+
if (!state.running)
|
|
3478
|
+
game?.step(0);
|
|
3479
|
+
});
|
|
3480
|
+
const controller = {
|
|
3481
|
+
get game() {
|
|
3482
|
+
return game;
|
|
3483
|
+
},
|
|
3484
|
+
getState: () => state,
|
|
3485
|
+
subscribe(listener) {
|
|
3486
|
+
listeners.add(listener);
|
|
3487
|
+
listener(state);
|
|
3488
|
+
return () => {
|
|
3489
|
+
listeners.delete(listener);
|
|
3490
|
+
};
|
|
3491
|
+
},
|
|
3492
|
+
play() {
|
|
3493
|
+
if (destroyed)
|
|
3494
|
+
return;
|
|
3495
|
+
run();
|
|
3496
|
+
game?.canvas.focus({ preventScroll: true });
|
|
3497
|
+
},
|
|
3498
|
+
pause() {
|
|
3499
|
+
if (destroyed)
|
|
3500
|
+
return;
|
|
3501
|
+
game?.stop();
|
|
3502
|
+
if (audio?.state === "running")
|
|
3503
|
+
audio.suspend();
|
|
3504
|
+
set({ running: false });
|
|
3505
|
+
},
|
|
3506
|
+
toggle() {
|
|
3507
|
+
if (state.running)
|
|
3508
|
+
controller.pause();
|
|
3509
|
+
else
|
|
3510
|
+
controller.play();
|
|
3511
|
+
},
|
|
3512
|
+
restart() {
|
|
3513
|
+
if (destroyed)
|
|
3514
|
+
return;
|
|
3515
|
+
close();
|
|
3516
|
+
set({ running: false, error: undefined });
|
|
3517
|
+
build();
|
|
3518
|
+
controller.play();
|
|
3519
|
+
},
|
|
3520
|
+
setMuted(value) {
|
|
3521
|
+
if (destroyed)
|
|
3522
|
+
return;
|
|
3523
|
+
if (value)
|
|
3524
|
+
game?.mute();
|
|
3525
|
+
else
|
|
3526
|
+
game?.unmute();
|
|
3527
|
+
set({ muted: value });
|
|
3528
|
+
},
|
|
3529
|
+
destroy() {
|
|
3530
|
+
if (destroyed)
|
|
3531
|
+
return;
|
|
3532
|
+
destroyed = true;
|
|
3533
|
+
stopTheme();
|
|
3534
|
+
close();
|
|
3535
|
+
set({ running: false });
|
|
3536
|
+
listeners.clear();
|
|
3537
|
+
}
|
|
3538
|
+
};
|
|
3539
|
+
build();
|
|
3540
|
+
if (autoStart)
|
|
3541
|
+
run();
|
|
3542
|
+
return controller;
|
|
3543
|
+
}
|
|
3544
|
+
// src/global.ts
|
|
3545
|
+
var script = typeof document === "undefined" ? null : document.currentScript;
|
|
3546
|
+
if (script?.hasAttribute("data-global") && typeof window !== "undefined") {
|
|
3547
|
+
const target = window;
|
|
3548
|
+
for (const [name, value] of Object.entries(exports_play))
|
|
3549
|
+
if (!["mouseX", "mouseY", "minX", "maxX", "minY", "maxY"].includes(name) && (!(name in target) || name === "Image" || name === "Text"))
|
|
3550
|
+
target[name] = value;
|
|
3551
|
+
for (const name of [
|
|
3552
|
+
"mouseX",
|
|
3553
|
+
"mouseY",
|
|
3554
|
+
"pMouseX",
|
|
3555
|
+
"pMouseY",
|
|
3556
|
+
"mouseXSpeed",
|
|
3557
|
+
"mouseYSpeed",
|
|
3558
|
+
"mouseDown",
|
|
3559
|
+
"keysDown",
|
|
3560
|
+
"width",
|
|
3561
|
+
"height",
|
|
3562
|
+
"minX",
|
|
3563
|
+
"maxX",
|
|
3564
|
+
"minY",
|
|
3565
|
+
"maxY",
|
|
3566
|
+
"stopped",
|
|
3567
|
+
"backdrop"
|
|
3568
|
+
])
|
|
3569
|
+
if (!(name in target))
|
|
3570
|
+
Object.defineProperty(target, name, {
|
|
3571
|
+
configurable: true,
|
|
3572
|
+
get: () => project2()[name]
|
|
3573
|
+
});
|
|
3574
|
+
}
|
|
3575
|
+
if (typeof window !== "undefined")
|
|
3576
|
+
window.WoofEngine = { ...exports_src, play: exports_play, ui: exports_ui };
|
|
3577
|
+
})();
|