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
+24 -1
View File
@@ -2,12 +2,13 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type { EntityData, EntityEvent, EntityInfo } from "@/common/protocol.ts";
import type { EntityJson } from "@/common/mod_data.ts";
import { type EntityJson, sound_of } from "@/common/mod_data.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import { wrap_angle } from "@/common/utils.ts";
import { animation_for, type EntityAnimation } from "../rendering/entity_animators.ts";
import type { ClientLevel } from "../level/client_level.ts";
import { Entity } from "./entity.ts";
import { sounds } from "../sounds.ts";
// how many ticks a server position takes to reach, like minecraft's lerpTo
const LERP_TICKS = 3;
@@ -16,6 +17,8 @@ const HURT_TICKS = 10;
const DEATH_TICKS = 20;
// how long its attack swing takes, like minecraft's mobs
const SWING_TICKS = 6;
// how long it goes between its ambient sounds, picked at random each time
const AMBIENT_TICKS = [80, 240];
// an entity the server simulates, like a mob. this only follows where it's told it is and keeps what it was told
// it looks like, for drawing it
@@ -40,6 +43,10 @@ export class RemoteEntity extends Entity {
#target = { x: 0, y: 0, z: 0, yaw: 0, pitch: 0, head_yaw: 0 };
#lerp_ticks = 0;
// what its sounds' ids start with, see ENTITY_SOUNDS
#sounds: string | undefined;
// until its next ambient sound
#ambient_ticks = random_ambient_ticks();
constructor(level: ClientLevel, info: Extract<EntityInfo, { kind: "entity" }>) {
// the size of mods' entities, player sized for kinds the client doesn't know
@@ -47,6 +54,7 @@ export class RemoteEntity extends Entity {
super(level, info.id, json?.width ?? 0.6, json?.height ?? 1.8, json ? json.height * 0.85 : 1.62);
this.type = info.type;
this.data = info.data;
this.#sounds = json?.sounds;
this.set_position(info.x, info.y, info.z);
this.yaw = info.yaw;
this.pitch = info.pitch;
@@ -69,11 +77,14 @@ export class RemoteEntity extends Entity {
on_event(event: EntityEvent) {
if (event === "hurt") {
this.hurt_time = HURT_TICKS;
sounds.play_of(this.#sounds, "hurt", { follow: this.id });
} else if (event === "swing") {
// swinging again halfway through doesn't restart it, like minecraft
if (this.swing_ticks === undefined || this.swing_ticks >= SWING_TICKS / 2) this.swing_ticks = 0;
} else {
this.dead = true;
// where it died, it's gone soon after
sounds.play_of(this.#sounds, "death", { at: { x: this.x, y: this.y, z: this.z } });
}
}
@@ -95,6 +106,13 @@ 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"));
if (--this.#ambient_ticks <= 0) {
this.#ambient_ticks = random_ambient_ticks();
sounds.play_of(this.#sounds, "ambient", { follow: this.id });
}
}
}
// how far through its attack swing it is, 0 to 1, like minecraft's attackTime. 0 when it isn't swinging
@@ -102,3 +120,8 @@ export class RemoteEntity extends Entity {
return this.swing_ticks === undefined ? 0 : Math.min(1, (this.swing_ticks + partial_tick) / SWING_TICKS);
}
}
function random_ambient_ticks() {
const [low, high] = AMBIENT_TICKS;
return low + Math.floor(Math.random() * (high - low));
}