All files / player-browser/src/view/beatoraja/play scroll-placement.ts

100% Statements 16/16
85.71% Branches 6/7
100% Functions 5/5
100% Lines 14/14

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                              17x 17x 17x 17x 14x   3x                       2x 2x                                                     42x 42x 42x                     42x 149x   2x    
// Where the beatoraja play skin puts notes and bar lines on the lane, and which of them it draws: the same distance
// integrator and draw window the shared playfield uses (`chart/note-draw-window.ts`). A chart with `#SCROLL` /
// `#SPEED` (bmson `scroll_events` included) draws by beatoraja's `LaneRenderer` rules, so SCROLL gimmicks show up as they
// do in beatoraja; every other chart draws by LR2's, as in the other skins — a skin is a display choice, not a ruleset.
 
import {
  createBeatResolver,
  createScrollTimeline,
  createSpeedTimeline,
  resolveChart,
  type TempoReversalPoint,
} from '@be-music/chart-model';
import type { BeMusicJSON } from '@be-music/chart-source';
import { createScrollDistanceMapper, type ScrollDistanceMapperLike } from '@be-music/player-engine/display';
import {
  changesScrollRate,
  collectResolvedLaneTimelineBeats,
  resolveNoteDrawRules,
  resolveNoteDrawWindow,
  resolveReversedDrawClock,
  type NoteDrawWindow,
} from '../../../chart/note-draw-window.ts';
 
/**
 * The chart's `#SCROLL` / `#SPEED` distance integrator, or `undefined` when it has neither (or only points at 1).
 * `ignoreSpeed` leaves `#SPEED` out, as beatoraja's `LaneRenderer` does while Visual CONSTANT is drawn.
 */
export function createBeatorajaScrollMapper(
  chart: BeMusicJSON,
  options: { readonly ignoreSpeed?: boolean } = {},
): ScrollDistanceMapperLike | undefined {
  const beatResolver = createBeatResolver(chart);
  const scroll = createScrollTimeline(chart, beatResolver);
  const speed = options.ignoreSpeed === true ? [] : createSpeedTimeline(chart, beatResolver);
  if (!changesScrollRate(scroll, speed)) {
    return undefined;
  }
  return createScrollDistanceMapper(scroll, speed, { invalidDistance: 0 });
}
 
/** The chart's lane timelines as beatoraja's `LaneRenderer` walks them. */
export interface BeatorajaLaneTimelines {
  /** Beats of the lane timelines, for the last timeline drawn past the lane top. */
  beats: Float64Array;
  /** Chart time of a beat, in seconds, for hiding bar lines whose time has come. */
  beatToSeconds: (beat: number) => number;
}
 
export function createBeatorajaLaneTimelines(chart: BeMusicJSON): BeatorajaLaneTimelines {
  const resolved = resolveChart(chart);
  return { beats: collectResolvedLaneTimelineBeats(resolved), beatToSeconds: resolved.timing.beatToSeconds };
}
 
export interface BeatorajaScrollPlacementInput {
  /** The beat the lane scrolls from: the mirrored display beat past an LR2 negative-BPM reversal. */
  currentBeat: number;
  /** Chart time of the playhead, in seconds. */
  nowSeconds: number;
  /** The chart's LR2 negative-BPM reversal, if any ({@link resolveReversedDrawClock}). */
  reversal?: TempoReversalPoint;
  mapper: ScrollDistanceMapperLike | undefined;
  /** The lane's height in beats of scroll distance (lane pixels over pixels per beat). */
  laneBeats: number;
  /** Beats of the chart's lane timelines ({@link createBeatorajaLaneTimelines}). */
  timelineBeats?: ArrayLike<number>;
  /** Bottom edge of the stage, in the lanes' coordinates (y grows downward). */
  stageBottomY: number;
}
 
export interface BeatorajaScrollPlacement {
  /** beatoraja's `LaneRenderer` window when the chart scrolls, LR2's otherwise ({@link resolveNoteDrawRules}). */
  window: NoteDrawWindow;
  /** Lane distance from the playhead to `beat`, in beats at scroll rate 1. */
  distance: (beat: number) => number;
}
 
export function resolveBeatorajaScrollPlacement(input: BeatorajaScrollPlacementInput): BeatorajaScrollPlacement {
  const { currentBeat, nowSeconds, reversal, mapper, laneBeats, timelineBeats, stageBottomY } = input;
  const clock = resolveReversedDrawClock(reversal, nowSeconds, currentBeat);
  const window = resolveNoteDrawWindow({
    rules: resolveNoteDrawRules(mapper !== undefined),
    currentBeat,
    nowSeconds: clock.nowSeconds,
    reachedBeat: clock.reachedBeat,
    mapper,
    laneBeats,
    timelineBeats,
    overscanBeats: 0,
    stageBottomY,
  });
  if (mapper === undefined) {
    return { window, distance: (beat) => beat - currentBeat };
  }
  return { window, distance: (beat) => mapper.distanceBetween(currentBeat, beat) };
}