Sounds on shared and server
This commit is contained in:
@@ -11,6 +11,8 @@ import { biome_season } from "@/common/worldgen/biomes.ts";
|
||||
import { Tags } from "@/common/tags.ts";
|
||||
import type { ItemStack } from "@/common/inventory.ts";
|
||||
import type { EntityEvent, EntityInfo, ServerMessage } from "@/common/protocol.ts";
|
||||
import type { PlayOptions } from "@/common/mod_api/shared.ts";
|
||||
import { sound_range, sound_to_play } from "@/common/mod_data.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
import type { ServerWorld } from "../world.ts";
|
||||
import type { DamageSource, Entity, EntityLevel, SavedEntity } from "./entity.ts";
|
||||
@@ -36,6 +38,21 @@ export interface EntityHost {
|
||||
inside_blocks?(entity: Entity): void;
|
||||
}
|
||||
|
||||
// a sound clients play, with only what's set. where is where it comes from, see the message in common/protocol.ts
|
||||
export function sound_message(
|
||||
sound: string,
|
||||
{ volume, pitch }: PlayOptions,
|
||||
where: { at?: [number, number, number]; entity?: string } = {},
|
||||
): ServerMessage {
|
||||
return {
|
||||
type: "sound",
|
||||
sound,
|
||||
...where,
|
||||
...(volume !== undefined && volume !== 1 && { volume }),
|
||||
...(pitch !== undefined && pitch !== 1 && { pitch }),
|
||||
};
|
||||
}
|
||||
|
||||
// what a client was last told about an entity, so only changes get sent
|
||||
interface Sent {
|
||||
x: number;
|
||||
@@ -189,6 +206,16 @@ export class EntityManager implements EntityLevel {
|
||||
}
|
||||
}
|
||||
|
||||
play_sound(entity: Entity, sound: string, options: PlayOptions) {
|
||||
const range = sound_range(sound_to_play(sound, options), options.volume);
|
||||
const message = sound_message(sound, options, { entity: entity.id });
|
||||
for (const [player, tracked] of this.#tracked) {
|
||||
if (tracked.has(entity.id) && player.hears(entity.x, entity.y, entity.z, range)) {
|
||||
this.#host.send(player, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
on_death(entity: Entity, source: DamageSource) {
|
||||
this.#host.on_entity_death?.(entity, source);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user