All files / player-ui/src/playfield parts.ts

95.69% Statements 89/93
86.66% Branches 65/75
91.66% Functions 11/12
98.71% Lines 77/78

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209                              49x   49x 49x   49x   49x 49x   49x 49x   49x 49x 49x 49x 49x                       13x 13x 13x 13x 13x 13x 13x 13x 13x 36x 36x   13x 26x 13x 13x 13x 24x 36x   24x 24x     13x 17x 13x 13x 13x 13x 9x 8x 8x 9x                                   13x 26x 26x 26x 2x 2x 2x   2x               13x                                                                                             13x 13x 2x 6x 2x 2x 3x 2x 2x 2x 2x 2x 6x 4x 4x   2x 2x                   2x         8x         8x 6x 5x 4x       2x       91x    
import {
  findWhiteLaneWidth,
  flashingGreatColor,
  isFlashingGreat,
  judgeDisplayWord,
  resolveJudgeWordY,
  resolveLanePlayLabel,
  resolveLaneRuns,
  type BeMusicGameplayFrame,
  type BeMusicRect,
  type BeMusicSide,
} from '@be-music/player-skin-sdk';
import type { BeMusicJudgmentWord, BeMusicLaneCoverArea, BeMusicPlayfieldParts } from '../models.ts';
 
/** Room a bomb's ring needs around the judge line: its largest radius plus half its stroke. */
const BOMB_REACH = 26;
/** How far above the judge line the judgment word's baseline sits, and the band the word and its combo take. */
const JUDGMENT_OFFSET = 90;
const JUDGMENT_ASCENT = 22;
/** Room above the word for the play label and, above that, the judge detail. */
const JUDGMENT_HEADROOM = 44;
/** The judge detail's colours, and the names the keys' and the scratch's lead with when they are shown apart. */
const JUDGE_DETAIL_COLORS = { fast: '#5b8cff', slow: '#ff6b7a', just: '#e5efff' } as const;
const JUDGE_DETAIL_LANES = { keys: 'KEY', scratch: 'SCR' } as const;
/** Baseline of the combo under the word, then the room its glyphs need below that. */
const COMBO_OFFSET = 24;
const COMBO_DESCENT = 6;
/** Colours a PGREAT's GREAT steps through. A steady GREAT keeps {@link JUDGMENT_COLOR}. */
const FLASHING_GREAT = [0xffffff, 0x5b8cff, 0xffd84d] as const;
const JUDGMENT_COLOR = '#e5efff';
const GOOD_COLOR = '#88b9ff';
const MISS_COLOR = '#ff5c6c';
const JUDGE_LINE_HEIGHT = 2;
 
/**
 * Splits a gameplay frame into the playfield's canvas components and the stage rect each one covers, so each can own a
 * canvas no larger than its part of the stage. Coordinates stay in stage design pixels.
 */
export function resolvePlayfieldParts(
  frame: Pick<
    BeMusicGameplayFrame,
    'layout' | 'lanes' | 'notes' | 'longNotes' | 'mines' | 'measureLines' | 'bombs' | 'bga' | 'runtime' | 'nowMs'
  >,
): BeMusicPlayfieldParts {
  const { layout, lanes } = frame;
  const { playfield, stage } = layout;
  const runs = resolveLaneRuns(lanes);
  const left = runs.length > 0 ? runs[0]!.left : playfield.left;
  const right = runs.length > 0 ? runs[runs.length - 1]!.right : playfield.right;
  const width = Math.max(0, right - left);
  let top = Infinity;
  let bottom = -Infinity;
  for (const lane of lanes) {
    top = Math.min(top, lane.top);
    bottom = Math.max(bottom, lane.bottom);
  }
  const laneRect = lanes.length > 0 ? rect(left, top, width, bottom - top) : rect(left, playfield.top, 0, 0);
  const lineYs = Object.values(playfield.sides).map((side) => side?.judgementY ?? playfield.judgementY);
  const minLineY = Math.min(playfield.judgementY, ...lineYs);
  const maxLineY = Math.max(playfield.judgementY, ...lineYs);
  const lineRuns = runs.map((run) => {
    const side = Object.values(playfield.sides).find(
      (side) => side && run.left >= side.left && run.right <= side.right,
    );
    const y = side?.judgementY ?? playfield.judgementY;
    return y === playfield.judgementY ? run : { ...run, y };
  });
  // The word's baseline under a judge line at `lineY`, moved by the player's judgment position and kept in the lanes.
  const wordY = (lineY: number): number =>
    resolveJudgeWordY(lineY - JUDGMENT_OFFSET, { ...playfield, judgementY: lineY });
  const judgmentY = wordY(playfield.judgementY);
  const judgeOffsetY = judgmentY - (playfield.judgementY - JUDGMENT_OFFSET);
  const words: BeMusicJudgmentWord[] = [];
  for (const side of frame.runtime.judgeSides ?? []) {
    if (!side.judge) continue;
    const bounds = playfield.sides[side.side];
    const combo = side.combo ?? 0;
    words.push({
      x: bounds ? (bounds.left + bounds.right) / 2 : playfield.centerX,
      text: judgeDisplayWord(side.judge),
      ...(bounds?.judgementY !== undefined && bounds.judgementY !== playfield.judgementY
        ? { y: wordY(bounds.judgementY) }
        : {}),
      color: judgmentColor(side.judge, frame.nowMs),
      ...(keepsCombo(side.judge) && combo > 0 ? { combo } : {}),
      ...(side.details?.length
        ? {
            details: side.details.map((detail) => ({
              text: detail.lane === undefined ? detail.text : `${JUDGE_DETAIL_LANES[detail.lane]} ${detail.text}`,
              color: JUDGE_DETAIL_COLORS[detail.tone],
            })),
          }
        : {}),
    });
  }
  for (const side of ['1P', '2P'] as const) {
    const bounds = playfield.sides[side];
    const mode = resolveLanePlayLabel(frame.runtime, side);
    if (!bounds || !mode) continue;
    const x = (bounds.left + bounds.right) / 2;
    const word = words.find((word) => word.x === x);
    Iif (word) words[words.indexOf(word)] = { ...word, mode };
    else
      words.push({
        x,
        y: wordY(bounds.judgementY ?? playfield.judgementY),
        text: '',
        color: JUDGMENT_COLOR,
        mode,
      });
  }
  return {
    stage: { width: stage.width, height: stage.height },
    bga: {
      rect: layout.bga ? rect(layout.bga.x, layout.bga.y, layout.bga.w, layout.bga.h) : rect(0, 0, 0, 0),
      data: { frame: frame.bga, shown: frame.runtime.hasBGA === true },
    },
    lanes: { rect: laneRect, data: { lanes } },
    // Notes may scroll below the judge line under a negative `#SCROLL`, down to the stage's bottom edge.
    notes: {
      rect: rect(left, playfield.top, width, stage.height - playfield.top),
      data: {
        notes: frame.notes,
        longNotes: frame.longNotes,
        mines: frame.mines,
        measureLines: frame.measureLines,
        nowMs: frame.nowMs,
        whiteLaneWidth: findWhiteLaneWidth(frame.lanes),
      },
    },
    laneCovers: {
      rect: rect(left, laneRect.y, width, Math.max(bottom, playfield.judgementY) - laneRect.y),
      data: { areas: resolveLaneCoverAreas(frame) },
    },
    judgeLine: {
      rect: rect(left, minLineY, width, maxLineY - minLineY + JUDGE_LINE_HEIGHT),
      data: { runs: lineRuns, y: playfield.judgementY },
    },
    bombs: {
      rect: rect(left, minLineY - BOMB_REACH, width, maxLineY - minLineY + BOMB_REACH * 2),
      data: { bombs: frame.bombs },
    },
    judgmentDisplay: {
      rect: rect(
        left,
        minLineY - JUDGMENT_OFFSET - JUDGMENT_ASCENT - JUDGMENT_HEADROOM + judgeOffsetY,
        width,
        maxLineY - minLineY + JUDGMENT_ASCENT + COMBO_OFFSET + COMBO_DESCENT + JUDGMENT_HEADROOM,
      ),
      data: { words, y: judgmentY },
    },
  };
}
 
/** Each covered side's lane area: its horizontal extent from the layout, its height from that side's lanes. */
function resolveLaneCoverAreas(
  frame: Pick<BeMusicGameplayFrame, 'layout' | 'lanes' | 'runtime'>,
): BeMusicLaneCoverArea[] {
  const covers = frame.runtime.laneCovers ?? [];
  if (covers.length === 0) return [];
  const sideOf = new Map<string, BeMusicSide>();
  for (const lane of frame.layout.lanes) Eif (lane.channel) sideOf.set(lane.channel, lane.side);
  const areas: BeMusicLaneCoverArea[] = [];
  for (const cover of covers) {
    if (!(cover.sudden > 0) && !(cover.hidden > 0) && !(cover.lift > 0)) continue;
    const bounds = frame.layout.playfield.sides[cover.side];
    Iif (!bounds) continue;
    let top = Infinity;
    let bottom = -Infinity;
    for (const lane of frame.lanes) {
      if (sideOf.get(lane.channel) !== cover.side) continue;
      top = Math.min(top, lane.top);
      bottom = Math.max(bottom, frame.layout.playfield.judgementY);
    }
    Iif (!(bottom > top)) continue;
    areas.push({
      left: bounds.left,
      right: bounds.right,
      top,
      bottom,
      sudden: cover.sudden,
      hidden: cover.hidden,
      ...(cover.lift > 0 ? { lift: cover.lift } : {}),
    });
  }
  return areas;
}
 
/** PERFECT, GREAT and GOOD keep the combo; BAD and POOR break it. */
function keepsCombo(judge: string): boolean {
  return judge === 'PERFECT' || judge === 'GREAT' || judge === 'GOOD';
}
 
/** A PGREAT prints as a GREAT whose colour steps with the play clock. The other words stay one colour. */
function judgmentColor(judge: string, nowMs: number): string {
  if (isFlashingGreat(judge)) return cssColor(flashingGreatColor(nowMs, FLASHING_GREAT));
  if (judge === 'GOOD') return GOOD_COLOR;
  if (judge === 'BAD' || judge === 'POOR') return MISS_COLOR;
  return JUDGMENT_COLOR;
}
 
function cssColor(color: number): string {
  return `#${color.toString(16).padStart(6, '0')}`;
}
 
function rect(x: number, y: number, w: number, h: number): BeMusicRect {
  return { x, y, w: Math.max(0, w), h: Math.max(0, h) };
}