dopaminejs 2.5.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 (81) 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 -1
  5. package/dist/core.js +2 -2
  6. package/dist/dopamine.cjs +1 -37
  7. package/dist/dopamine.js +3 -635
  8. package/dist/games.cjs +1 -0
  9. package/dist/games.js +82 -0
  10. package/dist/host-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 +24 -9
  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 +54 -24
  26. package/src/dopamine/core/DopamineError.js +7 -0
  27. package/src/dopamine/core/RewardSystem.js +323 -373
  28. package/src/dopamine/core/achievements.js +49 -0
  29. package/src/dopamine/core/clock.js +11 -0
  30. package/src/dopamine/core/levels.js +4 -0
  31. package/src/dopamine/core/merge.js +68 -0
  32. package/src/dopamine/core/middleware.js +58 -0
  33. package/src/dopamine/core/store.js +261 -0
  34. package/src/dopamine/core/streaks.js +55 -0
  35. package/src/dopamine/effects/ParticleSystem.js +30 -0
  36. package/src/dopamine/host.js +358 -0
  37. package/src/dopamine/ui/{GameUI.js → RewardUI.js} +166 -202
  38. package/src/games.js +138 -0
  39. package/src/index.js +11 -34
  40. package/src/progression.js +3 -0
  41. package/src/testing.js +489 -0
  42. package/types/core.d.ts +9 -1
  43. package/types/games.d.ts +46 -0
  44. package/types/index.d.ts +291 -384
  45. package/types/testing.d.ts +96 -0
  46. package/dist/DataService-BPXCldpo.cjs +0 -1
  47. package/dist/DataService-CH9_gv_8.js +0 -398
  48. package/dist/ScreenShake-9jgNXPS6.cjs +0 -1
  49. package/dist/ScreenShake-CLvO1wx4.js +0 -827
  50. package/dist/engine.cjs +0 -1
  51. package/dist/engine.js +0 -2
  52. package/src/core/Animator.js +0 -62
  53. package/src/core/Collider.js +0 -86
  54. package/src/core/Component.js +0 -38
  55. package/src/core/DopamineKernel.js +0 -170
  56. package/src/core/EventBus.js +0 -206
  57. package/src/core/Game.js +0 -90
  58. package/src/core/GameObject.js +0 -99
  59. package/src/core/PluginRegistry.js +0 -359
  60. package/src/core/Scene.js +0 -107
  61. package/src/core/Sprite.js +0 -51
  62. package/src/core/SystemRegistry.js +0 -263
  63. package/src/core/Vector2.js +0 -50
  64. package/src/dopamine/components/ParticleEmitter.js +0 -50
  65. package/src/dopamine/components/ScreenShake.js +0 -50
  66. package/src/dopamine/components/index.js +0 -2
  67. package/src/dopamine/index.js +0 -93
  68. package/src/dopamine/utils/TextureGenerator.js +0 -90
  69. package/src/engine.js +0 -38
  70. package/src/interfaces/IAudioSystem.js +0 -58
  71. package/src/interfaces/IParticleSystem.js +0 -51
  72. package/src/interfaces/IPhysicsSystem.js +0 -52
  73. package/src/interfaces/ISystem.js +0 -48
  74. package/src/interfaces/index.js +0 -7
  75. package/src/renderer/Renderer.js +0 -45
  76. package/src/systems/Director.js +0 -34
  77. package/src/systems/Input.js +0 -81
  78. package/src/systems/Loader.js +0 -48
  79. package/src/systems/Physics.js +0 -210
  80. package/src/systems/Ticker.js +0 -91
  81. package/src/systems/deprecate.js +0 -38
@@ -0,0 +1,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
+ }
@@ -30,6 +30,9 @@ export class SoundManager {
30
30
  // Queued follow-up tones, so destroy() can cancel them.
31
31
  this._timers = new Set();
32
32
 
33
+ // Synth keys added by plugins, by own key.
34
+ this._synths = new Map();
35
+
33
36
  // unlockOn removers, so destroy() takes listeners that never fired off too.
34
37
  this._unlocks = new Set();
35
38
  }
@@ -149,6 +152,29 @@ export class SoundManager {
149
152
  }
150
153
  }
151
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
+
152
178
  /**
153
179
  * Register and load multiple sounds. Before the audio context exists the
154
180
  * files cannot be decoded, so they load when initAudio() creates it.
@@ -207,7 +233,8 @@ export class SoundManager {
207
233
 
208
234
  /**
209
235
  * Play a sound by key (custom) or fallback to synth
210
- * @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.
211
238
  */
212
239
  async play(key) {
213
240
  if (this.muted) return;
@@ -236,15 +263,17 @@ export class SoundManager {
236
263
 
237
264
  // 3. Fallback to synth based on key name
238
265
  const synthMap = {
239
- 'jump': () => this.playJump(true),
240
- 'score': () => this.playScore(true),
241
- '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),
242
270
  'click': () => this.playClick(true),
243
- 'error': () => this.playError(true),
244
- 'gameover': () => this.playGameOver(true)
271
+ 'error': () => this.playError(true)
245
272
  };
246
273
 
247
- 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)) {
248
277
  synthMap[key]();
249
278
  }
250
279
  }
@@ -283,31 +312,31 @@ export class SoundManager {
283
312
 
284
313
  // --- Presets (Synth) ---
285
314
 
286
- playJump(force = false) {
287
- if (!force && this.assets.has('jump')) return this.play('jump');
288
- 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);
289
318
  }
290
319
 
291
- playScore(force = false) {
292
- if (!force && this.assets.has('score')) return this.play('score');
293
- this.playTone(800, 0.15, 'sine', 0.25);
294
- 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);
295
323
  }
296
324
 
297
- playGameOver(force = false) {
298
- if (!force && this.assets.has('gameover')) return this.play('gameover');
299
- this.playTone(300, 0.2, 'sawtooth', 0.3);
300
- this._later(() => this.playTone(200, 0.2, 'sawtooth', 0.3), 100);
301
- 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);
302
330
  }
303
331
 
304
- playClick(force = false) {
305
- if (!force && this.assets.has('click')) return this.play('click');
306
- 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);
307
336
  }
308
337
 
309
- playSuccess(force = false) {
310
- 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');
311
340
  this.playTone(600, 0.1, 'sine', 0.2);
312
341
  this._later(() => this.playTone(800, 0.2, 'sine', 0.2), 100);
313
342
  }
@@ -338,5 +367,6 @@ export class SoundManager {
338
367
  this.masterGain = null;
339
368
  this.assets.clear();
340
369
  this._loading.clear();
370
+ this._synths.clear();
341
371
  }
342
372
  }
@@ -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
+ }