dopaminejs 2.3.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,827 @@
1
+ //#region src/core/EventBus.js
2
+ var e = class {
3
+ constructor() {
4
+ this._listeners = /* @__PURE__ */ new Map(), this._onceListeners = /* @__PURE__ */ new Map(), this._sortedCache = /* @__PURE__ */ new Map();
5
+ }
6
+ on(e, t, n = 0) {
7
+ return this._listeners.has(e) || this._listeners.set(e, /* @__PURE__ */ new Set()), this._listeners.get(e).add({
8
+ callback: t,
9
+ priority: n
10
+ }), this._sortedCache.delete(e), this;
11
+ }
12
+ once(e, t) {
13
+ return this._onceListeners.has(e) || this._onceListeners.set(e, /* @__PURE__ */ new Set()), this._onceListeners.get(e).add(t), this;
14
+ }
15
+ off(e, t) {
16
+ let n = this._listeners.get(e);
17
+ if (n) for (let r of [...n]) r.callback === t && (n.delete(r), this._sortedCache.delete(e));
18
+ let r = this._onceListeners.get(e);
19
+ return r && r.delete(t), this;
20
+ }
21
+ emit(e, t) {
22
+ let n = this._getSortedListeners(e);
23
+ for (let r = 0; r < n.length; r++) this._invoke(n[r].callback, t, e);
24
+ let r = this._onceListeners.get(e);
25
+ if (r && r.size > 0) {
26
+ let n = [...r];
27
+ r.clear();
28
+ for (let r of n) this._invoke(r, t, e);
29
+ }
30
+ }
31
+ _invoke(e, t, n) {
32
+ try {
33
+ e(t);
34
+ } catch (e) {
35
+ console.error(`[DopamineJS] Listener for "${n}" threw:`, e);
36
+ }
37
+ }
38
+ _getSortedListeners(e) {
39
+ if (this._sortedCache.has(e)) return this._sortedCache.get(e);
40
+ let t = this._listeners.get(e);
41
+ if (!t || t.size === 0) return [];
42
+ let n = Array.from(t).sort((e, t) => t.priority - e.priority);
43
+ return this._sortedCache.set(e, n), n;
44
+ }
45
+ clear(e) {
46
+ e ? (this._listeners.delete(e), this._onceListeners.delete(e), this._sortedCache.delete(e)) : (this._listeners.clear(), this._onceListeners.clear(), this._sortedCache.clear());
47
+ }
48
+ hasListeners(e) {
49
+ let t = this._listeners.get(e), n = this._onceListeners.get(e);
50
+ return !!t?.size || !!n?.size;
51
+ }
52
+ };
53
+ e.Events = {
54
+ TICK: "tick",
55
+ FIXED_UPDATE: "fixed_update",
56
+ RENDER: "render",
57
+ COLLISION_ENTER: "collision_enter",
58
+ COLLISION_EXIT: "collision_exit",
59
+ XP_GAINED: "xp_gained",
60
+ LEVEL_UP: "level_up",
61
+ ACHIEVEMENT_UNLOCKED: "achievement_unlocked",
62
+ NEW_HIGH_SCORE: "new_high_score",
63
+ STREAK_UPDATED: "streak_updated",
64
+ SYSTEM_REGISTERED: "system_registered",
65
+ SYSTEM_UNREGISTERED: "system_unregistered",
66
+ PLUGIN_LOADED: "plugin_loaded",
67
+ PLUGIN_UNLOADED: "plugin_unloaded"
68
+ };
69
+ //#endregion
70
+ //#region src/core/SystemRegistry.js
71
+ var t = class {
72
+ constructor(e) {
73
+ this.kernel = e, this._systems = /* @__PURE__ */ new Map(), this._updateOrder = [], this._fixedUpdateOrder = [], this._needsRecompute = !1, this._failed = /* @__PURE__ */ new WeakSet();
74
+ }
75
+ _run(e, t, n) {
76
+ try {
77
+ e[t](n);
78
+ } catch (n) {
79
+ this._failed.has(e) || (this._failed.add(e), console.error(`[SystemRegistry] System ${t}() threw:`, n));
80
+ }
81
+ }
82
+ register(e, t, { priority: n = 0, dependencies: r = [] } = {}) {
83
+ this._systems.has(e) && (console.warn(`[SystemRegistry] System "${e}" already registered. Replacing.`), this.unregister(e));
84
+ for (let t of r) this._systems.has(t) || console.warn(`[SystemRegistry] Dependency "${t}" not found for system "${e}"`);
85
+ return this._systems.set(e, {
86
+ system: t,
87
+ priority: n,
88
+ dependencies: r
89
+ }), t.init && t.init(this.kernel), this._needsRecompute = !0, this.kernel.events.emit("system_registered", {
90
+ name: e,
91
+ system: t
92
+ }), this;
93
+ }
94
+ get(e) {
95
+ let t = this._systems.get(e);
96
+ return t ? t.system : void 0;
97
+ }
98
+ has(e) {
99
+ return this._systems.has(e);
100
+ }
101
+ unregister(e) {
102
+ let t = this._systems.get(e);
103
+ if (!t) return !1;
104
+ try {
105
+ t.system.destroy && t.system.destroy();
106
+ } finally {
107
+ this._systems.delete(e), this._needsRecompute = !0, this.kernel.events.emit("system_unregistered", { name: e });
108
+ }
109
+ return !0;
110
+ }
111
+ update(e) {
112
+ this._needsRecompute && this._recomputeUpdateOrder();
113
+ for (let t = 0; t < this._updateOrder.length; t++) {
114
+ let n = this._updateOrder[t];
115
+ n.update && this._run(n, "update", e);
116
+ }
117
+ }
118
+ fixedUpdate(e) {
119
+ this._needsRecompute && this._recomputeUpdateOrder();
120
+ for (let t = 0; t < this._fixedUpdateOrder.length; t++) {
121
+ let n = this._fixedUpdateOrder[t];
122
+ n.fixedUpdate && this._run(n, "fixedUpdate", e);
123
+ }
124
+ }
125
+ _recomputeUpdateOrder() {
126
+ let e = Array.from(this._systems.entries()), t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map();
127
+ for (let [r, i] of e) t.set(r, i.dependencies), n.set(r, 0);
128
+ for (let [e, r] of t) for (let t of r) n.has(t) && n.set(e, n.get(e) + 1);
129
+ let r = [], i = [];
130
+ for (let [e, t] of n) t === 0 && r.push(e);
131
+ for (; r.length > 0;) {
132
+ r.sort((e, t) => {
133
+ let n = this._systems.get(e).priority;
134
+ return this._systems.get(t).priority - n;
135
+ });
136
+ let e = r.shift();
137
+ i.push(e);
138
+ for (let [i, a] of t) if (a.includes(e)) {
139
+ let e = n.get(i) - 1;
140
+ n.set(i, e), e === 0 && r.push(i);
141
+ }
142
+ }
143
+ i.length !== e.length && (console.error("[SystemRegistry] Circular dependency detected in systems!"), i.length = 0, i.push(...e.map(([e]) => e).sort((e, t) => this._systems.get(t).priority - this._systems.get(e).priority))), this._updateOrder = i.map((e) => this._systems.get(e).system).filter((e) => e.update), this._fixedUpdateOrder = i.map((e) => this._systems.get(e).system).filter((e) => e.fixedUpdate), this._needsRecompute = !1;
144
+ }
145
+ getSystemNames() {
146
+ return Array.from(this._systems.keys());
147
+ }
148
+ clear() {
149
+ for (let e of this._systems.keys()) this.unregister(e);
150
+ }
151
+ }, n = class {
152
+ constructor(e) {
153
+ this.kernel = e, this._plugins = /* @__PURE__ */ new Map(), this._loadOrder = [], this._inFlight = /* @__PURE__ */ new Set(), this._owned = /* @__PURE__ */ new Map();
154
+ }
155
+ _snapshot() {
156
+ let e = this.kernel.systems;
157
+ return new Map(e.getSystemNames().map((t) => [t, e.get(t)]));
158
+ }
159
+ _systemsSince(e) {
160
+ return this.kernel.systems.getSystemNames().filter((t) => !e.has(t));
161
+ }
162
+ _changedSince(e) {
163
+ let t = this.kernel.systems;
164
+ return t.getSystemNames().filter((n) => t.get(n) !== e.get(n));
165
+ }
166
+ _own(e, t) {
167
+ let n = this.kernel.systems, r = /* @__PURE__ */ new Map();
168
+ for (let e of t) {
169
+ let t = n.get(e);
170
+ [...this._owned.values()].some((n) => n.get(e) === t) || r.set(e, t);
171
+ }
172
+ this._owned.set(e, r);
173
+ }
174
+ _release(e) {
175
+ let t = this._owned.get(e);
176
+ if (t) {
177
+ this._owned.delete(e);
178
+ for (let [e, n] of t) if (this.kernel.systems.get(e) === n) try {
179
+ this.kernel.systems.unregister(e);
180
+ } catch (t) {
181
+ console.error(`[PluginRegistry] Error removing system "${e}":`, t);
182
+ }
183
+ }
184
+ }
185
+ _claim(e) {
186
+ for (let t of this._inFlight) for (let n of e) t.foreign.add(n);
187
+ }
188
+ _rollback(e) {
189
+ for (let t of e) try {
190
+ this.kernel.systems.unregister(t);
191
+ } catch (e) {
192
+ console.error(`[PluginRegistry] Error removing system "${t}":`, e);
193
+ }
194
+ }
195
+ _forget(e) {
196
+ this._plugins.delete(e);
197
+ let t = this._loadOrder.indexOf(e);
198
+ t > -1 && this._loadOrder.splice(t, 1);
199
+ }
200
+ use(e) {
201
+ if (!e.name) throw Error("[PluginRegistry] Plugin must have a name");
202
+ if (this._plugins.has(e.name)) return console.warn(`[PluginRegistry] Plugin "${e.name}" already registered. Skipping.`), this;
203
+ if (!e.init || typeof e.init != "function") throw Error(`[PluginRegistry] Plugin "${e.name}" must have an init() method`);
204
+ this._plugins.set(e.name, e), this._loadOrder.push(e.name);
205
+ let t = this._snapshot();
206
+ try {
207
+ e.init(this.kernel);
208
+ let n = this._changedSince(t);
209
+ this._own(e.name, n), this._claim(n), this.kernel.events.emit("plugin_loaded", {
210
+ name: e.name,
211
+ version: e.version
212
+ });
213
+ } catch (n) {
214
+ throw console.error(`[PluginRegistry] Failed to initialize plugin "${e.name}":`, n), this._forget(e.name), this._rollback(this._systemsSince(t)), n;
215
+ }
216
+ return this;
217
+ }
218
+ async useAsync(e) {
219
+ if (!e.name) throw Error("[PluginRegistry] Plugin must have a name");
220
+ if (this._plugins.has(e.name)) return console.warn(`[PluginRegistry] Plugin "${e.name}" already registered. Skipping.`), this;
221
+ if (!e.init || typeof e.init != "function") throw Error(`[PluginRegistry] Plugin "${e.name}" must have an init() method`);
222
+ this._plugins.set(e.name, e), this._loadOrder.push(e.name);
223
+ let t = {
224
+ before: this._snapshot(),
225
+ foreign: /* @__PURE__ */ new Set()
226
+ }, n = () => this._systemsSince(t.before).filter((e) => !t.foreign.has(e));
227
+ this._inFlight.add(t);
228
+ try {
229
+ await e.init(this.kernel), this._inFlight.delete(t);
230
+ let n = this.kernel.systems, r = this._changedSince(t.before).filter((e) => !t.foreign.has(e)), i = [...this._inFlight];
231
+ this._own(e.name, r.filter((e) => i.every((t) => t.before.get(e) === n.get(e)))), this._claim(r), this.kernel.events.emit("plugin_loaded", {
232
+ name: e.name,
233
+ version: e.version
234
+ });
235
+ } catch (r) {
236
+ console.error(`[PluginRegistry] Failed to initialize plugin "${e.name}":`, r), this._inFlight.delete(t), this._forget(e.name);
237
+ let i = n();
238
+ throw this._inFlight.size === 0 ? this._rollback(i) : i.length > 0 && console.warn(`[PluginRegistry] Left systems in place after "${e.name}" failed: ${i.join(", ")}`), r;
239
+ }
240
+ return this;
241
+ }
242
+ remove(e) {
243
+ let t = this._plugins.get(e);
244
+ if (!t) return !1;
245
+ if (t.destroy) try {
246
+ t.destroy();
247
+ } catch (t) {
248
+ console.error(`[PluginRegistry] Error destroying plugin "${e}":`, t);
249
+ }
250
+ this._plugins.delete(e);
251
+ let n = this._loadOrder.indexOf(e);
252
+ return n > -1 && this._loadOrder.splice(n, 1), this._release(e), this.kernel.events.emit("plugin_unloaded", { name: e }), !0;
253
+ }
254
+ get(e) {
255
+ return this._plugins.get(e);
256
+ }
257
+ has(e) {
258
+ return this._plugins.has(e);
259
+ }
260
+ getPluginNames() {
261
+ return Array.from(this._plugins.keys());
262
+ }
263
+ getLoadOrder() {
264
+ return [...this._loadOrder];
265
+ }
266
+ clear() {
267
+ for (let e = this._loadOrder.length - 1; e >= 0; e--) this.remove(this._loadOrder[e]);
268
+ }
269
+ }, r = class {
270
+ constructor(e = {}) {
271
+ this.width = e.width || 800, this.height = e.height || 600, this.backgroundColor = e.backgroundColor || "#000000", e.canvas ? (this.canvas = e.canvas, this.canvas.width = this.width, this.canvas.height = this.height) : (this.canvas = document.createElement("canvas"), this.canvas.width = this.width, this.canvas.height = this.height, (e.parent ? typeof e.parent == "string" ? document.querySelector(e.parent) : e.parent : document.body).appendChild(this.canvas)), this.ctx = this.canvas.getContext("2d");
272
+ }
273
+ clear() {
274
+ this.ctx.fillStyle = this.backgroundColor, this.ctx.fillRect(0, 0, this.width, this.height);
275
+ }
276
+ resize(e, t) {
277
+ this.width = e, this.height = t, this.canvas.width = e, this.canvas.height = t;
278
+ }
279
+ }, i = class {
280
+ constructor() {
281
+ this.running = !1, this.lastTime = 0, this.callbacks = /* @__PURE__ */ new Set(), this._frameHandle = null, this._tick = this._tick.bind(this), this._failed = /* @__PURE__ */ new WeakSet();
282
+ }
283
+ add(e) {
284
+ this.callbacks.add(e);
285
+ }
286
+ remove(e) {
287
+ this.callbacks.delete(e);
288
+ }
289
+ start() {
290
+ this.running || (this.running = !0, this.lastTime = performance.now(), this._frameHandle !== null && cancelAnimationFrame(this._frameHandle), this._frameHandle = requestAnimationFrame(this._tick));
291
+ }
292
+ stop() {
293
+ this.running = !1, this._frameHandle !== null && (cancelAnimationFrame(this._frameHandle), this._frameHandle = null);
294
+ }
295
+ _tick(e) {
296
+ if (this._frameHandle = null, !this.running) return;
297
+ let t = (e - this.lastTime) / 1e3;
298
+ this.lastTime = e;
299
+ let n = Math.max(0, Math.min(t, .1));
300
+ for (let e of [...this.callbacks]) try {
301
+ e(n);
302
+ } catch (t) {
303
+ this._failed.has(e) || (this._failed.add(e), console.error("[Ticker] Callback threw:", t));
304
+ }
305
+ this.running && (this._frameHandle = requestAnimationFrame(this._tick));
306
+ }
307
+ };
308
+ //#endregion
309
+ //#region src/systems/deprecate.js
310
+ function a(e, t) {
311
+ let n = !1;
312
+ return new Proxy(e, {
313
+ get(e, r, i) {
314
+ n || (n = !0, console.warn(t));
315
+ let a = Reflect.get(e, r, i);
316
+ return typeof a == "function" ? a.bind(e) : a;
317
+ },
318
+ set(e, r, i) {
319
+ return n || (n = !0, console.warn(t)), Reflect.set(e, r, i);
320
+ }
321
+ });
322
+ }
323
+ //#endregion
324
+ //#region src/core/Component.js
325
+ var o = class {
326
+ constructor() {
327
+ this.gameObject = null, this.kernel = null;
328
+ }
329
+ get physics() {
330
+ return this.kernel?.systems.get("physics");
331
+ }
332
+ get input() {
333
+ return this.kernel?.systems.get("input");
334
+ }
335
+ get loader() {
336
+ return this.kernel?.systems.get("loader");
337
+ }
338
+ get events() {
339
+ return this.kernel?.events;
340
+ }
341
+ onAttach() {}
342
+ onDetach() {}
343
+ update(e) {}
344
+ render(e) {}
345
+ }, s = class extends o {
346
+ constructor(e = "box", t = 50, n = 50, r = 25) {
347
+ super(), this.type = e, this.width = t, this.height = n, this.radius = r, this.tag = null;
348
+ }
349
+ get kernel() {
350
+ return this._kernel ?? null;
351
+ }
352
+ set kernel(e) {
353
+ e !== this._kernel && (this._unregister(), this._kernel = e, this._register());
354
+ }
355
+ onAttach() {
356
+ super.onAttach(), this._attached = !0, this._register();
357
+ }
358
+ onDetach() {
359
+ super.onDetach(), this._attached = !1, this._unregister();
360
+ }
361
+ _register() {
362
+ if (this._physics || !this._attached) return;
363
+ let e = this.physics;
364
+ e && (e.add(this), this._physics = e);
365
+ }
366
+ _unregister() {
367
+ this._physics &&= (this._physics.remove(this), null);
368
+ }
369
+ getBounds() {
370
+ let e = this.gameObject;
371
+ if (this.type === "box") return {
372
+ left: e.x - this.width / 2,
373
+ right: e.x + this.width / 2,
374
+ top: e.y - this.height / 2,
375
+ bottom: e.y + this.height / 2
376
+ };
377
+ if (this.type === "circle") return {
378
+ x: e.x,
379
+ y: e.y,
380
+ radius: this.radius
381
+ };
382
+ }
383
+ }, c = class {
384
+ constructor() {
385
+ this.colliders = [], this.kernel = null, this._pairs = /* @__PURE__ */ new Map(), this._step = 0;
386
+ }
387
+ init(e) {
388
+ this.kernel = e;
389
+ }
390
+ fixedUpdate(e) {
391
+ this.step();
392
+ }
393
+ destroy() {
394
+ this.colliders = [], this._pairs.clear();
395
+ }
396
+ add(e) {
397
+ this.colliders.push(e);
398
+ }
399
+ remove(t) {
400
+ let n = this.colliders.indexOf(t);
401
+ n > -1 && this.colliders.splice(n, 1);
402
+ let r = this._pairs.get(t);
403
+ if (r) {
404
+ this._pairs.delete(t);
405
+ let n = [...r.keys()];
406
+ r.clear();
407
+ for (let r of n) this._emit(e.Events.COLLISION_EXIT, t, r);
408
+ }
409
+ for (let [n, r] of this._pairs) r.delete(t) && this._emit(e.Events.COLLISION_EXIT, n, t);
410
+ }
411
+ step() {
412
+ let t = ++this._step;
413
+ for (let e = 0; e < this.colliders.length; e++) for (let n = e + 1; n < this.colliders.length; n++) {
414
+ let r = this.colliders[e], i = this.colliders[n];
415
+ this._intersects(r, r.getBounds(), i, i.getBounds()) && (this._seen(r, i, t), this._notifyCollision(r.gameObject, i.gameObject), this._notifyCollision(i.gameObject, r.gameObject));
416
+ }
417
+ for (let [n, r] of this._pairs) {
418
+ for (let [i, a] of r) if (a !== t) {
419
+ if (this._intersects(n, n.getBounds(), i, i.getBounds())) {
420
+ r.set(i, t);
421
+ continue;
422
+ }
423
+ r.delete(i), this._emit(e.Events.COLLISION_EXIT, n, i);
424
+ }
425
+ r.size === 0 && this._pairs.delete(n);
426
+ }
427
+ }
428
+ _seen(t, n, r) {
429
+ if (this._pairs.get(n)?.has(t)) {
430
+ this._pairs.get(n).set(t, r);
431
+ return;
432
+ }
433
+ let i = this._pairs.get(t);
434
+ i || (i = /* @__PURE__ */ new Map(), this._pairs.set(t, i));
435
+ let a = i.has(n);
436
+ i.set(n, r), a || this._emit(e.Events.COLLISION_ENTER, t, n);
437
+ }
438
+ _emit(e, t, n) {
439
+ this.kernel?.events?.emit(e, {
440
+ a: t,
441
+ b: n
442
+ });
443
+ }
444
+ _notifyCollision(e, t) {
445
+ e && e.components && e.components.forEach((e) => {
446
+ e.onCollisionEnter && e.onCollisionEnter(t);
447
+ });
448
+ }
449
+ checkOverlap(e, t) {
450
+ let n = [], r = e.getBounds();
451
+ for (let i of this.colliders) {
452
+ if (i === e || t && i.tag !== t) continue;
453
+ let a = i.getBounds();
454
+ this._intersects(e, r, i, a) && n.push(i);
455
+ }
456
+ return n;
457
+ }
458
+ _intersects(e, t, n, r) {
459
+ if (e.type === "box" && n.type === "box") return t.left < r.right && t.right > r.left && t.top < r.bottom && t.bottom > r.top;
460
+ if (e.type === "circle" && n.type === "circle") {
461
+ let e = t.x - r.x, n = t.y - r.y;
462
+ return Math.sqrt(e * e + n * n) < t.radius + r.radius;
463
+ }
464
+ return !1;
465
+ }
466
+ addBody(e) {
467
+ this.add(e);
468
+ }
469
+ removeBody(e) {
470
+ this.remove(e);
471
+ }
472
+ checkCollision(e, t) {
473
+ return this._intersects(e, e.getBounds(), t, t.getBounds());
474
+ }
475
+ raycast(e, t, n) {
476
+ return null;
477
+ }
478
+ setGravity(e, t) {}
479
+ }, l = a(new c(), "[DopamineJS] GlobalPhysics is deprecated. Use kernel.physics instead."), u = class {
480
+ constructor() {
481
+ this.keys = /* @__PURE__ */ new Map(), this.mouse = {
482
+ x: 0,
483
+ y: 0,
484
+ buttons: /* @__PURE__ */ new Map()
485
+ }, this.kernel = null, this._onKeyDown = this._onKeyDown.bind(this), this._onKeyUp = this._onKeyUp.bind(this), this._onMouseMove = this._onMouseMove.bind(this), this._onMouseDown = this._onMouseDown.bind(this), this._onMouseUp = this._onMouseUp.bind(this);
486
+ }
487
+ init(e) {
488
+ this.kernel = e, window.addEventListener("keydown", this._onKeyDown), window.addEventListener("keyup", this._onKeyUp), window.addEventListener("mousemove", this._onMouseMove), window.addEventListener("mousedown", this._onMouseDown), window.addEventListener("mouseup", this._onMouseUp);
489
+ }
490
+ destroy() {
491
+ window.removeEventListener("keydown", this._onKeyDown), window.removeEventListener("keyup", this._onKeyUp), window.removeEventListener("mousemove", this._onMouseMove), window.removeEventListener("mousedown", this._onMouseDown), window.removeEventListener("mouseup", this._onMouseUp);
492
+ }
493
+ _onKeyDown(e) {
494
+ this.keys.set(e.key, !0);
495
+ }
496
+ _onKeyUp(e) {
497
+ this.keys.set(e.key, !1);
498
+ }
499
+ _onMouseMove(e) {
500
+ this.mouse.x = e.clientX, this.mouse.y = e.clientY;
501
+ }
502
+ _onMouseDown(e) {
503
+ this.mouse.buttons.set(e.button, !0);
504
+ }
505
+ _onMouseUp(e) {
506
+ this.mouse.buttons.set(e.button, !1);
507
+ }
508
+ isKeyDown(e) {
509
+ return this.keys.get(e) || !1;
510
+ }
511
+ isMouseButtonDown(e = 0) {
512
+ return this.mouse.buttons.get(e) || !1;
513
+ }
514
+ }, d = a(new u(), "[DopamineJS] GlobalInput is deprecated. Use kernel.input instead."), f = class {
515
+ constructor() {
516
+ this.assets = /* @__PURE__ */ new Map(), this.kernel = null;
517
+ }
518
+ init(e) {
519
+ this.kernel = e;
520
+ }
521
+ destroy() {
522
+ this.assets.clear();
523
+ }
524
+ async loadImage(e, t) {
525
+ return new Promise((n, r) => {
526
+ let i = new Image();
527
+ i.onload = () => {
528
+ this.assets.set(e, i), n(i);
529
+ }, i.onerror = r, i.src = t;
530
+ });
531
+ }
532
+ get(e) {
533
+ return this.assets.get(e);
534
+ }
535
+ }, p = a(new f(), "[DopamineJS] GlobalLoader is deprecated. Use kernel.loader instead."), m = class {
536
+ constructor(r = {}) {
537
+ this.config = r, this._boundUpdate = this._update.bind(this), this.events = new e(), this.systems = new t(this), this.plugins = new n(this), this._registerCoreSystems(r), this.fixedTimestep = r.fixedTimestep || 1 / 60, this._accumulator = 0, this._maxAccumulator = this.fixedTimestep * 5;
538
+ }
539
+ _registerCoreSystems(e) {
540
+ this.systems.register("ticker", new i(), { priority: 100 });
541
+ let t = {
542
+ width: e.width,
543
+ height: e.height,
544
+ backgroundColor: e.backgroundColor,
545
+ canvas: e.canvas,
546
+ ...e.renderer
547
+ };
548
+ this.systems.register("renderer", new r(t), { priority: 90 }), this.systems.register("input", new u(), { priority: 80 }), this.systems.register("loader", new f(), { priority: 70 }), this.systems.register("physics", new c(), { priority: 60 });
549
+ }
550
+ start() {
551
+ let e = this.systems.get("ticker");
552
+ e && (e.add(this._boundUpdate), e.start()), this.events.emit("kernel_started");
553
+ }
554
+ stop() {
555
+ let e = this.systems.get("ticker");
556
+ e && e.stop(), this.events.emit("kernel_stopped");
557
+ }
558
+ _update(t) {
559
+ for (this.events.emit(e.Events.TICK, { dt: t }), this._accumulator += t, this._accumulator > this._maxAccumulator && (this._accumulator = this._maxAccumulator); this._accumulator >= this.fixedTimestep;) this.events.emit(e.Events.FIXED_UPDATE, { dt: this.fixedTimestep }), this.systems.fixedUpdate(this.fixedTimestep), this._accumulator -= this.fixedTimestep;
560
+ this.systems.update(t), this.events.emit(e.Events.RENDER, { dt: t });
561
+ }
562
+ get physics() {
563
+ return this.systems.get("physics");
564
+ }
565
+ get input() {
566
+ return this.systems.get("input");
567
+ }
568
+ get loader() {
569
+ return this.systems.get("loader");
570
+ }
571
+ get renderer() {
572
+ return this.systems.get("renderer");
573
+ }
574
+ get ticker() {
575
+ return this.systems.get("ticker");
576
+ }
577
+ destroy() {
578
+ this.stop();
579
+ let e = this.systems.get("ticker");
580
+ e && e.remove(this._boundUpdate), this.plugins.clear(), this.systems.clear(), this.events.clear();
581
+ }
582
+ }, h = class {
583
+ constructor() {
584
+ this.gameObjects = [], this._kernel = null;
585
+ }
586
+ get kernel() {
587
+ return this._kernel;
588
+ }
589
+ set kernel(e) {
590
+ this._kernel = e;
591
+ for (let t of this.gameObjects) t.kernel = e;
592
+ }
593
+ onEnter() {}
594
+ onExit() {}
595
+ add(e) {
596
+ return this.kernel && (e.kernel = this.kernel), e._detached && (e._detached = !1, this._eachComponent(e, (e) => e.onAttach())), this.gameObjects.push(e), e;
597
+ }
598
+ remove(e) {
599
+ let t = this.gameObjects.indexOf(e);
600
+ t > -1 && (this.gameObjects.splice(t, 1), e._detached = !0, this._eachComponent(e, (e) => e.onDetach()));
601
+ }
602
+ _eachComponent(e, t) {
603
+ for (let n of [...e.components]) t(n);
604
+ for (let n of e.children) this._eachComponent(n, t);
605
+ }
606
+ update(e) {
607
+ for (let t of [...this.gameObjects]) t._detached || t.update(e);
608
+ }
609
+ render(e) {
610
+ for (let t of [...this.gameObjects]) t._detached || t.render(e);
611
+ }
612
+ }, g = class {
613
+ constructor(e) {
614
+ this.game = e, this.currentScene = null;
615
+ }
616
+ run(e) {
617
+ this.currentScene && (this.currentScene.onExit(), this.currentScene.kernel = null), this.currentScene = e, this.currentScene.game = this.game, this.game?.kernel && (e.kernel = this.game.kernel), this.game.scene = e, e.onEnter();
618
+ }
619
+ }, _ = class {
620
+ constructor(t = {}) {
621
+ this.config = t, this.kernel = new m(t), this.director = new g(this), this.kernel.systems.register("director", this.director), this.kernel.events.on(e.Events.TICK, this._update.bind(this), 10), this.kernel.events.on(e.Events.RENDER, this._render.bind(this), 10);
622
+ }
623
+ get renderer() {
624
+ return this.kernel.renderer;
625
+ }
626
+ get ticker() {
627
+ return this.kernel.ticker;
628
+ }
629
+ get physics() {
630
+ return this.kernel.physics;
631
+ }
632
+ get input() {
633
+ return this.kernel.input;
634
+ }
635
+ start() {
636
+ this.kernel.start();
637
+ }
638
+ stop() {
639
+ this.kernel.stop();
640
+ }
641
+ setScene(e) {
642
+ e.kernel = this.kernel, this.director.run(e);
643
+ }
644
+ _update({ dt: e }) {
645
+ this.director.currentScene && this.director.currentScene.update(e);
646
+ }
647
+ _render({ dt: e }) {
648
+ this.renderer.clear(), this.director.currentScene && this.director.currentScene.render(this.renderer.ctx);
649
+ }
650
+ destroy() {
651
+ this.kernel.destroy();
652
+ }
653
+ }, v = class {
654
+ constructor(e = 0, t = 0) {
655
+ this.x = e, this.y = t, this.rotation = 0, this.scale = {
656
+ x: 1,
657
+ y: 1
658
+ }, this.components = [], this.children = [], this.parent = null, this._kernel = null;
659
+ }
660
+ get kernel() {
661
+ return this._kernel;
662
+ }
663
+ set kernel(e) {
664
+ this._kernel = e;
665
+ for (let t of this.components) t.kernel = e;
666
+ for (let t of this.children) t.kernel = e;
667
+ }
668
+ addChild(e) {
669
+ return e.parent = this, e.kernel = this.kernel, this.children.push(e), e;
670
+ }
671
+ addComponent(e) {
672
+ return e.gameObject = this, e.kernel = this.kernel, this.components.push(e), e.onAttach(), e;
673
+ }
674
+ getComponent(e) {
675
+ return this.components.find((t) => t instanceof e);
676
+ }
677
+ update(e) {
678
+ for (let t of this.components) t.update && t.update(e);
679
+ for (let t of this.children) t.update(e);
680
+ }
681
+ render(e) {
682
+ e.save(), e.translate(this.x, this.y), e.rotate(this.rotation), e.scale(this.scale.x, this.scale.y);
683
+ for (let t of this.components) t.render && t.render(e);
684
+ for (let t of this.children) t.render(e);
685
+ e.restore();
686
+ }
687
+ }, y = class e {
688
+ constructor(e = 0, t = 0) {
689
+ this.x = e, this.y = t;
690
+ }
691
+ add(e) {
692
+ return this.x += e.x, this.y += e.y, this;
693
+ }
694
+ sub(e) {
695
+ return this.x -= e.x, this.y -= e.y, this;
696
+ }
697
+ scale(e) {
698
+ return this.x *= e, this.y *= e, this;
699
+ }
700
+ distance(e) {
701
+ let t = this.x - e.x, n = this.y - e.y;
702
+ return Math.sqrt(t * t + n * n);
703
+ }
704
+ normalize() {
705
+ let e = Math.sqrt(this.x * this.x + this.y * this.y);
706
+ return e > 0 && (this.x /= e, this.y /= e), this;
707
+ }
708
+ clone() {
709
+ return new e(this.x, this.y);
710
+ }
711
+ static distance(e, t) {
712
+ return Math.sqrt((t.x - e.x) ** 2 + (t.y - e.y) ** 2);
713
+ }
714
+ }, b = class extends o {
715
+ constructor(e) {
716
+ super(), this.image = e, this.anchor = {
717
+ x: .5,
718
+ y: .5
719
+ }, this.width = e ? e.width : 0, this.height = e ? e.height : 0, this.frame = {
720
+ x: 0,
721
+ y: 0,
722
+ w: this.width,
723
+ h: this.height
724
+ };
725
+ }
726
+ setFrame(e, t, n, r) {
727
+ this.frame = {
728
+ x: e,
729
+ y: t,
730
+ w: n,
731
+ h: r
732
+ }, this.width = n, this.height = r;
733
+ }
734
+ setTexture(e) {
735
+ this.image = e, e && this.setFrame(0, 0, e.width, e.height);
736
+ }
737
+ render(e) {
738
+ if (!this.image) return;
739
+ let t = this.frame.w, n = this.frame.h, r = -t * this.anchor.x, i = -n * this.anchor.y;
740
+ e.drawImage(this.image, this.frame.x, this.frame.y, t, n, r, i, t, n);
741
+ }
742
+ }, x = class extends o {
743
+ constructor() {
744
+ super(), this.animations = {}, this.currentAnim = null, this.timer = 0, this.currentFrameIndex = 0, this.sprite = null;
745
+ }
746
+ onAttach() {
747
+ this.sprite = this.gameObject.getComponent(b), this.sprite || console.warn("Animator attached to GameObject without a Sprite component.");
748
+ }
749
+ addAnimation(e, t, n = 10, r = !0) {
750
+ this.animations[e] = {
751
+ frames: t,
752
+ fps: n,
753
+ loop: r
754
+ };
755
+ }
756
+ play(e) {
757
+ this.currentAnim && this.currentAnim.name === e || this.animations[e] && (this.currentAnim = {
758
+ name: e,
759
+ ...this.animations[e]
760
+ }, this.currentFrameIndex = 0, this.timer = 0, this._updateFrame());
761
+ }
762
+ update(e) {
763
+ if (!this.currentAnim || !this.sprite) return;
764
+ this.timer += e;
765
+ let t = 1 / this.currentAnim.fps;
766
+ this.timer >= t && (this.timer -= t, this.currentFrameIndex++, this.currentFrameIndex >= this.currentAnim.frames.length && (this.currentFrameIndex = this.currentAnim.loop ? 0 : this.currentAnim.frames.length - 1), this._updateFrame());
767
+ }
768
+ _updateFrame() {
769
+ let e = this.currentAnim.frames[this.currentFrameIndex];
770
+ this.sprite.setFrame(e.x, e.y, e.w, e.h);
771
+ }
772
+ }, S = class extends o {
773
+ constructor(e) {
774
+ super(), this.system = e;
775
+ }
776
+ play(e, t = {}) {
777
+ let n = this.gameObject.x, r = this.gameObject.y;
778
+ switch (e) {
779
+ case "confetti":
780
+ this.system.confetti(n, r, t.count);
781
+ break;
782
+ case "fire":
783
+ this.system.fire(n, r, t.count);
784
+ break;
785
+ case "sparkle":
786
+ this.system.sparkle(n, r, t.count, t.color);
787
+ break;
788
+ case "blood":
789
+ this.system.emit({
790
+ x: n,
791
+ y: r,
792
+ count: 10,
793
+ color: ["#8a0303", "#ff0000"],
794
+ speed: 4,
795
+ gravity: .1,
796
+ decay: .05
797
+ });
798
+ break;
799
+ default: this.system.emit({
800
+ x: n,
801
+ y: r,
802
+ ...t
803
+ });
804
+ }
805
+ }
806
+ }, C = class extends o {
807
+ constructor() {
808
+ super(), this.intensity = 0, this.duration = 0, this.timer = 0, this.originalPos = {
809
+ x: 0,
810
+ y: 0
811
+ }, this.isShaking = !1;
812
+ }
813
+ shake(e = 5, t = .2) {
814
+ this.isShaking || (this.originalPos.x = this.gameObject.x, this.originalPos.y = this.gameObject.y), this.intensity = e, this.duration = t, this.timer = 0, this.isShaking = !0;
815
+ }
816
+ update(e) {
817
+ if (!this.isShaking) return;
818
+ if (this.timer += e, this.timer >= this.duration) {
819
+ this.isShaking = !1, this.gameObject.x = this.originalPos.x, this.gameObject.y = this.originalPos.y;
820
+ return;
821
+ }
822
+ let t = (Math.random() - .5) * 2 * this.intensity, n = (Math.random() - .5) * 2 * this.intensity;
823
+ this.gameObject.x = this.originalPos.x + t, this.gameObject.y = this.originalPos.y + n;
824
+ }
825
+ };
826
+ //#endregion
827
+ export { e as C, t as S, s as _, y as a, r as b, g as c, p as d, f, c as g, l as h, b as i, h as l, u as m, S as n, v as o, d as p, x as r, _ as s, C as t, m as u, o as v, n as x, i as y };