All files / player-browser/src/skins/synesthesia judgements.ts

11.11% Statements 5/45
0% Branches 0/57
0% Functions 0/9
12.82% Lines 5/39

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                                                      24x                 24x                                                                                                                                                                                       24x   24x 24x                                                                                                      
import type { Container } from 'pixi.js';
import {
  effectProfile,
  flashingGreatColor,
  isFlashingGreat,
  judgeDisplayWord,
  resolveLanePlayLabel,
  punchScale,
  type BeMusicGameplayLayout,
  type BeMusicGameplayRuntime,
  type BeMusicJudgeDetail,
} from '@be-music/player-skin-sdk';
import { addHUDNumber, type ChildPool } from '../pixi-kit/index.ts';
import { addSynesthesiaHUDText as addHUDText } from './figures.ts';
import {
  SYN_AMBER,
  SYN_EMBER,
  SYN_CYAN,
  SYN_WHITE,
  SYN_MAGENTA,
  SYN_GREEN,
  SYN_RED,
  SYN_FLARE,
  displayStyle,
  labelStyle,
} from './style.ts';
type Runtime = BeMusicGameplayRuntime;
const JUDGE_COLORS: Record<string, number> = {
  PERFECT: SYN_FLARE,
  GREAT: SYN_AMBER,
  GOOD: SYN_CYAN,
  BAD: SYN_MAGENTA,
  POOR: SYN_RED,
};
 
/** Colours the PERFECT judgement's flashing GREAT cycles through. */
const FLASHING_GREAT = [SYN_CYAN, SYN_WHITE, SYN_AMBER, SYN_MAGENTA, SYN_GREEN] as const;
 
/**
 * Judgement word and combo as light: wide tracked type with a same-colour bloom. PERFECT prints as a GREAT that flashes
 * through the prism colours, so a clean run literally shimmers.
 */
export function drawJudgements(
  layer: Container,
  runtime: Runtime,
  layout: BeMusicGameplayLayout,
  wordY: number,
  pool: ChildPool,
): void {
  for (const side of ['1P', '2P'] as const) {
    const bounds = layout.playfield.sides[side];
    const mode = resolveLanePlayLabel(runtime, side);
    if (!bounds || !mode) continue;
    addHUDText(
      layer,
      mode,
      (bounds.left + bounds.right) / 2,
      wordY + ((bounds.judgementY ?? layout.playfield.judgementY) - layout.playfield.judgementY) - 28,
      { ...labelStyle(), fill: SYN_WHITE, anchorX: 0.5, anchorY: 0.5, maxWidth: bounds.right - bounds.left - 12 },
      pool,
    );
  }
  const displays = resolveJudgeDisplays(runtime, layout);
  const nowMs = runtime.nowMs ?? 0;
  const amount = effectProfile(runtime.effects).amount;
  for (const display of displays) {
    const judgeScale = punchScale(runtime.judgeAtMs, nowMs, 120, 0.22 * amount);
    const color = isFlashingGreat(display.judge)
      ? flashingGreatColor(nowMs, FLASHING_GREAT)
      : (JUDGE_COLORS[display.judge] ?? SYN_WHITE);
    addHUDText(
      layer,
      judgeDisplayWord(display.judge),
      display.x,
      wordY + display.lift,
      {
        scale: judgeScale,
        ...displayStyle(16, color),
        letterSpacing: 4,
        anchorX: 0.5,
        anchorY: 0.5,
        maxWidth: display.maxWidth,
        dropShadow: { color, alpha: 0.95, blur: 10, distance: 0 },
      },
      pool,
    );
    // The judge detail above the word: FAST / SLOW or the offset, the keys' and the scratch's side by side when apart.
    const details = display.details ?? [];
    details.forEach((detail, index) => {
      const spread = details.length > 1 ? (index - (details.length - 1) / 2) * JUDGE_DETAIL_SPREAD : 0;
      // Tabular figures: the milliseconds keep their place as they change from hit to hit.
      addHUDNumber(
        layer,
        detail.lane === undefined ? detail.text : `${JUDGE_DETAIL_LANES[detail.lane]} ${detail.text}`,
        display.x + spread,
        wordY + display.lift - (display.labelled ? 44 : 22),
        {
          ...displayStyle(9, JUDGE_DETAIL_COLORS[detail.tone]),
          letterSpacing: 1,
          anchorX: 0.5,
          anchorY: 0.5,
          maxWidth: details.length > 1 ? JUDGE_DETAIL_SPREAD - 6 : display.maxWidth,
        },
        pool,
      );
    });
    const combo = resolveVisibleCombo(display.judge, display.combo);
    if (combo > 0) {
      addHUDNumber(
        layer,
        formatCount(combo),
        display.x,
        wordY + display.lift + 28,
        {
          scale: punchScale(runtime.judgeAtMs, nowMs, 150, (combo % 100 === 0 ? 0.45 : 0.14) * amount),
          ...displayStyle(22, combo >= 200 ? SYN_AMBER : SYN_WHITE),
          anchorX: 0.5,
          anchorY: 0.5,
          maxWidth: Math.max(60, display.maxWidth - 40),
          dropShadow: { color: combo >= 200 ? SYN_AMBER : SYN_EMBER, alpha: 0.85, blur: 10, distance: 0 },
        },
        pool,
      );
    }
  }
}
 
/** The judge detail's colours: FAST and SLOW as the judge panel counts them, and white for a press on the note's time. */
const JUDGE_DETAIL_COLORS = { fast: SYN_CYAN, slow: SYN_MAGENTA, just: SYN_WHITE } as const;
/** The names the keys' and the scratch's details lead with when they are shown apart, and how far apart they sit. */
const JUDGE_DETAIL_LANES = { keys: 'KEY', scratch: 'SCR' } as const;
const JUDGE_DETAIL_SPREAD = 84;
 
function resolveJudgeDisplays(
  runtime: Runtime,
  layout: BeMusicGameplayLayout,
): Array<{
  judge: string;
  combo: number | undefined;
  x: number;
  lift: number;
  maxWidth: number;
  details?: readonly BeMusicJudgeDetail[];
  /** The side shows a play label (AUTO PLAY, REPLAY) above the word, which the detail keeps clear of. */
  labelled?: boolean;
}> {
  const centerOf = (side: '1P' | '2P'): number | undefined => {
    const bounds = layout.playfield.sides[side];
    return bounds ? (bounds.left + bounds.right) / 2 : undefined;
  };
  // A side's LIFT raises its judgement line, and its judgement word with it.
  const liftOf = (side: '1P' | '2P'): number =>
    (layout.playfield.sides[side]?.judgementY ?? layout.playfield.judgementY) - layout.playfield.judgementY;
  const doublePlay = centerOf('2P') !== undefined;
  const maxWidth = doublePlay ? 122 : 180;
  const fallbackX = layout.playfield.centerX;
  const sides = runtime.judgeSides?.filter((state) => typeof state.judge === 'string' && state.judge.length > 0);
  if (sides?.length) {
    return sides.map((state) => ({
      judge: state.judge!,
      combo: state.combo,
      x: centerOf(state.side) ?? fallbackX,
      lift: liftOf(state.side),
      maxWidth,
      details: state.details,
      labelled: resolveLanePlayLabel(runtime, state.side) !== undefined,
    }));
  }
  if (!runtime.lastJudge) return [];
  return [
    { judge: runtime.lastJudge, combo: runtime.combo, x: centerOf('1P') ?? fallbackX, lift: liftOf('1P'), maxWidth },
  ];
}
 
function resolveVisibleCombo(judge: string, combo: number | undefined): number {
  if (judge !== 'PERFECT' && judge !== 'GREAT' && judge !== 'GOOD') return 0;
  return combo !== undefined && Number.isFinite(combo) ? Math.max(0, Math.floor(combo)) : 0;
}
 
function formatCount(value: number | undefined): string {
  return String(Math.max(0, Math.floor(value ?? 0)));
}