Entity and block sounds !

This commit is contained in:
2026-11-22 08:23:11 -03:00
parent da44685f54
commit d18fe19027
17 changed files with 332 additions and 20 deletions
+26
View File
@@ -2,7 +2,15 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import { physics } from "@/common/physics.ts";
import { AIR } from "@/common/constants.ts";
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import type { ClientLevel } from "../level/client_level.ts";
import { sounds } from "../sounds.ts";
// how far it walks between steps, in blocks
const STEP_DISTANCE = 1.6;
// a block's step under the block's other sounds, which are as loud as their definitions say
const BLOCK_STEP_VOLUME = 0.15;
// anything that exists in the level and moves, like minecraft's Entity. position is the middle of its feet.
// it's simulated in fixed ticks (common/constants.ts), frames draw it between its last two positions
@@ -44,6 +52,9 @@ export abstract class Entity {
walk_speed = 0;
prev_walk_speed = 0;
// how far it's walked since its last step
#walked = 0;
// which way it hit something on each axis in the last move: 1 or -1, 0 for nothing.
// 1 on y means it's standing on something
colliding_x = 0;
@@ -80,6 +91,21 @@ 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) {
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<BlockRegistry>("blocks", nid);
if (!below) return;
this.#walked += Math.hypot(this.x - this.prev_x, this.z - this.prev_z);
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 });
else sounds.play_of(below.sounds, "step", { at, volume: BLOCK_STEP_VOLUME });
}
walk_animation(partial_tick: number) {
return {
position: this.walk_position - this.walk_speed * (1 - partial_tick),