diff --git a/client/sounds.ts b/client/sounds.ts index b7ab5c9..b64380c 100644 --- a/client/sounds.ts +++ b/client/sounds.ts @@ -163,12 +163,10 @@ export class Sounds { const at = options.follow !== undefined ? this.#entity_position(options.follow) : options.at; const playing = new Playing(json, file, { ...options }, this.clock(), at && { ...at }); - // an entity this client doesn't have can't be followed - if (options.follow !== undefined && !at) { - playing.done = true; - return playing; - } 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 + if (options.follow !== undefined && !at) return playing; this.#start(playing); if (!playing.voice && !options.loop) playing.done = true; return playing; @@ -205,7 +203,10 @@ export class Sounds { } this.#playing = this.#playing.filter((playing) => !playing.done); for (const playing of this.#playing) { - if (!playing.voice) this.#start(playing); + if (playing.voice) continue; + this.#start(playing); + // one waiting for its entity that can't play now never will + if (!playing.voice && !playing.options.loop) playing.done = true; } } diff --git a/mods/dangerous_business/blocks/factory_door.json b/mods/dangerous_business/blocks/factory_door.json index 921b7db..6e9fa7e 100644 --- a/mods/dangerous_business/blocks/factory_door.json +++ b/mods/dangerous_business/blocks/factory_door.json @@ -87,7 +87,11 @@ }, "interactive": true, "components": { - "dangerous_business:door": { "pairs_with": "dangerous_business:door" } + "dangerous_business:door": { + "pairs_with": "dangerous_business:door", + "open_sound": "dangerous_business:door/open", + "close_sound": "dangerous_business:door/close" + } } } } diff --git a/mods/dangerous_business/scripts/coilhead.ts b/mods/dangerous_business/scripts/coilhead.ts index 073b1d7..cbf09c4 100644 --- a/mods/dangerous_business/scripts/coilhead.ts +++ b/mods/dangerous_business/scripts/coilhead.ts @@ -30,6 +30,8 @@ export function register_coilhead(ctx: ServerContext) { start: () => { mob.stop(); mob.set_client_data("frozen", true); + mob.play_sound("dangerous_business:coilhead/stop"); + mob.play_sound("dangerous_business:coilhead/seen"); }, stop: () => mob.set_client_data("frozen", undefined), }); diff --git a/mods/dangerous_business/scripts/jester.ts b/mods/dangerous_business/scripts/jester.ts index 51b3784..99207b2 100644 --- a/mods/dangerous_business/scripts/jester.ts +++ b/mods/dangerous_business/scripts/jester.ts @@ -1,7 +1,14 @@ // the jester: a box on legs that wanders until it sees a player. it follows them for a while, stops to wind its crank, // and pops open, then runs down every player in the facility, faster and faster, until there's nobody left inside import type { MobRef, Player, ScriptGoal, ServerContext } from "cubi/server"; -import { type AnimatedEntity, AnimationState, type ClientContext, type Clip, type EntityPose } from "cubi/client"; +import { + type AnimatedEntity, + AnimationState, + type ClientContext, + type Clip, + type EntityPose, + type PlayingSound, +} from "cubi/client"; import { opens_doors } from "./coilhead.ts"; import { in_facility } from "./interior.ts"; @@ -29,6 +36,8 @@ const MAX_SPEED_STEPS = 8; const CHASE_SPEED = "dangerous_business:jester_chase"; // it only waits a moment at a closed door const OPEN_DOOR_TICKS = 20; +// the crank turns once a second while it cranks, clicking this many ticks into each, where its crank clip has it +const CRANK_CLICK_TICK = 12; export function register_jester(ctx: ServerContext) { const builtin = ctx.entities.goals; @@ -47,6 +56,7 @@ export function register_jester(ctx: ServerContext) { state = next; ticks = 0; mob.set_client_data("state", next); + if (next === AGGRO) mob.play_sound("dangerous_business:jester/pop"); }; // what it's doing, which the goals after it follow @@ -67,6 +77,7 @@ export function register_jester(ctx: ServerContext) { set_state(CRANKING); } } else if (state === CRANKING) { + if (ticks % 20 === CRANK_CLICK_TICK) mob.play_sound("dangerous_business:jester/turn_crank"); if (ticks >= CRANK_TICKS) set_state(AGGRO); } else { const target = nearest_player(ctx, mob); @@ -148,7 +159,8 @@ function nearest_player(ctx: ServerContext, mob: MobRef) { } // what bedrock's animation controller did: idle and walk while it roams, crank, then pop, then its chase. what its -// clips did with expressions is here as code +// clips did with expressions is here as code. its music box plays while it cranks, and it screams while it's popped, +// for whoever is near then, even if they came after it started export function register_jester_animator(ctx: ClientContext) { const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`); const idle = clip("idle"); @@ -163,12 +175,24 @@ export function register_jester_animator(ctx: ClientContext) { const cranking = new AnimationState(); const popped = new AnimationState(); const attacking = new AnimationState(); + let theme: PlayingSound | undefined; + let scream: PlayingSound | undefined; return { on_data(field, time) { if (field !== "state") return; const state = jester.get_client_data("state"); cranking.animate_when(state === CRANKING, time); popped.animate_when(state === AGGRO, time); + theme = sound_while( + state === CRANKING, + theme, + () => ctx.sounds.play("dangerous_business:jester/theme", { follow: jester.id }), + ); + scream = sound_while( + state === AGGRO, + scream, + () => ctx.sounds.play("dangerous_business:jester/scream", { follow: jester.id, loop: true }), + ); }, pose(pose, time) { if (cranking.started) { @@ -200,6 +224,15 @@ export function register_jester_animator(ctx: ClientContext) { }); } +// a sound that goes with a state: played once it holds, stopped once it doesn't +function sound_while(holds: boolean, sound: PlayingSound | undefined, play: () => PlayingSound) { + if (!holds) { + sound?.stop(); + return undefined; + } + return sound ?? play(); +} + // clip as it walks, a second of it for ticks of walk_position, and standing as strongly as it isn't walking function walking( pose: EntityPose, diff --git a/mods/dangerous_business/scripts/shared.ts b/mods/dangerous_business/scripts/shared.ts index 72216fc..4ca626e 100644 --- a/mods/dangerous_business/scripts/shared.ts +++ b/mods/dangerous_business/scripts/shared.ts @@ -110,13 +110,16 @@ function register_facility_door(ctx: SharedContext) { }); } -// the door component's params: "pairs_with" is the tag of doors it makes double doors with +// the door component's params: "pairs_with" is the tag of doors it makes double doors with, and the sounds it makes +// opening and closing export interface DoorParams { pairs_with: string; + open_sound: string; + close_sound: string; } -// what set_door_open needs, which shared, server and client phases all have -type DoorContext = Pick & { +// what set_door_open needs, which shared and server phases both have +type DoorContext = Pick & { world: Pick; }; @@ -128,11 +131,14 @@ function door_at(ctx: DoorContext, x: number, y: number, z: number, params: Door } // opens (open 1) or closes (open 0) the door with a half at x y z: both its halves, and the other door of a double -// door with it when it's open or closed like this one. players do it by clicking, monsters from their goals. returns -// whether it changed +// door with it when it's open or closed like this one, with its sound. players do it by clicking, monsters from their +// goals. returns whether it changed export function set_door_open(ctx: DoorContext, x: number, y: number, z: number, open: number, params: DoorParams) { const lower = ctx.world.get_state(x, y, z, "half") ? y - 1 : y; const state = (name: string, at_x = x, at_z = z) => ctx.world.get_state(at_x, lower, at_z, name); + if (state("open") !== open) { + ctx.sounds.play(open ? params.open_sound : params.close_sound, x + 0.5, lower + 1, z + 0.5); + } // both halves of the door whose lower half is at x z const set_halves = (at_x: number, at_z: number) => { if (ctx.world.get_block(at_x, lower + 1, at_z) === ctx.world.get_block(at_x, lower, at_z)) { diff --git a/mods/dangerous_business/sounds.json b/mods/dangerous_business/sounds.json new file mode 100644 index 0000000..81675bd --- /dev/null +++ b/mods/dangerous_business/sounds.json @@ -0,0 +1,29 @@ +{ + "format_version": 1, + "sounds": { + "jester/step": { "files": ["jester/stomp_1", "jester/stomp_2", "jester/stomp_3"], "category": "entities" }, + "jester/turn_crank": { + "files": ["jester/turn_crank_1", "jester/turn_crank_2", "jester/turn_crank_3"], + "category": "entities" + }, + "jester/theme": { "files": ["jester/theme"], "category": "entities", "range": 32 }, + "jester/pop": { "files": ["jester/pop"], "category": "entities", "range": 32 }, + "jester/scream": { "files": ["jester/scream"], "category": "entities", "range": 32 }, + "coilhead/step": { + "files": ["coilhead/footstep_1", "coilhead/footstep_2", "coilhead/footstep_3", "coilhead/footstep_4"], + "category": "entities" + }, + "coilhead/stop": { + "files": ["coilhead/spring_1", "coilhead/spring_2", "coilhead/spring_3"], + "category": "entities", + "range": 24 + }, + "coilhead/seen": { + "files": ["coilhead/spring_wobble_1", "coilhead/spring_wobble_2"], + "category": "entities", + "range": 24 + }, + "door/open": { "files": ["door/door_close_1", "door/door_close_2"], "category": "blocks" }, + "door/close": { "files": ["door/door_open_1", "door/door_open_2"], "category": "blocks" } + } +} diff --git a/mods/dangerous_business/sounds/coilhead/footstep_1.ogg b/mods/dangerous_business/sounds/coilhead/footstep_1.ogg new file mode 100644 index 0000000..8f018f5 Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/footstep_1.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/footstep_2.ogg b/mods/dangerous_business/sounds/coilhead/footstep_2.ogg new file mode 100644 index 0000000..caa0bd5 Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/footstep_2.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/footstep_3.ogg b/mods/dangerous_business/sounds/coilhead/footstep_3.ogg new file mode 100644 index 0000000..1a800b9 Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/footstep_3.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/footstep_4.ogg b/mods/dangerous_business/sounds/coilhead/footstep_4.ogg new file mode 100644 index 0000000..e9b98fb Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/footstep_4.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/spring_1.ogg b/mods/dangerous_business/sounds/coilhead/spring_1.ogg new file mode 100644 index 0000000..fd930e9 Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/spring_1.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/spring_2.ogg b/mods/dangerous_business/sounds/coilhead/spring_2.ogg new file mode 100644 index 0000000..6d3b1a1 Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/spring_2.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/spring_3.ogg b/mods/dangerous_business/sounds/coilhead/spring_3.ogg new file mode 100644 index 0000000..457486d Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/spring_3.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/spring_wobble_1.ogg b/mods/dangerous_business/sounds/coilhead/spring_wobble_1.ogg new file mode 100644 index 0000000..d58deb7 Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/spring_wobble_1.ogg differ diff --git a/mods/dangerous_business/sounds/coilhead/spring_wobble_2.ogg b/mods/dangerous_business/sounds/coilhead/spring_wobble_2.ogg new file mode 100644 index 0000000..8119d90 Binary files /dev/null and b/mods/dangerous_business/sounds/coilhead/spring_wobble_2.ogg differ diff --git a/mods/dangerous_business/sounds/door/door_close_1.ogg b/mods/dangerous_business/sounds/door/door_close_1.ogg new file mode 100644 index 0000000..f35aa3c Binary files /dev/null and b/mods/dangerous_business/sounds/door/door_close_1.ogg differ diff --git a/mods/dangerous_business/sounds/door/door_close_2.ogg b/mods/dangerous_business/sounds/door/door_close_2.ogg new file mode 100644 index 0000000..48150f9 Binary files /dev/null and b/mods/dangerous_business/sounds/door/door_close_2.ogg differ diff --git a/mods/dangerous_business/sounds/door/door_open_1.ogg b/mods/dangerous_business/sounds/door/door_open_1.ogg new file mode 100644 index 0000000..aa1dd1a Binary files /dev/null and b/mods/dangerous_business/sounds/door/door_open_1.ogg differ diff --git a/mods/dangerous_business/sounds/door/door_open_2.ogg b/mods/dangerous_business/sounds/door/door_open_2.ogg new file mode 100644 index 0000000..1d309bb Binary files /dev/null and b/mods/dangerous_business/sounds/door/door_open_2.ogg differ diff --git a/mods/dangerous_business/sounds/jester/pop.ogg b/mods/dangerous_business/sounds/jester/pop.ogg new file mode 100644 index 0000000..5bc1ca9 Binary files /dev/null and b/mods/dangerous_business/sounds/jester/pop.ogg differ diff --git a/mods/dangerous_business/sounds/jester/scream.ogg b/mods/dangerous_business/sounds/jester/scream.ogg new file mode 100644 index 0000000..50aa0ae Binary files /dev/null and b/mods/dangerous_business/sounds/jester/scream.ogg differ diff --git a/mods/dangerous_business/sounds/jester/stomp_1.ogg b/mods/dangerous_business/sounds/jester/stomp_1.ogg new file mode 100644 index 0000000..726173f Binary files /dev/null and b/mods/dangerous_business/sounds/jester/stomp_1.ogg differ diff --git a/mods/dangerous_business/sounds/jester/stomp_2.ogg b/mods/dangerous_business/sounds/jester/stomp_2.ogg new file mode 100644 index 0000000..4ffd7ae Binary files /dev/null and b/mods/dangerous_business/sounds/jester/stomp_2.ogg differ diff --git a/mods/dangerous_business/sounds/jester/stomp_3.ogg b/mods/dangerous_business/sounds/jester/stomp_3.ogg new file mode 100644 index 0000000..1724378 Binary files /dev/null and b/mods/dangerous_business/sounds/jester/stomp_3.ogg differ diff --git a/mods/dangerous_business/sounds/jester/theme.ogg b/mods/dangerous_business/sounds/jester/theme.ogg new file mode 100644 index 0000000..89e9daf Binary files /dev/null and b/mods/dangerous_business/sounds/jester/theme.ogg differ diff --git a/mods/dangerous_business/sounds/jester/turn_crank_1.ogg b/mods/dangerous_business/sounds/jester/turn_crank_1.ogg new file mode 100644 index 0000000..3ccc1da Binary files /dev/null and b/mods/dangerous_business/sounds/jester/turn_crank_1.ogg differ diff --git a/mods/dangerous_business/sounds/jester/turn_crank_2.ogg b/mods/dangerous_business/sounds/jester/turn_crank_2.ogg new file mode 100644 index 0000000..edf5f7c Binary files /dev/null and b/mods/dangerous_business/sounds/jester/turn_crank_2.ogg differ diff --git a/mods/dangerous_business/sounds/jester/turn_crank_3.ogg b/mods/dangerous_business/sounds/jester/turn_crank_3.ogg new file mode 100644 index 0000000..23a7870 Binary files /dev/null and b/mods/dangerous_business/sounds/jester/turn_crank_3.ogg differ diff --git a/tests/dangerous_business_test.ts b/tests/dangerous_business_test.ts index 5cbc2f7..12bf32b 100644 --- a/tests/dangerous_business_test.ts +++ b/tests/dangerous_business_test.ts @@ -9,8 +9,10 @@ import { ItemStack } from "@/common/inventory.ts"; import { get_state_value, set_state_value } from "@/common/utils.ts"; import { create_entity } from "@/server/game/entity/entity_types.ts"; import type { Mob } from "@/server/game/entity/mob.ts"; -import { copy_dir, errors_during, load_as_player, test_game, test_mob } from "./helpers.ts"; +import { copy_dir, errors_during, ListOutput, load_as_player, test_game, test_mob, VOLUMES } from "./helpers.ts"; import { animation_for } from "@/client/rendering/entity_animators.ts"; +import { sounds } from "@/client/sounds.ts"; +import type { EntityData } from "@/common/protocol.ts"; const HALF = "dangerous_business:concrete_wall_slab"; const CONNECTS = "dangerous_business:floor_grate"; @@ -154,6 +156,26 @@ Deno.test("a coilhead can't move while a player looks at it, and comes for them assertEquals(coilhead.client_data.frozen, undefined); }); +// the sounds conn was sent since last asked, as [sound, where]: the entity it follows, or the point it's at +const sounds_sent = (t: Awaited>["t"], conn: number): [string, unknown][] => + t.take(conn).filter((m) => m.type === "sound").map((m) => [m.sound, m.entity ?? m.at]); + +Deno.test("a coilhead springs once when a player's look freezes it", async () => { + const { t } = await setup(); + // looking south, away from it + t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 }); + const coilhead = spawn_coilhead(t); + for (let i = 0; i < 5; i++) t.game.tick(); + assertEquals(sounds_sent(t, 1), []); + + t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 }); + for (let i = 0; i < 40; i++) t.game.tick(); + assertEquals(sounds_sent(t, 1), [ + ["dangerous_business:coilhead/stop", coilhead.id], + ["dangerous_business:coilhead/seen", coilhead.id], + ]); +}); + Deno.test("a player can't stop a coilhead through a wall", async () => { const { t } = await setup(); for (let x = -3; x <= 3; x++) { @@ -257,6 +279,24 @@ Deno.test("a coilhead seen while it waits at a closed door stops waiting, and do assertEquals(coilhead.navigation.blocked_by, undefined); }); +Deno.test("a factory door sounds as it opens and closes, for everyone but the player whose click their client heard", async () => { + const { t, state } = await setup(); + const { open } = door_in_wall(t, state); + t.join(2, "bob"); + t.send(2, { type: "move", x: 1.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); + t.game.tick(); + t.take(1); + t.take(2); + + const click = () => t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] }); + click(); + assert(open()); + assertEquals(sounds_sent(t, 1), []); + assertEquals(sounds_sent(t, 2), [["dangerous_business:door/open", [0.5, 101, 0.5]]]); + click(); + assertEquals(sounds_sent(t, 2), [["dangerous_business:door/close", [0.5, 101, 0.5]]]); +}); + // a jester 6 blocks north of alice, in a state from its client data like one that was saved function spawn_jester(t: Awaited>["t"], state?: number) { const jester = create_entity(t.game.entities, JESTER)! as Mob; @@ -302,6 +342,20 @@ Deno.test("a popped jester runs down the nearest player faster and faster, and b assertEquals(speed(), walking); }); +Deno.test("a jester's crank clicks once a second while it winds, and it pops once", async () => { + const { t } = await setup(); + const jester = spawn_jester(t, 2); + for (let i = 0; i < 100; i++) t.game.tick(); + const cranking = sounds_sent(t, 1); + assertEquals(cranking.length, 5); + assertEquals(new Set(cranking.map(([sound]) => sound)), new Set(["dangerous_business:jester/turn_crank"])); + + for (let i = 0; i < 1000 && jester.client_data.state !== 3; i++) t.game.tick(); + assertEquals(sounds_sent(t, 1).filter(([sound]) => sound !== "dangerous_business:jester/turn_crank"), [ + ["dangerous_business:jester/pop", jester.id], + ]); +}); + // last, since it loads the mod the way a player gets it, in place of the server's Deno.test("a coilhead's head springs back and forth when it freezes, and stays still when it stands", async () => { const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" }); @@ -349,3 +403,49 @@ Deno.test("a jester keeps its tendrils in the box until it pops, and turns its c assertNotEquals(pose(2, 5).get("crank")!.rotation, pose(2, 15).get("crank")!.rotation); Deno.removeSync(dir, { recursive: true }); }); + +Deno.test("a jester's music box plays while it winds, and it screams while it's popped", async () => { + const dir = Deno.makeTempDirSync({ prefix: "dangerous_business_client_" }); + copy_dir("mods/dangerous_business", `${dir}/dangerous_business`); + await load_as_player(dir); + const output = new ListOutput(); + sounds.set_output(output); + const jester = { + id: "e1", + type: JESTER, + data: { client_data: { state: 2 } } as EntityData, + age: 0, + yaw: 0, + head_yaw: 0, + pitch: 0, + walk_animation: () => ({ position: 0, speed: 0 }), + attack_animation: () => 0, + }; + const frame = () => + sounds.frame( + { x: 0, y: 0, z: 0, yaw: 0, pitch: 0 }, + (id) => id === "e1" ? { x: 0, y: 0, z: 3 } : undefined, + VOLUMES, + ); + const set_state = (state: number) => { + const before = jester.data; + jester.data = { client_data: { state } }; + animation.data_changed(before); + frame(); + }; + // a player coming near while it already winds hears it too + const animation = animation_for(jester)!; + frame(); + const [theme] = output.playing; + assertEquals([theme.file.id, theme.settings.loop], ["dangerous_business:jester/theme", false]); + + set_state(3); + assert(theme.stopped); + const [scream] = output.playing; + assertEquals([scream.file.id, scream.settings.loop], ["dangerous_business:jester/scream", true]); + + set_state(1); + assertEquals(output.playing, []); + sounds.stop_all(); + Deno.removeSync(dir, { recursive: true }); +}); diff --git a/tests/helpers.ts b/tests/helpers.ts index 7fba115..22db1de 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -36,6 +36,8 @@ import { RecipeBook } from "@/common/mod_loader.ts"; import type { EntityJson } from "@/common/mod_data.ts"; import { mod_entity_factory } from "@/server/game/entity/mod_entity.ts"; import type { Mob } from "@/server/game/entity/mob.ts"; +import type { SoundFile, SoundOutput, Voice, VoiceSettings, Volumes } from "@/client/sounds.ts"; +import type { Position } from "@/common/mod_api/shared.ts"; // the repo's own mods/, read once: reading and checking every mod's files is most of starting a game in a test. only // mods/ is kept, since tests change the temporary folders they make between games @@ -482,3 +484,40 @@ export async function errors_during(fn: () => unknown): Promise { } return errors; } + +// a client's sound output that only keeps what it was told, see client/sounds.ts +export 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; + } +} + +export 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); + } +} + +// every sound category at full volume +export const VOLUMES: Volumes = { master: 1, music: 1, ambient: 1, blocks: 1, entities: 1, players: 1, ui: 1 }; diff --git a/tests/sound_test.ts b/tests/sound_test.ts index 0066ae3..c9a8ffd 100644 --- a/tests/sound_test.ts +++ b/tests/sound_test.ts @@ -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([["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(); + 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"));