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.
Files changed (83) hide show
  1. package/README.md +40 -50
  2. package/dist/DataService-E4pGQ1V3.cjs +1 -0
  3. package/dist/DataService-MfmvkqXR.js +737 -0
  4. package/dist/core.cjs +1 -0
  5. package/dist/core.js +2 -0
  6. package/dist/dopamine.cjs +1 -37
  7. package/dist/dopamine.js +3 -858
  8. package/dist/games.cjs +1 -0
  9. package/dist/games.js +82 -0
  10. package/dist/host-CkRL74ck.js +814 -0
  11. package/dist/host-Pzb-QBPU.cjs +37 -0
  12. package/dist/style.css +395 -2
  13. package/dist/testing.cjs +1 -0
  14. package/dist/testing.js +474 -0
  15. package/package.json +28 -8
  16. package/spec/README.md +227 -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 +916 -0
  22. package/spec/fixtures/scores.json +755 -0
  23. package/spec/fixtures/streaks.json +924 -0
  24. package/spec/schema.json +61 -0
  25. package/src/dopamine/audio/SoundManager.js +78 -24
  26. package/src/dopamine/core/DataService.js +25 -5
  27. package/src/dopamine/core/DopamineError.js +7 -0
  28. package/src/dopamine/core/RewardSystem.js +366 -368
  29. package/src/dopamine/core/achievements.js +49 -0
  30. package/src/dopamine/core/clock.js +11 -0
  31. package/src/dopamine/core/levels.js +4 -0
  32. package/src/dopamine/core/merge.js +68 -0
  33. package/src/dopamine/core/middleware.js +58 -0
  34. package/src/dopamine/core/store.js +261 -0
  35. package/src/dopamine/core/streaks.js +55 -0
  36. package/src/dopamine/effects/ParticleSystem.js +33 -3
  37. package/src/dopamine/feel/haptics.js +21 -0
  38. package/src/dopamine/feel/motion.js +9 -0
  39. package/src/dopamine/host.js +358 -0
  40. package/src/dopamine/ui/RewardUI.js +569 -0
  41. package/src/games.js +138 -0
  42. package/src/index.js +14 -33
  43. package/src/progression.js +16 -0
  44. package/src/testing.js +489 -0
  45. package/types/core.d.ts +35 -0
  46. package/types/games.d.ts +46 -0
  47. package/types/index.d.ts +348 -373
  48. package/types/testing.d.ts +96 -0
  49. package/dist/ScreenShake-9jgNXPS6.cjs +0 -1
  50. package/dist/ScreenShake-CLvO1wx4.js +0 -827
  51. package/dist/engine.cjs +0 -1
  52. package/dist/engine.js +0 -2
  53. package/src/core/Animator.js +0 -62
  54. package/src/core/Collider.js +0 -86
  55. package/src/core/Component.js +0 -38
  56. package/src/core/DopamineKernel.js +0 -170
  57. package/src/core/EventBus.js +0 -206
  58. package/src/core/Game.js +0 -90
  59. package/src/core/GameObject.js +0 -99
  60. package/src/core/PluginRegistry.js +0 -359
  61. package/src/core/Scene.js +0 -107
  62. package/src/core/Sprite.js +0 -51
  63. package/src/core/SystemRegistry.js +0 -263
  64. package/src/core/Vector2.js +0 -50
  65. package/src/dopamine/components/ParticleEmitter.js +0 -50
  66. package/src/dopamine/components/ScreenShake.js +0 -50
  67. package/src/dopamine/components/index.js +0 -2
  68. package/src/dopamine/index.js +0 -93
  69. package/src/dopamine/ui/GameUI.js +0 -420
  70. package/src/dopamine/utils/TextureGenerator.js +0 -90
  71. package/src/engine.js +0 -38
  72. package/src/interfaces/IAudioSystem.js +0 -58
  73. package/src/interfaces/IParticleSystem.js +0 -51
  74. package/src/interfaces/IPhysicsSystem.js +0 -52
  75. package/src/interfaces/ISystem.js +0 -48
  76. package/src/interfaces/index.js +0 -7
  77. package/src/renderer/Renderer.js +0 -45
  78. package/src/systems/Director.js +0 -34
  79. package/src/systems/Input.js +0 -81
  80. package/src/systems/Loader.js +0 -48
  81. package/src/systems/Physics.js +0 -210
  82. package/src/systems/Ticker.js +0 -91
  83. package/src/systems/deprecate.js +0 -38
@@ -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 - 'jump', 'score', 'success', etc.
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
- 'jump': () => this.playJump(true),
218
- 'score': () => this.playScore(true),
219
- 'success': () => this.playSuccess(true),
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 (hasOwn(synthMap, key)) {
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
- playJump(force = false) {
265
- if (!force && this.assets.has('jump')) return this.play('jump');
266
- this.playTone(400, 0.1, 'square', 0.2);
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
- playScore(force = false) {
270
- if (!force && this.assets.has('score')) return this.play('score');
271
- this.playTone(800, 0.15, 'sine', 0.25);
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
- playGameOver(force = false) {
276
- if (!force && this.assets.has('gameover')) return this.play('gameover');
277
- this.playTone(300, 0.2, 'sawtooth', 0.3);
278
- this._later(() => this.playTone(200, 0.2, 'sawtooth', 0.3), 100);
279
- this._later(() => this.playTone(150, 0.3, 'sawtooth', 0.3), 200);
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
- playClick(force = false) {
283
- if (!force && this.assets.has('click')) return this.play('click');
284
- this.playTone(600, 0.05, 'sine', 0.1);
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
- playSuccess(force = false) {
288
- if (!force && this.assets.has('success')) return this.play('success');
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
- * @param {string} key
60
- * @param {any} defaultValue
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
- const data = await this.storage.getItem(this.prefix + key);
66
- return data ? JSON.parse(data) : defaultValue;
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
  }
@@ -0,0 +1,7 @@
1
+ export class DopamineError extends Error {
2
+ constructor(code, message) {
3
+ super(message || code);
4
+ this.name = 'DopamineError';
5
+ this.code = code;
6
+ }
7
+ }