Offset on sounds api

This commit is contained in:
2026-11-22 08:35:54 -03:00
parent d18fe19027
commit 5d374091cd
5 changed files with 58 additions and 4 deletions
+7 -2
View File
@@ -40,7 +40,8 @@ export interface VoiceSettings {
volume: number;
pitch: number;
loop: boolean;
// seconds into the file it starts at, for a loop coming back partway. past the file's length it goes around
// seconds into the file it starts at, for one played with an offset or a loop coming back partway. past the file's
// length a loop goes around, and anything else has finished
offset: number;
}
@@ -157,12 +158,16 @@ export class Sounds {
play(sound: string, options: ClientPlayOptions = {}): PlayingSound {
const json = sound_to_play(sound, options);
const offset = options.offset ?? 0;
if (typeof offset !== "number" || !(offset >= 0) || !Number.isFinite(offset)) {
throw new Error("a sound's offset must be 0 or more seconds");
}
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}`);
const at = options.follow !== undefined ? this.#entity_position(options.follow) : options.at;
const playing = new Playing(json, file, { ...options }, this.clock(), at && { ...at });
const playing = new Playing(json, file, { ...options }, this.clock() - offset, at && { ...at });
this.#playing.push(playing);
// an entity this client doesn't have yet, like one whose animator is being made, has until the next frame to
// turn up