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

100% Statements 47/47
100% Branches 52/52
100% Functions 9/9
100% Lines 40/40

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                                                                    121x   121x     18x                       44x 44x 44x 2x   44x 10x   44x           6x   44x             1x   44x         10x 8x 6x 4x 2x         79x 23x 23x             121x     25x 25x 25x         7x 6x 6x 3x         2x                 33x   5x   24x   4x                             121x                         15x 15x 7x   8x    
import { STAGE_WIDTH } from './stage.ts';
 
/**
 * "Moments" — the showpiece events built-in skins stage during a play: combo milestones, the gauge crossing the clear
 * line, and a full combo. Pure state transitions so chrome renderers (which redraw every frame from a runtime snapshot)
 * can detect the frame an event happens and animate against its timestamp.
 */
 
export interface MomentInput {
  nowMs: number;
  combo: number;
  gauge: number;
  clearThreshold: number;
  survival: boolean;
  /** Notes judged so far (PG + GR + GD + BD + PR). */
  judged: number;
  totalNotes: number;
  bad: number;
  poor: number;
}
 
export interface MomentState {
  lastCombo: number;
  lastGauge: number;
  /** Most recent combo milestone (a multiple of {@link COMBO_MILESTONE}) and when it was reached. */
  milestone: { value: number; atMs: number } | undefined;
  /** When the gauge last rose onto / above the clear line from below. */
  clearAtMs: number | undefined;
  /** When the final note was judged with no BAD / POOR. */
  fullComboAtMs: number | undefined;
  /** When a combo of at least {@link COMBO_BREAK_MIN} broke, and how long it was. */
  comboBreak: { combo: number; atMs: number } | undefined;
}
 
export const COMBO_MILESTONE = 100;
/** Combos shorter than this break silently. */
export const COMBO_BREAK_MIN = 20;
 
export function createMomentState(): MomentState {
  return {
    lastCombo: 0,
    lastGauge: 0,
    milestone: undefined,
    clearAtMs: undefined,
    fullComboAtMs: undefined,
    comboBreak: undefined,
  };
}
 
/** Advances `state` by one frame of `input`, stamping any event that happened since the previous frame. */
export function updateMoments(state: MomentState, input: MomentInput): MomentState {
  const next: MomentState = { ...state, lastCombo: input.combo, lastGauge: input.gauge };
  const reached = Math.floor(input.combo / COMBO_MILESTONE);
  if (input.combo < state.lastCombo && state.lastCombo >= COMBO_BREAK_MIN) {
    next.comboBreak = { combo: state.lastCombo, atMs: input.nowMs };
  }
  if (reached >= 1 && reached > Math.floor(state.lastCombo / COMBO_MILESTONE)) {
    next.milestone = { value: reached * COMBO_MILESTONE, atMs: input.nowMs };
  }
  if (
    !input.survival &&
    input.clearThreshold > 0 &&
    state.lastGauge < input.clearThreshold &&
    input.gauge >= input.clearThreshold
  ) {
    next.clearAtMs = input.nowMs;
  }
  if (
    state.fullComboAtMs === undefined &&
    input.totalNotes > 0 &&
    input.judged >= input.totalNotes &&
    input.bad === 0 &&
    input.poor === 0
  ) {
    next.fullComboAtMs = input.nowMs;
  }
  return next;
}
 
/** Escalation tier for a combo: 0 below 25, then 1 / 2 / 3 / 4 from 25 / 50 / 100 / 200. */
export function comboTier(combo: number): 0 | 1 | 2 | 3 | 4 {
  if (combo >= 200) return 4;
  if (combo >= 100) return 3;
  if (combo >= 50) return 2;
  if (combo >= 25) return 1;
  return 0;
}
 
/** Progress (0..1) of a moment that started at `atMs` and lasts `durationMs`, or `undefined` when not running. */
export function momentProgress(atMs: number | undefined, nowMs: number, durationMs: number): number | undefined {
  if (atMs === undefined) return undefined;
  const t = (nowMs - atMs) / durationMs;
  return t >= 0 && t < 1 ? t : undefined;
}
 
/**
 * Per-scene moment state keyed by the chrome layer the renderer draws into, so each gameplay scene instance tracks its
 * own events without the renderer holding scene references.
 */
const STATES = new WeakMap<object, MomentState>();
 
export function trackMoments(key: object, input: MomentInput): MomentState {
  const state = updateMoments(STATES.get(key) ?? createMomentState(), input);
  STATES.set(key, state);
  return state;
}
 
/** Strength (1 → 0) of an impulse that fired at `atMs`, easing out over `durationMs`; 0 when absent or finished. */
export function impulse(atMs: number | undefined, nowMs: number, durationMs: number): number {
  if (atMs === undefined || durationMs <= 0) return 0;
  const t = (nowMs - atMs) / durationMs;
  if (t < 0 || t >= 1) return 0;
  return (1 - t) ** 2;
}
 
/** Scale for a "punch" on `atMs`: overshoots to `1 + amount` instantly and eases back to 1 over `durationMs`. */
export function punchScale(atMs: number | undefined, nowMs: number, durationMs: number, amount: number): number {
  return 1 + amount * impulse(atMs, nowMs, durationMs);
}
 
/** Multipliers skins apply per effect level: overall motion / particle `amount`, and whether screen-wide hits run. */
export function effectProfile(level: 'full' | 'reduced' | 'off' | undefined): {
  amount: number;
  screenWide: boolean;
  enabled: boolean;
} {
  switch (level) {
    case 'off':
      return { amount: 0, screenWide: false, enabled: false };
    case 'reduced':
      return { amount: 0.5, screenWide: false, enabled: true };
    default:
      return { amount: 1, screenWide: true, enabled: true };
  }
}
 
/** A screen region a combo milestone may play in without covering any lane. */
export interface MilestoneArea {
  x: number;
  y: number;
  w: number;
  h: number;
  /** `beside`: the column right of the lanes; `below`: the band under the judgement line (double play). */
  mode: 'beside' | 'below';
}
 
/** Narrowest column right of the lanes a milestone will use before falling back to the band below them. */
export const MILESTONE_MIN_WIDTH = 240;
 
/**
 * Where a combo milestone may play so notes stay visible: the column right of the playfield when it is at least
 * {@link MILESTONE_MIN_WIDTH} px wide, otherwise the band under the lanes (`lanesBottom`) — over the HUD for a moment
 * rather than over the notes. Coordinates are in the stage's design space (`stageWidth` × 480; the built-in skins'
 * 16:9 stage by default).
 */
export function resolveMilestoneArea(
  playfieldRight: number,
  lanesBottom = 346,
  stageWidth: number = STAGE_WIDTH,
): MilestoneArea {
  const free = stageWidth - playfieldRight - 16;
  if (free >= MILESTONE_MIN_WIDTH) {
    return { x: playfieldRight + 8, y: 40, w: free, h: lanesBottom - 40, mode: 'beside' };
  }
  return { x: 8, y: lanesBottom + 4, w: stageWidth - 16, h: 480 - lanesBottom - 8, mode: 'below' };
}