@wasm-gaming/blastem-wasm 0.1.3 → 0.2.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.
@@ -35,6 +35,7 @@ import {
35
35
  type EngineSDKConstructor,
36
36
  type EventsOf,
37
37
  } from '@wasm-gaming/engine-specs';
38
+ import { controlTables, keyCodeOf, remapPad, type BlastemControls, type BlastemControlTables } from './blastem.controls.js';
38
39
  import {
39
40
  BLASTEM_SPEC,
40
41
  BLASTEM_VIDEO,
@@ -54,6 +55,7 @@ export type {
54
55
  BlastemStorage,
55
56
  } from './blastem.options.js';
56
57
  export { BLASTEM_SPEC, BLASTEM_VIDEO } from './blastem.options.js';
58
+ export { BLASTEM_CONTROLS, controlTables, remapPad, type BlastemControls, type BlastemControlSpec } from './blastem.controls.js';
57
59
 
58
60
  const ENGINE_ID = 'blastem';
59
61
  const DEFAULT_STORAGE_NAMESPACE = 'default';
@@ -207,6 +209,27 @@ export class BlastemPlay extends EnginePlayBase<BlastemPayloads> {
207
209
  if (document.visibilityState === 'hidden') void this.flushSaves();
208
210
  };
209
211
 
212
+ #controls: BlastemControls = {};
213
+ #tables: BlastemControlTables = controlTables();
214
+ #getGamepads: (() => (Gamepad | null)[]) | null = null;
215
+
216
+ /**
217
+ * The keyboard, remapped (see `blastem.controls.ts`): a physical key bound to a control
218
+ * reaches BlastEm as that control's default key; a default key no control has any more is
219
+ * swallowed. Real presses only: the ones replayed here pass through.
220
+ */
221
+ #onKey = (event: KeyboardEvent): void => {
222
+ if (!event.isTrusted || this.#pauseOwner || !this.#module) return;
223
+ const control = this.#tables.keys.get(event.code);
224
+ if (!control && !this.#tables.blocked.has(event.code)) return;
225
+ event.preventDefault();
226
+ event.stopImmediatePropagation();
227
+ if (!control || event.repeat) return;
228
+ const replay = new KeyboardEvent(event.type, { code: control.code, key: control.key, bubbles: true, cancelable: true });
229
+ for (const name of ['keyCode', 'which']) Object.defineProperty(replay, name, { get: () => keyCodeOf(control) });
230
+ (this.#canvas ?? window).dispatchEvent(replay);
231
+ };
232
+
210
233
  constructor(init?: EngineInit<BlastemPayloads>, events?: EventEmitter<EventsOf<BlastemPayloads>>) {
211
234
  super(BLASTEM_SPEC, init, events);
212
235
  }
@@ -402,6 +425,16 @@ export class BlastemPlay extends EnginePlayBase<BlastemPayloads> {
402
425
  return this.#pauseOwner;
403
426
  }
404
427
 
428
+ /**
429
+ * Rebind the Mega Drive pad (ids from {@link BLASTEM_CONTROLS}): player 1's physical keys
430
+ * (`KeyboardEvent.code`) and every pad's buttons (standard mapping). What is left out keeps
431
+ * BlastEm's default. Live.
432
+ */
433
+ setControls(controls: BlastemControls): void {
434
+ this.#controls = controls;
435
+ this.#tables = controlTables(controls);
436
+ }
437
+
405
438
  /**
406
439
  * Freeze emulation and sound. The first caller owns the pause, and only that
407
440
  * owner's {@link resume} lifts it, so an overlay cannot resume a game the host had
@@ -490,6 +523,10 @@ export class BlastemPlay extends EnginePlayBase<BlastemPayloads> {
490
523
  }
491
524
 
492
525
  protected override async close(): Promise<void> {
526
+ window.removeEventListener('keydown', this.#onKey, true);
527
+ window.removeEventListener('keyup', this.#onKey, true);
528
+ if (this.#getGamepads) (navigator as Navigator & { getGamepads: () => (Gamepad | null)[] }).getGamepads = this.#getGamepads;
529
+ this.#getGamepads = null;
493
530
  if (this.#saveTimer) clearInterval(this.#saveTimer);
494
531
  this.#saveTimer = null;
495
532
  document.removeEventListener('visibilitychange', this.#onHidden);
@@ -570,6 +607,18 @@ export class BlastemPlay extends EnginePlayBase<BlastemPayloads> {
570
607
  const args: string[] = [];
571
608
  if (config.region && config.region !== 'auto') args.push('-r', REGION_FLAG[config.region]);
572
609
  args.push(`${ROM_DIR}/${name}`, String(BLASTEM_VIDEO.width), String(BLASTEM_VIDEO.height));
610
+ // The player's bindings sit in front of SDL: keys on the window (capture), pads through
611
+ // a wrapped navigator.getGamepads().
612
+ this.#tables = controlTables(this.#controls);
613
+ window.addEventListener('keydown', this.#onKey, true);
614
+ window.addEventListener('keyup', this.#onKey, true);
615
+ const nav = navigator as Navigator & { getGamepads?: () => (Gamepad | null)[] };
616
+ if (nav.getGamepads) {
617
+ const original = nav.getGamepads.bind(nav);
618
+ this.#getGamepads = nav.getGamepads;
619
+ nav.getGamepads = () =>
620
+ original().map((pad) => (pad ? (remapPad(pad, this.#tables.pad) as unknown as Gamepad) : pad));
621
+ }
573
622
  Module.callMain(args);
574
623
 
575
624
  if (this.#persist) {
@@ -666,7 +715,7 @@ export class BlastemPlay extends EnginePlayBase<BlastemPayloads> {
666
715
  export class BlastemSDK extends EngineSDKBase<BlastemPayloads> {
667
716
  static readonly id = ENGINE_ID;
668
717
  /** Kept in step with package.json by `node scripts/sync-version.mjs`. */
669
- static readonly version = '0.1.3';
718
+ static readonly version = '0.2.0';
670
719
  static readonly name = 'BlastEm';
671
720
  static readonly description =
672
721
  'BlastEm, the Sega Mega Drive / Genesis emulator by Michael Pavone, compiled to WebAssembly. Ships no games: the player provides the ROM, and it never leaves their browser.';