Sounds on shared and server
This commit is contained in:
@@ -15,6 +15,7 @@ import type { Season } from "@/common/worldgen/biomes.ts";
|
||||
import { type Collisions, type FluidInCell, physics } from "@/common/physics.ts";
|
||||
import type { EntityData, EntityEvent, EntityInfo } from "@/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
import type { PlayOptions } from "@/common/mod_api/shared.ts";
|
||||
import type { LivingEntity } from "./living_entity.ts";
|
||||
|
||||
// this far below the bottom of the world, entities have fallen out of it
|
||||
@@ -102,6 +103,8 @@ export interface EntityLevel {
|
||||
take_item(item: Entity, by: string): void;
|
||||
// sent to clients tracking the entity, for the renderer: "hurt" flashes it red, "death" starts falling over
|
||||
entity_event(entity: Entity, event: EntityEvent): void;
|
||||
// a sound following it, for the players tracking it within its range, see EntityRef.play_sound
|
||||
play_sound(entity: Entity, sound: string, options: PlayOptions): void;
|
||||
// it died, after dropping its things
|
||||
on_death(entity: Entity, source: DamageSource): void;
|
||||
// what it drops when it dies, from its type's loot table
|
||||
|
||||
@@ -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