Jester and coilhead sounds

This commit is contained in:
2026-11-22 07:54:31 -03:00
parent 8e587f6446
commit da44685f54
31 changed files with 249 additions and 62 deletions
+20 -47
View File
@@ -10,17 +10,8 @@ import { read_mod, write_mod } from "@/common/mod.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { SoundJson } from "@/common/mod_data.ts";
import type { Position } from "@/common/mod_api/shared.ts";
import {
type Listener,
type SoundFile,
type SoundOutput,
Sounds,
sounds,
type Voice,
type VoiceSettings,
type Volumes,
} from "@/client/sounds.ts";
import { copy_dir, load_as_player, stone_floor, test_game, test_mob } from "./helpers.ts";
import { type Listener, Sounds, sounds } from "@/client/sounds.ts";
import { copy_dir, ListOutput, load_as_player, stone_floor, test_game, test_mob, VOLUMES } from "./helpers.ts";
// what an ogg file starts with. nothing decodes them outside the browser, so the rest doesn't have to be audio
const OGG = (n: number) => new Uint8Array([0x4f, 0x67, 0x67, 0x53, n]);
@@ -102,42 +93,8 @@ Deno.test("a player's client gets the sounds of the server's mods", async () =>
Deno.removeSync(dir, { recursive: true });
});
// the client, with an output that only keeps what it was told
// the client
class ListVoice implements Voice {
ended = false;
stopped = false;
constructor(readonly file: SoundFile, public settings: VoiceSettings) {}
move(at: Position) {
this.settings = { ...this.settings, at };
}
set(volume: number, pitch: number, range: number) {
this.settings = { ...this.settings, volume, pitch, range };
}
stop() {
this.stopped = true;
}
}
class ListOutput implements SoundOutput {
// false like a browser before the player's first click
ready = true;
voices: ListVoice[] = [];
start(file: SoundFile, settings: VoiceSettings) {
if (!this.ready) return undefined;
const voice = new ListVoice(file, settings);
this.voices.push(voice);
return voice;
}
listen() {}
set_volumes() {}
// the ones still going
get playing() {
return this.voices.filter((voice) => !voice.stopped && !voice.ended);
}
}
const VOLUMES: Volumes = { master: 1, music: 1, ambient: 1, blocks: 1, entities: 1, players: 1, ui: 1 };
const at = (x: number): Listener => ({ x, y: 0, z: 0, yaw: 0, pitch: 0 });
// a client's sounds with these definitions, each with one file of its own name, and a clock tests move
@@ -229,7 +186,6 @@ Deno.test("a sound following an entity moves with it and stops when it goes", ()
const entities = new Map<string, Position>([["jester", { x: 3, y: 0, z: 0 }]]);
const where = (id: string) => entities.get(id);
client.frame(at(0), where, VOLUMES);
assert(!client.play("noisy:theme", { follow: "nobody" }).playing);
const theme = client.play("noisy:theme", { follow: "jester", loop: true });
assertEquals(output.voices[0].settings.at, { x: 3, y: 0, z: 0 });
@@ -243,6 +199,23 @@ Deno.test("a sound following an entity moves with it and stops when it goes", ()
assert(output.voices[0].stopped);
});
Deno.test("a sound following an entity this client doesn't have yet waits a frame for it", () => {
const { client, output } = client_sounds([{ id: "noisy:theme" }]);
const entities = new Map<string, Position>();
const where = (id: string) => entities.get(id);
// like one played as the entity's animator is made, before the entity is in the level
const late = client.play("noisy:theme", { follow: "jester" });
const nobody = client.play("noisy:theme", { follow: "nobody" });
assert(late.playing);
assertEquals(output.voices.length, 0);
entities.set("jester", { x: 3, y: 0, z: 0 });
client.frame(at(0), where, VOLUMES);
assert(late.playing);
assertEquals(output.voices.map((voice) => voice.settings.at), [{ x: 3, y: 0, z: 0 }]);
assert(!nobody.playing);
});
Deno.test("at most 64 sounds play at once, and a loop waits for room", () => {
const { client, output } = client_sounds([{ id: "noisy:thud" }, { id: "noisy:hum" }]);
const thuds = Array.from({ length: 64 }, () => client.play("noisy:thud"));