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

48.42% Statements 46/95
47.19% Branches 42/89
60% Functions 3/5
51.11% Lines 46/90

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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338                                                              25x             25x 25x 25x 25x 25x                                           25x 25x                       25x 25x 25x 25x 25x       25x 25x 25x               25x 25x 8x                     25x 25x 10x   25x 25x                                                                                                                                                                 8x 8x 8x 8x 8x 8x 8x                                     8x 8x 8x 8x 8x       8x                                 8x       10x 10x 10x 10x 10x     10x 10x                                     10x                                                                                                                            
import { Graphics, type Container } from 'pixi.js';
import { resolveGrooveRect, resolveMilestoneSpot } from './layout.ts';
import { drawReticle } from './draw.ts';
import {
  burstParticlePosition,
  burstParticles,
  emberColor,
  hsvToHex,
  projectPoint,
  rotateX,
  rotateY,
} from './space.ts';
import { SYN_AMBER, SYN_CYAN, SYN_DISPLAY_FONT, SYN_FLARE, SYN_MAGENTA, SYN_WHITE } from './style.ts';
import {
  easeOutCubic,
  type BeMusicRect,
  effectProfile,
  type MilestoneArea,
  momentProgress,
  trackMoments,
  resolveMilestoneArea,
  type BeMusicGameplayLayout,
  type BeMusicGameplayRuntime,
  type BeMusicSkinSettingValues,
  type BeMusicSkinToggleSetting,
  STAGE_WIDTH as DESIGN_WIDTH,
  stageProgress,
} from '@be-music/player-skin-sdk';
import { addHUDNumber, type ChildPool } from '../pixi-kit/index.ts';
import { addSynesthesiaHUDText as addHUDText } from './figures.ts';
 
export const COMBO_MILESTONE_SETTING = {
  kind: 'toggle',
  id: 'comboMilestones',
  label: '100-combo milestones',
  default: false,
} satisfies BeMusicSkinToggleSetting;
 
const MILESTONE_MS = 1300;
const CLEAR_MS = 1100;
const FULL_COMBO_MS = 2600;
const FULL_COMBO_BURST = burstParticles(77, 640);
const MILESTONE_COLORS = [SYN_AMBER, SYN_CYAN, SYN_MAGENTA] as const;
 
/**
 * Synesthesia showpieces, painted on the front layer:
 *
 * - the count-in: READY condenses out of wide-tracked light over a widening filament, then START flares out on the
 *   first beat;
 * - every 100 combo: the count glows in the middle of the BGA monitor, caught in a lock-on reticle that snaps shut on it;
 * - the clear line: a light sweep runs the length of the gauge;
 * - a full combo: a warm bloom, a 3D sphere of voxel sparks bursting from the centre of the screen, and FULL COMBO
 *   glowing gold.
 */
export function drawSynesthesiaMoments(
  chromeLayer: Container,
  layer: Container,
  runtime: BeMusicGameplayRuntime,
  playfieldCenterX: number,
  hue: number,
  pool: ChildPool,
  layout: BeMusicGameplayLayout,
  settings?: BeMusicSkinSettingValues,
): void {
  const nowMs = runtime.nowMs ?? 0;
  const moments = trackMoments(chromeLayer, {
    nowMs,
    combo: runtime.combo ?? 0,
    gauge: runtime.gauge ?? 0,
    clearThreshold: runtime.clearThreshold ?? 80,
    survival: runtime.gaugeSurvival === true,
    judged:
      (runtime.perfect ?? 0) + (runtime.great ?? 0) + (runtime.good ?? 0) + (runtime.bad ?? 0) + (runtime.poor ?? 0),
    totalNotes: runtime.totalNotes ?? 0,
    bad: runtime.bad ?? 0,
    poor: runtime.poor ?? 0,
  });
  const effects = effectProfile(runtime.effects);
  Iif (!effects.enabled) return;
  const graphics = pool.acquireGraphics();
  graphics.label = 'synesthesia/moments';
  graphics.blendMode = 'add';
 
  // Misses get no effect at all: the playfield stays still and clear so the next notes read.
 
  const { width, height } = layout.stage;
  const portrait = layout.orientation === 'portrait';
  Iif (runtime.chartMs !== undefined) {
    // Over the middle of the stage on the landscape stage; over the upper lanes, under the HUD, on a portrait one.
    const { top, judgementY } = layout.playfield;
    const countIn = portrait
      ? { x: playfieldCenterX, y: Math.round(top + (judgementY - top) * 0.35), half: (width - 40) / 2 }
      : { x: DESIGN_WIDTH / 2, y: 214, half: 290 };
    drawCountIn(graphics, layer, runtime.chartMs, countIn, hue, pool);
  }
  const milestone = momentProgress(moments.milestone?.atMs, nowMs, MILESTONE_MS);
  if (settings?.[COMBO_MILESTONE_SETTING.id] === true && milestone !== undefined && moments.milestone) {
    drawMilestone(
      graphics,
      layer,
      moments.milestone.value,
      milestone,
      resolveMilestoneArea(layout.playfield.right, 346, layout.stage.width),
      layout.bga,
      resolveMilestoneSpot(layout),
      pool,
    );
  }
  const clear = momentProgress(moments.clearAtMs, nowMs, CLEAR_MS);
  if (clear !== undefined) {
    drawClear(graphics, layer, clear, resolveGrooveRect(layout), pool);
  }
  const fullCombo = momentProgress(moments.fullComboAtMs, nowMs, FULL_COMBO_MS);
  Iif (fullCombo !== undefined) {
    drawFullCombo(graphics, layer, fullCombo, nowMs, hue, effects.screenWide, effects.amount, width, height, pool);
  }
}
 
/**
 * The count-in, centred on `at` (`half` is how far the filament under READY reaches either side once formed): READY
 * condensing out of wide-tracked light, then START flaring out on the first beat.
 */
function drawCountIn(
  graphics: Graphics,
  layer: Container,
  chartMs: number,
  at: { x: number; y: number; half: number },
  hue: number,
  pool: ChildPool,
): void {
  if (chartMs < -2600 || chartMs > 900) return;
  const color = hsvToHex(hue, 0.55, 1);
  const form = easeOutCubic(stageProgress(chartMs, -2600, 1400));
  const fade = 1 - stageProgress(chartMs, -300, 300);
  // The landscape filament grows to 290 either side; narrower stages scale the letter tracking with it.
  const reach = at.half / 290;
  if (fade > 0) {
    const cy = at.y;
    const half = 30 + (at.half - 30) * form;
    graphics.rect(at.x - half, cy + 22, half * 2, 1).fill({ color: SYN_WHITE, alpha: 0.85 * fade });
    graphics.rect(at.x - half, cy + 19, half * 2, 7).fill({ color, alpha: 0.18 * fade });
    const text = addHUDText(
      layer,
      'READY',
      at.x,
      cy,
      {
        size: 26,
        fill: SYN_WHITE,
        fontFamily: SYN_DISPLAY_FONT,
        letterSpacing: (10 + 40 * (1 - form)) * reach,
        anchorX: 0.5,
        anchorY: 0.5,
        dropShadow: { color, alpha: 1, blur: 14, distance: 0 },
      },
      pool,
    );
    text.alpha = form * fade;
  }
  // First beat: START flares out.
  const start = stageProgress(chartMs, 0, 900);
  if (start > 0 && start < 1) {
    const eased = easeOutCubic(start);
    const text = addHUDText(
      layer,
      'START',
      at.x,
      at.y,
      {
        size: 22,
        fill: SYN_WHITE,
        fontFamily: SYN_DISPLAY_FONT,
        letterSpacing: (12 + 20 * eased) * reach,
        anchorX: 0.5,
        anchorY: 0.5,
        dropShadow: { color, alpha: 1, blur: 14, distance: 0 },
      },
      pool,
    );
    text.alpha = 1 - start;
  }
}
 
function drawMilestone(
  graphics: Graphics,
  layer: Container,
  value: number,
  t: number,
  area: MilestoneArea,
  bga: BeMusicRect | undefined,
  spot: { x: number; y: number; w: number } | undefined,
  pool: ChildPool,
): void {
  // Each hundred takes the next accent light in turn: gold, electric blue, magenta.
  const color = MILESTONE_COLORS[Math.max(0, Math.floor(value / 100) - 1) % MILESTONE_COLORS.length]!;
  const compact = bga === undefined;
  const cx = bga ? bga.x + bga.w / 2 : (spot?.x ?? area.x + area.w / 2);
  const cy = bga ? bga.y + bga.h / 2 : (spot?.y ?? area.y + area.h / 2 - 10);
  const fit = bga ? Math.min(1, bga.w / 304, bga.h / 176) : spot ? Math.min(1, spot.w / 120) : 1;
  const alpha = Math.min(1, t / 0.12) * (1 - Math.max(0, (t - 0.7) / 0.3));
  const count = addHUDNumber(
    layer,
    String(value),
    cx,
    cy,
    {
      size: Math.round((compact ? 30 : 52) * fit),
      fill: SYN_WHITE,
      fontFamily: SYN_DISPLAY_FONT,
      letterSpacing: 4 * fit,
      maxWidth: (compact ? 112 : 184) * fit,
      stroke: { color: 0x000000, width: Math.max(2, 6 * fit), join: 'round' },
      anchorX: 0.5,
      anchorY: 0.5,
      dropShadow: { color, alpha: 1, blur: 18, distance: 0 },
      scale: 1.25 - 0.25 * easeOutCubic(Math.min(1, t / 0.25)),
    },
    pool,
  );
  for (const glyph of count) glyph.alpha = alpha;
  const snap = easeOutCubic(Math.min(1, t / 0.2));
  const lockW = (compact ? 120 : 190) * fit * (1.6 - 0.6 * snap);
  const lockH = (compact ? 74 : 110) * fit * (1.6 - 0.6 * snap);
  drawReticle(graphics, cx - lockW / 2, cy + (compact ? 14 * fit : 0) - lockH / 2, lockW, lockH, color, 0.9 * alpha, {
    arm: 14,
    width: 2,
  });
  const label = addHUDText(
    layer,
    'COMBO',
    cx,
    cy + (compact ? 28 : 38) * fit,
    {
      size: Math.max(6, Math.round(11 * fit)),
      stroke: { color: 0x000000, width: Math.max(2, 3 * fit), join: 'round' },
      fill: SYN_WHITE,
      fontFamily: SYN_DISPLAY_FONT,
      letterSpacing: 10 * fit,
      anchorX: 0.5,
      anchorY: 0.5,
      dropShadow: { color, alpha: 1, blur: 8, distance: 0 },
    },
    pool,
  );
  label.alpha = alpha;
}
 
function drawClear(graphics: Graphics, layer: Container, t: number, groove: BeMusicRect, pool: ChildPool): void {
  const color = SYN_AMBER;
  const head = groove.x + groove.w * easeOutCubic(Math.min(1, t / 0.5));
  const fade = 1 - Math.max(0, (t - 0.55) / 0.45);
  graphics.rect(groove.x, groove.y, head - groove.x, groove.h).fill({ color, alpha: 0.22 * fade });
  graphics
    .rect(Math.max(groove.x, Math.min(head - 2, groove.x + groove.w - 4)), groove.y, 4, groove.h)
    .fill({ color: SYN_WHITE, alpha: 0.9 * fade });
  graphics.circle(head, groove.y + groove.h / 2, Math.min(5, groove.h / 2)).fill({ color, alpha: 0.3 * fade });
  const text = addHUDText(
    layer,
    'CLEAR LINE',
    // The caption and sweep share the actual bar, regardless of the surrounding HUD arrangement.
    groove.x + groove.w / 2,
    groove.y + groove.h / 2,
    {
      size: Math.min(12, groove.h),
      fill: SYN_WHITE,
      fontFamily: SYN_DISPLAY_FONT,
      letterSpacing: 3,
      maxWidth: groove.w - 8,
      anchorX: 0.5,
      anchorY: 0.5,
      stroke: { color: 0x000000, width: 2 },
      dropShadow: { color: 0x000000, alpha: 1, blur: 4, distance: 0 },
    },
    pool,
  );
  text.alpha = Math.min(1, t / 0.15) * fade;
}
 
function drawFullCombo(
  graphics: Graphics,
  layer: Container,
  t: number,
  nowMs: number,
  hue: number,
  screenWide: boolean,
  amount: number,
  width: number,
  height: number,
  pool: ChildPool,
): void {
  const cx = width / 2;
  const cy = height / 2 - 10;
  const fadeOut = 1 - Math.max(0, (t - 0.8) / 0.2);
  const bloom = screenWide ? Math.max(0, 1 - t * 5) : 0;
  if (bloom > 0) {
    graphics.rect(0, 0, width, height).fill({ color: SYN_FLARE, alpha: 0.55 * bloom });
  }
  // A sphere of sparks bursting out of the centre, turning slowly in 3D.
  const burstT = Math.min(1, t / 0.85);
  const spin = nowMs / 2200;
  const sparkCount = Math.round(FULL_COMBO_BURST.length * amount);
  for (const particle of FULL_COMBO_BURST.slice(0, sparkCount)) {
    const life = burstT / particle.life;
    if (life >= 1) continue;
    const position = burstParticlePosition(particle, life, 360, 60);
    const point = projectPoint(rotateX(rotateY(position, spin), 0.3), cx, cy, 380);
    if (!point.visible) continue;
    // Mostly ember cooling from white-hot, with a scatter of blue and magenta voxels.
    const color =
      particle.hueShift > 0.055 ? SYN_CYAN : particle.hueShift < -0.065 ? SYN_MAGENTA : emberColor(1 - life * 0.7);
    const size = (0.5 + 1.1 * particle.size) * point.scale * (1 - life * 0.5);
    graphics.circle(point.x, point.y, size * 3).fill({ color, alpha: 0.06 * (1 - life) * fadeOut });
    graphics
      .rect(point.x - size, point.y - size, size * 2, size * 2)
      .fill({ color: life < 0.2 ? SYN_WHITE : color, alpha: (1 - life) * fadeOut });
  }
  const form = easeOutCubic(Math.min(1, t / 0.3));
  const color = emberColor(0.6 + 0.3 * Math.sin(nowMs / 260));
  const text = addHUDText(
    layer,
    'FULL COMBO',
    cx,
    cy,
    {
      size: width < height ? 28 : 38,
      fill: SYN_WHITE,
      fontFamily: SYN_DISPLAY_FONT,
      letterSpacing: 8 + 30 * (1 - form),
      anchorX: 0.5,
      anchorY: 0.5,
      dropShadow: { color, alpha: 1, blur: 22, distance: 0 },
      maxWidth: width - 40,
    },
    pool,
  );
  text.alpha = form * fadeOut;
}