Entity and block sounds !
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -198,6 +198,8 @@ export class LocalPlayer extends Player {
|
||||
if (Math.abs(this.vx) < MIN_SPEED) this.vx = 0;
|
||||
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);
|
||||
this.#send_position(
|
||||
(this.on_ground && !was_on_ground) || this.swimming !== was_swimming ||
|
||||
this.holding_sneak !== was_holding_sneak,
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -58,5 +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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+7
-1
@@ -5,7 +5,7 @@
|
||||
// sounds.json, see docs/modding/resources/sounds.md. this decides what plays, an output (client/audio_output.ts in the
|
||||
// browser) plays it
|
||||
import { EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import { sound_range, sound_to_play, type SoundCategory, type SoundJson } from "@/common/mod_data.ts";
|
||||
import { sound_of, sound_range, sound_to_play, type SoundCategory, type SoundJson } from "@/common/mod_data.ts";
|
||||
import type { ClientPlayOptions, PlayingSound } from "@/common/mod_api/client.ts";
|
||||
import type { Position } from "@/common/mod_api/shared.ts";
|
||||
|
||||
@@ -172,6 +172,12 @@ export class Sounds {
|
||||
return playing;
|
||||
}
|
||||
|
||||
// the sound a block's or entity's "sounds" prefix has for kind, when it has one, see BLOCK_SOUNDS and ENTITY_SOUNDS
|
||||
play_of(prefix: string | undefined, kind: string, options: ClientPlayOptions) {
|
||||
const sound = sound_of(prefix, kind);
|
||||
if (sound) this.play(sound, options);
|
||||
}
|
||||
|
||||
// before each frame: the player hears from listener, sounds following an entity go where it's drawn
|
||||
frame(listener: Listener, entity_position: (id: string) => Position | undefined, volumes: Volumes) {
|
||||
this.#listener = { x: listener.x, y: listener.y, z: listener.z };
|
||||
|
||||
Reference in New Issue
Block a user