dopaminejs 3.5.0 → 3.7.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.
@@ -1,835 +0,0 @@
1
- import { c as e, i as t, r as n, s as r, t as i } from "./DataService-QQBNqJtN.js";
2
- // 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{top:calc(var(--dopamine-toast-top,80px) + env(safe-area-inset-top,0px));right:var(--dopamine-toast-right,20px);left:var(--dopamine-toast-left,auto);background:var(--dopamine-toast-bg,linear-gradient(135deg, #667eea 0%, #764ba2 100%));border-radius:var(--dopamine-toast-radius,16px);box-shadow:var(--dopamine-toast-shadow,0 8px 32px #0006);z-index:10000;max-width:var(--dopamine-toast-max-width,320px);padding:16px;animation:.5s cubic-bezier(.68,-.55,.265,1.55) slideIn;position:fixed;overflow:hidden}.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:var(--dopamine-toast-muted,#ffffffe6);text-transform:uppercase;letter-spacing:.5px;font-size:12px;font-weight:600}.achievement-name{color:var(--dopamine-toast-text,white);margin:2px 0;font-size:18px;font-weight:800}.achievement-desc{color:var(--dopamine-toast-muted,#fffc);margin-bottom:4px;font-size:13px}.achievement-xp{color:var(--dopamine-toast-accent,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{right:var(--dopamine-toast-right,12px);max-width:min(var(--dopamine-toast-max-width,320px), calc(100% - 24px))}.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._cardQueue = [], this._cardActive = !1, 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 { title: n, body: r, meta: i, icon: a } = e;
148
- this._cardQueue.push([{
149
- title: n,
150
- body: r,
151
- meta: i,
152
- icon: a
153
- }, t]), this._nextCard();
154
- }
155
- _nextCard() {
156
- if (this._cardActive) return;
157
- let e = this._cardQueue.shift();
158
- if (e) {
159
- this._cardActive = !0;
160
- try {
161
- this._showCard(e[0], e[1]);
162
- } catch (e) {
163
- throw this._cardActive = !1, this._nextCard(), e;
164
- }
165
- }
166
- }
167
- _showCard(e, t) {
168
- let n = document.createElement("div");
169
- 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 ";
170
- let r = u(e.title);
171
- 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(() => {
172
- let e = n.getBoundingClientRect();
173
- this._burst("confetti", {
174
- x: e.left + e.width / 2,
175
- y: e.top + e.height / 2
176
- }, { count: 40 });
177
- }, 300), this._defer(() => {
178
- n.style.animation = "slideOut 0.3s ease-in forwards", this._defer(() => {
179
- this._remove(n), this._cardActive = !1, this._nextCard();
180
- }, 300);
181
- }, 4e3);
182
- }
183
- _levelPopup(e) {
184
- let t = typeof e.title == "string" && e.title !== "" ? e.title : this.strings.levelUp, n = u(e.body), r = document.createElement("div");
185
- 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(() => {
186
- for (let e = 0; e < 5; e++) this._defer(() => {
187
- this._burst("confetti", {
188
- x: Math.random() * window.innerWidth,
189
- y: Math.random() * window.innerHeight * .6
190
- }, { count: 30 });
191
- }, e * 200);
192
- }, 300), this._defer(() => {
193
- r.style.opacity = "0", this._defer(() => this._remove(r), 500);
194
- }, 2500);
195
- }
196
- _notice(e) {
197
- let t = window.innerWidth / 2, n = window.innerHeight / 3, r = u(e.title);
198
- e.tier === "legendary" ? (this._floatText({
199
- text: r,
200
- x: t,
201
- y: n,
202
- color: "#ffd700",
203
- size: "36px"
204
- }), this._burst("stars", {
205
- x: t,
206
- y: n
207
- }, { count: 12 }), this._burst("confetti", {
208
- x: t,
209
- y: n
210
- }, { count: 25 })) : e.tier === "rare" ? (this._floatText({
211
- text: r,
212
- x: t,
213
- y: n,
214
- color: "#ff6b6b",
215
- size: "32px"
216
- }), this._burst("fire", {
217
- x: t,
218
- y: n
219
- }, { count: 20 })) : this._floatText({
220
- text: r,
221
- x: t,
222
- y: n,
223
- color: "#fff",
224
- size: "24px"
225
- }), this._announce(r);
226
- }
227
- text(e) {
228
- this._floatText({ ...e });
229
- }
230
- _floatText({ text: e, x: t, y: n, color: r = "#ffd700", size: i = "24px" }) {
231
- if (!this.container) return;
232
- let a = document.createElement("div");
233
- 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);
234
- }
235
- _remove(e) {
236
- e.remove();
237
- let t = this._detached.indexOf(e);
238
- t > -1 && this._detached.splice(t, 1);
239
- }
240
- destroy() {
241
- for (let e of this._timers) clearTimeout(e);
242
- this._timers.clear(), this._cardQueue = [], this._cardActive = !1;
243
- for (let e of this._detached) e.remove();
244
- 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;
245
- }
246
- }, O = Object.freeze({ matches: !1 });
247
- function k(e = globalThis.window) {
248
- return typeof e?.matchMedia == "function" && e.matchMedia("(prefers-reduced-motion: reduce)") || O;
249
- }
250
- // src/dopamine/effects/ParticleSystem.js
251
- var A = (e, t) => Object.prototype.hasOwnProperty.call(e, t), j = {
252
- confetti: (e, t, n, r) => e.confetti(t, n, r),
253
- sparkle: (e, t, n, r, i) => e.sparkle(t, n, r, i),
254
- fire: (e, t, n, r) => e.fire(t, n, r),
255
- stars: (e, t, n, r) => e.starBurst(t, n, r),
256
- coins: (e, t, n, r) => e.coinShower(t, n, r)
257
- }, M = class {
258
- constructor(e = {}) {
259
- 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);
260
- }
261
- _resize() {
262
- 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;
263
- 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);
264
- }
265
- registerSprite(e, t) {
266
- let n = new Image();
267
- n.src = t, this.sprites.set(e, n);
268
- }
269
- registerEffect(e, t) {
270
- this.customEffects.set(e, t);
271
- }
272
- play(e, t, n, ...r) {
273
- typeof t == "object" && t && (r = n === void 0 ? [] : [n, ...r], {x: t, y: n} = t);
274
- let i = this.customEffects.get(e);
275
- i ? i(t, n, ...r) : console.warn(`[DopamineJS] Effect '${e}' not found.`);
276
- }
277
- emit(e) {
278
- 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);
279
- for (let e = 0; e < m; e++) {
280
- let e = this._getParticle(), r = d + (Math.random() - .5) * u, m = Math.random() * i;
281
- 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);
282
- }
283
- m > 0 && this._startAnimation();
284
- }
285
- _allowed(e) {
286
- if (this._reducedMotion?.matches || !Number.isFinite(e)) return 0;
287
- let t = this.maxParticles - this.particles.length;
288
- return Math.max(0, Math.min(Math.floor(e), t));
289
- }
290
- _getParticle() {
291
- return this.pool.length > 0 ? this.pool.pop() : {};
292
- }
293
- _recycleParticle(e) {
294
- this.pool.push(e);
295
- }
296
- burst(e, t, n) {
297
- if (typeof e != "string" || !A(j, e)) return;
298
- 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;
299
- j[e](this, r, i, a, n?.color);
300
- }
301
- confetti(e, t, n = 50) {
302
- this.emit({
303
- x: e,
304
- y: t,
305
- count: n,
306
- color: [
307
- "#ff6b6b",
308
- "#4ecdc4",
309
- "#45b7d1",
310
- "#f7dc6f",
311
- "#c06c84",
312
- "#6c5ce7"
313
- ],
314
- speed: 10,
315
- gravity: .3,
316
- decay: .015,
317
- size: 8,
318
- spread: Math.PI * 2
319
- });
320
- }
321
- coinShower(e, t, n = 10) {
322
- this.emit({
323
- x: e,
324
- y: t,
325
- count: n,
326
- color: "#ffd700",
327
- speed: 4,
328
- gravity: .4,
329
- decay: .012,
330
- size: 12,
331
- angle: -Math.PI / 2,
332
- spread: Math.PI / 2
333
- });
334
- }
335
- sparkle(e, t, n = 20, r = "#ffd700") {
336
- this.emit({
337
- x: e,
338
- y: t,
339
- count: n,
340
- color: r,
341
- speed: 3,
342
- gravity: -.05,
343
- decay: .03,
344
- size: 3
345
- });
346
- }
347
- fire(e, t, n = 15) {
348
- this.emit({
349
- x: e,
350
- y: t,
351
- count: n,
352
- color: [
353
- "#ff6b00",
354
- "#ff8800",
355
- "#ffaa00",
356
- "#ff4400"
357
- ],
358
- speed: 3,
359
- gravity: -.2,
360
- decay: .04,
361
- size: 5,
362
- angle: -Math.PI / 2,
363
- spread: Math.PI / 4
364
- });
365
- }
366
- starBurst(e, t, n = 8) {
367
- let r = this._allowed(n);
368
- for (let n = 0; n < r; n++) {
369
- let i = this._getParticle(), a = Math.PI * 2 / r * n;
370
- 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);
371
- }
372
- r > 0 && this._startAnimation();
373
- }
374
- _startAnimation() {
375
- this.isAnimating || (this.isAnimating = !0, this._lastFrameTime = null, this._animate());
376
- }
377
- _animate(e) {
378
- this._frameHandle = null, this.ctx.clearRect(0, 0, this.width, this.height);
379
- let t = 1;
380
- Number.isFinite(e) && (this._lastFrameTime !== null && (t = Math.max(0, Math.min(3, (e - this._lastFrameTime) / (1e3 / 60)))), this._lastFrameTime = e);
381
- for (let e = this.particles.length - 1; e >= 0; e--) {
382
- let n = this.particles[e];
383
- 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) {
384
- this._recycleParticle(n);
385
- let t = this.particles.pop();
386
- e < this.particles.length && (this.particles[e] = t);
387
- } else {
388
- 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)) {
389
- let e = this.sprites.get(n.sprite);
390
- e.complete && this.ctx.drawImage(e, -n.size / 2, -n.size / 2, n.size, n.size);
391
- } 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());
392
- this.ctx.restore();
393
- }
394
- }
395
- this.particles.length > 0 ? this._frameHandle = requestAnimationFrame((e) => this._animate(e)) : this.isAnimating = !1;
396
- }
397
- _drawStarPath(e, t, n, r, i) {
398
- let a = Math.PI / 2 * 3, o = e, s = t, c = Math.PI / n;
399
- this.ctx.beginPath(), this.ctx.moveTo(e, t - r);
400
- 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;
401
- this.ctx.lineTo(e, t - r), this.ctx.closePath();
402
- }
403
- clear() {
404
- this.particles.forEach((e) => this._recycleParticle(e)), this.particles = [], this.ctx.clearRect(0, 0, this.width, this.height);
405
- }
406
- destroy() {
407
- 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 = [];
408
- }
409
- }, N = (e, t) => Object.prototype.hasOwnProperty.call(e, t), P = class {
410
- constructor(e = {}) {
411
- 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();
412
- }
413
- unlockOn(e, t = [
414
- "pointerup",
415
- "touchend",
416
- "keydown"
417
- ]) {
418
- let n = () => {
419
- for (let n of t) e.removeEventListener(n, r);
420
- this._unlocks.delete(n);
421
- }, r = () => {
422
- n(), this.initAudio();
423
- };
424
- for (let n of t) e.addEventListener(n, r);
425
- return this._unlocks.add(n), n;
426
- }
427
- _later(e, t) {
428
- let n = setTimeout(() => {
429
- this._timers.delete(n), e();
430
- }, t);
431
- this._timers.add(n);
432
- }
433
- initAudio() {
434
- if (!this.audioContext) {
435
- let e = typeof window < "u" ? window.AudioContext || window.webkitAudioContext : void 0;
436
- if (!e) return !1;
437
- 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);
438
- }
439
- return this.audioContext.state === "suspended" && this.audioContext.resume(), !0;
440
- }
441
- setVolume(e) {
442
- return Number.isFinite(e) && (this._volume = Math.max(0, Math.min(1, e)), this.masterGain && (this.masterGain.gain.value = this._volume)), this._volume;
443
- }
444
- getVolume() {
445
- return this._volume;
446
- }
447
- _output() {
448
- return this.masterGain || this.audioContext.destination;
449
- }
450
- toggleMute() {
451
- return this.muted = !this.muted, this.storage.setItem(this.storageKey, this.muted), this.muted;
452
- }
453
- registerSound(e, t) {
454
- this.customSounds[e] = t, this.audioContext && this.loadSound(e, t);
455
- }
456
- register(e, t) {
457
- return typeof e != "string" || e === "" ? () => {} : typeof t == "string" ? (this.registerSound(e, t), () => {
458
- this.customSounds[e] === t && delete this.customSounds[e], this.assets.delete(e);
459
- }) : typeof t == "function" ? (this._synths.set(e, t), () => {
460
- this._synths.get(e) === t && this._synths.delete(e);
461
- }) : () => {};
462
- }
463
- async preloadSounds(e) {
464
- let t = Object.entries(e).map(([e, t]) => (this.customSounds[e] = t, this.loadSound(e, t)));
465
- await Promise.allSettled(t);
466
- }
467
- async loadSound(e, t) {
468
- if (!this.audioContext) return;
469
- if (this._loading.has(e)) return this._loading.get(e);
470
- let n = this._load(e, t).finally(() => this._loading.delete(e));
471
- return this._loading.set(e, n), n;
472
- }
473
- async _load(e, t) {
474
- try {
475
- let n = this.audioContext, r = await fetch(t);
476
- if (r.ok === !1) throw Error(`HTTP ${r.status}`);
477
- let i = await r.arrayBuffer(), a = await n.decodeAudioData(i);
478
- this.audioContext === n && this.assets.set(e, a);
479
- } catch (t) {
480
- console.warn(`[DopamineJS] Failed to load sound '${e}':`, t);
481
- }
482
- }
483
- async play(e) {
484
- if (this.muted || !this.initAudio()) return;
485
- if (this.assets.has(e)) {
486
- this._playBuffer(this.assets.get(e));
487
- return;
488
- }
489
- if (N(this.customSounds, e) && !this.assets.has(e)) {
490
- let t = this.audioContext;
491
- if (await this.loadSound(e, this.customSounds[e]), this.audioContext !== t) return;
492
- if (this.assets.has(e)) {
493
- this._playBuffer(this.assets.get(e));
494
- return;
495
- }
496
- }
497
- let t = {
498
- xp: () => this.playXP(!0),
499
- level_up: () => this.playLevelUp(!0),
500
- achievement: () => this.playAchievement(!0),
501
- personal_best: () => this.playPersonalBest(!0),
502
- click: () => this.playClick(!0),
503
- error: () => this.playError(!0)
504
- };
505
- this._synths.has(e) ? this._synths.get(e)(this, (e, t) => this._later(e, t)) : N(t, e) && t[e]();
506
- }
507
- _playBuffer(e) {
508
- let t = this.audioContext.createBufferSource();
509
- t.buffer = e, t.connect(this._output()), t.start(0);
510
- }
511
- playTone(e, t, n = "sine", r = .3) {
512
- if (this.muted || !this.initAudio()) return;
513
- let i = this.audioContext.createOscillator(), a = this.audioContext.createGain();
514
- i.connect(a), a.connect(this._output()), i.frequency.value = e, i.type = n;
515
- let o = Math.max(1e-4, r);
516
- 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);
517
- }
518
- playClick(e = !1) {
519
- if (!e && this.assets.has("click")) return this.play("click");
520
- this.playTone(600, .05, "sine", .1);
521
- }
522
- playXP(e = !1) {
523
- if (!e && this.assets.has("xp")) return this.play("xp");
524
- this.playTone(700, .06, "sine", .12);
525
- }
526
- playLevelUp(e = !1) {
527
- if (!e && this.assets.has("level_up")) return this.play("level_up");
528
- this.playTone(500, .12, "sine", .2), this._later(() => this.playTone(700, .12, "sine", .2), 110), this._later(() => this.playTone(900, .25, "sine", .2), 220);
529
- }
530
- playAchievement(e = !1) {
531
- if (!e && this.assets.has("achievement")) return this.play("achievement");
532
- this.playTone(660, .1, "triangle", .2), this._later(() => this.playTone(990, .25, "triangle", .2), 100);
533
- }
534
- playPersonalBest(e = !1) {
535
- if (!e && this.assets.has("personal_best")) return this.play("personal_best");
536
- this.playTone(600, .1, "sine", .2), this._later(() => this.playTone(800, .2, "sine", .2), 100);
537
- }
538
- playError(e = !1) {
539
- if (!e && this.assets.has("error")) return this.play("error");
540
- this.playTone(200, .2, "sawtooth", .3);
541
- }
542
- destroy() {
543
- for (let e of [...this._unlocks]) e();
544
- for (let e of this._timers) clearTimeout(e);
545
- this._timers.clear(), this.audioContext && (this.audioContext.close?.())?.catch?.(() => {}), this.audioContext = null, this.masterGain = null, this.assets.clear(), this._loading.clear(), this._synths.clear();
546
- }
547
- };
548
- // src/dopamine/feel/haptics.js
549
- function F({ nav: e = globalThis.navigator, enabled: t = !0 } = {}) {
550
- let n = !!t, r = (t) => {
551
- if (n) try {
552
- e.vibrate(t);
553
- } catch {}
554
- };
555
- return {
556
- tap: () => r(10),
557
- double: () => r([
558
- 10,
559
- 40,
560
- 10
561
- ]),
562
- setEnabled(e) {
563
- n = !!e;
564
- }
565
- };
566
- }
567
- // src/dopamine/host.js
568
- var I = 3, L = [
569
- "ui",
570
- "particles",
571
- "sound",
572
- "haptics"
573
- ], R = (e) => typeof e == "object" && !!e && typeof e.then == "function", z = (e, t) => {
574
- R(e) && e.then(void 0, (e) => t.error(e));
575
- };
576
- function B() {
577
- let e = typeof document < "u" && typeof window < "u";
578
- return {
579
- hasDom: e,
580
- reducedMotion: e ? k(window).matches === !0 : !1
581
- };
582
- }
583
- function V(e, t, n) {
584
- if (e === !1) return null;
585
- let r = e;
586
- return typeof e == "function" && (r = e(t)), r ?? n();
587
- }
588
- function H(e, t) {
589
- let n = `dopamine_plugin_${t}:`;
590
- return {
591
- getItem: (t) => e.getItem(n + t),
592
- setItem: (t, r) => e.setItem(n + t, r),
593
- removeItem: (t) => e.removeItem(n + t)
594
- };
595
- }
596
- function U(a = {}) {
597
- let o = a || {}, s = B(), c = {
598
- debug: () => {},
599
- info: () => {},
600
- warn: (...e) => console.warn(...e),
601
- error: (...e) => console.error(...e),
602
- ...o.log || {}
603
- }, l = o.storage || n(), u = o.clock || r, d = {
604
- ui: null,
605
- particles: null,
606
- sound: null,
607
- haptics: null
608
- }, f = {};
609
- for (let e of L) Object.defineProperty(f, e, {
610
- enumerable: !0,
611
- get: () => d[e]
612
- });
613
- Object.freeze(f);
614
- let p = {
615
- ...s,
616
- parts: f,
617
- storage: l,
618
- clock: u
619
- }, m = V(o.progress, p, () => new t(new i({ storage: l }), {
620
- clock: u,
621
- streakDays: o.streakDays,
622
- builtinAchievements: o.builtinAchievements,
623
- loadRetryMs: o.loadRetryMs
624
- }));
625
- if (typeof m?.on != "function" || typeof m?.off != "function") throw new e("invalid_progress", "progress needs on() and off()");
626
- 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());
627
- try {
628
- d.ui = V(o.ui, p, () => s.hasDom ? new D(f) : null);
629
- } catch (e) {
630
- throw h(), e;
631
- }
632
- function h() {
633
- for (let e of L) try {
634
- d[e]?.destroy?.();
635
- } catch (e) {
636
- c.error(e);
637
- }
638
- }
639
- let g = m, _ = [], v = !1, y = (e) => (t) => {
640
- try {
641
- z(e(t), c);
642
- } catch (e) {
643
- c.error(e);
644
- }
645
- }, b = () => {
646
- let e = d.ui;
647
- if (!e || typeof m.snapshot != "function" || typeof m.getXPForNextLevel != "function") return;
648
- let t = m.snapshot(), { total: n, progress: r } = m.getXPForNextLevel();
649
- e.hud({
650
- xp: t.xp,
651
- level: t.level,
652
- progress: r,
653
- streak: t.streak.current,
654
- next: n
655
- });
656
- };
657
- _.push(g.on("xp_gained", y((e) => {
658
- b(), e.leveledUp || d.sound?.play("xp");
659
- })), g.on("level_up", y((e) => {
660
- b(), d.ui?.popup({
661
- kind: "level",
662
- title: "",
663
- body: String(e.newLevel)
664
- }), d.sound?.play("level_up");
665
- })), g.on("achievement_unlocked", y((e) => {
666
- d.ui?.popup({
667
- kind: "achievement",
668
- title: e.name,
669
- body: e.description,
670
- icon: e.icon,
671
- meta: `+${e.xp ?? 0} XP`
672
- }), d.sound?.play("achievement");
673
- })), g.on("streak_updated", y(b)), g.on("player_reset", y(b)), g.on("load_recovered", y(b)), g.on("personal_best", y((e) => {
674
- d.ui?.popup({
675
- kind: "notice",
676
- title: `New High Score: ${e.score}!`,
677
- tier: "legendary"
678
- }), d.sound?.play("personal_best");
679
- })));
680
- let x = /* @__PURE__ */ new Map(), S = [];
681
- function C(t) {
682
- 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");
683
- if (t.api !== 3) throw new e("plugin_api", `${t.name} was built for api ${t.api}, this host is api 3`);
684
- if (x.has(t.name)) throw new e("plugin_duplicate", `${t.name} is already installed`);
685
- 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}`);
686
- 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`);
687
- }
688
- function w(e) {
689
- for (let t of e.offs) t();
690
- e.offs.clear(), x.delete(e.name);
691
- let t = S.indexOf(e);
692
- t > -1 && S.splice(t, 1);
693
- }
694
- function T(e) {
695
- if (e.removed) return;
696
- e.removed = !0;
697
- let t = e.disposer;
698
- e.disposer = null;
699
- try {
700
- t?.();
701
- } catch (e) {
702
- c.error(e);
703
- }
704
- w(e);
705
- }
706
- function E(t) {
707
- let { name: n } = t;
708
- return {
709
- progress: m,
710
- parts: f,
711
- events: {
712
- on(e, n) {
713
- let r = g.on(e, n), i = () => {
714
- r(), t.offs.delete(i);
715
- };
716
- return t.offs.add(i), i;
717
- },
718
- off: (e, t) => g.off(e, t),
719
- emit(t, r) {
720
- if (typeof t != "string" || !t.startsWith(`${n}:`)) throw new e("plugin_event", `${n} may only emit events named ${n}:<event>`);
721
- g.emit(t, r);
722
- }
723
- },
724
- storage: H(l, n),
725
- clock: u,
726
- log: c,
727
- env: {
728
- hasDom: s.hasDom,
729
- reducedMotion: s.reducedMotion
730
- },
731
- plugin: (e) => x.has(e) && x.get(e).done ? x.get(e).handle : null,
732
- expose: (e) => {
733
- t.handle = e;
734
- }
735
- };
736
- }
737
- function O(t) {
738
- if (v) throw new e("disposed", "the host was disposed");
739
- C(t);
740
- let n = {
741
- name: t.name,
742
- done: !1,
743
- removed: !1,
744
- disposer: null,
745
- handle: null,
746
- offs: /* @__PURE__ */ new Set()
747
- };
748
- x.set(t.name, n);
749
- 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) => {
750
- throw w(n), e;
751
- }, a;
752
- try {
753
- a = t.install(E(n));
754
- } catch (e) {
755
- return i(e);
756
- }
757
- return R(a) ? a.then(r, i) : r(a);
758
- }
759
- let k = null;
760
- function A(t) {
761
- if (v) throw new e("disposed", "the host was disposed");
762
- if (k === null) {
763
- let e = O(t);
764
- if (R(e)) {
765
- let t = e.then((e) => e, (e) => {
766
- throw e;
767
- });
768
- return k = t.then(() => {
769
- k = null;
770
- }, () => {
771
- k = null;
772
- }), {
773
- record: null,
774
- done: t
775
- };
776
- }
777
- return {
778
- record: e,
779
- done: Promise.resolve(e)
780
- };
781
- }
782
- let n = k.then(() => O(t));
783
- return k = n.then(() => {
784
- k = null;
785
- }, () => {
786
- k = null;
787
- }), {
788
- record: null,
789
- done: n
790
- };
791
- }
792
- function j(e) {
793
- let { record: t, done: n } = A(e);
794
- if (t) return () => T(t);
795
- let r = null, i = !1;
796
- return n.then((e) => {
797
- r = e, i && T(e);
798
- }, (e) => c.error(e)), () => {
799
- r ? T(r) : i = !0;
800
- };
801
- }
802
- let N = Promise.resolve();
803
- try {
804
- for (let e of o.plugins || []) {
805
- let { done: t } = A(e);
806
- N = N.then(() => t);
807
- }
808
- } catch (e) {
809
- throw I(), e;
810
- }
811
- N = N.catch((e) => {
812
- throw I(), e;
813
- });
814
- function I() {
815
- if (!v) {
816
- v = !0;
817
- for (let e of [...S].reverse()) T(e);
818
- for (let e of [...x.values()]) T(e);
819
- for (let e of _) e();
820
- _.length = 0, h(), m.clear?.();
821
- }
822
- }
823
- let U = N.then(() => typeof m.init == "function" ? m.init() : void 0).then(() => (v || b(), W));
824
- U.catch(() => {});
825
- let W = {
826
- progress: m,
827
- parts: f,
828
- events: g,
829
- ready: U,
830
- use: j,
831
- dispose: I
832
- };
833
- return W;
834
- }
835
- export { M as a, P as i, U as n, k as o, F as r, D as s, I as t };