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
+17
View File
@@ -14,8 +14,15 @@ import type { EntityJson } from "@/common/mod_data.ts";
import { RemoteEntity } from "./entity/remote_entity.ts";
import { Player } from "./entity/player.ts";
import type { Entity } from "./entity/entity.ts";
import { sounds } from "./sounds.ts";
const CREATIVE_DESTROY_DELAY = 2;
// a block being broken makes its hit sound every this many ticks, under its other sounds
const HIT_SOUND_TICKS = 4;
const HIT_VOLUME = 0.25;
// the middle of the block at x y z, where its sounds come from
const middle = ({ x, y, z }: { x: number; y: number; z: number }) => ({ x: x + 0.5, y: y + 0.5, z: z + 0.5 });
// breaking and using blocks against a server, like minecraft's MultiPlayerGameMode. the client times breaking
// and guesses the results so they feel instant, the server decides what really happens
@@ -26,6 +33,8 @@ export class MultiPlayerGameMode {
destroy_pos: { x: number; y: number; z: number } | undefined;
destroy_progress = 0;
destroy_time = 0;
// ticks spent breaking it, for its hit sound
#hit_ticks = 0;
// ticks left before creative can break another block while the button is held, so holding it doesn't clear a
// line of blocks every tick
destroy_delay = 0;
@@ -52,6 +61,10 @@ export class MultiPlayerGameMode {
const pos = this.destroy_pos;
if (!pos || pos.x !== hit.x || pos.y !== hit.y || pos.z !== hit.z) {
this.destroy_progress = 0;
this.#hit_ticks = 0;
}
if (this.#hit_ticks++ % HIT_SOUND_TICKS === 0) {
sounds.play_of(block_info.sounds, "hit", { at: middle(hit), volume: HIT_VOLUME });
}
this.destroy_pos = { x: hit.x, y: hit.y, z: hit.z };
// blocks without a toughness, like water, can only be broken in creative
@@ -61,6 +74,8 @@ export class MultiPlayerGameMode {
if (this.destroy_progress >= this.destroy_time) {
// show it right away, the server decides drops and corrects us if it disagrees
this.#predict((seq) => {
// the server sends it to everyone else, see GameServer.destroy_block
sounds.play_of(block_info.sounds, "break", { at: middle(hit) });
this.client.level.break_block(hit.x, hit.y, hit.z);
predict_neighbors(hit.x, hit.y, hit.z);
this.client.connection.send({ type: "break_block", x: hit.x, y: hit.y, z: hit.z, seq });
@@ -145,6 +160,8 @@ export class MultiPlayerGameMode {
below_id,
) && !(place_block?.has_collision && this.#occupied(target.x, target.y, target.z, place_block, place_value!));
if ((!block_info.interactive || skips_block) && place_id && placeable) {
// the server sends it to everyone else, see GameServer.#use_block
sounds.play_of(place_block!.sounds, "place", { at: middle(target) });
level.add_block({ ...target, id: place_id }, place_value! >>> 16);
predict_neighbors(target.x, target.y, target.z);
if (!this.client.player.creative) {