diff --git a/client/entity/entity.ts b/client/entity/entity.ts index 41b885b..f00c4f6 100644 --- a/client/entity/entity.ts +++ b/client/entity/entity.ts @@ -91,9 +91,9 @@ export abstract class Entity { this.walk_position += this.walk_speed; } - // after moving each tick: a step every STEP_DISTANCE it walks with a block under its feet, own when it has its own - // step sound (see ENTITY_SOUNDS), the block's otherwise - play_steps(own: string | undefined) { + // after moving each tick: a step every STEP_DISTANCE it walks with a block under its feet, what own gives when it + // has its own step sound (see ENTITY_SOUNDS), the block's otherwise + play_steps(own?: () => string | undefined) { const [x, y, z] = [Math.floor(this.x), Math.floor(this.y - 0.2), Math.floor(this.z)]; const nid = this.level.get_block(x, y, z); const below = nid === AIR ? undefined : EverythingRegistry.get_by_id("blocks", nid); @@ -102,7 +102,8 @@ export abstract class Entity { if (this.#walked < STEP_DISTANCE) return; this.#walked %= STEP_DISTANCE; const at = { x: this.x, y: this.y, z: this.z }; - if (own) sounds.play(own, { at }); + const sound = own?.(); + if (sound) sounds.play(sound, { at }); else sounds.play_of(below.sounds, "step", { at, volume: BLOCK_STEP_VOLUME }); } diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index 9659399..ea04847 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -199,7 +199,7 @@ export class LocalPlayer extends Player { if (Math.abs(this.vz) < MIN_SPEED) this.vz = 0; this.update_swim_animation(); // creeping and flying are quiet - if (this.on_ground && !this.sneaking) this.play_steps(undefined); + if (this.on_ground && !this.sneaking) this.play_steps(); this.#send_position( (this.on_ground && !was_on_ground) || this.swimming !== was_swimming || this.holding_sneak !== was_holding_sneak, diff --git a/client/entity/remote_entity.ts b/client/entity/remote_entity.ts index 8bf70af..35ad352 100644 --- a/client/entity/remote_entity.ts +++ b/client/entity/remote_entity.ts @@ -107,7 +107,10 @@ export class RemoteEntity extends Entity { this.update_walk_animation(); if (this.swing_ticks !== undefined && ++this.swing_ticks >= SWING_TICKS) this.swing_ticks = undefined; if (!this.dead) { - this.play_steps(sound_of(this.#sounds, "step")); + this.play_steps(() => { + const own = sound_of(this.#sounds, "step"); + return this.animation ? this.animation.step_sound(own) : own; + }); if (--this.#ambient_ticks <= 0) { this.#ambient_ticks = random_ambient_ticks(); sounds.play_of(this.#sounds, "ambient", { follow: this.id }); diff --git a/client/entity/remote_player.ts b/client/entity/remote_player.ts index 104e586..26a6ca2 100644 --- a/client/entity/remote_player.ts +++ b/client/entity/remote_player.ts @@ -58,6 +58,6 @@ export class RemotePlayer extends Player { this.z += (this.target_z - this.z) * t; this.update_walk_animation(); this.update_swim_animation(); - this.play_steps(undefined); + this.play_steps(); } } diff --git a/client/rendering/entity_animators.ts b/client/rendering/entity_animators.ts index 67f7f8c..c234949 100644 --- a/client/rendering/entity_animators.ts +++ b/client/rendering/entity_animators.ts @@ -74,6 +74,14 @@ export class EntityAnimation { return posed ? offsets : undefined; } + // the sound its next step makes, what the animator says when it has a step_sound, own otherwise + step_sound(own: string | undefined): string | undefined { + const animator = this.#animator; + if (!animator?.step_sound) return own; + let sound: string | undefined; + return this.#run("step_sound", () => sound = animator.step_sound!()) ? sound : own; + } + #on_data(field: string) { const animator = this.#animator; if (animator?.on_data) this.#run("on_data", () => animator.on_data!(field, this.#source.age)); diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index 5c7b66d..ff932e8 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -189,6 +189,9 @@ export interface EntityAnimator { // a field of its client data changed (see MobRef.set_client_data in cubi/server), or was already set when the // animator was made on_data?(field: string, time: number): void; + // the sound its next step makes in place of its json's step sound, or undefined for the step of the block it walks + // on, like a mob that only stomps once it's angry. see docs/modding/resources/sounds.md + step_sound?(): string | undefined; } // the entity an animator poses diff --git a/docs/modding/resources/sounds.md b/docs/modding/resources/sounds.md index a80eede..6dc6467 100644 --- a/docs/modding/resources/sounds.md +++ b/docs/modding/resources/sounds.md @@ -67,7 +67,15 @@ block, and `check-mods` reports a block or entity whose `sounds` has none of its The player breaking or placing a block hears it right away from their own client, and everyone else from the server. Players step with the blocks they walk on, except the one playing while they sneak or fly. Anything about a mob that depends on what it's doing is its scripts' to play, see [From the Server](#from-the-server) and -[Lasting Sounds](#lasting-sounds). +[Lasting Sounds](#lasting-sounds). A mob whose steps change with what it's doing gets them from its animator's +`step_sound`, which says the sound each step makes, or `undefined` for the block's step: + +```ts +ctx.render.register_entity_animator("my_mod:walker", (walker) => ({ + pose(pose, time) {/* ... */}, + step_sound: () => walker.get_client_data("angry") ? "my_mod:walker/stomp" : undefined, +})); +``` ## Playing Sounds diff --git a/mods/dangerous_business/entities/jester.json b/mods/dangerous_business/entities/jester.json index 3174f73..9a784c1 100644 --- a/mods/dangerous_business/entities/jester.json +++ b/mods/dangerous_business/entities/jester.json @@ -5,7 +5,6 @@ "model": "dangerous_business:jester", "texture": "dangerous_business:jester", "behavior": "dangerous_business:jester", - "sounds": "dangerous_business:jester", "width": 0.8, "height": 1.9, "max_health": 40, diff --git a/mods/dangerous_business/scripts/jester.ts b/mods/dangerous_business/scripts/jester.ts index cc2df34..aa26152 100644 --- a/mods/dangerous_business/scripts/jester.ts +++ b/mods/dangerous_business/scripts/jester.ts @@ -161,8 +161,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. its music box plays while it cranks, and it screams while it's popped. -// a player who comes upon it partway sees and hears it from as far as it's got, by when its state started +// clips did with expressions is here as code. its music box plays while it cranks, and it screams and stomps while +// it's popped. a player who comes upon it partway sees and hears it from as far as it's got, by when its state started export function register_jester_animator(ctx: ClientContext) { const clip = (name: string) => ctx.render.clip(`dangerous_business:jester/${name}`); const idle = clip("idle"); @@ -195,6 +195,8 @@ export function register_jester_animator(ctx: ClientContext) { theme = sound_while(state === CRANKING, theme, () => play("dangerous_business:jester/theme")); scream = sound_while(state === AGGRO, scream, () => play("dangerous_business:jester/scream")); }, + step_sound: () => + jester.get_client_data("state") === AGGRO ? "dangerous_business:jester/stomp" : undefined, pose(pose, time) { if (cranking.started) { pose.play_state(crank, cranking, time); diff --git a/mods/dangerous_business/sounds.json b/mods/dangerous_business/sounds.json index 81675bd..c9604eb 100644 --- a/mods/dangerous_business/sounds.json +++ b/mods/dangerous_business/sounds.json @@ -1,7 +1,7 @@ { "format_version": 1, "sounds": { - "jester/step": { "files": ["jester/stomp_1", "jester/stomp_2", "jester/stomp_3"], "category": "entities" }, + "jester/stomp": { "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" diff --git a/tests/animation_test.ts b/tests/animation_test.ts index 00f6aee..69b41bb 100644 --- a/tests/animation_test.ts +++ b/tests/animation_test.ts @@ -303,6 +303,20 @@ Deno.test("a clip plays from when its state started, and with walking as strongl assertAlmostEquals(animation.pose(15)!.get("arm")!.rotation[0], 22.5); }); +Deno.test("an animator's step_sound picks each step's sound, and one without it keeps the entity's own", () => { + const { type } = animator_type("stomper", (entity) => ({ + pose() {}, + step_sound: () => entity.get_client_data("angry") ? "test:stomp" : undefined, + })); + const entity = animated(type); + const animation = animation_for(entity)!; + assertEquals(animation.step_sound("test:own"), undefined); + entity.data = { client_data: { angry: true } }; + assertEquals(animation.step_sound("test:own"), "test:stomp"); + const { type: quiet } = animator_type("quiet", () => ({ pose() {} })); + assertEquals(animation_for(animated(quiet))!.step_sound("test:own"), "test:own"); +}); + Deno.test("an animator that throws is removed, and the entity is posed without it", async () => { const { type } = animator_type("thrower", () => ({ pose(pose) {