Sounds on shared and server
This commit is contained in:
@@ -241,6 +241,8 @@ export async function load_client_mods(listings: ModListing[], mods: Mod[]) {
|
||||
fluid_rules,
|
||||
recipes: () => recipe_book,
|
||||
running: () => rules_setup,
|
||||
// rules only run here to guess what the player's own actions do, so this is the sound their action makes
|
||||
play_sound: (sound, x, y, z, options) => sounds.play(sound, { ...options, at: { x, y, z } }),
|
||||
}));
|
||||
rules_setup = false;
|
||||
check_recipe_types(recipe_book);
|
||||
|
||||
@@ -12,6 +12,7 @@ import { ItemEntity } from "./entity/item_entity.ts";
|
||||
import { RemoteEntity } from "./entity/remote_entity.ts";
|
||||
import { Player } from "./entity/player.ts";
|
||||
import { menu_screens, receive_mod_message, receive_overlay_message } from "./mods.ts";
|
||||
import { sounds } from "./sounds.ts";
|
||||
|
||||
// minecraft's ChunkBatchSizeCalculator: how long loading a chunk takes here, so the server sends as many a tick as
|
||||
// fit in CHUNK_BUDGET_MS
|
||||
@@ -195,6 +196,20 @@ export class ClientPacketListener {
|
||||
case "break_progress":
|
||||
level.set_break_progress(message.x, message.y, message.z, message.stage);
|
||||
break;
|
||||
case "sound": {
|
||||
const at = message.at;
|
||||
try {
|
||||
sounds.play(message.sound, {
|
||||
at: at && { x: at[0], y: at[1], z: at[2] },
|
||||
follow: message.entity,
|
||||
volume: message.volume,
|
||||
pitch: message.pitch,
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn(`the server played a sound this client can't:`, e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "push": {
|
||||
const player = client.player;
|
||||
player.vx += message.motion[0];
|
||||
|
||||
+3
-7
@@ -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}`);
|
||||
|
||||
Reference in New Issue
Block a user