dopaminejs 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dopamine.js +1947 -0
- package/dist/dopamine.umd.cjs +45 -0
- package/package.json +24 -32
- package/src/core/Animator.js +62 -0
- package/src/core/Collider.js +50 -0
- package/src/core/Component.js +38 -0
- package/src/core/DopamineKernel.js +153 -0
- package/src/core/EventBus.js +183 -0
- package/src/core/Game.js +89 -0
- package/src/core/GameObject.js +83 -0
- package/src/core/PluginRegistry.js +197 -0
- package/src/core/Scene.js +65 -0
- package/src/core/Sprite.js +51 -0
- package/src/core/SystemRegistry.js +242 -0
- package/src/core/Vector2.js +50 -0
- package/src/{audio → dopamine/audio}/SoundManager.js +1 -3
- package/src/dopamine/components/ParticleEmitter.js +50 -0
- package/src/dopamine/components/ScreenShake.js +49 -0
- package/src/dopamine/components/index.js +4 -0
- package/src/dopamine/effects/ParticleSystem.js +327 -0
- package/src/dopamine/effects/WebGLParticleSystem.js +348 -0
- package/src/dopamine/index.js +62 -0
- package/src/dopamine/utils/TextureGenerator.js +90 -0
- package/src/index.js +34 -62
- package/src/interfaces/IAudioSystem.js +58 -0
- package/src/interfaces/IParticleSystem.js +51 -0
- package/src/interfaces/IPhysicsSystem.js +52 -0
- package/src/interfaces/ISystem.js +48 -0
- package/src/interfaces/index.js +7 -0
- package/src/renderer/Renderer.js +37 -0
- package/src/systems/Director.js +27 -0
- package/src/systems/Input.js +77 -0
- package/src/systems/Loader.js +44 -0
- package/src/systems/Physics.js +117 -0
- package/src/systems/Ticker.js +61 -0
- package/ARCHITECTURE.md +0 -71
- package/CHANGELOG.md +0 -27
- package/README.md +0 -213
- package/dist/dopamine.mjs +0 -724
- package/dist/dopamine.umd.js +0 -45
- package/src/effects/ParticleSystem.js +0 -257
- /package/src/{core → dopamine/core}/DataService.js +0 -0
- /package/src/{core → dopamine/core}/EventEmitter.js +0 -0
- /package/src/{core → dopamine/core}/RewardSystem.js +0 -0
- /package/src/{ui → dopamine/ui}/GameUI.js +0 -0
- /package/src/{ui → dopamine/ui}/dopamine.css +0 -0
package/dist/dopamine.mjs
DELETED
|
@@ -1,724 +0,0 @@
|
|
|
1
|
-
class p {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.listeners = /* @__PURE__ */ new Map();
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Subscribe to an event
|
|
7
|
-
* @param {string} event - Event name
|
|
8
|
-
* @param {Function} callback - Callback function
|
|
9
|
-
* @returns {Function} Unsubscribe function
|
|
10
|
-
*/
|
|
11
|
-
on(t, e) {
|
|
12
|
-
return this.listeners.has(t) || this.listeners.set(t, /* @__PURE__ */ new Set()), this.listeners.get(t).add(e), () => this.off(t, e);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Unsubscribe from an event
|
|
16
|
-
* @param {string} event - Event name
|
|
17
|
-
* @param {Function} callback - Callback function
|
|
18
|
-
*/
|
|
19
|
-
off(t, e) {
|
|
20
|
-
this.listeners.has(t) && this.listeners.get(t).delete(e);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Emit an event
|
|
24
|
-
* @param {string} event - Event name
|
|
25
|
-
* @param {*} data - Event data
|
|
26
|
-
*/
|
|
27
|
-
emit(t, e) {
|
|
28
|
-
this.listeners.has(t) && this.listeners.get(t).forEach((s) => {
|
|
29
|
-
try {
|
|
30
|
-
s(e);
|
|
31
|
-
} catch (i) {
|
|
32
|
-
console.error(`Error in listener for event '${t}':`, i);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Clear all listeners
|
|
38
|
-
*/
|
|
39
|
-
clear() {
|
|
40
|
-
this.listeners.clear();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
class m extends p {
|
|
44
|
-
constructor(t, e = {}) {
|
|
45
|
-
super(), this.dataService = t, this.player = null, this.achievements = { ...this._initAchievements(), ...e.achievements || {} };
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Initialize the reward system for current player
|
|
49
|
-
* @returns {Promise<Object>} Player data
|
|
50
|
-
*/
|
|
51
|
-
async init() {
|
|
52
|
-
return this.player = await this.dataService.load("player", this._getDefaultPlayer()), this._updateDailyStreak(), await this.save(), this.player;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Get default player data structure
|
|
56
|
-
*/
|
|
57
|
-
_getDefaultPlayer() {
|
|
58
|
-
return {
|
|
59
|
-
name: "Player",
|
|
60
|
-
xp: 0,
|
|
61
|
-
level: 1,
|
|
62
|
-
totalGamesPlayed: 0,
|
|
63
|
-
createdAt: Date.now(),
|
|
64
|
-
lastPlayedAt: Date.now(),
|
|
65
|
-
streak: {
|
|
66
|
-
current: 1,
|
|
67
|
-
longest: 1,
|
|
68
|
-
lastPlayDate: this._getTodayDateString()
|
|
69
|
-
},
|
|
70
|
-
achievements: {},
|
|
71
|
-
// achievement_id: { unlockedAt: timestamp, seen: boolean }
|
|
72
|
-
stats: {
|
|
73
|
-
// Dynamic stats object, games will add their own keys
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Save player data
|
|
79
|
-
*/
|
|
80
|
-
async save() {
|
|
81
|
-
this.player && (this.player.lastPlayedAt = Date.now(), await this.dataService.save("player", this.player));
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Add XP and handle level ups
|
|
85
|
-
* @param {number} amount - XP to add
|
|
86
|
-
* @param {string} reason - Why XP was awarded (for notifications)
|
|
87
|
-
* @returns {Object} { leveledUp: boolean, newLevel: number, xpGained: number }
|
|
88
|
-
*/
|
|
89
|
-
async addXP(t, e = "") {
|
|
90
|
-
const s = this.player.level;
|
|
91
|
-
this.player.xp += t;
|
|
92
|
-
const i = this._calculateLevel(this.player.xp), a = i > s;
|
|
93
|
-
return a && (this.player.level = i), await this.save(), this.emit("xp_gained", { amount: t, reason: e, leveledUp: a, newLevel: i }), a && this.emit("level_up", { oldLevel: s, newLevel: i }), { leveledUp: a, newLevel: i, xpGained: t };
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Calculate level from XP (exponential curve)
|
|
97
|
-
*/
|
|
98
|
-
_calculateLevel(t) {
|
|
99
|
-
const e = Math.floor((1 + Math.sqrt(1 + 8 * t / 50)) / 2);
|
|
100
|
-
return Math.max(1, e);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Get XP needed for next level
|
|
104
|
-
*/
|
|
105
|
-
getXPForNextLevel() {
|
|
106
|
-
const t = this.player.level + 1, e = 50 * t * (t - 1), s = 50 * this.player.level * (this.player.level - 1);
|
|
107
|
-
return {
|
|
108
|
-
total: e,
|
|
109
|
-
needed: e - this.player.xp,
|
|
110
|
-
progress: (this.player.xp - s) / (e - s)
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Record game result and award XP
|
|
115
|
-
* @param {string} gameName - Unique ID for the game
|
|
116
|
-
* @param {Object} result - Game-specific result data
|
|
117
|
-
*/
|
|
118
|
-
async recordGame(t, e) {
|
|
119
|
-
this.player.stats[t] || (this.player.stats[t] = { totalPlays: 0, highScore: 0 });
|
|
120
|
-
const s = this.player.stats[t];
|
|
121
|
-
s.totalPlays++, this.player.totalGamesPlayed++, e.score !== void 0 && e.score > s.highScore && (s.highScore = e.score, this.emit("new_high_score", { gameName: t, score: e.score })), Object.keys(e).forEach((a) => {
|
|
122
|
-
a !== "score" && (s[a] = (s[a] || 0) + (typeof e[a] == "number" ? e[a] : 0));
|
|
123
|
-
});
|
|
124
|
-
let i = 10;
|
|
125
|
-
e.score && (i += Math.floor(e.score / 5)), i = Math.floor(i * this.getStreakMultiplier()), await this.addXP(i, `Played ${t}`), await this.checkAchievements(t, e), await this.save();
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Check and unlock achievements
|
|
129
|
-
*/
|
|
130
|
-
async checkAchievements(t, e) {
|
|
131
|
-
const s = [];
|
|
132
|
-
for (const [i, a] of Object.entries(this.achievements))
|
|
133
|
-
this.player.achievements[i] || a.check(this.player, t, e) && (await this.unlockAchievement(i), s.push(a));
|
|
134
|
-
return s;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Unlock an achievement
|
|
138
|
-
*/
|
|
139
|
-
async unlockAchievement(t) {
|
|
140
|
-
const e = this.achievements[t];
|
|
141
|
-
return e ? (this.player.achievements[t] = {
|
|
142
|
-
unlockedAt: Date.now(),
|
|
143
|
-
seen: !1
|
|
144
|
-
}, await this.addXP(e.xp, `Achievement: ${e.name}`), this.emit("achievement_unlocked", e), await this.save(), !0) : !1;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Get all unlocked achievements
|
|
148
|
-
*/
|
|
149
|
-
getUnlockedAchievements() {
|
|
150
|
-
return Object.entries(this.player.achievements).map(([t, e]) => ({
|
|
151
|
-
...this.achievements[t],
|
|
152
|
-
id: t,
|
|
153
|
-
...e
|
|
154
|
-
}));
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Get achievements user hasn't seen the notification for yet
|
|
158
|
-
*/
|
|
159
|
-
getUnseenAchievements() {
|
|
160
|
-
return this.getUnlockedAchievements().filter((t) => !t.seen);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Mark achievements as seen
|
|
164
|
-
*/
|
|
165
|
-
async markAchievementsSeen(t) {
|
|
166
|
-
for (const e of t)
|
|
167
|
-
this.player.achievements[e] && (this.player.achievements[e].seen = !0);
|
|
168
|
-
await this.save();
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Update daily streak
|
|
172
|
-
*/
|
|
173
|
-
_updateDailyStreak() {
|
|
174
|
-
const t = this._getTodayDateString(), e = this.player.streak.lastPlayDate;
|
|
175
|
-
if (e === t)
|
|
176
|
-
return;
|
|
177
|
-
const s = this._getYesterdayDateString();
|
|
178
|
-
e === s ? (this.player.streak.current++, this.player.streak.current > this.player.streak.longest && (this.player.streak.longest = this.player.streak.current)) : this.player.streak.current = 1, this.player.streak.lastPlayDate = t;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Get streak multiplier for XP
|
|
182
|
-
*/
|
|
183
|
-
getStreakMultiplier() {
|
|
184
|
-
const t = this.player.streak.current;
|
|
185
|
-
return t >= 7 ? 2 : t >= 5 ? 1.5 : t >= 3 ? 1.25 : 1;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Get today's date as YYYY-MM-DD string
|
|
189
|
-
*/
|
|
190
|
-
_getTodayDateString() {
|
|
191
|
-
return (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Get yesterday's date as YYYY-MM-DD string
|
|
195
|
-
*/
|
|
196
|
-
_getYesterdayDateString() {
|
|
197
|
-
const t = /* @__PURE__ */ new Date();
|
|
198
|
-
return t.setDate(t.getDate() - 1), t.toISOString().split("T")[0];
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Initialize achievement definitions
|
|
202
|
-
* Can be extended by games
|
|
203
|
-
*/
|
|
204
|
-
_initAchievements() {
|
|
205
|
-
return {
|
|
206
|
-
// Universal achievements
|
|
207
|
-
first_game: {
|
|
208
|
-
name: "First Steps",
|
|
209
|
-
description: "Play your first game",
|
|
210
|
-
icon: "🎮",
|
|
211
|
-
xp: 50,
|
|
212
|
-
check: (t) => t.totalGamesPlayed >= 1
|
|
213
|
-
},
|
|
214
|
-
level_5: {
|
|
215
|
-
name: "Rising Star",
|
|
216
|
-
description: "Reach level 5",
|
|
217
|
-
icon: "⭐",
|
|
218
|
-
xp: 100,
|
|
219
|
-
check: (t) => t.level >= 5
|
|
220
|
-
},
|
|
221
|
-
level_10: {
|
|
222
|
-
name: "Expert Player",
|
|
223
|
-
description: "Reach level 10",
|
|
224
|
-
icon: "🏆",
|
|
225
|
-
xp: 250,
|
|
226
|
-
check: (t) => t.level >= 10
|
|
227
|
-
},
|
|
228
|
-
streak_3: {
|
|
229
|
-
name: "Dedicated",
|
|
230
|
-
description: "Play 3 days in a row",
|
|
231
|
-
icon: "🔥",
|
|
232
|
-
xp: 100,
|
|
233
|
-
check: (t) => t.streak.current >= 3
|
|
234
|
-
},
|
|
235
|
-
streak_7: {
|
|
236
|
-
name: "Unstoppable",
|
|
237
|
-
description: "Play 7 days in a row",
|
|
238
|
-
icon: "💥",
|
|
239
|
-
xp: 300,
|
|
240
|
-
check: (t) => t.streak.current >= 7
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
class u {
|
|
246
|
-
constructor(t = {}) {
|
|
247
|
-
this.storage = t.storage || window.localStorage, this.prefix = t.prefix || "dopamine_";
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Save data to local storage
|
|
251
|
-
* @param {string} key
|
|
252
|
-
* @param {any} data
|
|
253
|
-
*/
|
|
254
|
-
async save(t, e) {
|
|
255
|
-
try {
|
|
256
|
-
const s = JSON.stringify(e);
|
|
257
|
-
return this.storage.setItem(this.prefix + t, s), !0;
|
|
258
|
-
} catch (s) {
|
|
259
|
-
return console.error("Error saving data:", s), !1;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Load data from local storage
|
|
264
|
-
* @param {string} key
|
|
265
|
-
* @param {any} defaultValue
|
|
266
|
-
*/
|
|
267
|
-
async load(t, e = null) {
|
|
268
|
-
try {
|
|
269
|
-
const s = this.storage.getItem(this.prefix + t);
|
|
270
|
-
return s ? JSON.parse(s) : e;
|
|
271
|
-
} catch (s) {
|
|
272
|
-
return console.error("Error loading data:", s), e;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Clear specific data
|
|
277
|
-
* @param {string} key
|
|
278
|
-
*/
|
|
279
|
-
async clear(t) {
|
|
280
|
-
this.storage.removeItem(this.prefix + t);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
class y {
|
|
284
|
-
constructor(t) {
|
|
285
|
-
this.particleSystem = t, this.container = null, this.xpBar = null, this.levelDisplay = null, this.streakDisplay = null, this.init();
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Initialize UI overlay
|
|
289
|
-
*/
|
|
290
|
-
init() {
|
|
291
|
-
this.container = document.createElement("div"), this.container.id = "game-ui-overlay", this.container.innerHTML = `
|
|
292
|
-
<div class="game-ui-top-bar">
|
|
293
|
-
<div class="level-badge" id="level-badge">
|
|
294
|
-
<span class="level-label">LVL</span>
|
|
295
|
-
<span class="level-number" id="level-number">1</span>
|
|
296
|
-
</div>
|
|
297
|
-
|
|
298
|
-
<div class="xp-container">
|
|
299
|
-
<div class="xp-bar-bg">
|
|
300
|
-
<div class="xp-bar-fill" id="xp-bar-fill" style="width: 0%"></div>
|
|
301
|
-
</div>
|
|
302
|
-
<div class="xp-text" id="xp-text">0 /100 XP</div>
|
|
303
|
-
</div>
|
|
304
|
-
|
|
305
|
-
<div class="streak-badge" id="streak-badge">
|
|
306
|
-
<span class="streak-icon">🔥</span>
|
|
307
|
-
<span class="streak-number" id="streak-number">1</span>
|
|
308
|
-
</div>
|
|
309
|
-
</div>
|
|
310
|
-
`, document.body.appendChild(this.container), this.xpBar = document.getElementById("xp-bar-fill"), this.xpText = document.getElementById("xp-text"), this.levelDisplay = document.getElementById("level-number"), this.streakDisplay = document.getElementById("streak-number");
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Update XP bar
|
|
314
|
-
*/
|
|
315
|
-
updateXP(t, e, s) {
|
|
316
|
-
const i = t / s * 100;
|
|
317
|
-
this.xpBar.style.width = `${Math.min(100, i)}%`, this.xpText.textContent = `${t} / ${s} XP`, this.xpBar.style.animation = "none", setTimeout(() => {
|
|
318
|
-
this.xpBar.style.animation = "xp-pulse 0.3s ease-out";
|
|
319
|
-
}, 10);
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Update level display
|
|
323
|
-
*/
|
|
324
|
-
updateLevel(t) {
|
|
325
|
-
this.levelDisplay.textContent = t;
|
|
326
|
-
const e = document.getElementById("level-badge");
|
|
327
|
-
e.style.animation = "none", setTimeout(() => {
|
|
328
|
-
e.style.animation = "level-pulse 0.5s ease-out";
|
|
329
|
-
}, 10);
|
|
330
|
-
}
|
|
331
|
-
/**
|
|
332
|
-
* Update streak display
|
|
333
|
-
*/
|
|
334
|
-
updateStreak(t) {
|
|
335
|
-
this.streakDisplay.textContent = t;
|
|
336
|
-
const e = document.getElementById("streak-badge");
|
|
337
|
-
t >= 7 ? e.style.background = "linear-gradient(135deg, #ff6b00 0%, #ff4400 100%)" : t >= 3 && (e.style.background = "linear-gradient(135deg, #ff8800 0%, #ff6b00 100%)");
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Show achievement unlocked popup
|
|
341
|
-
*/
|
|
342
|
-
showAchievement(t) {
|
|
343
|
-
const e = document.createElement("div");
|
|
344
|
-
e.className = "achievement-popup", e.innerHTML = `
|
|
345
|
-
<div class="achievement-shine"></div>
|
|
346
|
-
<div class="achievement-content">
|
|
347
|
-
<div class="achievement-icon">${t.icon}</div>
|
|
348
|
-
<div class="achievement-info">
|
|
349
|
-
<div class="achievement-title">Achievement Unlocked!</div>
|
|
350
|
-
<div class="achievement-name">${t.name}</div>
|
|
351
|
-
<div class="achievement-desc">${t.description}</div>
|
|
352
|
-
<div class="achievement-xp">+${t.xp} XP</div>
|
|
353
|
-
</div>
|
|
354
|
-
</div>
|
|
355
|
-
`, document.body.appendChild(e), setTimeout(() => {
|
|
356
|
-
const s = e.getBoundingClientRect();
|
|
357
|
-
this.particleSystem.confetti(s.left + s.width / 2, s.top + s.height / 2, 40);
|
|
358
|
-
}, 300), setTimeout(() => {
|
|
359
|
-
e.style.animation = "slideOut 0.3s ease-in forwards", setTimeout(() => e.remove(), 300);
|
|
360
|
-
}, 4e3);
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Show level up screen
|
|
364
|
-
*/
|
|
365
|
-
showLevelUp(t, e) {
|
|
366
|
-
const s = document.createElement("div");
|
|
367
|
-
s.className = "level-up-overlay", s.innerHTML = `
|
|
368
|
-
<div class="level-up-content">
|
|
369
|
-
<div class="level-up-title">LEVEL UP!</div>
|
|
370
|
-
<div class="level-up-number">${e}</div>
|
|
371
|
-
<div class="level-up-subtitle">Amazing progress!</div>
|
|
372
|
-
</div>
|
|
373
|
-
`, document.body.appendChild(s), setTimeout(() => {
|
|
374
|
-
for (let i = 0; i < 5; i++)
|
|
375
|
-
setTimeout(() => {
|
|
376
|
-
const a = Math.random() * window.innerWidth, n = Math.random() * window.innerHeight * 0.6;
|
|
377
|
-
this.particleSystem.confetti(a, n, 30);
|
|
378
|
-
}, i * 200);
|
|
379
|
-
}, 300), setTimeout(() => {
|
|
380
|
-
s.style.opacity = "0", setTimeout(() => s.remove(), 500);
|
|
381
|
-
}, 2500);
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Show floating text (e.g., "+50 XP", "COMBO!")
|
|
385
|
-
*/
|
|
386
|
-
showFloatingText(t, e, s, i = "#ffd700", a = "24px") {
|
|
387
|
-
const n = document.createElement("div");
|
|
388
|
-
n.className = "floating-text", n.textContent = t, n.style.left = e + "px", n.style.top = s + "px", n.style.color = i, n.style.fontSize = a, document.body.appendChild(n), setTimeout(() => n.remove(), 2e3);
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Show combo multiplier
|
|
392
|
-
*/
|
|
393
|
-
showCombo(t, e, s) {
|
|
394
|
-
this.showFloatingText(`${t}× COMBO!`, e, s, "#ff6b6b", "32px"), this.particleSystem.fire(e, s, 20);
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Show "near miss"indicator
|
|
398
|
-
*/
|
|
399
|
-
showNearMiss(t, e) {
|
|
400
|
-
this.showFloatingText("CLOSE CALL!", t, e, "#45b7d1", "20px"), this.particleSystem.sparkle(t, e, 15, "#45b7d1");
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Show lucky moment (random multiplier)
|
|
404
|
-
*/
|
|
405
|
-
showLuckyMoment(t, e, s) {
|
|
406
|
-
this.showFloatingText(`LUCKY ${t}×!`, e, s, "#ffd700", "36px"), this.particleSystem.starBurst(e, s, 12), this.particleSystem.confetti(e, s, 25);
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Show generic notification
|
|
410
|
-
*/
|
|
411
|
-
showNotification(t, e = "normal") {
|
|
412
|
-
const s = window.innerWidth / 2, i = window.innerHeight / 3;
|
|
413
|
-
e === "legendary" ? this.showLuckyMoment(t, s, i) : e === "rare" ? this.showCombo(t, s, i) : this.showFloatingText(t, s, i, "#fff", "24px");
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Show Game Over Summary
|
|
417
|
-
*/
|
|
418
|
-
showSummary(t) {
|
|
419
|
-
const e = document.createElement("div");
|
|
420
|
-
e.className = "message-box", e.style.display = "flex", e.style.position = "absolute", e.style.top = "50%", e.style.left = "50%", e.style.transform = "translate(-50%, -50%)", e.style.zIndex = "1000";
|
|
421
|
-
let s = "";
|
|
422
|
-
t.metrics && (s = Object.entries(t.metrics).map(([i, a]) => `<div style="display:flex;justify-content:space-between;width:100%"><span>${i}:</span> <strong>${a}</strong></div>`).join("")), e.innerHTML = `
|
|
423
|
-
<h1>Game Over</h1>
|
|
424
|
-
<div style="font-size: 3rem; font-weight: bold; color: #e06020; margin: 10px 0;">${t.score}</div>
|
|
425
|
-
<div style="width: 100%; margin-bottom: 20px;">
|
|
426
|
-
${s}
|
|
427
|
-
</div>
|
|
428
|
-
<button class="btn" onclick="location.reload()">Play Again</button>
|
|
429
|
-
<button class="btn" onclick="window.location.href='index.html'" style="margin-top: 10px; background: #543847;">Exit</button>
|
|
430
|
-
`, document.body.appendChild(e);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
class v {
|
|
434
|
-
constructor(t = {}) {
|
|
435
|
-
this.canvasId = t.canvasId || "dopamine-particle-canvas", this.canvas = document.getElementById(this.canvasId), this.canvas || (this.canvas = document.createElement("canvas"), this.canvas.id = this.canvasId, this.canvas.style.position = "fixed", this.canvas.style.top = "0", this.canvas.style.left = "0", this.canvas.style.width = "100%", this.canvas.style.height = "100%", this.canvas.style.pointerEvents = "none", this.canvas.style.zIndex = t.zIndex || "9999", document.body.appendChild(this.canvas)), this.ctx = this.canvas.getContext("2d"), this.particles = [], this.isAnimating = !1, this._resize(), window.addEventListener("resize", () => this._resize());
|
|
436
|
-
}
|
|
437
|
-
_resize() {
|
|
438
|
-
this.canvas.width = window.innerWidth, this.canvas.height = window.innerHeight;
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Confetti explosion (for achievements, level ups)
|
|
442
|
-
*/
|
|
443
|
-
confetti(t, e, s = 50) {
|
|
444
|
-
const i = ["#ff6b6b", "#4ecdc4", "#45b7d1", "#f7dc6f", "#c06c84", "#6c5ce7"];
|
|
445
|
-
for (let a = 0; a < s; a++)
|
|
446
|
-
this.particles.push({
|
|
447
|
-
x: t,
|
|
448
|
-
y: e,
|
|
449
|
-
vx: (Math.random() - 0.5) * 10,
|
|
450
|
-
vy: (Math.random() - 0.5) * 10 - 5,
|
|
451
|
-
rotation: Math.random() * Math.PI * 2,
|
|
452
|
-
rotationSpeed: (Math.random() - 0.5) * 0.2,
|
|
453
|
-
color: i[Math.floor(Math.random() * i.length)],
|
|
454
|
-
size: Math.random() * 8 + 4,
|
|
455
|
-
life: 1,
|
|
456
|
-
decay: 0.015 + Math.random() * 0.01,
|
|
457
|
-
gravity: 0.3,
|
|
458
|
-
type: "confetti"
|
|
459
|
-
});
|
|
460
|
-
this._startAnimation();
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Sparkle effect (for score milestones, combos)
|
|
464
|
-
*/
|
|
465
|
-
sparkle(t, e, s = 20, i = "#ffd700") {
|
|
466
|
-
for (let a = 0; a < s; a++) {
|
|
467
|
-
const n = Math.PI * 2 / s * a, r = 2 + Math.random() * 3;
|
|
468
|
-
this.particles.push({
|
|
469
|
-
x: t,
|
|
470
|
-
y: e,
|
|
471
|
-
vx: Math.cos(n) * r,
|
|
472
|
-
vy: Math.sin(n) * r,
|
|
473
|
-
color: i,
|
|
474
|
-
size: Math.random() * 4 + 2,
|
|
475
|
-
life: 1,
|
|
476
|
-
decay: 0.02,
|
|
477
|
-
gravity: -0.1,
|
|
478
|
-
// Slight upward drift
|
|
479
|
-
type: "spark"
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
this._startAnimation();
|
|
483
|
-
}
|
|
484
|
-
/**
|
|
485
|
-
* Coin shower (for collecting coins)
|
|
486
|
-
*/
|
|
487
|
-
coinShower(t, e, s = 10) {
|
|
488
|
-
for (let i = 0; i < s; i++)
|
|
489
|
-
this.particles.push({
|
|
490
|
-
x: t + (Math.random() - 0.5) * 100,
|
|
491
|
-
y: e - Math.random() * 50,
|
|
492
|
-
vx: (Math.random() - 0.5) * 3,
|
|
493
|
-
vy: Math.random() * -2 - 3,
|
|
494
|
-
color: "#ffd700",
|
|
495
|
-
size: 12,
|
|
496
|
-
life: 1,
|
|
497
|
-
decay: 0.012,
|
|
498
|
-
gravity: 0.4,
|
|
499
|
-
type: "coin"
|
|
500
|
-
});
|
|
501
|
-
this._startAnimation();
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* Fire particles (for streaks, combos)
|
|
505
|
-
*/
|
|
506
|
-
fire(t, e, s = 15) {
|
|
507
|
-
const i = ["#ff6b00", "#ff8800", "#ffaa00", "#ff4400"];
|
|
508
|
-
for (let a = 0; a < s; a++)
|
|
509
|
-
this.particles.push({
|
|
510
|
-
x: t + (Math.random() - 0.5) * 20,
|
|
511
|
-
y: e,
|
|
512
|
-
vx: (Math.random() - 0.5) * 2,
|
|
513
|
-
vy: -Math.random() * 3 - 2,
|
|
514
|
-
color: i[Math.floor(Math.random() * i.length)],
|
|
515
|
-
size: Math.random() * 6 + 3,
|
|
516
|
-
life: 1,
|
|
517
|
-
decay: 0.025,
|
|
518
|
-
gravity: -0.2,
|
|
519
|
-
// Upward
|
|
520
|
-
type: "fire"
|
|
521
|
-
});
|
|
522
|
-
this._startAnimation();
|
|
523
|
-
}
|
|
524
|
-
/**
|
|
525
|
-
* Star burst (for "perfect" moments)
|
|
526
|
-
*/
|
|
527
|
-
starBurst(t, e, s = 8) {
|
|
528
|
-
for (let i = 0; i < s; i++) {
|
|
529
|
-
const a = Math.PI * 2 / s * i, n = 4;
|
|
530
|
-
this.particles.push({
|
|
531
|
-
x: t,
|
|
532
|
-
y: e,
|
|
533
|
-
vx: Math.cos(a) * n,
|
|
534
|
-
vy: Math.sin(a) * n,
|
|
535
|
-
rotation: a,
|
|
536
|
-
color: "#ffffff",
|
|
537
|
-
size: 15,
|
|
538
|
-
life: 1,
|
|
539
|
-
decay: 0.03,
|
|
540
|
-
gravity: 0,
|
|
541
|
-
type: "star"
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
this._startAnimation();
|
|
545
|
-
}
|
|
546
|
-
_startAnimation() {
|
|
547
|
-
this.isAnimating || (this.isAnimating = !0, this._animate());
|
|
548
|
-
}
|
|
549
|
-
_animate() {
|
|
550
|
-
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
551
|
-
for (let t = this.particles.length - 1; t >= 0; t--) {
|
|
552
|
-
const e = this.particles[t];
|
|
553
|
-
if (e.vy += e.gravity, e.x += e.vx, e.y += e.vy, e.life -= e.decay, e.rotationSpeed && (e.rotation += e.rotationSpeed), e.life <= 0) {
|
|
554
|
-
this.particles.splice(t, 1);
|
|
555
|
-
continue;
|
|
556
|
-
}
|
|
557
|
-
this.ctx.save(), this.ctx.globalAlpha = e.life, this.ctx.fillStyle = e.color, e.type === "confetti" ? (this.ctx.translate(e.x, e.y), this.ctx.rotate(e.rotation), this.ctx.fillRect(-e.size / 2, -e.size / 2, e.size, e.size / 2)) : e.type === "coin" ? (this.ctx.beginPath(), this.ctx.arc(e.x, e.y, e.size, 0, Math.PI * 2), this.ctx.fill(), this.ctx.fillStyle = "#ffee88", this.ctx.beginPath(), this.ctx.arc(e.x - e.size / 3, e.y - e.size / 3, e.size / 3, 0, Math.PI * 2), this.ctx.fill()) : e.type === "star" ? this._drawStar(e.x, e.y, 5, e.size, e.size / 2, e.rotation) : (this.ctx.beginPath(), this.ctx.arc(e.x, e.y, e.size, 0, Math.PI * 2), this.ctx.fill()), this.ctx.restore();
|
|
558
|
-
}
|
|
559
|
-
this.particles.length > 0 ? requestAnimationFrame(() => this._animate()) : this.isAnimating = !1;
|
|
560
|
-
}
|
|
561
|
-
_drawStar(t, e, s, i, a, n) {
|
|
562
|
-
let r = Math.PI / 2 * 3 + n;
|
|
563
|
-
const c = Math.PI / s;
|
|
564
|
-
this.ctx.beginPath(), this.ctx.moveTo(t, e - i);
|
|
565
|
-
for (let d = 0; d < s; d++) {
|
|
566
|
-
let l = t + Math.cos(r) * i, h = e + Math.sin(r) * i;
|
|
567
|
-
this.ctx.lineTo(l, h), r += c, l = t + Math.cos(r) * a, h = e + Math.sin(r) * a, this.ctx.lineTo(l, h), r += c;
|
|
568
|
-
}
|
|
569
|
-
this.ctx.lineTo(t, e - i), this.ctx.closePath(), this.ctx.fill();
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* Clear all particles
|
|
573
|
-
*/
|
|
574
|
-
clear() {
|
|
575
|
-
this.particles = [], this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
class f {
|
|
579
|
-
constructor(t = {}) {
|
|
580
|
-
this.audioContext = null, this.storageKey = t.storageKey || "dopamineSoundsMuted", this.muted = localStorage.getItem(this.storageKey) === "true", this.assets = /* @__PURE__ */ new Map(), this.soundPack = t.soundPack || "synth", this.customSounds = t.customSounds || {}, Object.keys(this.customSounds).length > 0 && this.preloadSounds(this.customSounds);
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Initialize Audio Context (must be called after user interaction)
|
|
584
|
-
*/
|
|
585
|
-
initAudio() {
|
|
586
|
-
if (!this.audioContext) {
|
|
587
|
-
const t = window.AudioContext || window.webkitAudioContext;
|
|
588
|
-
this.audioContext = new t();
|
|
589
|
-
}
|
|
590
|
-
this.audioContext.state === "suspended" && this.audioContext.resume();
|
|
591
|
-
}
|
|
592
|
-
/**
|
|
593
|
-
* Toggle mute state
|
|
594
|
-
* @returns {boolean} New mute state
|
|
595
|
-
*/
|
|
596
|
-
toggleMute() {
|
|
597
|
-
return this.muted = !this.muted, localStorage.setItem(this.storageKey, this.muted), this.muted;
|
|
598
|
-
}
|
|
599
|
-
/**
|
|
600
|
-
* Register a custom sound URL
|
|
601
|
-
* @param {string} key - Unique identifier (e.g., 'jump', 'win')
|
|
602
|
-
* @param {string} url - Path to audio file
|
|
603
|
-
*/
|
|
604
|
-
registerSound(t, e) {
|
|
605
|
-
this.customSounds[t] = e, this.audioContext && this.loadSound(t, e);
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Preload multiple sounds
|
|
609
|
-
* @param {Object} soundMap - { key: url }
|
|
610
|
-
*/
|
|
611
|
-
async preloadSounds(t) {
|
|
612
|
-
const e = Object.entries(t).map(([s, i]) => this.loadSound(s, i));
|
|
613
|
-
await Promise.allSettled(e);
|
|
614
|
-
}
|
|
615
|
-
/**
|
|
616
|
-
* Load and decode a sound file
|
|
617
|
-
*/
|
|
618
|
-
async loadSound(t, e) {
|
|
619
|
-
try {
|
|
620
|
-
if (!this.audioContext)
|
|
621
|
-
return;
|
|
622
|
-
const i = await (await fetch(e)).arrayBuffer(), a = await this.audioContext.decodeAudioData(i);
|
|
623
|
-
this.assets.set(t, a);
|
|
624
|
-
} catch (s) {
|
|
625
|
-
console.warn(`[DopamineJS] Failed to load sound '${t}':`, s);
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* Play a sound by key (custom) or fallback to synth
|
|
630
|
-
* @param {string} key - 'jump', 'score', 'success', etc.
|
|
631
|
-
*/
|
|
632
|
-
async play(t) {
|
|
633
|
-
if (this.muted) return;
|
|
634
|
-
if (this.initAudio(), this.assets.has(t)) {
|
|
635
|
-
this._playBuffer(this.assets.get(t));
|
|
636
|
-
return;
|
|
637
|
-
}
|
|
638
|
-
if (this.customSounds[t] && !this.assets.has(t) && (await this.loadSound(t, this.customSounds[t]), this.assets.has(t))) {
|
|
639
|
-
this._playBuffer(this.assets.get(t));
|
|
640
|
-
return;
|
|
641
|
-
}
|
|
642
|
-
const e = {
|
|
643
|
-
jump: () => this.playJump(!0),
|
|
644
|
-
// true = force synth
|
|
645
|
-
score: () => this.playScore(!0),
|
|
646
|
-
success: () => this.playSuccess(!0),
|
|
647
|
-
click: () => this.playClick(!0),
|
|
648
|
-
error: () => this.playError(!0),
|
|
649
|
-
gameover: () => this.playGameOver(!0)
|
|
650
|
-
};
|
|
651
|
-
e[t] && e[t]();
|
|
652
|
-
}
|
|
653
|
-
_playBuffer(t) {
|
|
654
|
-
const e = this.audioContext.createBufferSource();
|
|
655
|
-
e.buffer = t, e.connect(this.audioContext.destination), e.start(0);
|
|
656
|
-
}
|
|
657
|
-
/**
|
|
658
|
-
* Play a synthesized tone
|
|
659
|
-
*/
|
|
660
|
-
playTone(t, e, s = "sine", i = 0.3) {
|
|
661
|
-
if (this.muted) return;
|
|
662
|
-
this.initAudio();
|
|
663
|
-
const a = this.audioContext.createOscillator(), n = this.audioContext.createGain();
|
|
664
|
-
a.connect(n), n.connect(this.audioContext.destination), a.frequency.value = t, a.type = s, n.gain.setValueAtTime(i, this.audioContext.currentTime), n.gain.exponentialRampToValueAtTime(0.01, this.audioContext.currentTime + e), a.start(this.audioContext.currentTime), a.stop(this.audioContext.currentTime + e);
|
|
665
|
-
}
|
|
666
|
-
// --- Presets (Synth) ---
|
|
667
|
-
playJump(t = !1) {
|
|
668
|
-
if (!t && this.assets.has("jump")) return this.play("jump");
|
|
669
|
-
this.playTone(400, 0.1, "square", 0.2);
|
|
670
|
-
}
|
|
671
|
-
playScore(t = !1) {
|
|
672
|
-
if (!t && this.assets.has("score")) return this.play("score");
|
|
673
|
-
this.playTone(800, 0.15, "sine", 0.25), setTimeout(() => this.playTone(1e3, 0.15, "sine", 0.25), 50);
|
|
674
|
-
}
|
|
675
|
-
playGameOver(t = !1) {
|
|
676
|
-
if (!t && this.assets.has("gameover")) return this.play("gameover");
|
|
677
|
-
this.playTone(300, 0.2, "sawtooth", 0.3), setTimeout(() => this.playTone(200, 0.2, "sawtooth", 0.3), 100), setTimeout(() => this.playTone(150, 0.3, "sawtooth", 0.3), 200);
|
|
678
|
-
}
|
|
679
|
-
playClick(t = !1) {
|
|
680
|
-
if (!t && this.assets.has("click")) return this.play("click");
|
|
681
|
-
this.playTone(600, 0.05, "sine", 0.1);
|
|
682
|
-
}
|
|
683
|
-
playSuccess(t = !1) {
|
|
684
|
-
if (!t && this.assets.has("success")) return this.play("success");
|
|
685
|
-
this.playTone(600, 0.1, "sine", 0.2), setTimeout(() => this.playTone(800, 0.2, "sine", 0.2), 100);
|
|
686
|
-
}
|
|
687
|
-
playError(t = !1) {
|
|
688
|
-
if (!t && this.assets.has("error")) return this.play("error");
|
|
689
|
-
this.playTone(200, 0.2, "sawtooth", 0.3);
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
class g {
|
|
693
|
-
constructor(t = {}) {
|
|
694
|
-
this.config = t, this.dataService = new u(t.data || {}), this.rewardSystem = new m(this.dataService, t.rewards || {}), this.soundManager = new f(t.sound || {}), this.particleSystem = new v(t.particles || {}), this.gameUI = new y(this.particleSystem), this._bindEvents();
|
|
695
|
-
}
|
|
696
|
-
async init() {
|
|
697
|
-
return await this.rewardSystem.init(), console.log("DopamineJS Initialized 🚀"), {
|
|
698
|
-
rewardSystem: this.rewardSystem,
|
|
699
|
-
gameUI: this.gameUI,
|
|
700
|
-
particleSystem: this.particleSystem,
|
|
701
|
-
soundManager: this.soundManager
|
|
702
|
-
};
|
|
703
|
-
}
|
|
704
|
-
_bindEvents() {
|
|
705
|
-
this.rewardSystem.on("xp_gained", (t) => {
|
|
706
|
-
const { total: e, needed: s } = this.rewardSystem.getXPForNextLevel();
|
|
707
|
-
this.gameUI.updateXP(this.rewardSystem.player.xp, s, e), t.xpGained > 0;
|
|
708
|
-
}), this.rewardSystem.on("level_up", (t) => {
|
|
709
|
-
this.gameUI.updateLevel(t.newLevel), this.gameUI.showLevelUp(t.oldLevel, t.newLevel);
|
|
710
|
-
}), this.rewardSystem.on("achievement_unlocked", (t) => {
|
|
711
|
-
this.gameUI.showAchievement(t);
|
|
712
|
-
}), this.rewardSystem.on("new_high_score", (t) => {
|
|
713
|
-
this.gameUI.showNotification(`New High Score: ${t.score}!`, "legendary"), this.soundManager.playSuccess();
|
|
714
|
-
});
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
export {
|
|
718
|
-
u as DataService,
|
|
719
|
-
y as GameUI,
|
|
720
|
-
v as ParticleSystem,
|
|
721
|
-
m as RewardSystem,
|
|
722
|
-
f as SoundManager,
|
|
723
|
-
g as default
|
|
724
|
-
};
|