All files / player-skin-sdk/src layout.ts

100% Statements 20/20
100% Branches 23/23
100% Functions 4/4
100% Lines 19/19

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                                                    61x 61x 61x               61x 61x 753x 753x     753x 753x                           61x 61x                                                           46x 21x 21x 63x 63x 30x   33x     21x    
import type { ChartPlayVariant } from '@be-music/chart-model';
import { resolveLR2LaneIndex } from '@be-music/player-engine/lanes';
import { resolveBeMusicLaneKind } from './lane-geometry.ts';
import { resolveGameplayLaneGeometry } from './touch-layout.ts';
import type { BeMusicGameplayLayout, BeMusicStage, GameplayLayoutMode } from './types.ts';
 
/** What {@link resolveGameplayLayout} needs to know about the chart: its lane channels and play variant. */
export interface GameplayLayoutInput {
  /** True when the second bank belongs to another player or a replay ghost. */
  twoPlayer?: boolean;
  laneChannels?: readonly string[];
  laneCount?: number;
  playVariant?: ChartPlayVariant;
  /**
   * `'touch'` asks for the touch layout (see {@link resolveGameplayLaneGeometry}); play modes it does not cover keep the
   * standard one. Defaults to `'standard'`.
   */
  layoutMode?: GameplayLayoutMode;
}
 
/**
 * The gameplay layout the host hands to a skin's chrome every frame (`context.layout`), built from the chart's lanes and
 * the skin's stage — a portrait stage (taller than wide) gets the portrait playfield. Hosts call it; skins normally just read `context.layout`, but can call it to build a layout for a
 * preview or a test frame.
 */
export function resolveGameplayLayout(input: GameplayLayoutInput, stage: BeMusicStage): BeMusicGameplayLayout {
  const channels = input.laneChannels;
  const variant = input.playVariant;
  const { mode, orientation, lanes, left, right, top, judgementY, twoPlayer } = resolveGameplayLaneGeometry(
    channels,
    input.laneCount ?? channels?.length,
    variant,
    input.layoutMode,
    stage,
    input.twoPlayer,
  );
  const sides: BeMusicGameplayLayout['playfield']['sides'] = {};
  const layoutLanes = lanes.map((lane, index) => {
    const bounds = sides[lane.side];
    sides[lane.side] = bounds
      ? { left: Math.min(bounds.left, lane.x), right: Math.max(bounds.right, lane.x + lane.w) }
      : { left: lane.x, right: lane.x + lane.w };
    const channel = lane.channel;
    return {
      channel,
      kind: channel
        ? resolveBeMusicLaneKind(channel, resolveLR2LaneIndex(channel, variant), variant)
        : index === 0
          ? ('scratch' as const)
          : ('white' as const),
      side: lane.side,
      x: lane.x,
      w: lane.w,
    };
  });
  // The landscape touch layout gives the BGA's room to the lanes; a portrait stage keeps it above them.
  const bga =
    mode === 'touch' && orientation === 'landscape' ? undefined : stage.resolveBGARect(right, left, twoPlayer);
  return {
    mode,
    orientation,
    ...(twoPlayer ? { twoPlayer: true } : {}),
    stage: { width: stage.width, height: stage.height },
    lanes: layoutLanes,
    playfield: {
      left,
      right,
      centerX: (left + right) / 2,
      top,
      judgementY,
      sides,
    },
    bga: bga && bga.w > 0 && bga.h > 0 ? { ...bga } : undefined,
  };
}
 
/** A run of lanes that sit edge to edge (one play side in double play). */
export interface LaneRun {
  left: number;
  right: number;
}
 
/**
 * Groups lanes that touch into runs, left to right — one run in single play, one per side in That-Which-Must-Not-Be-Named double play, where
 * the 1P and 2P banks stand apart. Draw anything that spans the lanes (the judgement line, a frame) once per run so
 * it never crosses the gap between the sides.
 */
export function resolveLaneRuns(lanes: ReadonlyArray<{ x: number; w: number }>): LaneRun[] {
  const sorted = [...lanes].sort((a, b) => a.x - b.x);
  const runs: LaneRun[] = [];
  for (const lane of sorted) {
    const last = runs[runs.length - 1];
    if (last && lane.x <= last.right + 0.5) {
      last.right = Math.max(last.right, lane.x + lane.w);
    } else {
      runs.push({ left: lane.x, right: lane.x + lane.w });
    }
  }
  return runs;
}