All files / player-engine/src/course/nonstop scroll-speed.ts

100% Statements 28/28
96.55% Branches 28/29
100% Functions 5/5
100% Lines 21/21

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              262x                                     744x 744x 8x 9x 8x 8x 6x 6x 1x 1x   744x 744x 744x 5x 744x 9x   744x 3x 4x   744x    
/** A SCROLL or SPEED keyframe on the stitched clock. */
export interface NonstopKeyframe {
  seconds: number;
  value: number;
}
 
/** How long before a stage's window ends a SPEED hold keyframe sits, so the ramp to the next stage does not show. */
const SPEED_HOLD_SECONDS = 0.001;
 
/**
 * One stage's SCROLL (`interpolate: false`) or SPEED (`interpolate: true`) keyframes, laid on the stitched clock for
 * its window, so the stage scrolls as it would alone:
 *
 * - at the window's start, the value in force there, when the stage changed it before or an earlier stage changed it
 *   (`carried`; a stage without keyframes then resets to 1);
 * - its own keyframes inside the window;
 * - for SPEED, which ramps linearly to the next keyframe, the value in force just before the window ends, so the
 *   stitched chart does not ramp into the next stage's value across the seam.
 *
 * `origin` is the stitched time of the stage's own time zero, where SPEED starts at 1 before its first keyframe.
 */
export function placeNonstopKeyframes(
  own: readonly NonstopKeyframe[],
  window: { start: number; end: number },
  options: { interpolate: boolean; carried: boolean; origin: number },
): NonstopKeyframe[] {
  const points = options.interpolate ? [{ seconds: options.origin, value: 1 }, ...own] : own;
  const valueAt = (seconds: number): number => {
    let index = -1;
    for (let next = 0; next < points.length && points[next]!.seconds <= seconds; next += 1) index = next;
    const current = points[index];
    if (current === undefined) return 1;
    const following = points[index + 1];
    if (!options.interpolate || following === undefined || following.seconds <= current.seconds) return current.value;
    const ratio = (seconds - current.seconds) / (following.seconds - current.seconds);
    return current.value + (following.value - current.value) * ratio;
  };
  const placed: NonstopKeyframe[] = [];
  const hasStart = Number.isFinite(window.start);
  if (hasStart && (options.carried || own.some((keyframe) => keyframe.seconds <= window.start)))
    placed.push({ seconds: window.start, value: valueAt(window.start) });
  placed.push(
    ...own.filter((keyframe) => (!hasStart || keyframe.seconds > window.start) && keyframe.seconds < window.end),
  );
  if (options.interpolate && Number.isFinite(window.end) && (placed.length > 0 || own.length > 0)) {
    const seconds = Math.max(hasStart ? window.start : 0, window.end - SPEED_HOLD_SECONDS);
    Eif (placed.every((keyframe) => keyframe.seconds < seconds)) placed.push({ seconds, value: valueAt(seconds) });
  }
  return placed;
}