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.
- package/README.md +40 -50
- package/dist/DataService-E4pGQ1V3.cjs +1 -0
- package/dist/DataService-MfmvkqXR.js +737 -0
- package/dist/core.cjs +1 -1
- package/dist/core.js +2 -2
- package/dist/dopamine.cjs +1 -37
- package/dist/dopamine.js +3 -635
- 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 +24 -9
- 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 +54 -24
- package/src/dopamine/core/DopamineError.js +7 -0
- package/src/dopamine/core/RewardSystem.js +323 -373
- 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 +30 -0
- package/src/dopamine/host.js +358 -0
- package/src/dopamine/ui/{GameUI.js → RewardUI.js} +166 -202
- package/src/games.js +138 -0
- package/src/index.js +11 -34
- package/src/progression.js +3 -0
- package/src/testing.js +489 -0
- package/types/core.d.ts +9 -1
- package/types/games.d.ts +46 -0
- package/types/index.d.ts +291 -384
- package/types/testing.d.ts +96 -0
- package/dist/DataService-BPXCldpo.cjs +0 -1
- package/dist/DataService-CH9_gv_8.js +0 -398
- 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/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
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The composition root. createDopamine() builds the parts, wires the default
|
|
3
|
+
* feedback to the progress events and installs plugins on top.
|
|
4
|
+
*
|
|
5
|
+
* Nothing here touches the DOM until it is called.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { RewardSystem } from './core/RewardSystem.js';
|
|
9
|
+
import { DataService } from './core/DataService.js';
|
|
10
|
+
import { DopamineError } from './core/DopamineError.js';
|
|
11
|
+
import { systemClock } from './core/clock.js';
|
|
12
|
+
import { RewardUI } from './ui/RewardUI.js';
|
|
13
|
+
import { ParticleSystem } from './effects/ParticleSystem.js';
|
|
14
|
+
import { SoundManager } from './audio/SoundManager.js';
|
|
15
|
+
import { createHaptics } from './feel/haptics.js';
|
|
16
|
+
import { prefersReducedMotion } from './feel/motion.js';
|
|
17
|
+
import { resolveStorage } from './utils/storage.js';
|
|
18
|
+
|
|
19
|
+
export const HOST_API = 3;
|
|
20
|
+
|
|
21
|
+
const PART_NAMES = ['ui', 'particles', 'sound', 'haptics'];
|
|
22
|
+
|
|
23
|
+
const isThenable = (value) => value !== null && typeof value === 'object' && typeof value.then === 'function';
|
|
24
|
+
|
|
25
|
+
const settle = (value, log) => {
|
|
26
|
+
if (isThenable(value)) value.then(undefined, (error) => log.error(error));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function makeEnv() {
|
|
30
|
+
const hasDom = typeof document !== 'undefined' && typeof window !== 'undefined';
|
|
31
|
+
return { hasDom, reducedMotion: hasDom ? prefersReducedMotion(window).matches === true : false };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A part is a value, a factory `(env) => Part | null`, or `false` for none.
|
|
36
|
+
* A factory returning null (or no value at all) means use the default.
|
|
37
|
+
*/
|
|
38
|
+
function resolvePart(option, env, makeDefault) {
|
|
39
|
+
if (option === false) return null;
|
|
40
|
+
let part = option;
|
|
41
|
+
if (typeof option === 'function') part = option(env);
|
|
42
|
+
if (part === null || part === undefined) return makeDefault();
|
|
43
|
+
return part;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function namespacedStorage(storage, name) {
|
|
47
|
+
const prefix = `dopamine_plugin_${name}:`;
|
|
48
|
+
return {
|
|
49
|
+
getItem: (key) => storage.getItem(prefix + key),
|
|
50
|
+
setItem: (key, value) => storage.setItem(prefix + key, value),
|
|
51
|
+
removeItem: (key) => storage.removeItem(prefix + key)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {Object} [options]
|
|
57
|
+
* @param {Object|Function|null} [options.progress] - A RewardSystem-like object or a factory
|
|
58
|
+
* @param {Object|Function|false|null} [options.ui]
|
|
59
|
+
* @param {Object|Function|false|null} [options.particles]
|
|
60
|
+
* @param {Object|Function|false|null} [options.sound]
|
|
61
|
+
* @param {Object|Function|false|null} [options.haptics]
|
|
62
|
+
* @param {Object} [options.storage] - Storage port, default localStorage or memory
|
|
63
|
+
* @param {{ now(): number, localDate(ms?: number): string }} [options.clock]
|
|
64
|
+
* @param {'any'|'record'} [options.streakDays] - For the default progress object
|
|
65
|
+
* @param {Object[]} [options.plugins]
|
|
66
|
+
* @param {{ debug?: Function, info?: Function, warn?: Function, error?: Function }} [options.log]
|
|
67
|
+
*/
|
|
68
|
+
export function createDopamine(options = {}) {
|
|
69
|
+
const opts = options || {};
|
|
70
|
+
const env = makeEnv();
|
|
71
|
+
const log = {
|
|
72
|
+
debug: () => {},
|
|
73
|
+
info: () => {},
|
|
74
|
+
warn: (...args) => console.warn(...args),
|
|
75
|
+
error: (...args) => console.error(...args),
|
|
76
|
+
...(opts.log || {})
|
|
77
|
+
};
|
|
78
|
+
const storage = opts.storage || resolveStorage();
|
|
79
|
+
const clock = opts.clock || systemClock;
|
|
80
|
+
|
|
81
|
+
// Getters over a table that is only written while the host is built, so a
|
|
82
|
+
// plugin always reads the part that is current and cannot replace one.
|
|
83
|
+
const slots = { ui: null, particles: null, sound: null, haptics: null };
|
|
84
|
+
const parts = {};
|
|
85
|
+
for (const name of PART_NAMES) {
|
|
86
|
+
Object.defineProperty(parts, name, { enumerable: true, get: () => slots[name] });
|
|
87
|
+
}
|
|
88
|
+
Object.freeze(parts);
|
|
89
|
+
|
|
90
|
+
const factoryEnv = { ...env, parts, storage, clock };
|
|
91
|
+
|
|
92
|
+
const progress = resolvePart(opts.progress, factoryEnv, () => new RewardSystem(new DataService({ storage }), { clock, streakDays: opts.streakDays }));
|
|
93
|
+
if (typeof progress?.on !== 'function' || typeof progress?.off !== 'function') {
|
|
94
|
+
throw new DopamineError('invalid_progress', 'progress needs on() and off()');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
slots.particles = resolvePart(opts.particles, factoryEnv, () => (env.hasDom ? new ParticleSystem() : null));
|
|
98
|
+
slots.sound = resolvePart(opts.sound, factoryEnv, () => new SoundManager({ storage }));
|
|
99
|
+
slots.haptics = resolvePart(opts.haptics, factoryEnv, () => createHaptics());
|
|
100
|
+
try {
|
|
101
|
+
slots.ui = resolvePart(opts.ui, factoryEnv, () => (env.hasDom ? new RewardUI(parts) : null));
|
|
102
|
+
} catch (error) {
|
|
103
|
+
destroyParts();
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function destroyParts() {
|
|
108
|
+
for (const name of PART_NAMES) {
|
|
109
|
+
try {
|
|
110
|
+
slots[name]?.destroy?.();
|
|
111
|
+
} catch (error) {
|
|
112
|
+
log.error(error);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const events = progress;
|
|
118
|
+
const offs = [];
|
|
119
|
+
let disposed = false;
|
|
120
|
+
|
|
121
|
+
// Default feedback: the parts react to the core events.
|
|
122
|
+
const react = (fn) => (data) => {
|
|
123
|
+
try {
|
|
124
|
+
const result = fn(data);
|
|
125
|
+
settle(result, log);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
log.error(error);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const sync = () => {
|
|
131
|
+
const ui = slots.ui;
|
|
132
|
+
if (!ui || typeof progress.snapshot !== 'function' || typeof progress.getXPForNextLevel !== 'function') return;
|
|
133
|
+
const player = progress.snapshot();
|
|
134
|
+
const { total, progress: fraction } = progress.getXPForNextLevel();
|
|
135
|
+
ui.hud({ xp: player.xp, level: player.level, progress: fraction, streak: player.streak.current, next: total });
|
|
136
|
+
};
|
|
137
|
+
offs.push(
|
|
138
|
+
events.on('xp_gained', react((e) => {
|
|
139
|
+
sync();
|
|
140
|
+
if (!e.leveledUp) slots.sound?.play('xp');
|
|
141
|
+
})),
|
|
142
|
+
events.on('level_up', react((e) => {
|
|
143
|
+
sync();
|
|
144
|
+
slots.ui?.popup({ kind: 'level', title: '', body: String(e.newLevel) });
|
|
145
|
+
slots.sound?.play('level_up');
|
|
146
|
+
})),
|
|
147
|
+
events.on('achievement_unlocked', react((a) => {
|
|
148
|
+
slots.ui?.popup({
|
|
149
|
+
kind: 'achievement',
|
|
150
|
+
title: a.name,
|
|
151
|
+
body: a.description,
|
|
152
|
+
icon: a.icon,
|
|
153
|
+
meta: `+${a.xp ?? 0} XP`
|
|
154
|
+
});
|
|
155
|
+
slots.sound?.play('achievement');
|
|
156
|
+
})),
|
|
157
|
+
events.on('streak_updated', react(sync)),
|
|
158
|
+
events.on('personal_best', react((e) => {
|
|
159
|
+
slots.ui?.popup({ kind: 'notice', title: `New High Score: ${e.score}!`, tier: 'legendary' });
|
|
160
|
+
slots.sound?.play('personal_best');
|
|
161
|
+
}))
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
// Plugins, by name. A record is created when an install starts and is
|
|
165
|
+
// `done` once it has finished.
|
|
166
|
+
const plugins = new Map();
|
|
167
|
+
const order = [];
|
|
168
|
+
|
|
169
|
+
function check(plugin) {
|
|
170
|
+
if (plugin === null || typeof plugin !== 'object' || typeof plugin.name !== 'string' || !/^[a-z0-9-]+$/.test(plugin.name)
|
|
171
|
+
|| typeof plugin.install !== 'function') {
|
|
172
|
+
throw new DopamineError('plugin_invalid', 'a plugin needs a name (a-z, 0-9 and -) and an install function');
|
|
173
|
+
}
|
|
174
|
+
if (plugin.api !== HOST_API) {
|
|
175
|
+
throw new DopamineError(
|
|
176
|
+
'plugin_api',
|
|
177
|
+
`${plugin.name} was built for api ${plugin.api}, this host is api ${HOST_API}`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (plugins.has(plugin.name)) {
|
|
181
|
+
throw new DopamineError('plugin_duplicate', `${plugin.name} is already installed`);
|
|
182
|
+
}
|
|
183
|
+
for (const dep of plugin.after || []) {
|
|
184
|
+
if (!plugins.has(dep) || !plugins.get(dep).done) {
|
|
185
|
+
throw new DopamineError('plugin_order', `${plugin.name} must come after ${dep}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
for (const part of plugin.requires || []) {
|
|
189
|
+
if (!PART_NAMES.includes(part) || slots[part] === null) {
|
|
190
|
+
throw new DopamineError('plugin_requires', `${plugin.name} requires the ${part} part, which is not set`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function release(record) {
|
|
196
|
+
for (const off of record.offs) off();
|
|
197
|
+
record.offs.clear();
|
|
198
|
+
plugins.delete(record.name);
|
|
199
|
+
const i = order.indexOf(record);
|
|
200
|
+
if (i > -1) order.splice(i, 1);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function dispose1(record) {
|
|
204
|
+
if (record.removed) return;
|
|
205
|
+
record.removed = true;
|
|
206
|
+
const disposer = record.disposer;
|
|
207
|
+
record.disposer = null;
|
|
208
|
+
try {
|
|
209
|
+
disposer?.();
|
|
210
|
+
} catch (error) {
|
|
211
|
+
log.error(error);
|
|
212
|
+
}
|
|
213
|
+
release(record);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function context(record) {
|
|
217
|
+
const { name } = record;
|
|
218
|
+
const bus = {
|
|
219
|
+
on(event, callback) {
|
|
220
|
+
const off = events.on(event, callback);
|
|
221
|
+
const tracked = () => {
|
|
222
|
+
off();
|
|
223
|
+
record.offs.delete(tracked);
|
|
224
|
+
};
|
|
225
|
+
record.offs.add(tracked);
|
|
226
|
+
return tracked;
|
|
227
|
+
},
|
|
228
|
+
off: (event, callback) => events.off(event, callback),
|
|
229
|
+
emit(event, data) {
|
|
230
|
+
if (typeof event !== 'string' || !event.startsWith(`${name}:`)) {
|
|
231
|
+
throw new DopamineError('plugin_event', `${name} may only emit events named ${name}:<event>`);
|
|
232
|
+
}
|
|
233
|
+
events.emit(event, data);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
progress,
|
|
238
|
+
parts,
|
|
239
|
+
events: bus,
|
|
240
|
+
storage: namespacedStorage(storage, name),
|
|
241
|
+
clock,
|
|
242
|
+
log,
|
|
243
|
+
env: { hasDom: env.hasDom, reducedMotion: env.reducedMotion },
|
|
244
|
+
plugin: (other) => (plugins.has(other) && plugins.get(other).done ? plugins.get(other).handle : null),
|
|
245
|
+
expose: (handle) => { record.handle = handle; }
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Returns the record, or a promise of it when install is async.
|
|
250
|
+
function install(plugin) {
|
|
251
|
+
if (disposed) throw new DopamineError('disposed', 'the host was disposed');
|
|
252
|
+
check(plugin);
|
|
253
|
+
const record = {
|
|
254
|
+
name: plugin.name, done: false, removed: false, disposer: null, handle: null, offs: new Set()
|
|
255
|
+
};
|
|
256
|
+
plugins.set(plugin.name, record);
|
|
257
|
+
|
|
258
|
+
const finish = (disposer) => {
|
|
259
|
+
record.done = true;
|
|
260
|
+
if (typeof disposer === 'function') record.disposer = disposer;
|
|
261
|
+
if (record.removed || disposed) {
|
|
262
|
+
record.removed = false;
|
|
263
|
+
dispose1(record);
|
|
264
|
+
return record;
|
|
265
|
+
}
|
|
266
|
+
order.push(record);
|
|
267
|
+
return record;
|
|
268
|
+
};
|
|
269
|
+
const fail = (error) => {
|
|
270
|
+
release(record);
|
|
271
|
+
throw error;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
let result;
|
|
275
|
+
try {
|
|
276
|
+
result = plugin.install(context(record));
|
|
277
|
+
} catch (error) {
|
|
278
|
+
return fail(error);
|
|
279
|
+
}
|
|
280
|
+
if (isThenable(result)) {
|
|
281
|
+
return result.then(finish, fail);
|
|
282
|
+
}
|
|
283
|
+
return finish(result);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Installs run one after another, so `after` sees finished plugins only.
|
|
287
|
+
let tail = null;
|
|
288
|
+
function enqueue(plugin) {
|
|
289
|
+
if (disposed) throw new DopamineError('disposed', 'the host was disposed');
|
|
290
|
+
if (tail === null) {
|
|
291
|
+
const result = install(plugin);
|
|
292
|
+
if (isThenable(result)) {
|
|
293
|
+
const done = result.then((record) => record, (error) => { throw error; });
|
|
294
|
+
tail = done.then(() => { tail = null; }, () => { tail = null; });
|
|
295
|
+
return { record: null, done };
|
|
296
|
+
}
|
|
297
|
+
return { record: result, done: Promise.resolve(result) };
|
|
298
|
+
}
|
|
299
|
+
const done = tail.then(() => install(plugin));
|
|
300
|
+
tail = done.then(() => { tail = null; }, () => { tail = null; });
|
|
301
|
+
return { record: null, done };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function use(plugin) {
|
|
305
|
+
const { record, done } = enqueue(plugin);
|
|
306
|
+
if (record) return () => dispose1(record);
|
|
307
|
+
let target = null;
|
|
308
|
+
let early = false;
|
|
309
|
+
done.then((r) => {
|
|
310
|
+
target = r;
|
|
311
|
+
if (early) dispose1(r);
|
|
312
|
+
}, (error) => log.error(error));
|
|
313
|
+
return () => {
|
|
314
|
+
if (target) dispose1(target);
|
|
315
|
+
else early = true;
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let readyChain = Promise.resolve();
|
|
320
|
+
try {
|
|
321
|
+
for (const plugin of opts.plugins || []) {
|
|
322
|
+
const { done } = enqueue(plugin);
|
|
323
|
+
readyChain = readyChain.then(() => done);
|
|
324
|
+
}
|
|
325
|
+
} catch (error) {
|
|
326
|
+
// A plugin was refused: undo what was built.
|
|
327
|
+
dispose();
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// An install that rejects undoes the rest, as a refused plugin does.
|
|
332
|
+
readyChain = readyChain.catch((error) => {
|
|
333
|
+
dispose();
|
|
334
|
+
throw error;
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
function dispose() {
|
|
338
|
+
if (disposed) return;
|
|
339
|
+
disposed = true;
|
|
340
|
+
for (const record of [...order].reverse()) dispose1(record);
|
|
341
|
+
for (const record of [...plugins.values()]) dispose1(record);
|
|
342
|
+
for (const off of offs) off();
|
|
343
|
+
offs.length = 0;
|
|
344
|
+
destroyParts();
|
|
345
|
+
progress.clear?.();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const ready = readyChain
|
|
349
|
+
.then(() => (typeof progress.init === 'function' ? progress.init() : undefined))
|
|
350
|
+
.then(() => {
|
|
351
|
+
if (!disposed) sync();
|
|
352
|
+
return host;
|
|
353
|
+
});
|
|
354
|
+
ready.catch(() => {});
|
|
355
|
+
|
|
356
|
+
const host = { progress, parts, events, ready, use, dispose };
|
|
357
|
+
return host;
|
|
358
|
+
}
|