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.
- package/README.md +55 -50
- package/dist/DataService-D1uL6E8R.cjs +1 -0
- package/dist/DataService-DN6xwvXU.js +745 -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-7FzZ-yO7.cjs +37 -0
- package/dist/host-CdjhXPy-.js +815 -0
- package/dist/style.css +395 -2
- package/dist/testing.cjs +1 -0
- package/dist/testing.js +475 -0
- package/package.json +24 -9
- package/spec/README.md +228 -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 +957 -0
- package/spec/fixtures/scores.json +755 -0
- package/spec/fixtures/streaks.json +967 -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 +359 -374
- 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 +360 -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 +490 -0
- package/types/core.d.ts +9 -1
- package/types/games.d.ts +46 -0
- package/types/index.d.ts +311 -385
- 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
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* UI
|
|
3
|
-
*
|
|
2
|
+
* The default reward UI: a progress bar, achievement and level-up popups and
|
|
3
|
+
* floating text. Nothing here runs at import time.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import DEFAULT_CSS from './dopamine.css?inline';
|
|
7
|
+
|
|
6
8
|
const OVERLAY_ID = 'game-ui-overlay';
|
|
7
9
|
|
|
8
10
|
const DEFAULT_TEXT = {
|
|
9
11
|
levelUp: 'LEVEL UP!',
|
|
10
|
-
achievementUnlocked: 'Achievement Unlocked!'
|
|
11
|
-
nearMiss: 'CLOSE CALL!'
|
|
12
|
+
achievementUnlocked: 'Achievement Unlocked!'
|
|
12
13
|
};
|
|
13
14
|
|
|
15
|
+
const STYLE_ID = 'dopamine-reward-ui-css';
|
|
16
|
+
|
|
14
17
|
const DEFAULT_STREAK_ICON = '🔥';
|
|
15
18
|
|
|
16
19
|
// Config comes from callers, so an inherited property (a polluted prototype)
|
|
17
20
|
// must not count.
|
|
21
|
+
const textOf = (value) => (value === undefined || value === null ? '' : String(value));
|
|
22
|
+
|
|
18
23
|
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
|
|
19
24
|
const ownValue = (object, key) =>
|
|
20
25
|
(object !== null && typeof object === 'object' && hasOwn(object, key) ? object[key] : undefined);
|
|
@@ -141,47 +146,62 @@ function parseSafeSvg(markup) {
|
|
|
141
146
|
return cleanNode(root, { ids, prefix: `dj-icon-${++iconCounter}-` });
|
|
142
147
|
}
|
|
143
148
|
|
|
144
|
-
export class
|
|
149
|
+
export class RewardUI {
|
|
145
150
|
/**
|
|
146
|
-
* @param {Object}
|
|
151
|
+
* @param {{ particles?: Object|null }} [parts] - Read at call time, so a
|
|
152
|
+
* different particle renderer is picked up without rebuilding the UI.
|
|
147
153
|
* @param {Object} [config]
|
|
148
|
-
* @param {{levelUp?: string, achievementUnlocked?: string
|
|
154
|
+
* @param {{levelUp?: string, achievementUnlocked?: string}} [config.text]
|
|
149
155
|
* Replaces the built-in popup strings.
|
|
150
156
|
* @param {{streak?: string|Node}} [config.icons]
|
|
151
157
|
* @param {boolean} [config.safeIcons] - Render string icons as sanitised
|
|
152
|
-
* SVG or plain text
|
|
158
|
+
* SVG or plain text. Default true. False renders them as HTML.
|
|
159
|
+
* @param {boolean|string} [config.injectCss] - Add the stylesheet from
|
|
160
|
+
* code. A string is the CSS text. Default false: import
|
|
161
|
+
* `dopaminejs/style.css` instead.
|
|
153
162
|
*/
|
|
154
|
-
constructor(
|
|
155
|
-
this.
|
|
163
|
+
constructor(parts = {}, config = {}) {
|
|
164
|
+
this.parts = parts || {};
|
|
156
165
|
|
|
157
166
|
// Only strings override a default, so a non-string cannot break a
|
|
158
167
|
// popup. An empty string is allowed and hides that text.
|
|
159
168
|
const textConfig = ownValue(config, 'text');
|
|
160
|
-
this.
|
|
169
|
+
this.strings = { ...DEFAULT_TEXT };
|
|
161
170
|
for (const key of Object.keys(DEFAULT_TEXT)) {
|
|
162
171
|
const value = ownValue(textConfig, key);
|
|
163
|
-
if (typeof value === 'string') this.
|
|
172
|
+
if (typeof value === 'string') this.strings[key] = value;
|
|
164
173
|
}
|
|
165
|
-
this.safeIcons = ownValue(config, 'safeIcons')
|
|
174
|
+
this.safeIcons = ownValue(config, 'safeIcons') !== false;
|
|
166
175
|
this._streakIcon = ownValue(ownValue(config, 'icons'), 'streak') ?? DEFAULT_STREAK_ICON;
|
|
176
|
+
this._injectCss = ownValue(config, 'injectCss');
|
|
167
177
|
this.container = null;
|
|
168
178
|
this.xpBar = null;
|
|
169
179
|
this.levelDisplay = null;
|
|
170
180
|
this.streakDisplay = null;
|
|
181
|
+
this._style = null;
|
|
182
|
+
this._xp = null;
|
|
183
|
+
this._level = null;
|
|
171
184
|
|
|
172
185
|
// Everything this instance put in the DOM or scheduled, so destroy()
|
|
173
186
|
// can take it all back out again.
|
|
174
187
|
this._timers = new Set();
|
|
175
188
|
this._detached = [];
|
|
176
189
|
|
|
177
|
-
this.
|
|
190
|
+
this._build();
|
|
178
191
|
}
|
|
179
192
|
|
|
180
193
|
/**
|
|
181
|
-
*
|
|
194
|
+
* The overlay element, for themes. Null after destroy().
|
|
195
|
+
* @returns {HTMLElement|null}
|
|
182
196
|
*/
|
|
183
|
-
|
|
184
|
-
|
|
197
|
+
get root() {
|
|
198
|
+
return this.container;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @private */
|
|
202
|
+
_build() {
|
|
203
|
+
if (this._injectCss) this._addStyles();
|
|
204
|
+
|
|
185
205
|
this.container = document.createElement('div');
|
|
186
206
|
this.container.className = 'game-ui-overlay';
|
|
187
207
|
|
|
@@ -231,7 +251,7 @@ export class GameUI {
|
|
|
231
251
|
document.body.appendChild(this.container);
|
|
232
252
|
|
|
233
253
|
// Scoped to this instance. Document-wide getElementById would make a
|
|
234
|
-
// second
|
|
254
|
+
// second RewardUI silently rebind every element to the first one's DOM.
|
|
235
255
|
this.levelBadge = this.container.querySelector('.level-badge');
|
|
236
256
|
this.streakBadge = this.container.querySelector('.streak-badge');
|
|
237
257
|
this.xpBar = this.container.querySelector('.xp-bar-fill');
|
|
@@ -241,10 +261,24 @@ export class GameUI {
|
|
|
241
261
|
this._setIcon(this.container.querySelector('.streak-icon'), this._streakIcon);
|
|
242
262
|
}
|
|
243
263
|
|
|
264
|
+
/**
|
|
265
|
+
* Add the stylesheet once per page. The first instance owns the element
|
|
266
|
+
* and removes it on destroy; later ones share it.
|
|
267
|
+
* @private
|
|
268
|
+
*/
|
|
269
|
+
_addStyles() {
|
|
270
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
271
|
+
const css = typeof this._injectCss === 'string' ? this._injectCss : DEFAULT_CSS;
|
|
272
|
+
this._style = document.createElement('style');
|
|
273
|
+
this._style.id = STYLE_ID;
|
|
274
|
+
this._style.textContent = css;
|
|
275
|
+
document.head.appendChild(this._style);
|
|
276
|
+
}
|
|
277
|
+
|
|
244
278
|
/**
|
|
245
279
|
* Single place where icons reach the DOM. A Node is appended. A string is
|
|
246
|
-
*
|
|
247
|
-
*
|
|
280
|
+
* a sanitised SVG or plain text unless safeIcons is off; then it is HTML
|
|
281
|
+
* (see SECURITY.md).
|
|
248
282
|
* @private
|
|
249
283
|
*/
|
|
250
284
|
_setIcon(el, icon) {
|
|
@@ -298,6 +332,15 @@ export class GameUI {
|
|
|
298
332
|
}
|
|
299
333
|
}
|
|
300
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Ask the particle part for a burst, if there is one. The part is looked
|
|
337
|
+
* up on every call.
|
|
338
|
+
* @private
|
|
339
|
+
*/
|
|
340
|
+
_burst(kind, at, opts) {
|
|
341
|
+
this.parts.particles?.burst?.(kind, at, opts);
|
|
342
|
+
}
|
|
343
|
+
|
|
301
344
|
/**
|
|
302
345
|
* Replay a CSS animation by clearing it for one tick.
|
|
303
346
|
* @private
|
|
@@ -309,42 +352,32 @@ export class GameUI {
|
|
|
309
352
|
}
|
|
310
353
|
|
|
311
354
|
/**
|
|
312
|
-
*
|
|
313
|
-
* @param {number
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
* Without it the bar falls back to current / total, which is lifetime
|
|
318
|
-
* XP and starts every level after the first partly full.
|
|
355
|
+
* Draw the bar, the level and the streak.
|
|
356
|
+
* @param {{ xp: number, level: number, progress?: number, streak: number, next?: number }} state
|
|
357
|
+
* `progress` is the position within the current level, 0 to 1. `next` is
|
|
358
|
+
* the XP total that marks the next level. Without `progress` the bar
|
|
359
|
+
* falls back to xp / next.
|
|
319
360
|
*/
|
|
320
|
-
|
|
361
|
+
hud(state) {
|
|
362
|
+
if (!this.container || !state) return;
|
|
363
|
+
const { xp, level, progress, streak, next } = state;
|
|
364
|
+
|
|
321
365
|
const fraction = Number.isFinite(progress)
|
|
322
366
|
? progress
|
|
323
|
-
: (
|
|
367
|
+
: (next > 0 ? xp / next : 0);
|
|
324
368
|
this.xpBar.style.width = `${Math.max(0, Math.min(100, fraction * 100))}%`;
|
|
325
|
-
this.xpText.textContent = `${
|
|
326
|
-
|
|
327
|
-
this.
|
|
328
|
-
}
|
|
369
|
+
this.xpText.textContent = Number.isFinite(next) ? `${xp} / ${next} XP` : `${xp} XP`;
|
|
370
|
+
if (xp !== this._xp) this._replayAnimation(this.xpBar, 'xp-pulse 0.3s ease-out');
|
|
371
|
+
this._xp = xp;
|
|
329
372
|
|
|
330
|
-
/**
|
|
331
|
-
* Update level display
|
|
332
|
-
*/
|
|
333
|
-
updateLevel(level) {
|
|
334
373
|
this.levelDisplay.textContent = level;
|
|
335
|
-
this._replayAnimation(this.levelBadge, 'level-pulse 0.5s ease-out');
|
|
336
|
-
|
|
374
|
+
if (level !== this._level) this._replayAnimation(this.levelBadge, 'level-pulse 0.5s ease-out');
|
|
375
|
+
this._level = level;
|
|
337
376
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
*/
|
|
341
|
-
updateStreak(days) {
|
|
342
|
-
this.streakDisplay.textContent = days;
|
|
343
|
-
|
|
344
|
-
// Change color based on streak
|
|
345
|
-
if (days >= 7) {
|
|
377
|
+
this.streakDisplay.textContent = streak;
|
|
378
|
+
if (streak >= 7) {
|
|
346
379
|
this.streakBadge.style.background = 'linear-gradient(135deg, #ff6b00 0%, #ff4400 100%)';
|
|
347
|
-
} else if (
|
|
380
|
+
} else if (streak >= 3) {
|
|
348
381
|
this.streakBadge.style.background = 'linear-gradient(135deg, #ff8800 0%, #ff6b00 100%)';
|
|
349
382
|
} else {
|
|
350
383
|
this.streakBadge.style.background = '';
|
|
@@ -352,18 +385,37 @@ export class GameUI {
|
|
|
352
385
|
}
|
|
353
386
|
|
|
354
387
|
/**
|
|
355
|
-
* Show
|
|
388
|
+
* Show a popup. Every field is set as text. Only `icon` can carry markup,
|
|
389
|
+
* and only when safeIcons is off; a Node is appended as is.
|
|
390
|
+
*
|
|
391
|
+
* - `achievement`: `title` is the name, `body` the description, `meta`
|
|
392
|
+
* an extra line such as "+50 XP".
|
|
393
|
+
* - `level`: `body` is the new level. `title` replaces the heading and
|
|
394
|
+
* falls back to the configured level-up text.
|
|
395
|
+
* - `notice`: `title` is a message floated over the page. `tier` picks
|
|
396
|
+
* the colour and the burst.
|
|
397
|
+
* - `custom` (and any other kind): the achievement layout without the heading.
|
|
356
398
|
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
* appended as is. Every other field is game state
|
|
360
|
-
* and could carry a player-supplied or server-supplied string, so it goes
|
|
361
|
-
* in as text.
|
|
399
|
+
* @param {{ kind: string, title?: string, body?: string, icon?: string|Node,
|
|
400
|
+
* meta?: string, tier?: 'common'|'rare'|'legendary' }} popup
|
|
362
401
|
*/
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
402
|
+
popup(popup) {
|
|
403
|
+
if (!this.container || !popup || typeof popup !== 'object') return;
|
|
404
|
+
const kind = ownValue(popup, 'kind');
|
|
405
|
+
if (kind === 'level') {
|
|
406
|
+
this._levelPopup(popup);
|
|
407
|
+
} else if (kind === 'notice') {
|
|
408
|
+
this._notice(popup);
|
|
409
|
+
} else {
|
|
410
|
+
this._card(popup, kind === 'achievement');
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** @private */
|
|
415
|
+
_card(popup, withHeading) {
|
|
416
|
+
const card = document.createElement('div');
|
|
417
|
+
card.className = 'achievement-popup';
|
|
418
|
+
card.innerHTML = `
|
|
367
419
|
<div class="achievement-shine"></div>
|
|
368
420
|
<div class="achievement-content">
|
|
369
421
|
<div class="achievement-icon"></div>
|
|
@@ -376,32 +428,32 @@ export class GameUI {
|
|
|
376
428
|
</div>
|
|
377
429
|
`;
|
|
378
430
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
431
|
+
const title = textOf(popup.title);
|
|
432
|
+
card.querySelector('.achievement-title').textContent = withHeading ? this.strings.achievementUnlocked : '';
|
|
433
|
+
this._setIcon(card.querySelector('.achievement-icon'), popup.icon);
|
|
434
|
+
card.querySelector('.achievement-name').textContent = title;
|
|
435
|
+
card.querySelector('.achievement-desc').textContent = textOf(popup.body);
|
|
436
|
+
card.querySelector('.achievement-xp').textContent = textOf(popup.meta);
|
|
384
437
|
|
|
385
|
-
this._mount(
|
|
386
|
-
this._announce(
|
|
438
|
+
this._mount(card);
|
|
439
|
+
this._announce(withHeading ? `${this.strings.achievementUnlocked}: ${title}` : title);
|
|
387
440
|
|
|
388
|
-
// Trigger confetti at popup location
|
|
389
441
|
this._defer(() => {
|
|
390
|
-
const rect =
|
|
391
|
-
this.
|
|
442
|
+
const rect = card.getBoundingClientRect();
|
|
443
|
+
this._burst('confetti', { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }, { count: 40 });
|
|
392
444
|
}, 300);
|
|
393
445
|
|
|
394
|
-
// Remove after animation
|
|
395
446
|
this._defer(() => {
|
|
396
|
-
|
|
397
|
-
this._defer(() => this._remove(
|
|
447
|
+
card.style.animation = 'slideOut 0.3s ease-in forwards';
|
|
448
|
+
this._defer(() => this._remove(card), 300);
|
|
398
449
|
}, 4000);
|
|
399
450
|
}
|
|
400
451
|
|
|
401
|
-
/**
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
452
|
+
/** @private */
|
|
453
|
+
_levelPopup(popup) {
|
|
454
|
+
const heading = typeof popup.title === 'string' && popup.title !== '' ? popup.title : this.strings.levelUp;
|
|
455
|
+
const level = textOf(popup.body);
|
|
456
|
+
|
|
405
457
|
const overlay = document.createElement('div');
|
|
406
458
|
overlay.className = 'level-up-overlay';
|
|
407
459
|
overlay.innerHTML = `
|
|
@@ -411,161 +463,71 @@ export class GameUI {
|
|
|
411
463
|
<div class="level-up-subtitle">Amazing progress!</div>
|
|
412
464
|
</div>
|
|
413
465
|
`;
|
|
414
|
-
overlay.querySelector('.level-up-title').textContent =
|
|
415
|
-
overlay.querySelector('.level-up-number').textContent =
|
|
466
|
+
overlay.querySelector('.level-up-title').textContent = heading;
|
|
467
|
+
overlay.querySelector('.level-up-number').textContent = level;
|
|
416
468
|
|
|
417
469
|
this._mount(overlay);
|
|
418
|
-
this._announce(`Level up: ${
|
|
470
|
+
this._announce(`Level up: ${level}`);
|
|
419
471
|
|
|
420
|
-
// Fireworks effect
|
|
421
472
|
this._defer(() => {
|
|
422
473
|
for (let i = 0; i < 5; i++) {
|
|
423
474
|
this._defer(() => {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
475
|
+
this._burst('confetti', {
|
|
476
|
+
x: Math.random() * window.innerWidth,
|
|
477
|
+
y: Math.random() * window.innerHeight * 0.6
|
|
478
|
+
}, { count: 30 });
|
|
427
479
|
}, i * 200);
|
|
428
480
|
}
|
|
429
481
|
}, 300);
|
|
430
482
|
|
|
431
|
-
// Remove after 3 seconds
|
|
432
483
|
this._defer(() => {
|
|
433
484
|
overlay.style.opacity = '0';
|
|
434
485
|
this._defer(() => this._remove(overlay), 500);
|
|
435
486
|
}, 2500);
|
|
436
487
|
}
|
|
437
488
|
|
|
438
|
-
/**
|
|
439
|
-
|
|
440
|
-
*/
|
|
441
|
-
showFloatingText(text, x, y, color = '#ffd700', size = '24px') {
|
|
442
|
-
const floater = document.createElement('div');
|
|
443
|
-
floater.className = 'floating-text';
|
|
444
|
-
floater.textContent = text;
|
|
445
|
-
floater.style.left = x + 'px';
|
|
446
|
-
floater.style.top = y + 'px';
|
|
447
|
-
floater.style.color = color;
|
|
448
|
-
floater.style.fontSize = size;
|
|
449
|
-
|
|
450
|
-
this._mount(floater);
|
|
451
|
-
|
|
452
|
-
this._defer(() => this._remove(floater), 2000);
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* Show combo multiplier
|
|
457
|
-
*/
|
|
458
|
-
showCombo(multiplier, x, y) {
|
|
459
|
-
this.showFloatingText(`${multiplier}× COMBO!`, x, y, '#ff6b6b', '32px');
|
|
460
|
-
this.particleSystem.fire(x, y, 20);
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* Show "near miss" indicator
|
|
465
|
-
*/
|
|
466
|
-
showNearMiss(x, y) {
|
|
467
|
-
this.showFloatingText(this.text.nearMiss, x, y, '#45b7d1', '20px');
|
|
468
|
-
this.particleSystem.sparkle(x, y, 15, '#45b7d1');
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Show lucky moment (random multiplier)
|
|
473
|
-
*/
|
|
474
|
-
showLuckyMoment(multiplier, x, y) {
|
|
475
|
-
this.showFloatingText(`LUCKY ${multiplier}×!`, x, y, '#ffd700', '36px');
|
|
476
|
-
this.particleSystem.starBurst(x, y, 12);
|
|
477
|
-
this.particleSystem.confetti(x, y, 25);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Show generic notification
|
|
482
|
-
*/
|
|
483
|
-
showNotification(message, type = 'normal') {
|
|
489
|
+
/** @private */
|
|
490
|
+
_notice(popup) {
|
|
484
491
|
const x = window.innerWidth / 2;
|
|
485
492
|
const y = window.innerHeight / 3;
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
this.
|
|
491
|
-
this.
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
this.
|
|
495
|
-
this.particleSystem.fire(x, y, 20);
|
|
493
|
+
const message = textOf(popup.title);
|
|
494
|
+
|
|
495
|
+
if (popup.tier === 'legendary') {
|
|
496
|
+
this._floatText({ text: message, x, y, color: '#ffd700', size: '36px' });
|
|
497
|
+
this._burst('stars', { x, y }, { count: 12 });
|
|
498
|
+
this._burst('confetti', { x, y }, { count: 25 });
|
|
499
|
+
} else if (popup.tier === 'rare') {
|
|
500
|
+
this._floatText({ text: message, x, y, color: '#ff6b6b', size: '32px' });
|
|
501
|
+
this._burst('fire', { x, y }, { count: 20 });
|
|
496
502
|
} else {
|
|
497
|
-
this.
|
|
503
|
+
this._floatText({ text: message, x, y, color: '#fff', size: '24px' });
|
|
498
504
|
}
|
|
499
505
|
|
|
500
506
|
this._announce(message);
|
|
501
507
|
}
|
|
502
508
|
|
|
503
509
|
/**
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
* @param {Object} data
|
|
507
|
-
* @param {number|string} data.score - Final score
|
|
508
|
-
* @param {Object} [data.metrics] - Label/value pairs listed under the score
|
|
509
|
-
* @param {Function} [data.onReplay] - Replay button handler (default: reload)
|
|
510
|
-
* @param {Function} [data.onExit] - Exit button handler (button hidden if omitted)
|
|
510
|
+
* Float a short text over the page, then remove it.
|
|
511
|
+
* @param {{ text: string, x?: number, y?: number, color?: string }} options
|
|
511
512
|
*/
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
overlay.style.display = 'flex';
|
|
516
|
-
overlay.style.position = 'absolute';
|
|
517
|
-
overlay.style.top = '50%';
|
|
518
|
-
overlay.style.left = '50%';
|
|
519
|
-
overlay.style.transform = 'translate(-50%, -50%)';
|
|
520
|
-
overlay.style.zIndex = '1000';
|
|
521
|
-
|
|
522
|
-
const heading = document.createElement('h1');
|
|
523
|
-
heading.textContent = 'Game Over';
|
|
524
|
-
|
|
525
|
-
const score = document.createElement('div');
|
|
526
|
-
score.className = 'summary-score';
|
|
527
|
-
score.style.cssText = 'font-size: 3rem; font-weight: bold; color: #e06020; margin: 10px 0;';
|
|
528
|
-
score.textContent = data.score;
|
|
529
|
-
|
|
530
|
-
const metrics = document.createElement('div');
|
|
531
|
-
metrics.className = 'summary-metrics';
|
|
532
|
-
metrics.style.cssText = 'width: 100%; margin-bottom: 20px;';
|
|
533
|
-
|
|
534
|
-
for (const [key, value] of Object.entries(data.metrics || {})) {
|
|
535
|
-
const row = document.createElement('div');
|
|
536
|
-
row.style.cssText = 'display:flex;justify-content:space-between;width:100%';
|
|
537
|
-
|
|
538
|
-
const label = document.createElement('span');
|
|
539
|
-
label.textContent = `${key}:`;
|
|
540
|
-
|
|
541
|
-
const amount = document.createElement('strong');
|
|
542
|
-
amount.textContent = value;
|
|
543
|
-
|
|
544
|
-
row.append(label, amount);
|
|
545
|
-
metrics.appendChild(row);
|
|
546
|
-
}
|
|
513
|
+
text(options) {
|
|
514
|
+
this._floatText({ ...options });
|
|
515
|
+
}
|
|
547
516
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
if (data.onExit) {
|
|
559
|
-
const exit = document.createElement('button');
|
|
560
|
-
exit.className = 'btn';
|
|
561
|
-
exit.textContent = 'Exit';
|
|
562
|
-
exit.style.cssText = 'margin-top: 10px; background: #543847;';
|
|
563
|
-
exit.addEventListener('click', data.onExit);
|
|
564
|
-
overlay.appendChild(exit);
|
|
565
|
-
}
|
|
517
|
+
/** @private */
|
|
518
|
+
_floatText({ text, x, y, color = '#ffd700', size = '24px' }) {
|
|
519
|
+
if (!this.container) return;
|
|
520
|
+
const floater = document.createElement('div');
|
|
521
|
+
floater.className = 'floating-text';
|
|
522
|
+
floater.textContent = textOf(text);
|
|
523
|
+
floater.style.left = (Number.isFinite(x) ? x : window.innerWidth / 2) + 'px';
|
|
524
|
+
floater.style.top = (Number.isFinite(y) ? y : window.innerHeight / 3) + 'px';
|
|
525
|
+
floater.style.color = color;
|
|
526
|
+
floater.style.fontSize = size;
|
|
566
527
|
|
|
567
|
-
this._mount(
|
|
568
|
-
|
|
528
|
+
this._mount(floater);
|
|
529
|
+
|
|
530
|
+
this._defer(() => this._remove(floater), 2000);
|
|
569
531
|
}
|
|
570
532
|
|
|
571
533
|
/**
|
|
@@ -592,6 +554,8 @@ export class GameUI {
|
|
|
592
554
|
this._detached = [];
|
|
593
555
|
|
|
594
556
|
this.container?.remove();
|
|
557
|
+
this._style?.remove();
|
|
558
|
+
this._style = null;
|
|
595
559
|
this.container = null;
|
|
596
560
|
this.liveRegion = null;
|
|
597
561
|
this.xpBar = null;
|
package/src/games.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Game feedback as a plugin: combo, near miss, lucky moment and the run
|
|
3
|
+
* summary, drawn through the ui and particles parts, plus the jump, score and
|
|
4
|
+
* gameover sounds. Nothing here runs at import time.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const TEXT = {
|
|
8
|
+
combo: '{n}× COMBO!',
|
|
9
|
+
nearMiss: 'CLOSE CALL!',
|
|
10
|
+
luckyMoment: 'LUCKY {n}×!',
|
|
11
|
+
gameOver: 'Game Over',
|
|
12
|
+
playAgain: 'Play Again',
|
|
13
|
+
exit: 'Exit'
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
|
|
17
|
+
|
|
18
|
+
const SOUNDS = {
|
|
19
|
+
jump: (s) => s.playTone(400, 0.1, 'square', 0.2),
|
|
20
|
+
score: (s, later) => {
|
|
21
|
+
s.playTone(800, 0.15, 'sine', 0.25);
|
|
22
|
+
later(() => s.playTone(1000, 0.15, 'sine', 0.25), 50);
|
|
23
|
+
},
|
|
24
|
+
gameover: (s, later) => {
|
|
25
|
+
s.playTone(300, 0.2, 'sawtooth', 0.3);
|
|
26
|
+
later(() => s.playTone(200, 0.2, 'sawtooth', 0.3), 100);
|
|
27
|
+
later(() => s.playTone(150, 0.3, 'sawtooth', 0.3), 200);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const el = (tag, text, css) => {
|
|
32
|
+
const node = document.createElement(tag);
|
|
33
|
+
if (text !== undefined) node.textContent = text;
|
|
34
|
+
if (css) node.style.cssText = css;
|
|
35
|
+
return node;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {Object} [options]
|
|
40
|
+
* @param {{combo?: string, nearMiss?: string, luckyMoment?: string,
|
|
41
|
+
* gameOver?: string, playAgain?: string, exit?: string}} [options.text]
|
|
42
|
+
* Replaces the built-in strings. `{n}` is the multiplier. An empty string
|
|
43
|
+
* hides that text.
|
|
44
|
+
*/
|
|
45
|
+
export function gameFeedback(options) {
|
|
46
|
+
const strings = { ...TEXT };
|
|
47
|
+
const custom = options !== null && typeof options === 'object' && hasOwn(options, 'text') ? options.text : null;
|
|
48
|
+
for (const key of Object.keys(TEXT)) {
|
|
49
|
+
if (custom !== null && typeof custom === 'object' && hasOwn(custom, key) && typeof custom[key] === 'string') {
|
|
50
|
+
strings[key] = custom[key];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let ctx = null;
|
|
55
|
+
const overlays = new Set();
|
|
56
|
+
|
|
57
|
+
const text = (key, n, x, y, color, size) => {
|
|
58
|
+
const label = strings[key].replace('{n}', () => String(n));
|
|
59
|
+
if (label !== '') ctx.parts.ui.text({ text: label, x, y, color, size });
|
|
60
|
+
};
|
|
61
|
+
const burst = (kind, x, y, opts) => ctx.parts.particles?.burst(kind, { x, y }, opts);
|
|
62
|
+
|
|
63
|
+
const feedback = {
|
|
64
|
+
name: 'games',
|
|
65
|
+
version: '1.0.0',
|
|
66
|
+
api: 3,
|
|
67
|
+
requires: ['ui'],
|
|
68
|
+
|
|
69
|
+
combo(multiplier, x, y) {
|
|
70
|
+
if (!ctx) return;
|
|
71
|
+
text('combo', multiplier, x, y, '#ff6b6b', '32px');
|
|
72
|
+
burst('fire', x, y, { count: 20 });
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
nearMiss(x, y) {
|
|
76
|
+
if (!ctx) return;
|
|
77
|
+
text('nearMiss', '', x, y, '#45b7d1', '20px');
|
|
78
|
+
burst('sparkle', x, y, { count: 15, color: '#45b7d1' });
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
luckyMoment(multiplier, x, y) {
|
|
82
|
+
if (!ctx) return;
|
|
83
|
+
text('luckyMoment', multiplier, x, y, '#ffd700', '36px');
|
|
84
|
+
burst('stars', x, y, { count: 12 });
|
|
85
|
+
burst('confetti', x, y, { count: 25 });
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @param {{ score: number|string, metrics?: Object, onReplay?: Function, onExit?: Function }} data
|
|
90
|
+
* @returns {HTMLElement|null} The overlay, removed when the plugin is disposed.
|
|
91
|
+
*/
|
|
92
|
+
summary(data) {
|
|
93
|
+
if (!ctx || !ctx.env.hasDom || data === null || typeof data !== 'object') return null;
|
|
94
|
+
const box = el('div', undefined, 'display:flex;flex-direction:column;align-items:center;position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);z-index:1000');
|
|
95
|
+
box.className = 'message-box';
|
|
96
|
+
if (strings.gameOver !== '') box.append(el('h1', strings.gameOver));
|
|
97
|
+
box.append(el('div', data.score, 'font-size:3rem;font-weight:bold;color:#e06020;margin:10px 0'));
|
|
98
|
+
|
|
99
|
+
const metrics = el('div', undefined, 'width:100%;margin-bottom:20px');
|
|
100
|
+
const list = data.metrics !== null && typeof data.metrics === 'object' ? data.metrics : {};
|
|
101
|
+
for (const [label, value] of Object.entries(list)) {
|
|
102
|
+
const row = el('div', undefined, 'display:flex;justify-content:space-between;width:100%');
|
|
103
|
+
row.append(el('span', `${label}:`), el('strong', value));
|
|
104
|
+
metrics.append(row);
|
|
105
|
+
}
|
|
106
|
+
box.append(metrics);
|
|
107
|
+
|
|
108
|
+
// Listeners, not onclick attributes, so a script-src CSP allows it.
|
|
109
|
+
const button = (label, handler) => {
|
|
110
|
+
const b = el('button', label);
|
|
111
|
+
b.className = 'btn';
|
|
112
|
+
b.addEventListener('click', handler);
|
|
113
|
+
box.append(b);
|
|
114
|
+
};
|
|
115
|
+
if (strings.playAgain !== '') {
|
|
116
|
+
button(strings.playAgain, typeof data.onReplay === 'function' ? data.onReplay : () => window.location.reload());
|
|
117
|
+
}
|
|
118
|
+
if (typeof data.onExit === 'function' && strings.exit !== '') button(strings.exit, data.onExit);
|
|
119
|
+
|
|
120
|
+
document.body.append(box);
|
|
121
|
+
overlays.add(box);
|
|
122
|
+
return box;
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
install(context) {
|
|
126
|
+
ctx = context;
|
|
127
|
+
const removers = Object.keys(SOUNDS).map((key) => ctx.parts.sound?.register?.(key, SOUNDS[key]));
|
|
128
|
+
context.expose(feedback);
|
|
129
|
+
return () => {
|
|
130
|
+
for (const remove of removers) remove?.();
|
|
131
|
+
for (const box of overlays) box.remove();
|
|
132
|
+
overlays.clear();
|
|
133
|
+
ctx = null;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
return feedback;
|
|
138
|
+
}
|