dopaminejs 2.5.0 → 3.1.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 +55 -50
  2. package/dist/DataService-D1uL6E8R.cjs +1 -0
  3. package/dist/DataService-DN6xwvXU.js +745 -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-7FzZ-yO7.cjs +37 -0
  11. package/dist/host-CdjhXPy-.js +815 -0
  12. package/dist/style.css +395 -2
  13. package/dist/testing.cjs +1 -0
  14. package/dist/testing.js +475 -0
  15. package/package.json +24 -9
  16. package/spec/README.md +228 -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 +957 -0
  22. package/spec/fixtures/scores.json +755 -0
  23. package/spec/fixtures/streaks.json +967 -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 +359 -374
  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 +360 -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 +490 -0
  42. package/types/core.d.ts +9 -1
  43. package/types/games.d.ts +46 -0
  44. package/types/index.d.ts +311 -385
  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
@@ -1,99 +0,0 @@
1
- import { Component } from './Component.js';
2
-
3
- /**
4
- * Base Entity class.
5
- * Has position, scale, rotation, and a list of Components.
6
- */
7
- export class GameObject {
8
- constructor(x = 0, y = 0) {
9
- this.x = x;
10
- this.y = y;
11
- this.rotation = 0;
12
- this.scale = { x: 1, y: 1 };
13
-
14
- this.components = [];
15
- this.children = [];
16
- this.parent = null;
17
- this._kernel = null;
18
- }
19
-
20
- get kernel() {
21
- return this._kernel;
22
- }
23
-
24
- set kernel(value) {
25
- this._kernel = value;
26
- // Propagate to components
27
- for (const comp of this.components) {
28
- comp.kernel = value;
29
- }
30
- // Propagate to children
31
- for (const child of this.children) {
32
- child.kernel = value;
33
- }
34
- }
35
-
36
- /**
37
- * Add a child object.
38
- * @param {GameObject} child
39
- */
40
- addChild(child) {
41
- child.parent = this;
42
- child.kernel = this.kernel; // Propagate kernel
43
- this.children.push(child);
44
- return child;
45
- }
46
-
47
- /**
48
- * Add a component.
49
- * @param {Component} component
50
- */
51
- addComponent(component) {
52
- component.gameObject = this;
53
- component.kernel = this.kernel; // Inject kernel reference
54
- this.components.push(component);
55
- component.onAttach();
56
- return component;
57
- }
58
-
59
- /**
60
- * Get a component by class type.
61
- * @param {class} type
62
- */
63
- getComponent(type) {
64
- return this.components.find(c => c instanceof type);
65
- }
66
-
67
- update(dt) {
68
- // Update components
69
- for (const component of this.components) {
70
- if (component.update) component.update(dt);
71
- }
72
-
73
- // Update children
74
- for (const child of this.children) {
75
- child.update(dt);
76
- }
77
- }
78
-
79
- render(ctx) {
80
- ctx.save();
81
-
82
- // Apply Transforms
83
- ctx.translate(this.x, this.y);
84
- ctx.rotate(this.rotation);
85
- ctx.scale(this.scale.x, this.scale.y);
86
-
87
- // Render components
88
- for (const component of this.components) {
89
- if (component.render) component.render(ctx);
90
- }
91
-
92
- // Render children
93
- for (const child of this.children) {
94
- child.render(ctx);
95
- }
96
-
97
- ctx.restore();
98
- }
99
- }
@@ -1,359 +0,0 @@
1
- /**
2
- * PluginRegistry - Manages plugin lifecycle
3
- *
4
- * Plugins can extend the engine by:
5
- * - Registering new systems
6
- * - Replacing existing systems
7
- * - Adding event listeners
8
- * - Modifying kernel configuration
9
- */
10
- export class PluginRegistry {
11
- constructor(kernel) {
12
- this.kernel = kernel;
13
-
14
- // Map<pluginName, plugin>
15
- this._plugins = new Map();
16
-
17
- // Track plugin load order for proper cleanup
18
- this._loadOrder = [];
19
-
20
- // Async inits in flight. Each notes the systems other plugins added
21
- // meanwhile, so a failed one removes only its own.
22
- this._inFlight = new Set();
23
-
24
- // Map<pluginName, Map<systemName, system>>: what each plugin
25
- // registered during init, so remove() can unregister it.
26
- this._owned = new Map();
27
- }
28
-
29
- /**
30
- * Every registered system by name.
31
- * @private
32
- */
33
- _snapshot() {
34
- const systems = this.kernel.systems;
35
- return new Map(systems.getSystemNames().map((name) => [name, systems.get(name)]));
36
- }
37
-
38
- /**
39
- * @private
40
- */
41
- _systemsSince(before) {
42
- return this.kernel.systems.getSystemNames().filter((name) => !before.has(name));
43
- }
44
-
45
- /**
46
- * Names of the systems added or replaced since a snapshot. A plugin that
47
- * swaps in its own 'audio' adds no new name, so names alone miss it.
48
- * @private
49
- */
50
- _changedSince(before) {
51
- const systems = this.kernel.systems;
52
- return systems.getSystemNames().filter((name) => systems.get(name) !== before.get(name));
53
- }
54
-
55
- /**
56
- * Remember the systems a plugin registered during init.
57
- * @private
58
- */
59
- _own(pluginName, names) {
60
- const systems = this.kernel.systems;
61
- const owned = new Map();
62
-
63
- for (const name of names) {
64
- const system = systems.get(name);
65
-
66
- // A plugin loaded from inside this one's init() already owns it.
67
- const taken = [...this._owned.values()].some((other) => other.get(name) === system);
68
- if (!taken) owned.set(name, system);
69
- }
70
-
71
- this._owned.set(pluginName, owned);
72
- }
73
-
74
- /**
75
- * Unregister the systems a removed plugin registered during init.
76
- * @private
77
- */
78
- _release(pluginName) {
79
- const owned = this._owned.get(pluginName);
80
- if (!owned) return;
81
-
82
- this._owned.delete(pluginName);
83
-
84
- for (const [name, system] of owned) {
85
- // Replaced or unregistered since. It is no longer this plugin's.
86
- if (this.kernel.systems.get(name) !== system) continue;
87
-
88
- try {
89
- this.kernel.systems.unregister(name);
90
- } catch (error) {
91
- console.error(`[PluginRegistry] Error removing system "${name}":`, error);
92
- }
93
- }
94
- }
95
-
96
- /**
97
- * Mark systems as belonging to a plugin that loaded.
98
- * @private
99
- */
100
- _claim(names) {
101
- for (const pending of this._inFlight) {
102
- for (const name of names) {
103
- pending.foreign.add(name);
104
- }
105
- }
106
- }
107
-
108
- /**
109
- * Remove the systems a failed plugin registered. They would keep updating
110
- * with nothing left to remove them.
111
- * @private
112
- */
113
- _rollback(names) {
114
- for (const name of names) {
115
- try {
116
- this.kernel.systems.unregister(name);
117
- } catch (error) {
118
- // The caller is about to rethrow the init error. This one
119
- // must not replace it or stop the loop.
120
- console.error(`[PluginRegistry] Error removing system "${name}":`, error);
121
- }
122
- }
123
- }
124
-
125
- /**
126
- * Forget a plugin whose init failed.
127
- *
128
- * Removed by name: with two async inits in flight, the failed plugin is
129
- * not necessarily the last entry.
130
- * @private
131
- */
132
- _forget(name) {
133
- this._plugins.delete(name);
134
-
135
- const index = this._loadOrder.indexOf(name);
136
- if (index > -1) {
137
- this._loadOrder.splice(index, 1);
138
- }
139
- }
140
-
141
- /**
142
- * Register and initialize a plugin
143
- *
144
- * Plugin interface:
145
- * {
146
- * name: string,
147
- * version?: string,
148
- * init(kernel): void,
149
- * destroy?(): void
150
- * }
151
- *
152
- * @param {Object} plugin - Plugin instance
153
- * @returns {PluginRegistry} - For chaining
154
- */
155
- use(plugin) {
156
- if (!plugin.name) {
157
- throw new Error('[PluginRegistry] Plugin must have a name');
158
- }
159
-
160
- if (this._plugins.has(plugin.name)) {
161
- console.warn(`[PluginRegistry] Plugin "${plugin.name}" already registered. Skipping.`);
162
- return this;
163
- }
164
-
165
- if (!plugin.init || typeof plugin.init !== 'function') {
166
- throw new Error(`[PluginRegistry] Plugin "${plugin.name}" must have an init() method`);
167
- }
168
-
169
- // Store plugin
170
- this._plugins.set(plugin.name, plugin);
171
- this._loadOrder.push(plugin.name);
172
-
173
- // Initialize plugin
174
- const systemsBefore = this._snapshot();
175
-
176
- try {
177
- plugin.init(this.kernel);
178
-
179
- const mine = this._changedSince(systemsBefore);
180
- this._own(plugin.name, mine);
181
- this._claim(mine);
182
-
183
- // Emit event
184
- this.kernel.events.emit('plugin_loaded', {
185
- name: plugin.name,
186
- version: plugin.version
187
- });
188
- } catch (error) {
189
- console.error(`[PluginRegistry] Failed to initialize plugin "${plugin.name}":`, error);
190
- this._forget(plugin.name);
191
- this._rollback(this._systemsSince(systemsBefore));
192
-
193
- throw error;
194
- }
195
-
196
- return this;
197
- }
198
-
199
- /**
200
- * Register and initialize a plugin asynchronously
201
- * Useful for plugins that need to load external resources
202
- *
203
- * @param {Object} plugin - Plugin instance with async init
204
- * @returns {Promise<PluginRegistry>} - For chaining
205
- */
206
- async useAsync(plugin) {
207
- if (!plugin.name) {
208
- throw new Error('[PluginRegistry] Plugin must have a name');
209
- }
210
-
211
- if (this._plugins.has(plugin.name)) {
212
- console.warn(`[PluginRegistry] Plugin "${plugin.name}" already registered. Skipping.`);
213
- return this;
214
- }
215
-
216
- if (!plugin.init || typeof plugin.init !== 'function') {
217
- throw new Error(`[PluginRegistry] Plugin "${plugin.name}" must have an init() method`);
218
- }
219
-
220
- // Store plugin
221
- this._plugins.set(plugin.name, plugin);
222
- this._loadOrder.push(plugin.name);
223
-
224
- const pending = {
225
- before: this._snapshot(),
226
- foreign: new Set()
227
- };
228
- const own = () => this._systemsSince(pending.before).filter((name) => !pending.foreign.has(name));
229
- this._inFlight.add(pending);
230
-
231
- // Initialize plugin (await if it returns a promise)
232
- try {
233
- await plugin.init(this.kernel);
234
-
235
- this._inFlight.delete(pending);
236
-
237
- const systems = this.kernel.systems;
238
- const mine = this._changedSince(pending.before).filter((name) => !pending.foreign.has(name));
239
-
240
- // With another init still running, a system that appeared after
241
- // that one began could be its work. Nobody owns those, so they
242
- // stay registered when either plugin is removed.
243
- const others = [...this._inFlight];
244
- this._own(plugin.name, mine.filter(
245
- (name) => others.every((other) => other.before.get(name) === systems.get(name))
246
- ));
247
- this._claim(mine);
248
-
249
- // Emit event
250
- this.kernel.events.emit('plugin_loaded', {
251
- name: plugin.name,
252
- version: plugin.version
253
- });
254
- } catch (error) {
255
- console.error(`[PluginRegistry] Failed to initialize plugin "${plugin.name}":`, error);
256
- this._inFlight.delete(pending);
257
- this._forget(plugin.name);
258
-
259
- const leftover = own();
260
- if (this._inFlight.size === 0) {
261
- this._rollback(leftover);
262
- } else if (leftover.length > 0) {
263
- // Another init is still running and may have registered some
264
- // of these. Removing them could break a plugin that loads.
265
- console.warn(`[PluginRegistry] Left systems in place after "${plugin.name}" failed: ${leftover.join(', ')}`);
266
- }
267
-
268
- throw error;
269
- }
270
-
271
- return this;
272
- }
273
-
274
- /**
275
- * Remove a plugin
276
- *
277
- * Calls the plugin's destroy(), then unregisters the systems it
278
- * registered during init() that are still in place. Not tracked, and so
279
- * the plugin's to unregister in destroy(): a system it registers after
280
- * init(), and one registered while two useAsync() inits overlapped.
281
- *
282
- * @param {string} name - Plugin name
283
- * @returns {boolean} - True if plugin was found and removed
284
- */
285
- remove(name) {
286
- const plugin = this._plugins.get(name);
287
- if (!plugin) {
288
- return false;
289
- }
290
-
291
- // Call destroy if available
292
- if (plugin.destroy) {
293
- try {
294
- plugin.destroy();
295
- } catch (error) {
296
- console.error(`[PluginRegistry] Error destroying plugin "${name}":`, error);
297
- }
298
- }
299
-
300
- this._plugins.delete(name);
301
-
302
- // Remove from load order
303
- const index = this._loadOrder.indexOf(name);
304
- if (index > -1) {
305
- this._loadOrder.splice(index, 1);
306
- }
307
-
308
- this._release(name);
309
-
310
- // Emit event
311
- this.kernel.events.emit('plugin_unloaded', { name });
312
-
313
- return true;
314
- }
315
-
316
- /**
317
- * Get a plugin by name
318
- * @param {string} name - Plugin name
319
- * @returns {Object|undefined} - Plugin instance
320
- */
321
- get(name) {
322
- return this._plugins.get(name);
323
- }
324
-
325
- /**
326
- * Check if a plugin is registered
327
- * @param {string} name - Plugin name
328
- * @returns {boolean}
329
- */
330
- has(name) {
331
- return this._plugins.has(name);
332
- }
333
-
334
- /**
335
- * Get all registered plugin names
336
- * @returns {string[]}
337
- */
338
- getPluginNames() {
339
- return Array.from(this._plugins.keys());
340
- }
341
-
342
- /**
343
- * Get plugin load order
344
- * @returns {string[]}
345
- */
346
- getLoadOrder() {
347
- return [...this._loadOrder];
348
- }
349
-
350
- /**
351
- * Clear all plugins (in reverse load order)
352
- */
353
- clear() {
354
- // Unload in reverse order
355
- for (let i = this._loadOrder.length - 1; i >= 0; i--) {
356
- this.remove(this._loadOrder[i]);
357
- }
358
- }
359
- }
package/src/core/Scene.js DELETED
@@ -1,107 +0,0 @@
1
- /**
2
- * Represents a specific state or screen in the game (e.g., Menu, Level1).
3
- */
4
- export class Scene {
5
- constructor() {
6
- this.gameObjects = [];
7
- this._kernel = null; // Injected by Game
8
- }
9
-
10
- get kernel() {
11
- return this._kernel;
12
- }
13
-
14
- /**
15
- * Objects added before the scene had a kernel receive it here. Scenes are
16
- * often built in a constructor, before Game.setScene() injects the kernel.
17
- * The Director sets it to null on exit, which takes colliders out of
18
- * physics until the scene runs again.
19
- */
20
- set kernel(value) {
21
- this._kernel = value;
22
-
23
- for (const gameObject of this.gameObjects) {
24
- gameObject.kernel = value;
25
- }
26
- }
27
-
28
- /**
29
- * Called when the scene is added to the Game.
30
- */
31
- onEnter() { }
32
-
33
- /**
34
- * Called when the scene is removed from the Game.
35
- */
36
- onExit() { }
37
-
38
- /**
39
- * Add a GameObject to the scene.
40
- * @param {GameObject} gameObject
41
- */
42
- add(gameObject) {
43
- if (this.kernel) {
44
- gameObject.kernel = this.kernel; // Inject kernel
45
- }
46
-
47
- // Coming back after a remove(): components were detached then.
48
- if (gameObject._detached) {
49
- gameObject._detached = false;
50
- this._eachComponent(gameObject, (component) => component.onAttach());
51
- }
52
-
53
- this.gameObjects.push(gameObject);
54
- return gameObject;
55
- }
56
-
57
- /**
58
- * Remove a GameObject from the scene.
59
- * @param {GameObject} gameObject
60
- */
61
- remove(gameObject) {
62
- const index = this.gameObjects.indexOf(gameObject);
63
- if (index > -1) {
64
- this.gameObjects.splice(index, 1);
65
-
66
- // Lets components release what they hold. A Collider left in
67
- // physics keeps colliding for an object that is gone.
68
- gameObject._detached = true;
69
- this._eachComponent(gameObject, (component) => component.onDetach());
70
- }
71
- }
72
-
73
- /**
74
- * Visit the components of an object and of all its children.
75
- * @private
76
- */
77
- _eachComponent(gameObject, fn) {
78
- for (const component of [...gameObject.components]) {
79
- fn(component);
80
- }
81
- for (const child of gameObject.children) {
82
- this._eachComponent(child, fn);
83
- }
84
- }
85
-
86
- /**
87
- * Update loop for the scene.
88
- * @param {number} dt
89
- */
90
- update(dt) {
91
- // Snapshot: an object may add or remove objects mid-frame, and
92
- // splicing the live array would skip the one after it.
93
- for (const obj of [...this.gameObjects]) {
94
- if (!obj._detached) obj.update(dt);
95
- }
96
- }
97
-
98
- /**
99
- * Render loop for the scene.
100
- * @param {CanvasRenderingContext2D} ctx
101
- */
102
- render(ctx) {
103
- for (const obj of [...this.gameObjects]) {
104
- if (!obj._detached) obj.render(ctx);
105
- }
106
- }
107
- }
@@ -1,51 +0,0 @@
1
- import { Component } from '../core/Component.js';
2
-
3
- export class Sprite extends Component {
4
- constructor(image) {
5
- super();
6
- this.image = image; // Image or Canvas element
7
- this.anchor = { x: 0.5, y: 0.5 };
8
- this.width = image ? image.width : 0;
9
- this.height = image ? image.height : 0;
10
-
11
- // Frame support (for spritesheets)
12
- this.frame = { x: 0, y: 0, w: this.width, h: this.height };
13
- }
14
-
15
- setFrame(x, y, w, h) {
16
- this.frame = { x, y, w, h };
17
- this.width = w;
18
- this.height = h;
19
- }
20
-
21
- setTexture(image) {
22
- this.image = image;
23
- // Reset frame if full texture
24
- if (image) {
25
- this.setFrame(0, 0, image.width, image.height);
26
- }
27
- }
28
-
29
- render(ctx) {
30
- if (!this.image) return;
31
-
32
- // The transform (pos, rot, scale) is already handled by the Scene/GO before calling render?
33
- // Wait, our GameObject implementation currently propagates render but doesn't apply context transform automatically in the base class loop?
34
- // Let's check GameObject.js.
35
-
36
- // Checking GameObject.js...
37
- // "ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(this.rotation); ctx.scale(this.scale.x, this.scale.y); ... ctx.restore();"
38
- // YES, it handles logic. So we render at 0,0 local space.
39
-
40
- const w = this.frame.w;
41
- const h = this.frame.h;
42
- const offsetX = -w * this.anchor.x;
43
- const offsetY = -h * this.anchor.y;
44
-
45
- ctx.drawImage(
46
- this.image,
47
- this.frame.x, this.frame.y, w, h, // Source
48
- offsetX, offsetY, w, h // Destination
49
- );
50
- }
51
- }