dopaminejs 2.4.0 → 3.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/README.md +40 -50
- package/dist/DataService-E4pGQ1V3.cjs +1 -0
- package/dist/DataService-MfmvkqXR.js +737 -0
- package/dist/core.cjs +1 -0
- package/dist/core.js +2 -0
- package/dist/dopamine.cjs +1 -37
- package/dist/dopamine.js +3 -858
- package/dist/games.cjs +1 -0
- package/dist/games.js +82 -0
- package/dist/host-CkRL74ck.js +814 -0
- package/dist/host-Pzb-QBPU.cjs +37 -0
- package/dist/style.css +395 -2
- package/dist/testing.cjs +1 -0
- package/dist/testing.js +474 -0
- package/package.json +28 -8
- package/spec/README.md +227 -0
- package/spec/VERSION +1 -0
- package/spec/fixtures/achievements.json +611 -0
- package/spec/fixtures/levels.json +421 -0
- package/spec/fixtures/merge.json +1739 -0
- package/spec/fixtures/migrate.json +916 -0
- package/spec/fixtures/scores.json +755 -0
- package/spec/fixtures/streaks.json +924 -0
- package/spec/schema.json +61 -0
- package/src/dopamine/audio/SoundManager.js +78 -24
- package/src/dopamine/core/DataService.js +25 -5
- package/src/dopamine/core/DopamineError.js +7 -0
- package/src/dopamine/core/RewardSystem.js +366 -368
- package/src/dopamine/core/achievements.js +49 -0
- package/src/dopamine/core/clock.js +11 -0
- package/src/dopamine/core/levels.js +4 -0
- package/src/dopamine/core/merge.js +68 -0
- package/src/dopamine/core/middleware.js +58 -0
- package/src/dopamine/core/store.js +261 -0
- package/src/dopamine/core/streaks.js +55 -0
- package/src/dopamine/effects/ParticleSystem.js +33 -3
- package/src/dopamine/feel/haptics.js +21 -0
- package/src/dopamine/feel/motion.js +9 -0
- package/src/dopamine/host.js +358 -0
- package/src/dopamine/ui/RewardUI.js +569 -0
- package/src/games.js +138 -0
- package/src/index.js +14 -33
- package/src/progression.js +16 -0
- package/src/testing.js +489 -0
- package/types/core.d.ts +35 -0
- package/types/games.d.ts +46 -0
- package/types/index.d.ts +348 -373
- package/types/testing.d.ts +96 -0
- package/dist/ScreenShake-9jgNXPS6.cjs +0 -1
- package/dist/ScreenShake-CLvO1wx4.js +0 -827
- package/dist/engine.cjs +0 -1
- package/dist/engine.js +0 -2
- package/src/core/Animator.js +0 -62
- package/src/core/Collider.js +0 -86
- package/src/core/Component.js +0 -38
- package/src/core/DopamineKernel.js +0 -170
- package/src/core/EventBus.js +0 -206
- package/src/core/Game.js +0 -90
- package/src/core/GameObject.js +0 -99
- package/src/core/PluginRegistry.js +0 -359
- package/src/core/Scene.js +0 -107
- package/src/core/Sprite.js +0 -51
- package/src/core/SystemRegistry.js +0 -263
- package/src/core/Vector2.js +0 -50
- package/src/dopamine/components/ParticleEmitter.js +0 -50
- package/src/dopamine/components/ScreenShake.js +0 -50
- package/src/dopamine/components/index.js +0 -2
- package/src/dopamine/index.js +0 -93
- package/src/dopamine/ui/GameUI.js +0 -420
- package/src/dopamine/utils/TextureGenerator.js +0 -90
- package/src/engine.js +0 -38
- package/src/interfaces/IAudioSystem.js +0 -58
- package/src/interfaces/IParticleSystem.js +0 -51
- package/src/interfaces/IPhysicsSystem.js +0 -52
- package/src/interfaces/ISystem.js +0 -48
- package/src/interfaces/index.js +0 -7
- package/src/renderer/Renderer.js +0 -45
- package/src/systems/Director.js +0 -34
- package/src/systems/Input.js +0 -81
- package/src/systems/Loader.js +0 -48
- package/src/systems/Physics.js +0 -210
- package/src/systems/Ticker.js +0 -91
- package/src/systems/deprecate.js +0 -38
package/spec/schema.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://github.com/BaryoDev/dopaminejs/spec/schema.json",
|
|
4
|
+
"title": "DopamineJS player document, contract 3",
|
|
5
|
+
"description": "What export() returns and what validate() and merge() accept. Only v is required on input; validate fills in the rest. Keys inside the per-device maps are device ids, which are never __proto__, constructor or prototype, and a document holds at most 20 of them across xpBy, xpSpentBy, gamesBy, playsBy and sumsBy.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"required": ["v"],
|
|
8
|
+
"additionalProperties": false,
|
|
9
|
+
"properties": {
|
|
10
|
+
"v": { "const": 3 },
|
|
11
|
+
"name": { "type": "string" },
|
|
12
|
+
"createdAt": { "type": "number" },
|
|
13
|
+
"lastPlayedAt": { "type": "number" },
|
|
14
|
+
"level": { "type": "integer", "minimum": 1 },
|
|
15
|
+
"longest": { "type": "integer", "minimum": 0 },
|
|
16
|
+
"playDates": {
|
|
17
|
+
"type": "array",
|
|
18
|
+
"items": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" }
|
|
19
|
+
},
|
|
20
|
+
"xpBy": { "$ref": "#/$defs/amounts" },
|
|
21
|
+
"xpSpentBy": { "$ref": "#/$defs/amounts" },
|
|
22
|
+
"gamesBy": { "$ref": "#/$defs/counts" },
|
|
23
|
+
"playsBy": {
|
|
24
|
+
"type": "object",
|
|
25
|
+
"additionalProperties": { "$ref": "#/$defs/counts" }
|
|
26
|
+
},
|
|
27
|
+
"best": { "$ref": "#/$defs/amounts" },
|
|
28
|
+
"sumsBy": {
|
|
29
|
+
"type": "object",
|
|
30
|
+
"additionalProperties": {
|
|
31
|
+
"type": "object",
|
|
32
|
+
"additionalProperties": {
|
|
33
|
+
"type": "object",
|
|
34
|
+
"additionalProperties": { "type": "number" }
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"achievements": {
|
|
39
|
+
"type": "object",
|
|
40
|
+
"additionalProperties": {
|
|
41
|
+
"type": "object",
|
|
42
|
+
"additionalProperties": false,
|
|
43
|
+
"properties": {
|
|
44
|
+
"unlockedAt": { "type": "number" },
|
|
45
|
+
"seen": { "type": "boolean" },
|
|
46
|
+
"reward": { "type": "number", "minimum": 0 }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"$defs": {
|
|
52
|
+
"amounts": {
|
|
53
|
+
"type": "object",
|
|
54
|
+
"additionalProperties": { "type": "number", "minimum": 0 }
|
|
55
|
+
},
|
|
56
|
+
"counts": {
|
|
57
|
+
"type": "object",
|
|
58
|
+
"additionalProperties": { "type": "integer", "minimum": 0 }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -29,6 +29,31 @@ export class SoundManager {
|
|
|
29
29
|
|
|
30
30
|
// Queued follow-up tones, so destroy() can cancel them.
|
|
31
31
|
this._timers = new Set();
|
|
32
|
+
|
|
33
|
+
// Synth keys added by plugins, by own key.
|
|
34
|
+
this._synths = new Map();
|
|
35
|
+
|
|
36
|
+
// unlockOn removers, so destroy() takes listeners that never fired off too.
|
|
37
|
+
this._unlocks = new Set();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Start audio on the first gesture on `target`. Browsers keep an
|
|
42
|
+
* AudioContext silent until the page gets one, and a touch pointerdown
|
|
43
|
+
* is not one, so the default waits for pointerup. Returns a remover.
|
|
44
|
+
*/
|
|
45
|
+
unlockOn(target, events = ['pointerup', 'touchend', 'keydown']) {
|
|
46
|
+
const remove = () => {
|
|
47
|
+
for (const type of events) target.removeEventListener(type, unlock);
|
|
48
|
+
this._unlocks.delete(remove);
|
|
49
|
+
};
|
|
50
|
+
const unlock = () => {
|
|
51
|
+
remove();
|
|
52
|
+
this.initAudio();
|
|
53
|
+
};
|
|
54
|
+
for (const type of events) target.addEventListener(type, unlock);
|
|
55
|
+
this._unlocks.add(remove);
|
|
56
|
+
return remove;
|
|
32
57
|
}
|
|
33
58
|
|
|
34
59
|
/**
|
|
@@ -127,6 +152,29 @@ export class SoundManager {
|
|
|
127
152
|
}
|
|
128
153
|
}
|
|
129
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Add a sound key. A string is a file URL. A function is a synth,
|
|
157
|
+
* called as `(sound, later)` when the key plays; `later(fn, ms)` is a
|
|
158
|
+
* timeout that destroy() cancels. Returns a remover.
|
|
159
|
+
* @param {string} key
|
|
160
|
+
* @param {string|Function} source
|
|
161
|
+
*/
|
|
162
|
+
register(key, source) {
|
|
163
|
+
if (typeof key !== 'string' || key === '') return () => {};
|
|
164
|
+
if (typeof source === 'string') {
|
|
165
|
+
this.registerSound(key, source);
|
|
166
|
+
return () => {
|
|
167
|
+
if (this.customSounds[key] === source) delete this.customSounds[key];
|
|
168
|
+
this.assets.delete(key);
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (typeof source !== 'function') return () => {};
|
|
172
|
+
this._synths.set(key, source);
|
|
173
|
+
return () => {
|
|
174
|
+
if (this._synths.get(key) === source) this._synths.delete(key);
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
130
178
|
/**
|
|
131
179
|
* Register and load multiple sounds. Before the audio context exists the
|
|
132
180
|
* files cannot be decoded, so they load when initAudio() creates it.
|
|
@@ -185,7 +233,8 @@ export class SoundManager {
|
|
|
185
233
|
|
|
186
234
|
/**
|
|
187
235
|
* Play a sound by key (custom) or fallback to synth
|
|
188
|
-
* @param {string} key - '
|
|
236
|
+
* @param {string} key - A preset ('xp', 'level_up', 'achievement', 'personal_best',
|
|
237
|
+
* 'click', 'error'), a registered key, or a synth name. An unknown key is ignored.
|
|
189
238
|
*/
|
|
190
239
|
async play(key) {
|
|
191
240
|
if (this.muted) return;
|
|
@@ -214,15 +263,17 @@ export class SoundManager {
|
|
|
214
263
|
|
|
215
264
|
// 3. Fallback to synth based on key name
|
|
216
265
|
const synthMap = {
|
|
217
|
-
'
|
|
218
|
-
'
|
|
219
|
-
'
|
|
266
|
+
'xp': () => this.playXP(true),
|
|
267
|
+
'level_up': () => this.playLevelUp(true),
|
|
268
|
+
'achievement': () => this.playAchievement(true),
|
|
269
|
+
'personal_best': () => this.playPersonalBest(true),
|
|
220
270
|
'click': () => this.playClick(true),
|
|
221
|
-
'error': () => this.playError(true)
|
|
222
|
-
'gameover': () => this.playGameOver(true)
|
|
271
|
+
'error': () => this.playError(true)
|
|
223
272
|
};
|
|
224
273
|
|
|
225
|
-
if (
|
|
274
|
+
if (this._synths.has(key)) {
|
|
275
|
+
this._synths.get(key)(this, (fn, delay) => this._later(fn, delay));
|
|
276
|
+
} else if (hasOwn(synthMap, key)) {
|
|
226
277
|
synthMap[key]();
|
|
227
278
|
}
|
|
228
279
|
}
|
|
@@ -261,31 +312,31 @@ export class SoundManager {
|
|
|
261
312
|
|
|
262
313
|
// --- Presets (Synth) ---
|
|
263
314
|
|
|
264
|
-
|
|
265
|
-
if (!force && this.assets.has('
|
|
266
|
-
this.playTone(
|
|
315
|
+
playClick(force = false) {
|
|
316
|
+
if (!force && this.assets.has('click')) return this.play('click');
|
|
317
|
+
this.playTone(600, 0.05, 'sine', 0.1);
|
|
267
318
|
}
|
|
268
319
|
|
|
269
|
-
|
|
270
|
-
if (!force && this.assets.has('
|
|
271
|
-
this.playTone(
|
|
272
|
-
this._later(() => this.playTone(1000, 0.15, 'sine', 0.25), 50);
|
|
320
|
+
playXP(force = false) {
|
|
321
|
+
if (!force && this.assets.has('xp')) return this.play('xp');
|
|
322
|
+
this.playTone(700, 0.06, 'sine', 0.12);
|
|
273
323
|
}
|
|
274
324
|
|
|
275
|
-
|
|
276
|
-
if (!force && this.assets.has('
|
|
277
|
-
this.playTone(
|
|
278
|
-
this._later(() => this.playTone(
|
|
279
|
-
this._later(() => this.playTone(
|
|
325
|
+
playLevelUp(force = false) {
|
|
326
|
+
if (!force && this.assets.has('level_up')) return this.play('level_up');
|
|
327
|
+
this.playTone(500, 0.12, 'sine', 0.2);
|
|
328
|
+
this._later(() => this.playTone(700, 0.12, 'sine', 0.2), 110);
|
|
329
|
+
this._later(() => this.playTone(900, 0.25, 'sine', 0.2), 220);
|
|
280
330
|
}
|
|
281
331
|
|
|
282
|
-
|
|
283
|
-
if (!force && this.assets.has('
|
|
284
|
-
this.playTone(
|
|
332
|
+
playAchievement(force = false) {
|
|
333
|
+
if (!force && this.assets.has('achievement')) return this.play('achievement');
|
|
334
|
+
this.playTone(660, 0.1, 'triangle', 0.2);
|
|
335
|
+
this._later(() => this.playTone(990, 0.25, 'triangle', 0.2), 100);
|
|
285
336
|
}
|
|
286
337
|
|
|
287
|
-
|
|
288
|
-
if (!force && this.assets.has('
|
|
338
|
+
playPersonalBest(force = false) {
|
|
339
|
+
if (!force && this.assets.has('personal_best')) return this.play('personal_best');
|
|
289
340
|
this.playTone(600, 0.1, 'sine', 0.2);
|
|
290
341
|
this._later(() => this.playTone(800, 0.2, 'sine', 0.2), 100);
|
|
291
342
|
}
|
|
@@ -300,6 +351,8 @@ export class SoundManager {
|
|
|
300
351
|
* Browsers cap the number of live AudioContexts per page.
|
|
301
352
|
*/
|
|
302
353
|
destroy() {
|
|
354
|
+
for (const remove of [...this._unlocks]) remove();
|
|
355
|
+
|
|
303
356
|
for (const id of this._timers) {
|
|
304
357
|
clearTimeout(id);
|
|
305
358
|
}
|
|
@@ -314,5 +367,6 @@ export class SoundManager {
|
|
|
314
367
|
this.masterGain = null;
|
|
315
368
|
this.assets.clear();
|
|
316
369
|
this._loading.clear();
|
|
370
|
+
this._synths.clear();
|
|
317
371
|
}
|
|
318
372
|
}
|
|
@@ -56,15 +56,35 @@ export class DataService {
|
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Load data from local storage
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
*
|
|
60
|
+
* A failed read returns `defaultValue`, so a caller cannot tell "nothing
|
|
61
|
+
* saved" from "could not read". Pass `{ strict: true }` to get the error
|
|
62
|
+
* instead; unparseable data then rejects with `code: 'DOPAMINE_CORRUPT'`
|
|
63
|
+
* and the raw string on `raw`.
|
|
64
|
+
* @param {string} key
|
|
65
|
+
* @param {any} defaultValue
|
|
66
|
+
* @param {{ strict?: boolean }} [options]
|
|
61
67
|
*/
|
|
62
|
-
async load(key, defaultValue = null) {
|
|
68
|
+
async load(key, defaultValue = null, options = {}) {
|
|
69
|
+
let data;
|
|
63
70
|
try {
|
|
64
71
|
await this._tail;
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
data = await this.storage.getItem(this.prefix + key);
|
|
73
|
+
} catch (e) {
|
|
74
|
+
if (options.strict) throw e;
|
|
75
|
+
console.error('Error loading data:', e);
|
|
76
|
+
return defaultValue;
|
|
77
|
+
}
|
|
78
|
+
if (!data) return defaultValue;
|
|
79
|
+
try {
|
|
80
|
+
return JSON.parse(data);
|
|
67
81
|
} catch (e) {
|
|
82
|
+
if (options.strict) {
|
|
83
|
+
throw Object.assign(new Error(`[DopamineJS] saved "${key}" is not valid JSON`), {
|
|
84
|
+
code: 'DOPAMINE_CORRUPT',
|
|
85
|
+
raw: data,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
68
88
|
console.error('Error loading data:', e);
|
|
69
89
|
return defaultValue;
|
|
70
90
|
}
|