dopaminejs 2.5.0 → 3.1.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.
Files changed (81) hide show
  1. package/README.md +55 -50
  2. package/dist/DataService-D1uL6E8R.cjs +1 -0
  3. package/dist/DataService-DN6xwvXU.js +745 -0
  4. package/dist/core.cjs +1 -1
  5. package/dist/core.js +2 -2
  6. package/dist/dopamine.cjs +1 -37
  7. package/dist/dopamine.js +3 -635
  8. package/dist/games.cjs +1 -0
  9. package/dist/games.js +82 -0
  10. package/dist/host-7FzZ-yO7.cjs +37 -0
  11. package/dist/host-CdjhXPy-.js +815 -0
  12. package/dist/style.css +395 -2
  13. package/dist/testing.cjs +1 -0
  14. package/dist/testing.js +475 -0
  15. package/package.json +24 -9
  16. package/spec/README.md +228 -0
  17. package/spec/VERSION +1 -0
  18. package/spec/fixtures/achievements.json +611 -0
  19. package/spec/fixtures/levels.json +421 -0
  20. package/spec/fixtures/merge.json +1739 -0
  21. package/spec/fixtures/migrate.json +957 -0
  22. package/spec/fixtures/scores.json +755 -0
  23. package/spec/fixtures/streaks.json +967 -0
  24. package/spec/schema.json +61 -0
  25. package/src/dopamine/audio/SoundManager.js +54 -24
  26. package/src/dopamine/core/DopamineError.js +7 -0
  27. package/src/dopamine/core/RewardSystem.js +359 -374
  28. package/src/dopamine/core/achievements.js +49 -0
  29. package/src/dopamine/core/clock.js +11 -0
  30. package/src/dopamine/core/levels.js +4 -0
  31. package/src/dopamine/core/merge.js +68 -0
  32. package/src/dopamine/core/middleware.js +58 -0
  33. package/src/dopamine/core/store.js +261 -0
  34. package/src/dopamine/core/streaks.js +55 -0
  35. package/src/dopamine/effects/ParticleSystem.js +30 -0
  36. package/src/dopamine/host.js +360 -0
  37. package/src/dopamine/ui/{GameUI.js → RewardUI.js} +166 -202
  38. package/src/games.js +138 -0
  39. package/src/index.js +11 -34
  40. package/src/progression.js +3 -0
  41. package/src/testing.js +490 -0
  42. package/types/core.d.ts +9 -1
  43. package/types/games.d.ts +46 -0
  44. package/types/index.d.ts +311 -385
  45. package/types/testing.d.ts +96 -0
  46. package/dist/DataService-BPXCldpo.cjs +0 -1
  47. package/dist/DataService-CH9_gv_8.js +0 -398
  48. package/dist/ScreenShake-9jgNXPS6.cjs +0 -1
  49. package/dist/ScreenShake-CLvO1wx4.js +0 -827
  50. package/dist/engine.cjs +0 -1
  51. package/dist/engine.js +0 -2
  52. package/src/core/Animator.js +0 -62
  53. package/src/core/Collider.js +0 -86
  54. package/src/core/Component.js +0 -38
  55. package/src/core/DopamineKernel.js +0 -170
  56. package/src/core/EventBus.js +0 -206
  57. package/src/core/Game.js +0 -90
  58. package/src/core/GameObject.js +0 -99
  59. package/src/core/PluginRegistry.js +0 -359
  60. package/src/core/Scene.js +0 -107
  61. package/src/core/Sprite.js +0 -51
  62. package/src/core/SystemRegistry.js +0 -263
  63. package/src/core/Vector2.js +0 -50
  64. package/src/dopamine/components/ParticleEmitter.js +0 -50
  65. package/src/dopamine/components/ScreenShake.js +0 -50
  66. package/src/dopamine/components/index.js +0 -2
  67. package/src/dopamine/index.js +0 -93
  68. package/src/dopamine/utils/TextureGenerator.js +0 -90
  69. package/src/engine.js +0 -38
  70. package/src/interfaces/IAudioSystem.js +0 -58
  71. package/src/interfaces/IParticleSystem.js +0 -51
  72. package/src/interfaces/IPhysicsSystem.js +0 -52
  73. package/src/interfaces/ISystem.js +0 -48
  74. package/src/interfaces/index.js +0 -7
  75. package/src/renderer/Renderer.js +0 -45
  76. package/src/systems/Director.js +0 -34
  77. package/src/systems/Input.js +0 -81
  78. package/src/systems/Loader.js +0 -48
  79. package/src/systems/Physics.js +0 -210
  80. package/src/systems/Ticker.js +0 -91
  81. package/src/systems/deprecate.js +0 -38
@@ -0,0 +1,815 @@
1
+ import { c as e, i as t, r as n, s as r, t as i } from "./DataService-DN6xwvXU.js";
2
+ //#region src/dopamine/ui/dopamine.css?inline
3
+ var a = "#game-ui-overlay,.game-ui-overlay{pointer-events:none;z-index:1000;width:100%;font-family:Inter,-apple-system,BlinkMacSystemFont,Segoe UI,sans-serif;position:fixed;top:0;left:0}.game-ui-top-bar{-webkit-backdrop-filter:blur(10px);backdrop-filter:blur(10px);background:#0000004d;align-items:center;gap:12px;padding:12px 16px;display:flex}.level-badge{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);border-radius:12px;flex-direction:column;align-items:center;min-width:60px;padding:8px 12px;display:flex;box-shadow:0 4px 12px #667eea66}.level-label{color:#fffc;letter-spacing:.5px;font-size:10px;font-weight:600}.level-number{color:#fff;font-size:24px;font-weight:800;line-height:1}.xp-container{flex-direction:column;flex:1;gap:4px;display:flex}.xp-bar-bg{background:#fff3;border-radius:6px;height:12px;overflow:hidden;box-shadow:inset 0 2px 4px #0003}.xp-bar-fill{background:linear-gradient(90deg,#4facfe 0%,#00f2fe 100%);border-radius:6px;height:100%;transition:width .5s ease-out;box-shadow:0 0 10px #4facfe99}.xp-text{color:#fff;text-align:center;text-shadow:0 1px 2px #00000080;font-size:11px;font-weight:600}.streak-badge{background:linear-gradient(135deg,#f80 0%,#f40 100%);border-radius:12px;align-items:center;gap:6px;padding:8px 12px;display:flex;box-shadow:0 4px 12px #f806}.streak-icon{font-size:20px;animation:2s infinite flicker}.streak-number{color:#fff;font-size:18px;font-weight:800}.achievement-popup{z-index:10000;background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);border-radius:16px;max-width:320px;padding:16px;animation:.5s cubic-bezier(.68,-.55,.265,1.55) slideIn;position:fixed;top:80px;right:20px;overflow:hidden;box-shadow:0 8px 32px #0006}.achievement-shine{background:linear-gradient(90deg,#0000,#ffffff4d,#0000);width:100%;height:100%;animation:2s infinite shine;position:absolute;top:0;left:-100%}.achievement-content{align-items:center;gap:12px;display:flex;position:relative}.achievement-icon{font-size:48px;animation:.6s ease-out bounce}.achievement-info{flex:1}.achievement-title{color:#ffffffe6;text-transform:uppercase;letter-spacing:.5px;font-size:12px;font-weight:600}.achievement-name{color:#fff;margin:2px 0;font-size:18px;font-weight:800}.achievement-desc{color:#fffc;margin-bottom:4px;font-size:13px}.achievement-xp{color:gold;background:#fff3;border-radius:8px;padding:4px 8px;font-size:12px;font-weight:700;display:inline-block}.level-up-overlay{z-index:10001;background:#000000d9;justify-content:center;align-items:center;width:100%;height:100%;animation:.3s ease-out fadeIn;display:flex;position:fixed;top:0;left:0}.level-up-content{text-align:center;animation:.5s cubic-bezier(.68,-.55,.265,1.55) scaleIn}.level-up-title{background:linear-gradient(135deg,gold 0%,#ffed4e 100%);-webkit-text-fill-color:transparent;text-transform:uppercase;letter-spacing:4px;-webkit-background-clip:text;background-clip:text;margin-bottom:16px;font-size:32px;font-weight:800}.level-up-number{color:#fff;text-shadow:0 0 20px #ffd700cc,0 0 40px #ffd70099,0 0 60px #ffd70066;font-size:120px;font-weight:900;animation:1s ease-in-out infinite alternate glow}.level-up-subtitle{color:#fffc;margin-top:16px;font-size:24px;font-weight:600}.floating-text{text-shadow:2px 2px #000,-1px -1px #000,1px -1px #000,-1px 1px #000;pointer-events:none;z-index:9998;font-weight:800;animation:2s ease-out forwards floatUp;position:fixed}@keyframes slideIn{0%{opacity:0;transform:translate(400px)}to{opacity:1;transform:translate(0)}}@keyframes slideOut{0%{opacity:1;transform:translate(0)}to{opacity:0;transform:translate(400px)}}@keyframes fadeIn{0%{opacity:0}to{opacity:1}}@keyframes scaleIn{0%{opacity:0;transform:scale(.5)}to{opacity:1;transform:scale(1)}}@keyframes bounce{0%,to{transform:translateY(0)}50%{transform:translateY(-10px)}}@keyframes glow{0%{text-shadow:0 0 20px #ffd700cc,0 0 40px #ffd70099,0 0 60px #ffd70066}to{text-shadow:0 0 30px gold,0 0 60px #ffd700cc,0 0 90px #ffd70099}}@keyframes shine{0%{left:-100%}30%{left:100%}to{left:100%}}@keyframes flicker{0%,to{opacity:1}50%{opacity:.8}}@keyframes floatUp{0%{opacity:1;transform:translateY(0)}to{opacity:0;transform:translateY(-100px)}}@keyframes xp-pulse{0%{transform:scaleY(1)}50%{transform:scaleY(1.2)}to{transform:scaleY(1)}}@keyframes level-pulse{0%{transform:scale(1)}50%{transform:scale(1.1)}to{transform:scale(1)}}@media (width<=480px){.game-ui-top-bar{gap:8px;padding:10px 12px}.level-badge{min-width:50px;padding:6px 10px}.level-number{font-size:20px}.xp-bar-bg{height:10px}.xp-text{font-size:10px}.achievement-popup{max-width:calc(100% - 24px);right:12px}.level-up-number{font-size:80px}}@media (prefers-reduced-motion:reduce){.game-ui-overlay *,.achievement-popup,.achievement-popup *,.level-up-overlay,.level-up-overlay *{transition:none!important;animation:none!important}.floating-text{animation:none!important}}", o = "game-ui-overlay", s = {
4
+ levelUp: "LEVEL UP!",
5
+ achievementUnlocked: "Achievement Unlocked!"
6
+ }, c = "dopamine-reward-ui-css", l = "🔥", u = (e) => e == null ? "" : String(e), d = (e, t) => Object.prototype.hasOwnProperty.call(e, t), f = (e, t) => typeof e == "object" && e && d(e, t) ? e[t] : void 0, p = "http://www.w3.org/2000/svg", m = /* @__PURE__ */ new Set([
7
+ "svg",
8
+ "g",
9
+ "path",
10
+ "circle",
11
+ "ellipse",
12
+ "rect",
13
+ "line",
14
+ "polyline",
15
+ "polygon",
16
+ "text",
17
+ "tspan",
18
+ "title",
19
+ "desc",
20
+ "defs",
21
+ "linearGradient",
22
+ "radialGradient",
23
+ "stop",
24
+ "clipPath",
25
+ "mask"
26
+ ]), h = /* @__PURE__ */ new Set(/* @__PURE__ */ "viewBox.width.height.x.y.x1.y1.x2.y2.cx.cy.r.rx.ry.d.points.transform.fill.fill-opacity.fill-rule.stroke.stroke-width.stroke-linecap.stroke-linejoin.stroke-opacity.opacity.offset.stop-color.stop-opacity.gradientUnits.gradientTransform.clip-path.mask.clip-rule.font-size.font-family.font-weight.text-anchor.id.aria-hidden.aria-label.role".split(".")), g = /* @__PURE__ */ new Set([
27
+ "fill",
28
+ "stroke",
29
+ "clip-path",
30
+ "mask"
31
+ ]), _ = /* @__PURE__ */ new Set(["transform", "gradientTransform"]), v = /^url\(\s*#([A-Za-z][\w.:-]*)\s*\)$/, y = /^[#a-zA-Z0-9(),.%\s-]+$/, b = /^[a-zA-Z0-9(),.\s-]+$/, x = /^[A-Za-z][\w.:-]*$/, S = 0;
32
+ function C(e, t) {
33
+ if (e.nodeType !== 1 || e.namespaceURI !== p || !m.has(e.localName)) return;
34
+ let n = e.getAttribute("id");
35
+ n && x.test(n) && t.add(n);
36
+ for (let n of Array.from(e.childNodes)) C(n, t);
37
+ }
38
+ function w(e, t, n) {
39
+ if (t.includes("\\")) return null;
40
+ let r = t.trim();
41
+ if (e === "id") return x.test(r) ? n.prefix + r : null;
42
+ if (g.has(e)) {
43
+ let e = v.exec(r);
44
+ return e ? n.ids.has(e[1]) ? `url(#${n.prefix}${e[1]})` : null : y.test(r) && !/url/i.test(r) ? r : null;
45
+ }
46
+ return _.has(e) ? b.test(r) && !/url/i.test(r) ? r : null : t.includes("(") ? e === "stop-color" && y.test(r) && !/url/i.test(r) ? r : null : t;
47
+ }
48
+ function T(e, t) {
49
+ if (e.nodeType === 3 || e.nodeType === 4) return document.createTextNode(e.data);
50
+ if (e.nodeType !== 1 || e.namespaceURI !== p || !m.has(e.localName)) return null;
51
+ let n = document.createElementNS(p, e.localName);
52
+ for (let r of Array.from(e.attributes)) {
53
+ if (r.namespaceURI !== null || !h.has(r.name)) continue;
54
+ let e = w(r.name, r.value, t);
55
+ e !== null && n.setAttribute(r.name, e);
56
+ }
57
+ for (let r of Array.from(e.childNodes)) {
58
+ let e = T(r, t);
59
+ e && n.appendChild(e);
60
+ }
61
+ return n;
62
+ }
63
+ function E(e) {
64
+ let t, n = /^\s*<svg[\s>]/.test(e) && !/^\s*<svg\b[^>]*\sxmlns\s*=/.test(e) ? e.replace(/^(\s*<svg)/, `$1 xmlns="${p}"`) : e;
65
+ try {
66
+ t = new DOMParser().parseFromString(n, "image/svg+xml");
67
+ } catch {
68
+ return null;
69
+ }
70
+ let r = t.documentElement;
71
+ if (!r || r.localName !== "svg" || t.getElementsByTagName("parsererror").length > 0) return null;
72
+ let i = /* @__PURE__ */ new Set();
73
+ return C(r, i), T(r, {
74
+ ids: i,
75
+ prefix: `dj-icon-${++S}-`
76
+ });
77
+ }
78
+ var D = class {
79
+ constructor(e = {}, t = {}) {
80
+ this.parts = e || {};
81
+ let n = f(t, "text");
82
+ this.strings = { ...s };
83
+ for (let e of Object.keys(s)) {
84
+ let t = f(n, e);
85
+ typeof t == "string" && (this.strings[e] = t);
86
+ }
87
+ this.safeIcons = f(t, "safeIcons") !== !1, this._streakIcon = f(f(t, "icons"), "streak") ?? l, this._injectCss = f(t, "injectCss"), this.container = null, this.xpBar = null, this.levelDisplay = null, this.streakDisplay = null, this._style = null, this._xp = null, this._level = null, this._timers = /* @__PURE__ */ new Set(), this._detached = [], this._build();
88
+ }
89
+ get root() {
90
+ return this.container;
91
+ }
92
+ _build() {
93
+ this._injectCss && this._addStyles(), this.container = document.createElement("div"), this.container.className = "game-ui-overlay", document.getElementById(o) || (this.container.id = o), this.container.innerHTML = "\n <div class=\"game-ui-top-bar\">\n <div class=\"level-badge\">\n <span class=\"level-label\">LVL</span>\n <span class=\"level-number\">1</span>\n </div>\n\n <div class=\"xp-container\">\n <div class=\"xp-bar-bg\">\n <div class=\"xp-bar-fill\" style=\"width: 0%\"></div>\n </div>\n <div class=\"xp-text\">0 / 100 XP</div>\n </div>\n\n <div class=\"streak-badge\">\n <span class=\"streak-icon\"></span>\n <span class=\"streak-number\">1</span>\n </div>\n </div>\n ", this.liveRegion = document.createElement("div"), this.liveRegion.className = "dopamine-live-region", this.liveRegion.setAttribute("aria-live", "polite"), this.liveRegion.setAttribute("role", "status"), Object.assign(this.liveRegion.style, {
94
+ position: "absolute",
95
+ width: "1px",
96
+ height: "1px",
97
+ overflow: "hidden",
98
+ clipPath: "inset(50%)",
99
+ whiteSpace: "nowrap"
100
+ }), this.container.appendChild(this.liveRegion), document.body.appendChild(this.container), this.levelBadge = this.container.querySelector(".level-badge"), this.streakBadge = this.container.querySelector(".streak-badge"), this.xpBar = this.container.querySelector(".xp-bar-fill"), this.xpText = this.container.querySelector(".xp-text"), this.levelDisplay = this.container.querySelector(".level-number"), this.streakDisplay = this.container.querySelector(".streak-number"), this._setIcon(this.container.querySelector(".streak-icon"), this._streakIcon);
101
+ }
102
+ _addStyles() {
103
+ if (document.getElementById(c)) return;
104
+ let e = typeof this._injectCss == "string" ? this._injectCss : a;
105
+ this._style = document.createElement("style"), this._style.id = c, this._style.textContent = e, document.head.appendChild(this._style);
106
+ }
107
+ _setIcon(e, t) {
108
+ if (e.textContent = "", typeof Node < "u" && t instanceof Node) e.appendChild(t);
109
+ else if (typeof t != "string") return;
110
+ else if (!this.safeIcons) e.innerHTML = t;
111
+ else {
112
+ let n = E(t);
113
+ n ? e.appendChild(n) : e.textContent = t;
114
+ }
115
+ }
116
+ _defer(e, t) {
117
+ let n = setTimeout(() => {
118
+ this._timers.delete(n), e();
119
+ }, t);
120
+ return this._timers.add(n), n;
121
+ }
122
+ _mount(e) {
123
+ return document.body.appendChild(e), this._detached.push(e), e;
124
+ }
125
+ _announce(e) {
126
+ this.liveRegion && (this.liveRegion.textContent = e);
127
+ }
128
+ _burst(e, t, n) {
129
+ this.parts.particles?.burst?.(e, t, n);
130
+ }
131
+ _replayAnimation(e, t) {
132
+ e && (e.style.animation = "none", this._defer(() => {
133
+ e.style.animation = t;
134
+ }, 10));
135
+ }
136
+ hud(e) {
137
+ if (!this.container || !e) return;
138
+ let { xp: t, level: n, progress: r, streak: i, next: a } = e, o = Number.isFinite(r) ? r : a > 0 ? t / a : 0;
139
+ this.xpBar.style.width = `${Math.max(0, Math.min(100, o * 100))}%`, this.xpText.textContent = Number.isFinite(a) ? `${t} / ${a} XP` : `${t} XP`, t !== this._xp && this._replayAnimation(this.xpBar, "xp-pulse 0.3s ease-out"), this._xp = t, this.levelDisplay.textContent = n, n !== this._level && this._replayAnimation(this.levelBadge, "level-pulse 0.5s ease-out"), this._level = n, this.streakDisplay.textContent = i, i >= 7 ? this.streakBadge.style.background = "linear-gradient(135deg, #ff6b00 0%, #ff4400 100%)" : i >= 3 ? this.streakBadge.style.background = "linear-gradient(135deg, #ff8800 0%, #ff6b00 100%)" : this.streakBadge.style.background = "";
140
+ }
141
+ popup(e) {
142
+ if (!this.container || !e || typeof e != "object") return;
143
+ let t = f(e, "kind");
144
+ t === "level" ? this._levelPopup(e) : t === "notice" ? this._notice(e) : this._card(e, t === "achievement");
145
+ }
146
+ _card(e, t) {
147
+ let n = document.createElement("div");
148
+ n.className = "achievement-popup", n.innerHTML = "\n <div class=\"achievement-shine\"></div>\n <div class=\"achievement-content\">\n <div class=\"achievement-icon\"></div>\n <div class=\"achievement-info\">\n <div class=\"achievement-title\"></div>\n <div class=\"achievement-name\"></div>\n <div class=\"achievement-desc\"></div>\n <div class=\"achievement-xp\"></div>\n </div>\n </div>\n ";
149
+ let r = u(e.title);
150
+ n.querySelector(".achievement-title").textContent = t ? this.strings.achievementUnlocked : "", this._setIcon(n.querySelector(".achievement-icon"), e.icon), n.querySelector(".achievement-name").textContent = r, n.querySelector(".achievement-desc").textContent = u(e.body), n.querySelector(".achievement-xp").textContent = u(e.meta), this._mount(n), this._announce(t ? `${this.strings.achievementUnlocked}: ${r}` : r), this._defer(() => {
151
+ let e = n.getBoundingClientRect();
152
+ this._burst("confetti", {
153
+ x: e.left + e.width / 2,
154
+ y: e.top + e.height / 2
155
+ }, { count: 40 });
156
+ }, 300), this._defer(() => {
157
+ n.style.animation = "slideOut 0.3s ease-in forwards", this._defer(() => this._remove(n), 300);
158
+ }, 4e3);
159
+ }
160
+ _levelPopup(e) {
161
+ let t = typeof e.title == "string" && e.title !== "" ? e.title : this.strings.levelUp, n = u(e.body), r = document.createElement("div");
162
+ r.className = "level-up-overlay", r.innerHTML = "\n <div class=\"level-up-content\">\n <div class=\"level-up-title\"></div>\n <div class=\"level-up-number\"></div>\n <div class=\"level-up-subtitle\">Amazing progress!</div>\n </div>\n ", r.querySelector(".level-up-title").textContent = t, r.querySelector(".level-up-number").textContent = n, this._mount(r), this._announce(`Level up: ${n}`), this._defer(() => {
163
+ for (let e = 0; e < 5; e++) this._defer(() => {
164
+ this._burst("confetti", {
165
+ x: Math.random() * window.innerWidth,
166
+ y: Math.random() * window.innerHeight * .6
167
+ }, { count: 30 });
168
+ }, e * 200);
169
+ }, 300), this._defer(() => {
170
+ r.style.opacity = "0", this._defer(() => this._remove(r), 500);
171
+ }, 2500);
172
+ }
173
+ _notice(e) {
174
+ let t = window.innerWidth / 2, n = window.innerHeight / 3, r = u(e.title);
175
+ e.tier === "legendary" ? (this._floatText({
176
+ text: r,
177
+ x: t,
178
+ y: n,
179
+ color: "#ffd700",
180
+ size: "36px"
181
+ }), this._burst("stars", {
182
+ x: t,
183
+ y: n
184
+ }, { count: 12 }), this._burst("confetti", {
185
+ x: t,
186
+ y: n
187
+ }, { count: 25 })) : e.tier === "rare" ? (this._floatText({
188
+ text: r,
189
+ x: t,
190
+ y: n,
191
+ color: "#ff6b6b",
192
+ size: "32px"
193
+ }), this._burst("fire", {
194
+ x: t,
195
+ y: n
196
+ }, { count: 20 })) : this._floatText({
197
+ text: r,
198
+ x: t,
199
+ y: n,
200
+ color: "#fff",
201
+ size: "24px"
202
+ }), this._announce(r);
203
+ }
204
+ text(e) {
205
+ this._floatText({ ...e });
206
+ }
207
+ _floatText({ text: e, x: t, y: n, color: r = "#ffd700", size: i = "24px" }) {
208
+ if (!this.container) return;
209
+ let a = document.createElement("div");
210
+ a.className = "floating-text", a.textContent = u(e), a.style.left = (Number.isFinite(t) ? t : window.innerWidth / 2) + "px", a.style.top = (Number.isFinite(n) ? n : window.innerHeight / 3) + "px", a.style.color = r, a.style.fontSize = i, this._mount(a), this._defer(() => this._remove(a), 2e3);
211
+ }
212
+ _remove(e) {
213
+ e.remove();
214
+ let t = this._detached.indexOf(e);
215
+ t > -1 && this._detached.splice(t, 1);
216
+ }
217
+ destroy() {
218
+ for (let e of this._timers) clearTimeout(e);
219
+ this._timers.clear();
220
+ for (let e of this._detached) e.remove();
221
+ this._detached = [], this.container?.remove(), this._style?.remove(), this._style = null, this.container = null, this.liveRegion = null, this.xpBar = null, this.xpText = null, this.levelBadge = null, this.streakBadge = null, this.levelDisplay = null, this.streakDisplay = null, this._streakIcon = null;
222
+ }
223
+ }, O = Object.freeze({ matches: !1 });
224
+ function k(e = globalThis.window) {
225
+ return typeof e?.matchMedia == "function" && e.matchMedia("(prefers-reduced-motion: reduce)") || O;
226
+ }
227
+ //#endregion
228
+ //#region src/dopamine/effects/ParticleSystem.js
229
+ var A = (e, t) => Object.prototype.hasOwnProperty.call(e, t), j = {
230
+ confetti: (e, t, n, r) => e.confetti(t, n, r),
231
+ sparkle: (e, t, n, r, i) => e.sparkle(t, n, r, i),
232
+ fire: (e, t, n, r) => e.fire(t, n, r),
233
+ stars: (e, t, n, r) => e.starBurst(t, n, r),
234
+ coins: (e, t, n, r) => e.coinShower(t, n, r)
235
+ }, M = class {
236
+ constructor(e = {}) {
237
+ this.container = e.container ? typeof e.container == "string" ? document.querySelector(e.container) : e.container : document.body, this.container ||= (console.warn(`[DopamineJS] Particle container '${e.container}' not found. Using document.body.`), document.body), this.maxParticles = Number.isFinite(e.maxParticles) ? Math.max(0, Math.floor(e.maxParticles)) : 5e3, this._reducedMotion = e.respectReducedMotion === !1 ? null : k(window), this.canvasId = e.canvasId || "dopamine-particle-canvas", this.canvas = document.createElement("canvas"), this.canvas.classList.add("dopamine-particle-canvas"), document.getElementById(this.canvasId) || (this.canvas.id = this.canvasId), this.canvas.style.pointerEvents = "none", this.canvas.style.zIndex = e.zIndex || "9999", this.container === document.body ? (this.canvas.style.position = "fixed", this.canvas.style.top = "0", this.canvas.style.left = "0") : (this.canvas.style.position = "absolute", this.canvas.style.top = "0", this.canvas.style.left = "0", window.getComputedStyle(this.container).position === "static" && (this.container.style.position = "relative")), this.container.appendChild(this.canvas), this.ctx = this.canvas.getContext("2d"), this.particles = [], this.pool = [], this.sprites = /* @__PURE__ */ new Map(), this.customEffects = /* @__PURE__ */ new Map(), this.isAnimating = !1, this._onResize = () => this._resize(), this._resizeObserver = null, this._frameHandle = null, this._lastFrameTime = null, this._resize(), window.ResizeObserver && this.container !== document.body ? (this._resizeObserver = new ResizeObserver(this._onResize), this._resizeObserver.observe(this.container)) : window.addEventListener("resize", this._onResize);
238
+ }
239
+ _resize() {
240
+ let e = this.container === document.body ? window.innerWidth : this.container.clientWidth, t = this.container === document.body ? window.innerHeight : this.container.clientHeight, n = window.devicePixelRatio || 1;
241
+ this.width = e, this.height = t, this.canvas.width = Math.round(e * n), this.canvas.height = Math.round(t * n), this.canvas.style.width = `${e}px`, this.canvas.style.height = `${t}px`, this.ctx.setTransform(n, 0, 0, n, 0, 0);
242
+ }
243
+ registerSprite(e, t) {
244
+ let n = new Image();
245
+ n.src = t, this.sprites.set(e, n);
246
+ }
247
+ registerEffect(e, t) {
248
+ this.customEffects.set(e, t);
249
+ }
250
+ play(e, t, n, ...r) {
251
+ typeof t == "object" && t && (r = n === void 0 ? [] : [n, ...r], {x: t, y: n} = t);
252
+ let i = this.customEffects.get(e);
253
+ i ? i(t, n, ...r) : console.warn(`[DopamineJS] Effect '${e}' not found.`);
254
+ }
255
+ emit(e) {
256
+ let { x: t, y: n, count: r = 10, speed: i = 5, gravity: a = .1, decay: o = .02, life: s = 1, color: c = "#ffd700", sprite: l = null, spread: u = Math.PI * 2, angle: d = 0, size: f = 5, type: p = null } = e, m = this._allowed(r);
257
+ for (let e = 0; e < m; e++) {
258
+ let e = this._getParticle(), r = d + (Math.random() - .5) * u, m = Math.random() * i;
259
+ e.x = t, e.y = n, e.vx = Math.cos(r) * m, e.vy = Math.sin(r) * m, e.life = s, e.decay = o * (.8 + Math.random() * .4), e.gravity = a, e.color = Array.isArray(c) ? c[Math.floor(Math.random() * c.length)] : c, e.size = f * (.8 + Math.random() * .4), e.sprite = l, e.type = p, e.rotation = Math.random() * Math.PI * 2, e.rotationSpeed = (Math.random() - .5) * .2, this.particles.push(e);
260
+ }
261
+ m > 0 && this._startAnimation();
262
+ }
263
+ _allowed(e) {
264
+ if (this._reducedMotion?.matches || !Number.isFinite(e)) return 0;
265
+ let t = this.maxParticles - this.particles.length;
266
+ return Math.max(0, Math.min(Math.floor(e), t));
267
+ }
268
+ _getParticle() {
269
+ return this.pool.length > 0 ? this.pool.pop() : {};
270
+ }
271
+ _recycleParticle(e) {
272
+ this.pool.push(e);
273
+ }
274
+ burst(e, t, n) {
275
+ if (typeof e != "string" || !A(j, e)) return;
276
+ let r = Number.isFinite(t?.x) ? t.x : this.width / 2, i = Number.isFinite(t?.y) ? t.y : this.height / 2, a = Number.isFinite(n?.count) ? n.count : void 0;
277
+ j[e](this, r, i, a, n?.color);
278
+ }
279
+ confetti(e, t, n = 50) {
280
+ this.emit({
281
+ x: e,
282
+ y: t,
283
+ count: n,
284
+ color: [
285
+ "#ff6b6b",
286
+ "#4ecdc4",
287
+ "#45b7d1",
288
+ "#f7dc6f",
289
+ "#c06c84",
290
+ "#6c5ce7"
291
+ ],
292
+ speed: 10,
293
+ gravity: .3,
294
+ decay: .015,
295
+ size: 8,
296
+ spread: Math.PI * 2
297
+ });
298
+ }
299
+ coinShower(e, t, n = 10) {
300
+ this.emit({
301
+ x: e,
302
+ y: t,
303
+ count: n,
304
+ color: "#ffd700",
305
+ speed: 4,
306
+ gravity: .4,
307
+ decay: .012,
308
+ size: 12,
309
+ angle: -Math.PI / 2,
310
+ spread: Math.PI / 2
311
+ });
312
+ }
313
+ sparkle(e, t, n = 20, r = "#ffd700") {
314
+ this.emit({
315
+ x: e,
316
+ y: t,
317
+ count: n,
318
+ color: r,
319
+ speed: 3,
320
+ gravity: -.05,
321
+ decay: .03,
322
+ size: 3
323
+ });
324
+ }
325
+ fire(e, t, n = 15) {
326
+ this.emit({
327
+ x: e,
328
+ y: t,
329
+ count: n,
330
+ color: [
331
+ "#ff6b00",
332
+ "#ff8800",
333
+ "#ffaa00",
334
+ "#ff4400"
335
+ ],
336
+ speed: 3,
337
+ gravity: -.2,
338
+ decay: .04,
339
+ size: 5,
340
+ angle: -Math.PI / 2,
341
+ spread: Math.PI / 4
342
+ });
343
+ }
344
+ starBurst(e, t, n = 8) {
345
+ let r = this._allowed(n);
346
+ for (let n = 0; n < r; n++) {
347
+ let i = this._getParticle(), a = Math.PI * 2 / r * n;
348
+ i.x = e, i.y = t, i.vx = Math.cos(a) * 4, i.vy = Math.sin(a) * 4, i.life = 1, i.decay = .03, i.gravity = 0, i.color = "#ffffff", i.size = 15, i.sprite = null, i.type = "star", i.rotation = a, i.rotationSpeed = 0, this.particles.push(i);
349
+ }
350
+ r > 0 && this._startAnimation();
351
+ }
352
+ _startAnimation() {
353
+ this.isAnimating || (this.isAnimating = !0, this._lastFrameTime = null, this._animate());
354
+ }
355
+ _animate(e) {
356
+ this._frameHandle = null, this.ctx.clearRect(0, 0, this.width, this.height);
357
+ let t = 1;
358
+ Number.isFinite(e) && (this._lastFrameTime !== null && (t = Math.max(0, Math.min(3, (e - this._lastFrameTime) / (1e3 / 60)))), this._lastFrameTime = e);
359
+ for (let e = this.particles.length - 1; e >= 0; e--) {
360
+ let n = this.particles[e];
361
+ if (n.vy += n.gravity * t, n.x += n.vx * t, n.y += n.vy * t, n.life -= n.decay * t, n.rotationSpeed && (n.rotation += n.rotationSpeed * t), n.life <= 0) {
362
+ this._recycleParticle(n);
363
+ let t = this.particles.pop();
364
+ e < this.particles.length && (this.particles[e] = t);
365
+ continue;
366
+ }
367
+ if (this.ctx.save(), this.ctx.globalAlpha = n.life, this.ctx.translate(n.x, n.y), this.ctx.rotate(n.rotation), n.sprite && this.sprites.has(n.sprite)) {
368
+ let e = this.sprites.get(n.sprite);
369
+ e.complete && this.ctx.drawImage(e, -n.size / 2, -n.size / 2, n.size, n.size);
370
+ } else n.type === "star" ? (this.ctx.fillStyle = n.color, this._drawStarPath(0, 0, 5, n.size, n.size / 2), this.ctx.fill()) : (this.ctx.fillStyle = n.color, this.ctx.beginPath(), this.ctx.arc(0, 0, n.size, 0, Math.PI * 2), this.ctx.fill());
371
+ this.ctx.restore();
372
+ }
373
+ this.particles.length > 0 ? this._frameHandle = requestAnimationFrame((e) => this._animate(e)) : this.isAnimating = !1;
374
+ }
375
+ _drawStarPath(e, t, n, r, i) {
376
+ let a = Math.PI / 2 * 3, o = e, s = t, c = Math.PI / n;
377
+ this.ctx.beginPath(), this.ctx.moveTo(e, t - r);
378
+ for (let l = 0; l < n; l++) o = e + Math.cos(a) * r, s = t + Math.sin(a) * r, this.ctx.lineTo(o, s), a += c, o = e + Math.cos(a) * i, s = t + Math.sin(a) * i, this.ctx.lineTo(o, s), a += c;
379
+ this.ctx.lineTo(e, t - r), this.ctx.closePath();
380
+ }
381
+ clear() {
382
+ this.particles.forEach((e) => this._recycleParticle(e)), this.particles = [], this.ctx.clearRect(0, 0, this.width, this.height);
383
+ }
384
+ destroy() {
385
+ this.clear(), this.isAnimating = !1, this._frameHandle !== null && (cancelAnimationFrame(this._frameHandle), this._frameHandle = null), this._resizeObserver &&= (this._resizeObserver.disconnect(), null), window.removeEventListener("resize", this._onResize), this.canvas.remove(), this.sprites.clear(), this.customEffects.clear(), this.pool = [];
386
+ }
387
+ }, N = (e, t) => Object.prototype.hasOwnProperty.call(e, t), P = class {
388
+ constructor(e = {}) {
389
+ this.audioContext = null, this.storageKey = e.storageKey || "dopamineSoundsMuted", this.storage = e.storage || n(), this.muted = this.storage.getItem(this.storageKey) === "true", this.assets = /* @__PURE__ */ new Map(), this.customSounds = e.customSounds || {}, this.masterGain = null, this._volume = 1, this.setVolume(e.volume ?? 1), this._loading = /* @__PURE__ */ new Map(), this._timers = /* @__PURE__ */ new Set(), this._synths = /* @__PURE__ */ new Map(), this._unlocks = /* @__PURE__ */ new Set();
390
+ }
391
+ unlockOn(e, t = [
392
+ "pointerup",
393
+ "touchend",
394
+ "keydown"
395
+ ]) {
396
+ let n = () => {
397
+ for (let n of t) e.removeEventListener(n, r);
398
+ this._unlocks.delete(n);
399
+ }, r = () => {
400
+ n(), this.initAudio();
401
+ };
402
+ for (let n of t) e.addEventListener(n, r);
403
+ return this._unlocks.add(n), n;
404
+ }
405
+ _later(e, t) {
406
+ let n = setTimeout(() => {
407
+ this._timers.delete(n), e();
408
+ }, t);
409
+ this._timers.add(n);
410
+ }
411
+ initAudio() {
412
+ if (!this.audioContext) {
413
+ let e = typeof window < "u" ? window.AudioContext || window.webkitAudioContext : void 0;
414
+ if (!e) return !1;
415
+ this.audioContext = new e(), this.masterGain = this.audioContext.createGain(), this.masterGain.gain.value = this._volume, this.masterGain.connect(this.audioContext.destination), this.preloadSounds(this.customSounds);
416
+ }
417
+ return this.audioContext.state === "suspended" && this.audioContext.resume(), !0;
418
+ }
419
+ setVolume(e) {
420
+ return Number.isFinite(e) ? (this._volume = Math.max(0, Math.min(1, e)), this.masterGain && (this.masterGain.gain.value = this._volume), this._volume) : this._volume;
421
+ }
422
+ getVolume() {
423
+ return this._volume;
424
+ }
425
+ _output() {
426
+ return this.masterGain || this.audioContext.destination;
427
+ }
428
+ toggleMute() {
429
+ return this.muted = !this.muted, this.storage.setItem(this.storageKey, this.muted), this.muted;
430
+ }
431
+ registerSound(e, t) {
432
+ this.customSounds[e] = t, this.audioContext && this.loadSound(e, t);
433
+ }
434
+ register(e, t) {
435
+ return typeof e != "string" || e === "" ? () => {} : typeof t == "string" ? (this.registerSound(e, t), () => {
436
+ this.customSounds[e] === t && delete this.customSounds[e], this.assets.delete(e);
437
+ }) : typeof t == "function" ? (this._synths.set(e, t), () => {
438
+ this._synths.get(e) === t && this._synths.delete(e);
439
+ }) : () => {};
440
+ }
441
+ async preloadSounds(e) {
442
+ let t = Object.entries(e).map(([e, t]) => (this.customSounds[e] = t, this.loadSound(e, t)));
443
+ await Promise.allSettled(t);
444
+ }
445
+ async loadSound(e, t) {
446
+ if (!this.audioContext) return;
447
+ if (this._loading.has(e)) return this._loading.get(e);
448
+ let n = this._load(e, t).finally(() => this._loading.delete(e));
449
+ return this._loading.set(e, n), n;
450
+ }
451
+ async _load(e, t) {
452
+ try {
453
+ let n = this.audioContext, r = await fetch(t);
454
+ if (r.ok === !1) throw Error(`HTTP ${r.status}`);
455
+ let i = await r.arrayBuffer(), a = await n.decodeAudioData(i);
456
+ this.audioContext === n && this.assets.set(e, a);
457
+ } catch (t) {
458
+ console.warn(`[DopamineJS] Failed to load sound '${e}':`, t);
459
+ }
460
+ }
461
+ async play(e) {
462
+ if (this.muted || !this.initAudio()) return;
463
+ if (this.assets.has(e)) {
464
+ this._playBuffer(this.assets.get(e));
465
+ return;
466
+ }
467
+ if (N(this.customSounds, e) && !this.assets.has(e)) {
468
+ let t = this.audioContext;
469
+ if (await this.loadSound(e, this.customSounds[e]), this.audioContext !== t) return;
470
+ if (this.assets.has(e)) {
471
+ this._playBuffer(this.assets.get(e));
472
+ return;
473
+ }
474
+ }
475
+ let t = {
476
+ xp: () => this.playXP(!0),
477
+ level_up: () => this.playLevelUp(!0),
478
+ achievement: () => this.playAchievement(!0),
479
+ personal_best: () => this.playPersonalBest(!0),
480
+ click: () => this.playClick(!0),
481
+ error: () => this.playError(!0)
482
+ };
483
+ this._synths.has(e) ? this._synths.get(e)(this, (e, t) => this._later(e, t)) : N(t, e) && t[e]();
484
+ }
485
+ _playBuffer(e) {
486
+ let t = this.audioContext.createBufferSource();
487
+ t.buffer = e, t.connect(this._output()), t.start(0);
488
+ }
489
+ playTone(e, t, n = "sine", r = .3) {
490
+ if (this.muted || !this.initAudio()) return;
491
+ let i = this.audioContext.createOscillator(), a = this.audioContext.createGain();
492
+ i.connect(a), a.connect(this._output()), i.frequency.value = e, i.type = n;
493
+ let o = Math.max(1e-4, r);
494
+ a.gain.setValueAtTime(o, this.audioContext.currentTime), a.gain.exponentialRampToValueAtTime(.01, this.audioContext.currentTime + t), i.start(this.audioContext.currentTime), i.stop(this.audioContext.currentTime + t);
495
+ }
496
+ playClick(e = !1) {
497
+ if (!e && this.assets.has("click")) return this.play("click");
498
+ this.playTone(600, .05, "sine", .1);
499
+ }
500
+ playXP(e = !1) {
501
+ if (!e && this.assets.has("xp")) return this.play("xp");
502
+ this.playTone(700, .06, "sine", .12);
503
+ }
504
+ playLevelUp(e = !1) {
505
+ if (!e && this.assets.has("level_up")) return this.play("level_up");
506
+ this.playTone(500, .12, "sine", .2), this._later(() => this.playTone(700, .12, "sine", .2), 110), this._later(() => this.playTone(900, .25, "sine", .2), 220);
507
+ }
508
+ playAchievement(e = !1) {
509
+ if (!e && this.assets.has("achievement")) return this.play("achievement");
510
+ this.playTone(660, .1, "triangle", .2), this._later(() => this.playTone(990, .25, "triangle", .2), 100);
511
+ }
512
+ playPersonalBest(e = !1) {
513
+ if (!e && this.assets.has("personal_best")) return this.play("personal_best");
514
+ this.playTone(600, .1, "sine", .2), this._later(() => this.playTone(800, .2, "sine", .2), 100);
515
+ }
516
+ playError(e = !1) {
517
+ if (!e && this.assets.has("error")) return this.play("error");
518
+ this.playTone(200, .2, "sawtooth", .3);
519
+ }
520
+ destroy() {
521
+ for (let e of [...this._unlocks]) e();
522
+ for (let e of this._timers) clearTimeout(e);
523
+ this._timers.clear(), this.audioContext && (this.audioContext.close?.())?.catch?.(() => {}), this.audioContext = null, this.masterGain = null, this.assets.clear(), this._loading.clear(), this._synths.clear();
524
+ }
525
+ };
526
+ //#endregion
527
+ //#region src/dopamine/feel/haptics.js
528
+ function F({ nav: e = globalThis.navigator, enabled: t = !0 } = {}) {
529
+ let n = !!t, r = (t) => {
530
+ if (n) try {
531
+ e.vibrate(t);
532
+ } catch {}
533
+ };
534
+ return {
535
+ tap: () => r(10),
536
+ double: () => r([
537
+ 10,
538
+ 40,
539
+ 10
540
+ ]),
541
+ setEnabled(e) {
542
+ n = !!e;
543
+ }
544
+ };
545
+ }
546
+ //#endregion
547
+ //#region src/dopamine/host.js
548
+ var I = 3, L = [
549
+ "ui",
550
+ "particles",
551
+ "sound",
552
+ "haptics"
553
+ ], R = (e) => typeof e == "object" && !!e && typeof e.then == "function", z = (e, t) => {
554
+ R(e) && e.then(void 0, (e) => t.error(e));
555
+ };
556
+ function B() {
557
+ let e = typeof document < "u" && typeof window < "u";
558
+ return {
559
+ hasDom: e,
560
+ reducedMotion: e ? k(window).matches === !0 : !1
561
+ };
562
+ }
563
+ function V(e, t, n) {
564
+ if (e === !1) return null;
565
+ let r = e;
566
+ return typeof e == "function" && (r = e(t)), r ?? n();
567
+ }
568
+ function H(e, t) {
569
+ let n = `dopamine_plugin_${t}:`;
570
+ return {
571
+ getItem: (t) => e.getItem(n + t),
572
+ setItem: (t, r) => e.setItem(n + t, r),
573
+ removeItem: (t) => e.removeItem(n + t)
574
+ };
575
+ }
576
+ function U(a = {}) {
577
+ let o = a || {}, s = B(), c = {
578
+ debug: () => {},
579
+ info: () => {},
580
+ warn: (...e) => console.warn(...e),
581
+ error: (...e) => console.error(...e),
582
+ ...o.log || {}
583
+ }, l = o.storage || n(), u = o.clock || r, d = {
584
+ ui: null,
585
+ particles: null,
586
+ sound: null,
587
+ haptics: null
588
+ }, f = {};
589
+ for (let e of L) Object.defineProperty(f, e, {
590
+ enumerable: !0,
591
+ get: () => d[e]
592
+ });
593
+ Object.freeze(f);
594
+ let p = {
595
+ ...s,
596
+ parts: f,
597
+ storage: l,
598
+ clock: u
599
+ }, m = V(o.progress, p, () => new t(new i({ storage: l }), {
600
+ clock: u,
601
+ streakDays: o.streakDays,
602
+ builtinAchievements: o.builtinAchievements
603
+ }));
604
+ if (typeof m?.on != "function" || typeof m?.off != "function") throw new e("invalid_progress", "progress needs on() and off()");
605
+ d.particles = V(o.particles, p, () => s.hasDom ? new M() : null), d.sound = V(o.sound, p, () => new P({ storage: l })), d.haptics = V(o.haptics, p, () => F());
606
+ try {
607
+ d.ui = V(o.ui, p, () => s.hasDom ? new D(f) : null);
608
+ } catch (e) {
609
+ throw h(), e;
610
+ }
611
+ function h() {
612
+ for (let e of L) try {
613
+ d[e]?.destroy?.();
614
+ } catch (e) {
615
+ c.error(e);
616
+ }
617
+ }
618
+ let g = m, _ = [], v = !1, y = (e) => (t) => {
619
+ try {
620
+ z(e(t), c);
621
+ } catch (e) {
622
+ c.error(e);
623
+ }
624
+ }, b = () => {
625
+ let e = d.ui;
626
+ if (!e || typeof m.snapshot != "function" || typeof m.getXPForNextLevel != "function") return;
627
+ let t = m.snapshot(), { total: n, progress: r } = m.getXPForNextLevel();
628
+ e.hud({
629
+ xp: t.xp,
630
+ level: t.level,
631
+ progress: r,
632
+ streak: t.streak.current,
633
+ next: n
634
+ });
635
+ };
636
+ _.push(g.on("xp_gained", y((e) => {
637
+ b(), e.leveledUp || d.sound?.play("xp");
638
+ })), g.on("level_up", y((e) => {
639
+ b(), d.ui?.popup({
640
+ kind: "level",
641
+ title: "",
642
+ body: String(e.newLevel)
643
+ }), d.sound?.play("level_up");
644
+ })), g.on("achievement_unlocked", y((e) => {
645
+ d.ui?.popup({
646
+ kind: "achievement",
647
+ title: e.name,
648
+ body: e.description,
649
+ icon: e.icon,
650
+ meta: `+${e.xp ?? 0} XP`
651
+ }), d.sound?.play("achievement");
652
+ })), g.on("streak_updated", y(b)), g.on("player_reset", y(b)), g.on("personal_best", y((e) => {
653
+ d.ui?.popup({
654
+ kind: "notice",
655
+ title: `New High Score: ${e.score}!`,
656
+ tier: "legendary"
657
+ }), d.sound?.play("personal_best");
658
+ })));
659
+ let x = /* @__PURE__ */ new Map(), S = [];
660
+ function C(t) {
661
+ if (typeof t != "object" || !t || typeof t.name != "string" || !/^[a-z0-9-]+$/.test(t.name) || typeof t.install != "function") throw new e("plugin_invalid", "a plugin needs a name (a-z, 0-9 and -) and an install function");
662
+ if (t.api !== 3) throw new e("plugin_api", `${t.name} was built for api ${t.api}, this host is api 3`);
663
+ if (x.has(t.name)) throw new e("plugin_duplicate", `${t.name} is already installed`);
664
+ for (let n of t.after || []) if (!x.has(n) || !x.get(n).done) throw new e("plugin_order", `${t.name} must come after ${n}`);
665
+ for (let n of t.requires || []) if (!L.includes(n) || d[n] === null) throw new e("plugin_requires", `${t.name} requires the ${n} part, which is not set`);
666
+ }
667
+ function w(e) {
668
+ for (let t of e.offs) t();
669
+ e.offs.clear(), x.delete(e.name);
670
+ let t = S.indexOf(e);
671
+ t > -1 && S.splice(t, 1);
672
+ }
673
+ function T(e) {
674
+ if (e.removed) return;
675
+ e.removed = !0;
676
+ let t = e.disposer;
677
+ e.disposer = null;
678
+ try {
679
+ t?.();
680
+ } catch (e) {
681
+ c.error(e);
682
+ }
683
+ w(e);
684
+ }
685
+ function E(t) {
686
+ let { name: n } = t;
687
+ return {
688
+ progress: m,
689
+ parts: f,
690
+ events: {
691
+ on(e, n) {
692
+ let r = g.on(e, n), i = () => {
693
+ r(), t.offs.delete(i);
694
+ };
695
+ return t.offs.add(i), i;
696
+ },
697
+ off: (e, t) => g.off(e, t),
698
+ emit(t, r) {
699
+ if (typeof t != "string" || !t.startsWith(`${n}:`)) throw new e("plugin_event", `${n} may only emit events named ${n}:<event>`);
700
+ g.emit(t, r);
701
+ }
702
+ },
703
+ storage: H(l, n),
704
+ clock: u,
705
+ log: c,
706
+ env: {
707
+ hasDom: s.hasDom,
708
+ reducedMotion: s.reducedMotion
709
+ },
710
+ plugin: (e) => x.has(e) && x.get(e).done ? x.get(e).handle : null,
711
+ expose: (e) => {
712
+ t.handle = e;
713
+ }
714
+ };
715
+ }
716
+ function O(t) {
717
+ if (v) throw new e("disposed", "the host was disposed");
718
+ C(t);
719
+ let n = {
720
+ name: t.name,
721
+ done: !1,
722
+ removed: !1,
723
+ disposer: null,
724
+ handle: null,
725
+ offs: /* @__PURE__ */ new Set()
726
+ };
727
+ x.set(t.name, n);
728
+ let r = (e) => (n.done = !0, typeof e == "function" && (n.disposer = e), n.removed || v ? (n.removed = !1, T(n), n) : (S.push(n), n)), i = (e) => {
729
+ throw w(n), e;
730
+ }, a;
731
+ try {
732
+ a = t.install(E(n));
733
+ } catch (e) {
734
+ return i(e);
735
+ }
736
+ return R(a) ? a.then(r, i) : r(a);
737
+ }
738
+ let k = null;
739
+ function A(t) {
740
+ if (v) throw new e("disposed", "the host was disposed");
741
+ if (k === null) {
742
+ let e = O(t);
743
+ if (R(e)) {
744
+ let t = e.then((e) => e, (e) => {
745
+ throw e;
746
+ });
747
+ return k = t.then(() => {
748
+ k = null;
749
+ }, () => {
750
+ k = null;
751
+ }), {
752
+ record: null,
753
+ done: t
754
+ };
755
+ }
756
+ return {
757
+ record: e,
758
+ done: Promise.resolve(e)
759
+ };
760
+ }
761
+ let n = k.then(() => O(t));
762
+ return k = n.then(() => {
763
+ k = null;
764
+ }, () => {
765
+ k = null;
766
+ }), {
767
+ record: null,
768
+ done: n
769
+ };
770
+ }
771
+ function j(e) {
772
+ let { record: t, done: n } = A(e);
773
+ if (t) return () => T(t);
774
+ let r = null, i = !1;
775
+ return n.then((e) => {
776
+ r = e, i && T(e);
777
+ }, (e) => c.error(e)), () => {
778
+ r ? T(r) : i = !0;
779
+ };
780
+ }
781
+ let N = Promise.resolve();
782
+ try {
783
+ for (let e of o.plugins || []) {
784
+ let { done: t } = A(e);
785
+ N = N.then(() => t);
786
+ }
787
+ } catch (e) {
788
+ throw I(), e;
789
+ }
790
+ N = N.catch((e) => {
791
+ throw I(), e;
792
+ });
793
+ function I() {
794
+ if (!v) {
795
+ v = !0;
796
+ for (let e of [...S].reverse()) T(e);
797
+ for (let e of [...x.values()]) T(e);
798
+ for (let e of _) e();
799
+ _.length = 0, h(), m.clear?.();
800
+ }
801
+ }
802
+ let U = N.then(() => typeof m.init == "function" ? m.init() : void 0).then(() => (v || b(), W));
803
+ U.catch(() => {});
804
+ let W = {
805
+ progress: m,
806
+ parts: f,
807
+ events: g,
808
+ ready: U,
809
+ use: j,
810
+ dispose: I
811
+ };
812
+ return W;
813
+ }
814
+ //#endregion
815
+ export { M as a, P as i, U as n, k as o, F as r, D as s, I as t };