Sounds on shared and server

This commit is contained in:
2026-11-22 07:44:13 -03:00
parent 47934b272c
commit 8e587f6446
16 changed files with 386 additions and 30 deletions
+3 -7
View File
@@ -5,14 +5,12 @@
// sounds.json, see docs/modding/resources/sounds.md. this decides what plays, an output (client/audio_output.ts in the
// browser) plays it
import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { SoundCategory, SoundJson } from "@/common/mod_data.ts";
import { sound_range, sound_to_play, type SoundCategory, type SoundJson } from "@/common/mod_data.ts";
import type { ClientPlayOptions, PlayingSound } from "@/common/mod_api/client.ts";
import type { Position } from "@/common/mod_api/shared.ts";
export type Volumes = Record<"master" | SoundCategory, number>;
// how far away a sound is heard at volume 1 when its definition doesn't say
const DEFAULT_RANGE = 16;
// sounds playing at once at most. past that a new one is dropped, and loops wait for room
const MAX_VOICES = 64;
@@ -114,9 +112,8 @@ class Playing implements PlayingSound {
return (this.json.pitch ?? 1) * this.#pitch;
}
// a louder sound is heard further, like a sound at volume 2 twice as far
get range() {
return (this.json.range ?? DEFAULT_RANGE) * Math.max(1, this.total_volume);
return sound_range(this.json, this.#volume);
}
// whether the listener is close enough to hear it, always for one heard as it is
@@ -159,8 +156,7 @@ export class Sounds {
}
play(sound: string, options: ClientPlayOptions = {}): PlayingSound {
const json = EverythingRegistry.get<SoundJson>("sounds", sound);
if (!json) throw new Error(`unknown sound ${sound}`);
const json = sound_to_play(sound, options);
const path = json.files[Math.floor(Math.random() * json.files.length)];
const file = this.#files.get(`${sound.split(":")[0]}:${path}`);
if (!file) throw new Error(`sound ${sound} has no file ${path}`);