All files / player-terminal/src/tui chart-note-visual.ts

100% Statements 10/10
94.44% Branches 17/18
100% Functions 3/3
100% Lines 10/10

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                                    3x       14x                             172x 163x 9x             172x 2x       7x 7x                     414x    
/**
 * Terminal presentation of the chart-authored display families (#843, #931). A character grid has no opacity, colour
 * cycle, rotation or sub-cell size, so a note shows while its HIDDEN opacity is at least one half, CHANGE-HS and the Y
 * wave (`YWAVE*`, `XYWAVE*`) move it along the lane, and the CRAZY size, X wave, 3D wave, STAR, SPIN and CLONE effects
 * are not drawn (they change nothing but the browser's sprites). The playfield-level options are `tui.ts`'s: REVERSE
 * flips the grid, HIDJUDGE blanks the judge line and DARK hides the BGA.
 */
 
import { resolveChartNoteVisual, type ChartOptionState } from '@be-music/chart-model';
 
export interface TUIChartNotePlacement {
  /** Approach distance after CHANGE-HS and the Y wave, in the grid's scroll-window units. */
  distance: number;
  /** False when HIDDEN hides the note in the grid. */
  visible: boolean;
}
 
/** Opacity at or above which a terminal cell still shows a note. */
export const TUI_CHART_HIDDEN_VISIBLE_ALPHA = 0.5;
 
/** True when `state` moves or hides notes on the grid: HIDDEN (not M-SUDDEN), CHANGE-HS or a Y wave. */
function movesGridNotes(state: ChartOptionState): boolean {
  return (
    (state.hidden !== undefined && state.hidden !== 'M-SUDDEN') ||
    state.changeHS !== undefined ||
    state.crazyWave === 'YWAVE' ||
    state.crazyWave === 'YWAVE2' ||
    state.crazyWave === 'XYWAVE' ||
    state.crazyWave === 'XYWAVE2'
  );
}
 
/** Places one note's head (or a long note's tail) on the grid. Identity without HIDDEN, CHANGE-HS or a Y wave. */
export function placeTUIChartNote(
  state: ChartOptionState | undefined,
  input: { distance: number; windowBeats: number; channel: string; beat: number; seconds: number; combo?: number },
): TUIChartNotePlacement {
  if (state === undefined || !movesGridNotes(state) || !(input.windowBeats > 0))
    return { distance: input.distance, visible: true };
  const note = {
    channel: input.channel,
    beat: input.beat,
    seconds: input.seconds,
    ...(input.combo !== undefined ? { combo: input.combo } : {}),
  };
  // A note past the line (a held long note's head) reads as on the line.
  if (!Number.isFinite(input.distance))
    return {
      distance: input.distance,
      visible: resolveChartNoteVisual(state, { ...note, progress: 0 }).alpha >= TUI_CHART_HIDDEN_VISIBLE_ALPHA,
    };
  const visual = resolveChartNoteVisual(state, { ...note, progress: input.distance / input.windowBeats });
  return {
    distance: visual.progress * input.windowBeats,
    visible: visual.alpha >= TUI_CHART_HIDDEN_VISIBLE_ALPHA,
  };
}
 
/**
 * The grid's rows under REVERSE: row `row` of `rowCount` (0 at the top, the judge row at the bottom) moves to the
 * mirrored row, so notes rise from the bottom to a judge row at the top. Identity otherwise.
 */
export function resolveTUIReversedRow(row: number, rowCount: number, reverse: boolean): number {
  return reverse ? rowCount - 1 - row : row;
}