diff --git a/CLAUDE.md b/CLAUDE.md index 9bc75e7..c3666db 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -85,6 +85,9 @@ The game is a static site, deployed to GitHub Pages from `main` by [.github/work The **API** lives in [server/](server/) (its README lists settings and routes): Fastify, SQLite via `node:sqlite` (loaded with `createRequire`, since Vite can't bundle it), bundles and covers stored by SHA-256 ([server/blobs.ts](server/blobs.ts)). It imports the game's own `readBundle` / `buildRegistry` / `defaultPack`, so a published pack passes exactly the checks the game runs; keep `src/content` free of browser-only code. Sessions are an opaque token in an HttpOnly cookie (hash stored); state-changing requests need the `x-fight-forge: 1` header. `npm run server` runs it (`DEV_LOGIN=1` allows sign-in by name for development); its tests (`server/app.test.ts`, `fastify.inject`, in-memory store) run with the rest. The client is [src/net/api.ts](src/net/api.ts): with no `VITE_API_URL`, `apiEnabled()` is false and online UI stays hidden. The Forge's pack page has [PublishTools](src/components/forge/PublishTools.tsx): sign in, choose visibility, publish the current version (a published version must be newer; it offers the next patch version). The start menu's BROWSE PACKS opens the **gallery** (`gameScreen: 'gallery'`, [src/components/Gallery.tsx](src/components/Gallery.tsx)): search, sort, a creator filter, and a pack page with Play (`fetchBundle`, cached per version in the Cache API since bundles are immutable, then `installBundle` and `startRun` at the pack's first own level) and a share link. `?pack=` (`linkedPack()`) opens straight to that pack's page. **Remix in Forge** (`remixBundle` in workspace.ts) copies a published pack into the workspace under a new id (`-remix`), version 1.0.0, with `meta.remixOf` crediting the original; the server links a published remix to its original (`remix_of`), and pack pages show "Remix of" and the list of remixes. Likes are one per user (`POST /packs/:id/like`, sort `likes`). The gallery marks packs updated since this browser last played them ([src/net/played.ts](src/net/played.ts), localStorage). **Moderation**: anyone signed in can ⚑ Report a pack; moderators (`ADMINS` setting, `provider:name`) get a Reports panel ([Moderation.tsx](src/components/Moderation.tsx)) to take a pack down, dismiss reports or ban its owner, each logged in `moderation_actions`, and Take down / Put back on pack pages. A taken-down pack, or any pack of a banned user, is visible only to its owner (marked in PublishTools) and admins. Owners can remove their pack from the server. Limits are server-side: publishes and reports per user per hour, and writes per IP. Bundles check models with `glbProblem` (a whole glTF 2.0 file with no outside URIs) and audio with `audioType`; tests build models with `tinyGlb` from [src/test/glb.ts](src/test/glb.ts). +### Audio +Sound is content. The content's `sounds` (`SoundDef`, [src/audio/types.ts](src/audio/types.ts)) are `synth` (wave, pitch sweep, duration: no file needed) or `file` (an `audio` asset or a site URL); `music` is a looping `pattern` (tempo and tracks of space-separated notes like `A2 . C3 -`, parsed by [src/audio/notes.ts](src/audio/notes.ts)) or a `file`. `settings.audio` names the sound for each event (`hit`, `hurt`, `block`, `ko`, `pickup`, `clear`); a move can add `sound` (played as its active frames begin) and `hitSound`; a level can name `music`. [src/audio/engine.ts](src/audio/engine.ts) is the WebAudio engine: `playSound(id)` (from CombatSystem, Pickups, LevelDirector), `playMusic` / `stopMusic` (GameScene, per level), `duckMusic` while frozen, and `previewSound` / `previewMusic` for the Forge's [SoundTools](src/components/forge/SoundTools.tsx) (▶ Play, and "Use an audio file…" via `addAudioAsset`). Audio starts on the first click or key (`installAudioUnlock` in main.tsx). Master / music / effects volumes are a per-browser preference ([src/audio/volume.ts](src/audio/volume.ts)) set from [VolumeControls](src/components/VolumeControls.tsx) on the start menu (SOUND) and the pause menu. `audioLog` keeps the last sounds asked for, for headless tests. + ### Fighters and controllers Player and enemies are both **fighters**. [src/components/FighterBody.tsx](src/components/FighterBody.tsx) is the one shared body: rigid body and collider, a `fighter:` sim system (order `SYSTEM_ORDER.fighters`, before attacks and combat) that steps the hit reaction, asks a controller for an `Intent`, moves (or slides with knockback while reacting), jumps, updates facing and the fighter state machine, plus the model, attack tint, hurt flash and hitbox overlay. [Player.tsx](src/components/Player.tsx) and [Enemy.tsx](src/components/Enemy.tsx) are thin wrappers that supply the controller and start moves (the player's combo; an enemy's attack when it takes the slot). Both use `forwardRef` so `GameScene` holds refs to their bodies. diff --git a/content/default/levels.json b/content/default/levels.json index 7899b58..b07b8a5 100644 --- a/content/default/levels.json +++ b/content/default/levels.json @@ -3,6 +3,7 @@ "id": "level-1", "name": "Back Alley", "theme": "default", + "music": "alley", "segments": [ { "length": 45, @@ -252,6 +253,7 @@ "id": "level-2", "name": "The Docks", "theme": "docks", + "music": "docks", "segments": [ { "length": 40, diff --git a/content/default/moves.json b/content/default/moves.json index 509918b..ecc6f0f 100644 --- a/content/default/moves.json +++ b/content/default/moves.json @@ -1,5 +1,6 @@ { "jab": { + "sound": "whoosh", "startup": 0.05, "active": 0.08, "recovery": 0.17, @@ -25,6 +26,7 @@ "next": "jab2" }, "jab2": { + "sound": "whoosh", "startup": 0.05, "active": 0.08, "recovery": 0.17, @@ -50,6 +52,7 @@ "next": "hook" }, "hook": { + "sound": "whoosh", "startup": 0.08, "active": 0.1, "recovery": 0.22, @@ -75,6 +78,8 @@ "next": "uppercut" }, "uppercut": { + "sound": "whoosh", + "hitSound": "heavy", "startup": 0.12, "active": 0.1, "recovery": 0.35, @@ -100,6 +105,8 @@ } }, "jumpKick": { + "sound": "whoosh", + "hitSound": "heavy", "startup": 0.06, "active": 0.25, "recovery": 0.1, @@ -125,6 +132,8 @@ "air": true }, "dashPunch": { + "sound": "whoosh", + "hitSound": "heavy", "startup": 0.1, "active": 0.15, "recovery": 0.35, @@ -151,6 +160,8 @@ "lunge": 7 }, "rushPunch": { + "sound": "whoosh", + "hitSound": "heavy", "startup": 0.2, "active": 0.15, "recovery": 0.5, @@ -176,6 +187,7 @@ "lunge": 9 }, "enemyPunch": { + "sound": "whoosh", "startup": 0.4, "active": 0.1, "recovery": 0.4, diff --git a/content/default/music.json b/content/default/music.json new file mode 100644 index 0000000..b84885e --- /dev/null +++ b/content/default/music.json @@ -0,0 +1,36 @@ +{ + "alley": { + "kind": "pattern", + "tempo": 112, + "volume": 0.35, + "tracks": [ + { + "wave": "square", + "volume": 0.35, + "notes": "A2 . A2 . C3 . A2 . G2 . G2 . E2 . G2 . A2 . A2 . C3 . D3 . E3 - D3 . C3 . G2 ." + }, + { + "wave": "triangle", + "volume": 0.5, + "notes": "A4 - - - . . E4 . G4 - - - . . D4 . A4 - - - . . C5 . B4 - A4 - G4 - E4 -" + } + ] + }, + "docks": { + "kind": "pattern", + "tempo": 96, + "volume": 0.35, + "tracks": [ + { + "wave": "sawtooth", + "volume": 0.25, + "notes": "D2 . D2 D2 . . F2 . G2 . G2 G2 . . A2 . D2 . D2 D2 . . C3 . A2 . G2 . F2 . E2 ." + }, + { + "wave": "sine", + "volume": 0.5, + "notes": "D4 - - - F4 - - - A4 - - - G4 - F4 - E4 - - - C4 - - - D4 - - - - - - -" + } + ] + } +} diff --git a/content/default/settings.json b/content/default/settings.json index 98631b8..97b8703 100644 --- a/content/default/settings.json +++ b/content/default/settings.json @@ -131,5 +131,13 @@ "pause": ["escape"], "hitboxes": ["h"] } + }, + "audio": { + "hit": "punch", + "hurt": "hurt", + "block": "block", + "ko": "ko", + "pickup": "pickup", + "clear": "clear" } } diff --git a/content/default/sounds.json b/content/default/sounds.json new file mode 100644 index 0000000..98f8e6f --- /dev/null +++ b/content/default/sounds.json @@ -0,0 +1,66 @@ +{ + "whoosh": { + "kind": "synth", + "wave": "noise", + "freq": 1800, + "freqEnd": 600, + "duration": 0.09, + "volume": 0.25 + }, + "punch": { + "kind": "synth", + "wave": "square", + "freq": 180, + "freqEnd": 55, + "duration": 0.11, + "volume": 0.5 + }, + "heavy": { + "kind": "synth", + "wave": "sawtooth", + "freq": 140, + "freqEnd": 35, + "duration": 0.2, + "volume": 0.6 + }, + "hurt": { + "kind": "synth", + "wave": "sawtooth", + "freq": 320, + "freqEnd": 120, + "duration": 0.16, + "volume": 0.45 + }, + "block": { + "kind": "synth", + "wave": "triangle", + "freq": 1100, + "freqEnd": 800, + "duration": 0.07, + "volume": 0.4 + }, + "ko": { + "kind": "synth", + "wave": "square", + "freq": 400, + "freqEnd": 40, + "duration": 0.45, + "volume": 0.5 + }, + "pickup": { + "kind": "synth", + "wave": "sine", + "freq": 660, + "freqEnd": 1320, + "duration": 0.14, + "volume": 0.45 + }, + "clear": { + "kind": "synth", + "wave": "triangle", + "freq": 523, + "freqEnd": 1046, + "duration": 0.6, + "volume": 0.5 + } +} diff --git a/src/audio/audio.test.ts b/src/audio/audio.test.ts new file mode 100644 index 0000000..d1b3a8a --- /dev/null +++ b/src/audio/audio.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from 'vitest' +import { noteFreq, parseNotes, patternSteps, stepSeconds } from './notes' +import { DEFAULT_VOLUMES, parseVolumes } from './volume' + +describe('music notes', () => { + it('names notes by pitch', () => { + expect(noteFreq('A4')).toBe(440) + expect(noteFreq('A3')).toBe(220) + expect(noteFreq('C4')).toBeCloseTo(261.63, 2) + expect(noteFreq('C#4')).toBeCloseTo(noteFreq('Db4')!, 6) + expect(noteFreq('H2')).toBeNull() + expect(noteFreq('.')).toBeNull() + }) + + it('reads rests and holds', () => { + const { notes, steps } = parseNotes('A4 - - . C5 . - E5') + expect(steps).toBe(8) + expect(notes.map((n) => [n.step, n.steps])).toEqual([ + [0, 3], + [4, 1], + [7, 1], + ]) + }) + + it('times steps as sixteenth notes and loops on the longest track', () => { + expect(stepSeconds(120)).toBe(0.125) + expect(patternSteps([{ wave: 'sine', volume: 1, notes: 'A4 .' }, { wave: 'square', volume: 1, notes: 'A2 . . .' }])).toBe(4) + }) +}) + +describe('volume settings', () => { + it('fills in and clamps stored values', () => { + expect(parseVolumes(null)).toEqual(DEFAULT_VOLUMES) + expect(parseVolumes({ master: 2, music: -1, sfx: 'loud' })).toEqual({ master: 1, music: 0, sfx: DEFAULT_VOLUMES.sfx }) + }) +}) diff --git a/src/audio/engine.ts b/src/audio/engine.ts new file mode 100644 index 0000000..5bc63c2 --- /dev/null +++ b/src/audio/engine.ts @@ -0,0 +1,285 @@ +// The game's sound: a small WebAudio engine. Sound effects and music come +// from the active content (`getConfig().sounds` / `.music`), read when they +// play. Synth sounds and music patterns are made on the fly, so the default +// pack needs no audio files; file sounds are fetched and decoded once. +// +// Browsers start audio suspended until the player clicks or presses a key; +// `installAudioUnlock` resumes it then. Until it runs, sounds are skipped. + +import { getConfig } from '../content/active' +import { parseNotes, patternSteps, stepSeconds } from './notes' +import type { MusicDef, SoundDef } from './types' +import { loadVolumes, saveVolumes, type Volumes } from './volume' + +interface Graph { + ctx: AudioContext + master: GainNode + music: GainNode + sfx: GainNode + noise: AudioBuffer +} + +let graph: Graph | null = null +let volumes: Volumes = loadVolumes() +let ducked = false + +/** The last sounds asked for, newest last (for tests and debugging; nothing reads it in play). */ +export const audioLog: { kind: 'sfx' | 'music' | 'stop'; id: string }[] = [] +const log = (kind: 'sfx' | 'music' | 'stop', id: string) => { + audioLog.push({ kind, id }) + if (audioLog.length > 50) audioLog.shift() +} + +const ensure = (): Graph | null => { + if (graph) return graph + const Ctx = typeof window === 'undefined' ? undefined : (window.AudioContext ?? (window as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext) + if (!Ctx) return null + const ctx = new Ctx() + const master = ctx.createGain() + const music = ctx.createGain() + const sfx = ctx.createGain() + music.connect(master) + sfx.connect(master) + master.connect(ctx.destination) + const noise = ctx.createBuffer(1, ctx.sampleRate, ctx.sampleRate) + const data = noise.getChannelData(0) + for (let i = 0; i < data.length; i++) data[i] = Math.random() * 2 - 1 + graph = { ctx, master, music, sfx, noise } + applyVolumes() + return graph +} + +const applyVolumes = () => { + if (!graph) return + const t = graph.ctx.currentTime + graph.master.gain.setTargetAtTime(volumes.master, t, 0.02) + graph.sfx.gain.setTargetAtTime(volumes.sfx, t, 0.02) + graph.music.gain.setTargetAtTime(volumes.music * (ducked ? 0.35 : 1), t, 0.08) +} + +const running = (): Graph | null => { + const g = ensure() + return g && g.ctx.state === 'running' ? g : null +} + +/** Resume audio on the player's first click or key press (browsers require one). */ +let unlockInstalled = false +export const installAudioUnlock = () => { + if (unlockInstalled || typeof window === 'undefined') return + unlockInstalled = true + const unlock = () => { + const g = ensure() + if (!g) return + void g.ctx.resume().then(() => { + if (g.ctx.state !== 'running') return + window.removeEventListener('pointerdown', unlock) + window.removeEventListener('keydown', unlock) + if (wanted && !current) startMusic(wanted) + }) + } + window.addEventListener('pointerdown', unlock) + window.addEventListener('keydown', unlock) +} + +export const getVolumes = (): Volumes => ({ ...volumes }) +export const setVolumes = (v: Volumes) => { + volumes = { ...v } + saveVolumes(volumes) + applyVolumes() +} + +// ------------------------------------------------------------ files + +const buffers = new Map>() +const fileBuffer = (g: Graph, url: string): Promise => { + let b = buffers.get(url) + if (!b) { + b = fetch(url) + .then((r) => (r.ok ? r.arrayBuffer() : Promise.reject(new Error(String(r.status))))) + .then((data) => g.ctx.decodeAudioData(data)) + .catch((e: unknown) => { + console.warn(`Sound "${url}" failed to load`, e) + return null + }) + buffers.set(url, b) + } + return b +} + +// ------------------------------------------------------------ sound effects + +const playSynth = (g: Graph, d: Extract, out: AudioNode) => { + const t = g.ctx.currentTime + const env = g.ctx.createGain() + env.gain.setValueAtTime(0.0001, t) + env.gain.exponentialRampToValueAtTime(Math.max(0.0001, d.volume), t + 0.005) + env.gain.exponentialRampToValueAtTime(0.0001, t + d.duration) + env.connect(out) + let src: AudioScheduledSourceNode + if (d.wave === 'noise') { + // Noise through a band-pass filter whose centre slides like a pitch. + const n = g.ctx.createBufferSource() + n.buffer = g.noise + const filter = g.ctx.createBiquadFilter() + filter.type = 'bandpass' + filter.Q.value = 1.2 + filter.frequency.setValueAtTime(d.freq, t) + if (d.freqEnd) filter.frequency.exponentialRampToValueAtTime(d.freqEnd, t + d.duration) + n.connect(filter).connect(env) + src = n + } else { + const osc = g.ctx.createOscillator() + osc.type = d.wave + osc.frequency.setValueAtTime(d.freq, t) + if (d.freqEnd) osc.frequency.exponentialRampToValueAtTime(d.freqEnd, t + d.duration) + osc.connect(env) + src = osc + } + src.start(t) + src.stop(t + d.duration + 0.02) +} + +const playDef = (g: Graph, d: SoundDef) => { + if (d.kind === 'synth') return playSynth(g, d, g.sfx) + void fileBuffer(g, d.url).then((buf) => { + if (!buf) return + const src = g.ctx.createBufferSource() + const gain = g.ctx.createGain() + gain.gain.value = d.volume + src.buffer = buf + src.connect(gain).connect(g.sfx) + src.start() + }) +} + +/** Play a sound effect by id (from the active content); no id, or an unknown one, is silence. */ +export const playSound = (id: string | undefined) => { + if (!id) return + log('sfx', id) + const def = getConfig().sounds[id] + const g = running() + if (def && g) playDef(g, def) +} + +/** Play a definition directly (the Forge's preview of a sound being edited). */ +export const previewSound = (def: SoundDef) => { + const g = ensure() + if (!g) return + void g.ctx.resume().then(() => playDef(g, def)) +} + +// ------------------------------------------------------------ music + +interface Playing { + id: string + stop: () => void +} +let current: Playing | null = null +/** The music the game wants, kept while audio is still locked so it can start on unlock. */ +let wanted: string | null = null + +const startPattern = (g: Graph, d: Extract): (() => void) => { + const out = g.ctx.createGain() + out.gain.value = d.volume + out.connect(g.music) + const step = stepSeconds(d.tempo) + const length = patternSteps(d.tracks) + const tracks = d.tracks.map((t) => ({ ...t, ...parseNotes(t.notes) })) + let nextStep = 0 + let nextTime = g.ctx.currentTime + 0.05 + const schedule = () => { + // Schedule a little ahead of the clock, so timer jitter never makes a gap. + while (nextTime < g.ctx.currentTime + 0.2) { + const at = nextStep % length + for (const tr of tracks) { + for (const n of tr.notes) { + if (n.step !== at) continue + const osc = g.ctx.createOscillator() + const env = g.ctx.createGain() + const end = nextTime + n.steps * step + osc.type = tr.wave + osc.frequency.value = n.freq + env.gain.setValueAtTime(0, nextTime) + env.gain.linearRampToValueAtTime(tr.volume, nextTime + 0.01) + env.gain.setTargetAtTime(0, end - Math.min(0.05, step / 2), 0.02) + osc.connect(env).connect(out) + osc.start(nextTime) + osc.stop(end + 0.1) + } + } + nextStep++ + nextTime += step + } + } + schedule() + const timer = setInterval(schedule, 50) + return () => { + clearInterval(timer) + out.gain.setTargetAtTime(0, g.ctx.currentTime, 0.05) + setTimeout(() => out.disconnect(), 400) + } +} + +const startFile = (g: Graph, d: Extract): (() => void) => { + let src: AudioBufferSourceNode | null = null + let stopped = false + const gain = g.ctx.createGain() + gain.gain.value = d.volume + gain.connect(g.music) + void fileBuffer(g, d.url).then((buf) => { + if (!buf || stopped) return + src = g.ctx.createBufferSource() + src.buffer = buf + src.loop = true + src.connect(gain) + src.start() + }) + return () => { + stopped = true + src?.stop() + gain.disconnect() + } +} + +const startDef = (g: Graph, def: MusicDef) => (def.kind === 'pattern' ? startPattern(g, def) : startFile(g, def)) + +const startMusic = (id: string) => { + const def = getConfig().music[id] + const g = running() + if (!def || !g) return + current = { id, stop: startDef(g, def) } +} + +/** Play a music definition directly (the Forge's preview), replacing any music; null stops it. */ +export const previewMusic = (def: MusicDef | null) => { + stopMusic() + const g = ensure() + if (!g || !def) return + void g.ctx.resume().then(() => { + current = { id: '(preview)', stop: startDef(g, def) } + }) +} + +/** Loop a music track by id; the same id keeps playing, another replaces it, none stops it. */ +export const playMusic = (id: string | undefined) => { + if (!id) return stopMusic() + if (wanted === id && (current?.id === id || !running())) return + stopMusic() + wanted = id + log('music', id) + startMusic(id) +} + +export const stopMusic = () => { + if (wanted) log('stop', wanted) + wanted = null + current?.stop() + current = null +} + +/** Turn the music down while the game is paused or frozen, and back up after. */ +export const duckMusic = (on: boolean) => { + if (ducked === on) return + ducked = on + applyVolumes() +} diff --git a/src/audio/notes.ts b/src/audio/notes.ts new file mode 100644 index 0000000..cd005f5 --- /dev/null +++ b/src/audio/notes.ts @@ -0,0 +1,48 @@ +// Turning a music pattern's text into timed notes. Pure, so it's tested +// without a browser; the engine schedules what this returns. + +import type { MusicTrack } from './types' + +const SEMITONES: Record = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 } + +/** Frequency in Hz of a note like A4 (440), C#3 or Bb2; null if it isn't one. */ +export const noteFreq = (name: string): number | null => { + const m = /^([A-Ga-g])(#|b)?([0-8])$/.exec(name) + if (!m) return null + const semitone = SEMITONES[m[1].toUpperCase()] + (m[2] === '#' ? 1 : m[2] === 'b' ? -1 : 0) + const midi = (Number(m[3]) + 1) * 12 + semitone + return 440 * 2 ** ((midi - 69) / 12) +} + +export interface PatternNote { + /** First step (0-based). */ + step: number + /** Steps it lasts ("-" holds extend it). */ + steps: number + freq: number +} + +/** The notes of a track: "." rests, "-" holds the note before (a hold after a rest is a rest). */ +export const parseNotes = (notes: string): { notes: PatternNote[]; steps: number } => { + const tokens = notes.trim().split(/\s+/).filter(Boolean) + const out: PatternNote[] = [] + let current: PatternNote | null = null + tokens.forEach((t, step) => { + if (t === '-') { + if (current) current.steps++ + return + } + current = null + const freq = noteFreq(t) + if (freq === null) return + current = { step, steps: 1, freq } + out.push(current) + }) + return { notes: out, steps: tokens.length } +} + +/** Seconds per step: a step is a sixteenth note (a quarter of a beat). */ +export const stepSeconds = (tempo: number): number => 60 / tempo / 4 + +/** The pattern loops after its longest track. */ +export const patternSteps = (tracks: MusicTrack[]): number => Math.max(1, ...tracks.map((t) => parseNotes(t.notes).steps)) diff --git a/src/audio/types.ts b/src/audio/types.ts new file mode 100644 index 0000000..fb20ee0 --- /dev/null +++ b/src/audio/types.ts @@ -0,0 +1,32 @@ +// Audio definitions as the game uses them (the content's `sounds`, `music` +// and `settings.audio`, with file sources resolved to URLs). + +export type SynthWave = 'sine' | 'square' | 'sawtooth' | 'triangle' | 'noise' + +export type SoundDef = + | { kind: 'synth'; wave: SynthWave; freq: number; freqEnd?: number; duration: number; volume: number } + | { kind: 'file'; url: string; volume: number } + +export interface MusicTrack { + wave: Exclude + volume: number + /** Space-separated steps: a note (C4, F#3, Bb2), "." for a rest, "-" to hold the note before. */ + notes: string +} + +export type MusicDef = { kind: 'pattern'; tempo: number; volume: number; tracks: MusicTrack[] } | { kind: 'file'; url: string; volume: number } + +/** Which sound plays for each game event; leave one out for silence. */ +export interface AudioSettings { + /** A hit lands (unless the move has its own `hitSound`). */ + hit?: string + /** The player is hit. */ + hurt?: string + /** A hit is guarded. */ + block?: string + /** An enemy is knocked out. */ + ko?: string + pickup?: string + /** The level is cleared. */ + clear?: string +} diff --git a/src/audio/volume.ts b/src/audio/volume.ts new file mode 100644 index 0000000..30facff --- /dev/null +++ b/src/audio/volume.ts @@ -0,0 +1,34 @@ +// The player's volume settings: per browser, like key remapping. + +export interface Volumes { + master: number + music: number + sfx: number +} + +export const DEFAULT_VOLUMES: Volumes = { master: 0.8, music: 0.6, sfx: 0.9 } + +const KEY = 'fight-forge-volume' +const clamp = (v: unknown, fallback: number) => (typeof v === 'number' && Number.isFinite(v) ? Math.min(1, Math.max(0, v)) : fallback) + +/** Stored volumes, with anything missing or broken replaced by the default. */ +export const parseVolumes = (raw: unknown): Volumes => { + const o = (raw && typeof raw === 'object' ? raw : {}) as Partial> + return { master: clamp(o.master, DEFAULT_VOLUMES.master), music: clamp(o.music, DEFAULT_VOLUMES.music), sfx: clamp(o.sfx, DEFAULT_VOLUMES.sfx) } +} + +export const loadVolumes = (): Volumes => { + try { + return parseVolumes(JSON.parse(localStorage.getItem(KEY) ?? '{}')) + } catch { + return { ...DEFAULT_VOLUMES } + } +} + +export const saveVolumes = (v: Volumes) => { + try { + localStorage.setItem(KEY, JSON.stringify(v)) + } catch { + // Private mode: the setting lasts for this visit only. + } +} diff --git a/src/components/CombatSystem.tsx b/src/components/CombatSystem.tsx index 2f9a233..bf972ef 100644 --- a/src/components/CombatSystem.tsx +++ b/src/components/CombatSystem.tsx @@ -14,6 +14,7 @@ import { rollDrop } from '../sim/pickups' import { combatTargets, scaledDamage } from '../sim/rules' import { propBox } from '../sim/props' import type { FighterRuntime } from '../sim/fighter' +import { playSound } from '../audio/engine' const PLAYER = 'player' @@ -83,6 +84,8 @@ export const CombatSystem = ({ if (!record || record.instance !== attacker.move.instance) { record = { instance: attacker.move.instance, targets: new Set() } hits.current.set(attackerName, record) + // The swing: once per move instance, as its active frames begin. + playSound(move.sound) } const rules = getConfig().rules @@ -101,7 +104,11 @@ export const CombatSystem = ({ const dir = knockbackDirection(a.x, t.x, attacker.facing.x) const v = targetBody.linvel() let damage = move.damage - if (target.blocking && facesAttacker(target.facing.x, t.x, a.x)) { + const audio = getConfig().audio + const guarded = target.blocking && facesAttacker(target.facing.x, t.x, a.x) + if (guarded) playSound(audio.block) + else playSound(targetName === PLAYER ? (audio.hurt ?? move.hitSound ?? audio.hit) : (move.hitSound ?? audio.hit)) + if (guarded) { const blocked = blockedHit(move, getConfig().actions.block) damage = blocked.damage applyBlockstun(target.reaction, blocked.blockstun) @@ -124,11 +131,10 @@ export const CombatSystem = ({ } else { const enemy = store.enemies.find((e) => e.id === targetName) store.damageEnemy(targetName, damage) + const defeated = !!enemy && !useGameStore.getState().enemies.some((e) => e.id === targetName) + if (defeated) playSound(audio.ko) // Defeated: maybe drop a pickup where it fell. - const dropped = - enemy && !useGameStore.getState().enemies.some((e) => e.id === targetName) - ? rollDrop(getConfig().enemyTypes[enemy.type].drop) - : null + const dropped = defeated ? rollDrop(getConfig().enemyTypes[enemy.type].drop) : null if (dropped) { useGameStore .getState() @@ -146,6 +152,7 @@ export const CombatSystem = ({ if (!def?.breakable || record.targets.has(prop.id)) continue if (!boxesOverlap(hitbox, propBox(prop, def, floorTop))) continue record.targets.add(prop.id) + playSound(move.hitSound ?? config.audio.hit) if (!useGameStore.getState().damageProp(prop.id, move.damage)) continue const dropped = rollDrop(def.breakable.drop) if (dropped) useGameStore.getState().spawnPickup({ id: `drop-${prop.id}`, pickup: dropped, x: prop.x, z: prop.z }) diff --git a/src/components/ControlsPanel.tsx b/src/components/ControlsPanel.tsx index b2e09c4..268087d 100644 --- a/src/components/ControlsPanel.tsx +++ b/src/components/ControlsPanel.tsx @@ -1,6 +1,7 @@ import { useEffect, useState } from 'react' import { getBindings, getOverride, getPackBindings, setOverride } from '../content/bindings' import { ACTION_NAMES, INPUT_ACTIONS, actionLabel, rebind, type InputAction } from '../sim/input' +import { VolumeControls } from './VolumeControls' const small = { padding: '3px 8px', @@ -21,6 +22,7 @@ const small = { */ export const ControlsPanel = ({ onChange }: { onChange: () => void }) => { const [open, setOpen] = useState(false) + const [soundOpen, setSoundOpen] = useState(false) const [listening, setListening] = useState(null) // The bindings live outside React; bump to re-render after a change. const [, setVersion] = useState(0) @@ -54,7 +56,7 @@ export const ControlsPanel = ({ onChange }: { onChange: () => void }) => { position: 'absolute', top: '20px', left: '20px', - width: open ? '320px' : 'auto', + width: open || soundOpen ? '320px' : 'auto', maxHeight: 'calc(100vh - 40px)', overflowY: 'auto', padding: '14px', @@ -104,6 +106,20 @@ export const ControlsPanel = ({ onChange }: { onChange: () => void }) => { )} +
+ + {soundOpen && ( +
+ +
+ )} +
) } diff --git a/src/components/GameScene.tsx b/src/components/GameScene.tsx index 16d7333..7ef7190 100644 --- a/src/components/GameScene.tsx +++ b/src/components/GameScene.tsx @@ -21,6 +21,7 @@ import { LevelDirector } from './LevelDirector' import { Backdrop } from './Backdrop' import { Props } from './Props' import { sceneProbe } from '../sim/probe' +import { duckMusic, playMusic, stopMusic } from '../audio/engine' export const GameScene = () => { // The scene remounts for each level (keyed on gameId), so read the level once. @@ -54,6 +55,15 @@ export const GameScene = () => { sceneProbe.playerX = () => null } }, []) + // The level's music loops while the scene is up, quieter while it's frozen (paused, results, editing). + useEffect(() => { + playMusic(LEVEL.music) + return () => { + stopMusic() + duckMusic(false) + } + }, [LEVEL]) + useEffect(() => duckMusic(frozen), [frozen]) // Clean up enemy refs when enemies are removed useEffect(() => { const currentEnemyIds = new Set(enemies.map((e) => e.id)) diff --git a/src/components/LevelDirector.tsx b/src/components/LevelDirector.tsx index cfd50b8..6762cd2 100644 --- a/src/components/LevelDirector.tsx +++ b/src/components/LevelDirector.tsx @@ -7,6 +7,7 @@ import { useSimLoop, useSimSystem } from '../sim/simContext' import { createLevelRuntime, placeItems, placeProps, stepLevel, type Level, type SpawnDef } from '../sim/level' import { offscreenSpawn, type StageView } from '../sim/stage' import { scaledHealth } from '../sim/rules' +import { playSound } from '../audio/engine' // The "GO →" prompt hides once the player has walked this far past the // released lock, or after this long. @@ -111,6 +112,7 @@ export const LevelDirector = ({ store.setLevelProgress({ goPrompt: true }) } else if (e.kind === 'complete') { store.completeLevel(loop.time) + playSound(getConfig().audio.clear) return } } diff --git a/src/components/PauseMenu.tsx b/src/components/PauseMenu.tsx index e364dfc..6b2f4d6 100644 --- a/src/components/PauseMenu.tsx +++ b/src/components/PauseMenu.tsx @@ -1,6 +1,7 @@ import { useEffect } from 'react' import { useGameStore } from '../store/gameStore' import { actionForKey } from '../content/bindings' +import { VolumeControls } from './VolumeControls' export const PauseMenu = () => { const isPaused = useGameStore((state) => state.isPaused) @@ -124,6 +125,10 @@ export const PauseMenu = () => { +
+ +
+
{ const def = getConfig().pickups[item.pickup] @@ -49,7 +50,10 @@ export const Pickups = ({ playerPositionRef }: { playerPositionRef: RefObject = { master: 'Master', music: 'Music', sfx: 'Effects' } + +/** Master, music and effects volume: this browser's preference, applied at once. */ +export const VolumeControls = () => { + const [v, setV] = useState(getVolumes) + return ( +
+ {(Object.keys(LABELS) as (keyof Volumes)[]).map((k) => ( + + ))} +
+ ) +} diff --git a/src/components/forge/Forge.tsx b/src/components/forge/Forge.tsx index ad05894..48fafde 100644 --- a/src/components/forge/Forge.tsx +++ b/src/components/forge/Forge.tsx @@ -25,6 +25,7 @@ import { EnemyTools } from './EnemyTools' import { LevelTools } from './LevelTools' import { FighterTools } from './FighterTools' import { MoveTools } from './MoveTools' +import { SoundTools } from './SoundTools' import { PackInfoTools } from './PackInfoTools' import { PublishTools } from './PublishTools' import { apiEnabled } from '../../net/api' @@ -394,6 +395,9 @@ export const Forge = () => { put(wp, false)} onError={(text) => setMessage({ text, error: true })} />
)} + {(node.group === 'sounds' || node.group === 'music') && ( + put(wp, false)} onError={(text) => setMessage({ text, error: true })} /> + )} {node.group === 'moves' && (
put(wp, false)} /> diff --git a/src/components/forge/SoundTools.tsx b/src/components/forge/SoundTools.tsx new file mode 100644 index 0000000..b168b69 --- /dev/null +++ b/src/components/forge/SoundTools.tsx @@ -0,0 +1,105 @@ +import { useEffect, useRef, useState } from 'react' +import { previewMusic, previewSound } from '../../audio/engine' +import type { MusicDef, SoundDef } from '../../audio/types' +import { siteUrl } from '../../content/siteUrl' +import { addAudioAsset, audioMime } from '../../forge/assets' +import { assetsOf } from '../../forge/fighter' +import { setIn } from '../../forge/paths' +import { nodeValue, overrideNode, type TreeNode } from '../../forge/tree' +import { editPack, type PackJson, type WorkspacePack } from '../../forge/workspace' +import { sectionHeading, ui } from './ui' + +interface Props { + pack: WorkspacePack + node: TreeNode + save: (wp: WorkspacePack) => void + onError: (text: string) => void +} + +type FileDef = { kind: 'file'; url?: string; asset?: string; volume: number } + +/** A playable URL for a file sound: the pack's own bytes as a blob, or a site path. */ +const useFileUrl = (pack: WorkspacePack, def: FileDef | null): string | null => { + const [url, setUrl] = useState(null) + const asset = def?.asset + const path = asset ? String(assetsOf(pack.json).find((a) => a.id === asset)?.path ?? '') : '' + const bytes = path ? pack.files[path] : undefined + useEffect(() => { + if (!def) return setUrl(null) + if (def.url) return setUrl(siteUrl(def.url)) + if (!bytes) return setUrl(null) + const u = URL.createObjectURL(new Blob([bytes as Uint8Array], { type: audioMime(bytes) })) + setUrl(u) + return () => URL.revokeObjectURL(u) + }, [def?.url, bytes]) // eslint-disable-line react-hooks/exhaustive-deps + return url +} + +/** + * Tools for a sound or a piece of music: hear it (▶), and use an audio file + * of your own instead of the synth. The rest is edited in the form below. + */ +export const SoundTools = ({ pack, node, save, onError }: Props) => { + const value = (nodeValue(pack.json, node) ?? {}) as { kind?: string } & Record + const isMusic = node.group === 'music' + const file = value.kind === 'file' ? (value as unknown as FileDef) : null + const url = useFileUrl(pack, file) + const [playing, setPlaying] = useState(false) + const input = useRef(null) + + // Stop a music preview when leaving this item. + useEffect(() => () => previewMusic(null), [node.key]) + + const resolved = (): SoundDef | MusicDef | null => { + if (!file) return value as unknown as SoundDef | MusicDef + return url ? { kind: 'file', url, volume: file.volume } : null + } + + const play = () => { + const def = resolved() + if (!def) return onError('The audio file for this is missing.') + if (!isMusic) return previewSound(def as SoundDef) + if (playing) { + previewMusic(null) + setPlaying(false) + } else { + previewMusic(def as MusicDef) + setPlaying(true) + } + } + + const takeFile = async (f: File) => { + const r = addAudioAsset(pack, f.name, new Uint8Array(await f.arrayBuffer())) + if (!r.ok) return onError(r.message) + const volume = typeof value.volume === 'number' ? value.volume : 0.8 + save(editPack(r.pack, setIn(overrideNode(r.pack.json, node), node.path, { kind: 'file', asset: r.assetId, volume }) as PackJson)) + } + + return ( +
+
{isMusic ? 'LISTEN' : 'HEAR IT'}
+
+ + + { + const f = e.target.files?.[0] + e.target.value = '' + if (f) void takeFile(f) + }} + /> + + {file ? 'Plays an audio file from the pack.' : isMusic ? 'A looping pattern played by the synth.' : 'Made by the synth: no file needed.'} + +
+
+ ) +} diff --git a/src/config/__snapshots__/defaults.test.ts.snap b/src/config/__snapshots__/defaults.test.ts.snap index 184dd95..4aa0634 100644 --- a/src/config/__snapshots__/defaults.test.ts.snap +++ b/src/config/__snapshots__/defaults.test.ts.snap @@ -44,6 +44,14 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "standOff": 0.8, }, }, + "audio": { + "block": "block", + "clear": "clear", + "hit": "punch", + "hurt": "hurt", + "ko": "ko", + "pickup": "pickup", + }, "body": { "colliderHalfExtents": [ 0.25, @@ -282,6 +290,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "speed": 1.4, }, "damage": 16, + "hitSound": "heavy", "hitbox": { "forward": 0.8, "halfX": 0.7, @@ -299,6 +308,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "knockdown": true, }, "recovery": 0.35, + "sound": "whoosh", "startup": 0.1, }, "enemyPunch": { @@ -323,6 +333,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu }, }, "recovery": 0.4, + "sound": "whoosh", "startup": 0.4, }, "hook": { @@ -348,6 +359,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu }, }, "recovery": 0.22, + "sound": "whoosh", "startup": 0.08, }, "jab": { @@ -373,6 +385,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu }, }, "recovery": 0.17, + "sound": "whoosh", "startup": 0.05, }, "jab2": { @@ -398,6 +411,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu }, }, "recovery": 0.17, + "sound": "whoosh", "startup": 0.05, }, "jumpKick": { @@ -408,6 +422,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "speed": 2, }, "damage": 14, + "hitSound": "heavy", "hitbox": { "forward": 0.7, "halfX": 0.6, @@ -423,6 +438,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu }, }, "recovery": 0.1, + "sound": "whoosh", "startup": 0.06, }, "rushPunch": { @@ -432,6 +448,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "speed": 1.2, }, "damage": 12, + "hitSound": "heavy", "hitbox": { "forward": 0.8, "halfX": 0.6, @@ -448,6 +465,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu }, }, "recovery": 0.5, + "sound": "whoosh", "startup": 0.2, }, "uppercut": { @@ -457,6 +475,7 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "speed": 1.5, }, "damage": 20, + "hitSound": "heavy", "hitbox": { "forward": 0.8, "halfX": 0.55, @@ -473,9 +492,46 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "knockdown": true, }, "recovery": 0.35, + "sound": "whoosh", "startup": 0.12, }, }, + "music": { + "alley": { + "kind": "pattern", + "tempo": 112, + "tracks": [ + { + "notes": "A2 . A2 . C3 . A2 . G2 . G2 . E2 . G2 . A2 . A2 . C3 . D3 . E3 - D3 . C3 . G2 .", + "volume": 0.35, + "wave": "square", + }, + { + "notes": "A4 - - - . . E4 . G4 - - - . . D4 . A4 - - - . . C5 . B4 - A4 - G4 - E4 -", + "volume": 0.5, + "wave": "triangle", + }, + ], + "volume": 0.35, + }, + "docks": { + "kind": "pattern", + "tempo": 96, + "tracks": [ + { + "notes": "D2 . D2 D2 . . F2 . G2 . G2 G2 . . A2 . D2 . D2 D2 . . C3 . A2 . G2 . F2 . E2 .", + "volume": 0.25, + "wave": "sawtooth", + }, + { + "notes": "D4 - - - F4 - - - A4 - - - G4 - F4 - E4 - - - C4 - - - D4 - - - - - - -", + "volume": 0.5, + "wave": "sine", + }, + ], + "volume": 0.35, + }, + }, "physics": { "gravity": [ 0, @@ -633,6 +689,72 @@ exports[`DEFAULT_CONFIG > matches the snapshot (update it deliberately when retu "scoring": { "enemyKill": 100, }, + "sounds": { + "block": { + "duration": 0.07, + "freq": 1100, + "freqEnd": 800, + "kind": "synth", + "volume": 0.4, + "wave": "triangle", + }, + "clear": { + "duration": 0.6, + "freq": 523, + "freqEnd": 1046, + "kind": "synth", + "volume": 0.5, + "wave": "triangle", + }, + "heavy": { + "duration": 0.2, + "freq": 140, + "freqEnd": 35, + "kind": "synth", + "volume": 0.6, + "wave": "sawtooth", + }, + "hurt": { + "duration": 0.16, + "freq": 320, + "freqEnd": 120, + "kind": "synth", + "volume": 0.45, + "wave": "sawtooth", + }, + "ko": { + "duration": 0.45, + "freq": 400, + "freqEnd": 40, + "kind": "synth", + "volume": 0.5, + "wave": "square", + }, + "pickup": { + "duration": 0.14, + "freq": 660, + "freqEnd": 1320, + "kind": "synth", + "volume": 0.45, + "wave": "sine", + }, + "punch": { + "duration": 0.11, + "freq": 180, + "freqEnd": 55, + "kind": "synth", + "volume": 0.5, + "wave": "square", + }, + "whoosh": { + "duration": 0.09, + "freq": 1800, + "freqEnd": 600, + "kind": "synth", + "volume": 0.25, + "wave": "noise", + }, + }, "spawning": { "height": 2, "offscreenMargin": 2, diff --git a/src/config/defaults.ts b/src/config/defaults.ts index 65a347a..b06b2b9 100644 --- a/src/config/defaults.ts +++ b/src/config/defaults.ts @@ -8,6 +8,7 @@ import type { DropDef, PickupDef } from '../sim/pickups' import type { Bindings } from '../sim/input' import type { BackdropLayer } from '../sim/backdrop' import type { PropDef } from '../sim/props' +import type { AudioSettings, MusicDef, SoundDef } from '../audio/types' /** * Every gameplay tuning value in one typed object. The values come from @@ -216,6 +217,12 @@ export interface GameConfig { themes: Record /** Stage objects by id (crates, barrels, walls, platforms), placed by levels. */ props: Record + /** Which sound plays for each game event. */ + audio: AudioSettings + /** Sound effects by id. */ + sounds: Record + /** Music by id; each level may name one. */ + music: Record } /** The box humanoid: its feet sit 0.65 below its origin, so drop it 0.35 onto the collider bottom. */ diff --git a/src/content/defaultPack.ts b/src/content/defaultPack.ts index ae1b3c3..cfcf3f1 100644 --- a/src/content/defaultPack.ts +++ b/src/content/defaultPack.ts @@ -10,6 +10,8 @@ import enemyTypes from '../../content/default/enemy-types.json' import pickups from '../../content/default/pickups.json' import themes from '../../content/default/themes.json' import props from '../../content/default/props.json' +import sounds from '../../content/default/sounds.json' +import music from '../../content/default/music.json' import levels from '../../content/default/levels.json' /** The default pack as unvalidated JSON (run it through `buildRegistry`). */ @@ -22,5 +24,7 @@ export const defaultPack = (): Record => ({ pickups, themes, props, + sounds, + music, levels, }) diff --git a/src/content/registry.test.ts b/src/content/registry.test.ts index 2cb2e77..2ca74a7 100644 --- a/src/content/registry.test.ts +++ b/src/content/registry.test.ts @@ -479,3 +479,43 @@ describe('themes and props', () => { expect(build(defaultPack(), old).get('level:old')?.theme).toBe('default') }) }) + +describe('audio content', () => { + it('checks the sounds and music that moves, levels and settings name', () => { + const errors = errorsOf( + base(), + addon({ + assets: [{ id: 'pic', kind: 'texture', path: 'p.png' }], + settings: { audio: { hit: 'nope' } }, + moves: { jab: { ...(base().moves as Record).jab, sound: 'missing' } }, + sounds: { pic: { kind: 'file', asset: 'pic', volume: 1 }, both: { kind: 'file', asset: 'pic', url: '/x.ogg', volume: 1 } }, + music: { tune: { kind: 'pattern', tempo: 100, volume: 0.5, tracks: [{ wave: 'sine', volume: 1, notes: 'A4 H9' }] } }, + levels: [{ ...(base().levels as object[])[0], id: 'extra', music: 'silence' }], + }) + ) + expect(errors.map((e) => `${e.path}: ${e.message}`)).toEqual([ + 'sounds.both: needs exactly one of "url" or "asset"', + 'music.tune.tracks[0].notes: use notes like C4, F#3 or Bb2, "." for a rest and "-" to hold, separated by spaces', + ]) + const refs = errorsOf( + base(), + addon({ + assets: [{ id: 'pic', kind: 'texture', path: 'p.png' }], + settings: { audio: { hit: 'nope' } }, + moves: { jab: { ...(base().moves as Record).jab, sound: 'missing' } }, + sounds: { pic: { kind: 'file', asset: 'pic', volume: 1 } }, + levels: [{ ...(base().levels as object[])[0], id: 'extra', music: 'silence' }], + }) + ) + expect(paths(refs).sort()).toEqual(['levels.extra.music', 'moves.jab.sound', 'settings.audio.hit', 'sounds.pic.asset']) + }) + + it('gives file sounds a URL and keeps synth sounds as they are', () => { + const reg = build(base(), addon({ sounds: { tune: { kind: 'file', url: '/audio/t.ogg', volume: 0.5 } } })) + const { config, levels } = reg.toGameConfig() + expect(config.sounds.tune).toEqual({ kind: 'file', url: '/audio/t.ogg', volume: 0.5 }) + expect(config.sounds.punch.kind).toBe('synth') + expect(levels[0].music).toBe('alley') + expect(config.audio.hit).toBe('punch') + }) +}) diff --git a/src/content/registry.ts b/src/content/registry.ts index a02e7ef..3a5385f 100644 --- a/src/content/registry.ts +++ b/src/content/registry.ts @@ -6,6 +6,7 @@ import { siteUrl } from './siteUrl' import type { ZodIssue } from 'zod' import type { GameConfig, ModelRef } from '../config/defaults' +import type { MusicDef, SoundDef } from '../audio/types' import type { Level } from '../sim/level' import { cancelTime, moveDuration } from '../sim/moves' import { migratePack } from './migrations' @@ -21,6 +22,8 @@ import { type Pack, type PickupDefData, type PropDefData, + type SoundDefData, + type MusicDefData, type ThemeDefData, type Settings, } from './schemas' @@ -36,7 +39,7 @@ export interface ContentError { export type Result = { ok: true; value: T } | { ok: false; errors: ContentError[] } /** Kinds of definitions a reference like `move:jab` can name. */ -export type ContentKind = 'move' | 'ai' | 'enemyType' | 'pickup' | 'theme' | 'prop' | 'level' | 'asset' +export type ContentKind = 'move' | 'ai' | 'enemyType' | 'pickup' | 'theme' | 'prop' | 'sound' | 'music' | 'level' | 'asset' interface KindMap { move: MoveDefData @@ -45,6 +48,8 @@ interface KindMap { pickup: PickupDefData theme: ThemeDefData prop: PropDefData + sound: SoundDefData + music: MusicDefData level: LevelDef asset: AssetDef } @@ -64,6 +69,7 @@ export const SETTINGS_SECTIONS = [ 'scoring', 'rules', 'input', + 'audio', ] as const satisfies readonly (keyof Settings)[] // ------------------------------------------------------------------ parsing @@ -103,6 +109,8 @@ export interface ContentData { pickups: Readonly> themes: Readonly> props: Readonly> + sounds: Readonly> + music: Readonly> /** In play order. */ levels: readonly LevelDef[] assets: Readonly> @@ -120,6 +128,8 @@ export class ContentRegistry { readonly pickups: Readonly> readonly themes: Readonly> readonly props: Readonly> + readonly sounds: Readonly> + readonly music: Readonly> readonly levels: readonly LevelDef[] readonly assets: Readonly> private readonly origins: ReadonlyMap @@ -133,6 +143,8 @@ export class ContentRegistry { this.pickups = data.pickups this.themes = data.themes this.props = data.props + this.sounds = data.sounds + this.music = data.music this.levels = data.levels this.assets = data.assets this.origins = data.origins @@ -152,6 +164,10 @@ export class ContentRegistry { return this.themes case 'prop': return this.props + case 'sound': + return this.sounds + case 'music': + return this.music case 'asset': return this.assets case 'level': @@ -196,6 +212,10 @@ export class ContentRegistry { const { asset, url, ...rest } = m return { ...rest, url: url !== undefined ? siteUrl(url) : assetUrl(this.resolve(`asset:${asset ?? ''}`)) } } + const audioUrl = (m: { url?: string; asset?: string }) => + m.url !== undefined ? siteUrl(m.url) : assetUrl(this.resolve(`asset:${m.asset ?? ''}`)) + const sound = (d: SoundDefData): SoundDef => (d.kind === 'file' ? { kind: 'file', url: audioUrl(d), volume: d.volume } : d) + const music = (d: MusicDefData): MusicDef => (d.kind === 'file' ? { kind: 'file', url: audioUrl(d), volume: d.volume } : d) const s = this.settings const config: GameConfig = { ...s, @@ -208,6 +228,8 @@ export class ContentRegistry { pickups: { ...this.pickups }, themes: { ...this.themes }, props: { ...this.props }, + sounds: Object.fromEntries(Object.entries(this.sounds).map(([id, d]) => [id, sound(d)])), + music: Object.fromEntries(Object.entries(this.music).map(([id, d]) => [id, music(d)])), } return { config, levels: [...this.levels] } } @@ -244,6 +266,8 @@ export const buildRegistry = (inputs: unknown[]): Result => { const pickups: Record = {} const themes: Record = {} const props: Record = {} + const sounds: Record = {} + const music: Record = {} const assets: Record = {} const levels: LevelDef[] = [] @@ -269,6 +293,8 @@ export const buildRegistry = (inputs: unknown[]): Result => { merge('pickup', pickups, pack.pickups) merge('theme', themes, pack.themes) merge('prop', props, pack.props) + merge('sound', sounds, pack.sounds) + merge('music', music, pack.music) const assetIds = new Set() pack.assets.forEach((a, i) => { @@ -305,6 +331,8 @@ export const buildRegistry = (inputs: unknown[]): Result => { pickups, themes, props, + sounds, + music, levels, assets, origins, @@ -342,9 +370,21 @@ export const checkReferences = (reg: ContentRegistry): ContentError[] => { for (const action of ['left', 'right', 'attack', 'pause'] as const) if (reg.settings.input.bindings[action].length === 0) err('settings:input', `settings.input.bindings.${action}`, 'needs at least one key') + const checkAudioFile = (d: { kind: string; asset?: string }, origin: string, path: string) => { + if (d.kind !== 'file' || d.asset === undefined) return + const asset = reg.get(`asset:${d.asset}`) + if (!asset) err(origin, `${path}.asset`, `unknown asset "${d.asset}"`) + else if (asset.kind !== 'audio') err(origin, `${path}.asset`, `asset "${d.asset}" is a ${asset.kind}, not audio`) + } + for (const [id, d] of Object.entries(reg.sounds)) checkAudioFile(d, `sound:${id}`, `sounds.${id}`) + for (const [id, d] of Object.entries(reg.music)) checkAudioFile(d, `music:${id}`, `music.${id}`) + for (const [event, id] of Object.entries(reg.settings.audio)) expect('sound', id, 'settings:audio', `settings.audio.${event}`) + for (const [id, m] of Object.entries(reg.moves)) { const origin = `move:${id}` expect('move', m.next, origin, `moves.${id}.next`) + expect('sound', m.sound, origin, `moves.${id}.sound`) + expect('sound', m.hitSound, origin, `moves.${id}.hitSound`) const cancel = cancelTime(m) if (cancel < m.startup || cancel > moveDuration(m)) err(origin, `moves.${id}.cancelFrom`, `${cancel}s is outside the move (startup ${m.startup}s to end ${moveDuration(m)}s)`) @@ -374,6 +414,7 @@ export const checkReferences = (reg: ContentRegistry): ContentError[] => { const origin = `level:${level.id}` const base = `levels.${level.id}` expect('theme', level.theme, origin, `${base}.theme`) + expect('music', level.music, origin, `${base}.music`) let goals = 0 level.segments.forEach((seg, si) => { const sp = `${base}.segments[${si}]` diff --git a/src/content/schemas.ts b/src/content/schemas.ts index c896f54..8c06279 100644 --- a/src/content/schemas.ts +++ b/src/content/schemas.ts @@ -85,6 +85,67 @@ export const modelRefSchema = z.discriminatedUnion('kind', [ ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'needs exactly one of "url" or "asset"' }) }) +// -------------------------------------------------------------------- audio + +const fileRef = { + /** A URL (files in `public/` are served from the site root)... */ + url: z.string().min(1).optional(), + /** ...or an audio asset of the pack. Exactly one of the two. */ + asset: idSchema.optional(), +} +const oneSource = (m: { kind: string; url?: string; asset?: string }, ctx: z.RefinementCtx) => { + if (m.kind === 'file' && (m.url === undefined) === (m.asset === undefined)) + ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'needs exactly one of "url" or "asset"' }) +} + +export const SYNTH_WAVES = ['sine', 'square', 'sawtooth', 'triangle', 'noise'] as const + +/** A sound effect: made on the fly by a tiny synth (no file needed), or an audio file. */ +export const soundSchema = z + .discriminatedUnion('kind', [ + z + .object({ + kind: z.literal('synth'), + wave: z.enum(SYNTH_WAVES), + /** Pitch in Hz at the start... */ + freq: positive, + /** ...sliding to this by the end. */ + freqEnd: positive.optional(), + /** Seconds. */ + duration: positive, + volume: fraction, + }) + .strict(), + z.object({ kind: z.literal('file'), ...fileRef, volume: fraction }).strict(), + ]) + .superRefine(oneSource) + +/** A note name like C4, F#3 or Bb2; "." is a rest and "-" holds the note before. */ +const NOTE_TOKEN = /^(?:[A-Ga-g](?:#|b)?[0-8]|\.|-)$/ +const notesSchema = z + .string() + .refine((s) => s.trim() !== '' && s.trim().split(/\s+/).every((t) => NOTE_TOKEN.test(t)), { + message: 'use notes like C4, F#3 or Bb2, "." for a rest and "-" to hold, separated by spaces', + }) + +/** Music: a looping step pattern played by the synth, or an audio file. */ +export const musicSchema = z + .discriminatedUnion('kind', [ + z + .object({ + kind: z.literal('pattern'), + /** Beats per minute; each note step is a quarter of a beat. */ + tempo: positive, + volume: fraction, + tracks: z + .array(z.object({ wave: z.enum(['sine', 'square', 'sawtooth', 'triangle']), volume: fraction, notes: notesSchema }).strict()) + .min(1), + }) + .strict(), + z.object({ kind: z.literal('file'), ...fileRef, volume: fraction }).strict(), + ]) + .superRefine(oneSource) + // -------------------------------------------------------------------- moves export const hitboxSchema = z @@ -113,6 +174,10 @@ export const moveSchema = z /** Move id a buffered press chains into. */ next: idSchema.optional(), cancelFrom: nonNegative.optional(), + /** Sound played when the move starts (a whoosh). */ + sound: idSchema.optional(), + /** Sound played when it lands, instead of the audio settings' `hit`. */ + hitSound: idSchema.optional(), }) .strict() @@ -267,6 +332,17 @@ export const settingsSchema = z .strict(), }) .strict(), + /** Which sound plays for each game event (ids in the content's `sounds`); leave one out for silence. */ + audio: z + .object({ + hit: idSchema.optional(), + hurt: idSchema.optional(), + block: idSchema.optional(), + ko: idSchema.optional(), + pickup: idSchema.optional(), + clear: idSchema.optional(), + }) + .strict(), }) .strict() @@ -353,7 +429,14 @@ export const segmentSchema = z .strict() export const levelSchema = z - .object({ id: idSchema, name: z.string().min(1), theme: idSchema, segments: z.array(segmentSchema).min(1) }) + .object({ + id: idSchema, + name: z.string().min(1), + theme: idSchema, + /** Music that loops while the level plays. */ + music: idSchema.optional(), + segments: z.array(segmentSchema).min(1), + }) .strict() // --------------------------------------------------------------------- pack @@ -388,6 +471,8 @@ export const packSchema = z pickups: z.record(idSchema, pickupSchema).default({}), themes: z.record(idSchema, themeSchema).default({}), props: z.record(idSchema, propSchema).default({}), + sounds: z.record(idSchema, soundSchema).default({}), + music: z.record(idSchema, musicSchema).default({}), /** In play order. A level with the same id as an earlier pack's replaces it in place. */ levels: z.array(levelSchema).default([]), }) @@ -405,6 +490,8 @@ export type Settings = z.infer export type LevelDef = z.infer export type ThemeDefData = z.infer export type PropDefData = z.infer +export type SoundDefData = z.infer +export type MusicDefData = z.infer export type PackMeta = z.infer /** A pack after parsing (defaults filled in). */ export type Pack = z.infer diff --git a/src/content/typeChecks.ts b/src/content/typeChecks.ts index 07ad205..0916b44 100644 --- a/src/content/typeChecks.ts +++ b/src/content/typeChecks.ts @@ -8,15 +8,18 @@ import type { AiBehavior } from '../sim/controllers' import type { Level } from '../sim/level' import type { MoveDef } from '../sim/moves' import type { PickupDef } from '../sim/pickups' +import type { MusicDef, SoundDef } from '../audio/types' import type { AiBehaviorDef, EnemyTypeData, LevelDef, ModelRefDef, + MusicDefData, MoveDefData, PickupDefData, PropDefData, Settings, + SoundDefData, ThemeDefData, } from './schemas' @@ -26,7 +29,9 @@ type Check = T // The schema's gltf model may name a pack asset instead of a URL; otherwise identical. type RuntimeModelData = Exclude | (Omit, 'url'> & { url?: string; asset?: string }) type Runtime = { [K in keyof T]: T[K] extends ModelRef ? RuntimeModelData : T[K] } -type SettingsKeys = Exclude +type SettingsKeys = Exclude +// Sounds and music may name a pack asset instead of a URL; otherwise identical. +type FileData = T extends { kind: 'file' } ? Omit & { url?: string; asset?: string } : T export type ContentTypeChecks = [ Check>, @@ -40,5 +45,7 @@ export type ContentTypeChecks = [ Check>, Check>, Check>, + Check>>, + Check>>, Check['m'], RuntimeModelData>>, ] diff --git a/src/forge/assets.test.ts b/src/forge/assets.test.ts index 0d41254..bd0f548 100644 --- a/src/forge/assets.test.ts +++ b/src/forge/assets.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import { readBundle } from '../content/bundle' -import { addImageAsset, assetUsage, setCover } from './assets' +import { addAudioAsset, addImageAsset, assetUsage, setCover } from './assets' import { bindKey, unbindKey } from './bindings' import { addModelAsset, gltfModelFor } from './fighter' import { setIn } from './paths' @@ -24,6 +24,19 @@ describe('forge pack assets', () => { expect(setCover(wp.json, undefined).meta).not.toHaveProperty('cover') }) + it('adds an audio file a sound can play, which survives export', async () => { + const OGG = new TextEncoder().encode('OggS and some sound') + const r = addAudioAsset(newPack('Loud', []), 'Big Hit.ogg', OGG) + if (!r.ok) throw new Error(r.message) + expect(r.pack.json.assets).toEqual([{ id: 'big-hit', kind: 'audio', path: 'audio/big-hit.ogg' }]) + const json = setIn(r.pack.json, ['sounds', 'boom'], { kind: 'file', asset: 'big-hit', volume: 0.7 }) as PackJson + expect(validatePack(json)).toEqual([]) + expect(assetUsage(json).get('big-hit')).toEqual(['sounds.boom']) + const read = await readBundle(await exportBundle({ ...r.pack, json })) + expect(read.ok && read.value.files.get('big-hit')).toEqual(OGG) + expect(addAudioAsset(newPack('x', []), 'x.mp3', PNG)).toEqual({ ok: false, message: '"x.mp3" isn\'t an MP3, Ogg or WAV file' }) + }) + it('refuses files that are not images', () => { const r = addImageAsset(newPack('x', []), 'cover.png', GLB) expect(r.ok).toBe(false) diff --git a/src/forge/assets.ts b/src/forge/assets.ts index 995c97b..44216b7 100644 --- a/src/forge/assets.ts +++ b/src/forge/assets.ts @@ -1,7 +1,7 @@ // Pack assets in the Forge: adding a cover image, and finding where each // asset is used so the creator can tell what's safe to remove. -import { BUNDLE_LIMITS, imageType } from '../content/bundle' +import { audioType, BUNDLE_LIMITS, imageType } from '../content/bundle' import { assetsOf, fileStem } from './fighter' import { pathString } from './paths' import { uniqueId, type PackJson, type WorkspacePack } from './workspace' @@ -26,6 +26,23 @@ export const addImageAsset = ( return { ok: true, pack: { ...wp, json, files: { ...wp.files, [path]: bytes }, updatedAt: Date.now() }, assetId: id } } +/** Add an MP3, Ogg or WAV file to the pack as an audio asset. */ +export const addAudioAsset = ( + wp: WorkspacePack, + filename: string, + bytes: Uint8Array +): { ok: true; pack: WorkspacePack; assetId: string } | { ok: false; message: string } => { + const type = audioType(bytes) + if (!type) return { ok: false, message: `"${filename}" isn't an MP3, Ogg or WAV file` } + if (bytes.length > BUNDLE_LIMITS.audio) + return { ok: false, message: `"${filename}" is ${bytes.length} bytes; audio files can be up to ${BUNDLE_LIMITS.audio}` } + const assets = assetsOf(wp.json) + const id = uniqueId(fileStem(filename, 'sound'), assets.map((a) => String(a.id))) + const path = `audio/${id}.${type}` + const json = { ...wp.json, assets: [...assets, { id, kind: 'audio', path }] } + return { ok: true, pack: { ...wp, json, files: { ...wp.files, [path]: bytes }, updatedAt: Date.now() }, assetId: id } +} + /** Make an image asset the pack's cover (or clear it with undefined). */ export const setCover = (json: PackJson, assetId: string | undefined): PackJson => { const meta = { ...((json.meta ?? {}) as Record) } @@ -54,3 +71,6 @@ export const assetUsage = (json: PackJson): Map => { /** A data or blob URL is made by the caller; this gives the MIME type for an image's bytes. */ export const imageMime = (bytes: Uint8Array): string => `image/${imageType(bytes) ?? 'png'}` + +/** The MIME type of an audio file's bytes. */ +export const audioMime = (bytes: Uint8Array): string => ({ mp3: 'audio/mpeg', ogg: 'audio/ogg', wav: 'audio/wav' })[audioType(bytes) ?? 'mp3'] diff --git a/src/forge/tree.ts b/src/forge/tree.ts index edb02a2..d950c23 100644 --- a/src/forge/tree.ts +++ b/src/forge/tree.ts @@ -24,7 +24,7 @@ export interface TreeNode { base?: unknown } -export type RecordGroup = 'moves' | 'ai' | 'enemyTypes' | 'pickups' | 'themes' | 'props' +export type RecordGroup = 'moves' | 'ai' | 'enemyTypes' | 'pickups' | 'themes' | 'props' | 'sounds' | 'music' export type GroupId = 'pack' | 'settings' | RecordGroup | 'levels' export const GROUPS: { id: GroupId; label: string; item: string }[] = [ @@ -36,10 +36,12 @@ export const GROUPS: { id: GroupId; label: string; item: string }[] = [ { id: 'pickups', label: 'Pickups', item: 'pickup' }, { id: 'themes', label: 'Themes', item: 'theme' }, { id: 'props', label: 'Props', item: 'prop' }, + { id: 'sounds', label: 'Sounds', item: 'sound' }, + { id: 'music', label: 'Music', item: 'music' }, { id: 'levels', label: 'Levels', item: 'level' }, ] -const RECORD_GROUPS: RecordGroup[] = ['moves', 'ai', 'enemyTypes', 'pickups', 'themes', 'props'] +const RECORD_GROUPS: RecordGroup[] = ['moves', 'ai', 'enemyTypes', 'pickups', 'themes', 'props', 'sounds', 'music'] /** The pack's own name, version and description. */ export const packInfoSchema = packSchema.pick({ name: true, version: true, meta: true }) diff --git a/src/main.tsx b/src/main.tsx index 4aff025..e857e9a 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,6 +1,9 @@ import { StrictMode } from 'react' import { createRoot } from 'react-dom/client' import App from './App.tsx' +import { installAudioUnlock } from './audio/engine' + +installAudioUnlock() createRoot(document.getElementById('root')!).render( diff --git a/src/sim/level.ts b/src/sim/level.ts index 60819ce..086ec31 100644 --- a/src/sim/level.ts +++ b/src/sim/level.ts @@ -63,6 +63,8 @@ export interface Level { name: string /** Theme id in the content's `themes`: how the stage looks. */ theme: string + /** Music id in the content's `music`, looping while the level plays. */ + music?: string segments: Segment[] } diff --git a/src/sim/moves.ts b/src/sim/moves.ts index 23c6dc0..757e8b4 100644 --- a/src/sim/moves.ts +++ b/src/sim/moves.ts @@ -52,6 +52,10 @@ export interface MoveDef { next?: string /** Seconds into the move from which a buffered press cancels into `next`; defaults to the end of the active phase. */ cancelFrom?: number + /** Sound id played when the move starts. */ + sound?: string + /** Sound id played when it lands, instead of the audio settings' `hit`. */ + hitSound?: string } export type MoveSet = Record