All files / player-engine/src/course/nonstop bmson-bga.ts

100% Statements 26/26
100% Branches 22/22
100% Functions 7/7
100% Lines 20/20

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 49 50 51 52 53 54                      416x 416x 67x 66x 416x   416x 44x 17x   416x 416x 416x 416x 461x 461x     96x 96x 96x             461x 460x                       415x    
import { buildBmsonBGAEventTimeline, type BmsonBGAEventCue, type ResolvedChart } from '@be-music/chart-model';
import type { NonstopStagePlacement } from './plan.ts';
 
/** Place resolved image references and authored animation time on the course clock. */
export function placeNonstopBmsonBGA(
  chart: ResolvedChart,
  placement: NonstopStagePlacement,
  window: { start: number; end: number },
  stage: number,
  bmp: (key: string) => string | undefined,
): BmsonBGAEventCue[] | undefined {
  const place = (seconds: number) => placement.offsetSeconds + seconds / placement.rate;
  const source = buildBmsonBGAEventTimeline(chart.source, chart.timing, { includeOrdinaryLayerClears: true })
    .filter((cue) => cue.seconds <= (chart.reversal?.seconds ?? Infinity))
    .map((cue) => ({ ...cue, seconds: place(cue.seconds) }));
  const start = Math.max(0, window.start);
  let initial: BmsonBGAEventCue | undefined;
  for (const cue of source) {
    if (cue.seconds > start) break;
    initial = cue;
  }
  const clear: BmsonBGAEventCue = { seconds: start, plane: 'layer', condition: 'always', interval: 0, sequences: [] };
  const selected = [initial ?? clear, ...source.filter((cue) => cue.seconds > start && cue.seconds < window.end)];
  const result: BmsonBGAEventCue[] = [];
  for (const cue of selected) {
    let failed = false;
    const sequences = cue.sequences.map((sequence) => ({
      ...sequence,
      frames: sequence.frames.map((frame) => {
        const key = frame.key === undefined ? undefined : bmp(frame.key);
        if (frame.key !== undefined && key === undefined) failed = true;
        return {
          ...frame,
          elapsedSeconds: frame.elapsedSeconds / placement.rate,
          ...(key !== undefined ? { key } : {}),
        };
      }),
    }));
    if (failed) return undefined;
    result.push({
      ...cue,
      seconds: Math.max(start, cue.seconds),
      animationStartSeconds: cue.seconds,
      interval: cue.interval / placement.rate,
      sequences,
      stage,
      playbackRate: placement.rate,
      ...(Number.isFinite(window.end) ? { endSeconds: window.end } : {}),
      ...(chart.reversal ? { freezeSeconds: place(chart.reversal.seconds) } : {}),
    });
  }
  return result;
}