All files / player-browser/src/skins/native play.ts

0% Statements 0/17
0% Branches 0/12
0% Functions 0/8
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                               
import type {
  BeMusicDOMGameplayFrame,
  BeMusicDOMGameplayActions,
  BeMusicDOMGameplayScreen,
} from '@be-music/player-skin-sdk';
import { mountElement } from './mount.ts';
 
export function mountGameplay(container: HTMLElement, actions: BeMusicDOMGameplayActions): BeMusicDOMGameplayScreen {
  let previous: BeMusicDOMGameplayFrame | undefined;
  let lastUpdateMs = -Infinity;
  // Mounted first so the HUD stays above the lanes.
  const playfield = mountElement(container, 'be-music-playfield', {});
  const hud = mountElement<BeMusicDOMGameplayFrame>(
    container,
    'be-music-gameplay-hud',
    {
      'pause-request': () => actions.pause(),
      'retry-request': () => actions.restart(),
      'quit-request': () => actions.quit(),
    },
    (frame) => {
      const nowMs = frame.runtime.nowMs ?? frame.seconds * 1000;
      if (
        previous &&
        previous.stage === frame.stage &&
        previous.layout.orientation === frame.layout.orientation &&
        previous.layout.stage.width === frame.layout.stage.width &&
        previous.layout.stage.height === frame.layout.stage.height &&
        previous.state.view === frame.state.view &&
        nowMs >= lastUpdateMs &&
        nowMs - lastUpdateMs < 80
      )
        return false;
      previous = frame;
      lastUpdateMs = nowMs;
      return true;
    },
  );
  return {
    update: (frame) => hud.update(frame),
    drawPlayfield: (frame) => playfield.update(frame),
    dispose() {
      hud.dispose();
      playfield.dispose();
    },
  };
}