Blocks interacting on the world api

This commit is contained in:
2026-10-15 12:49:44 -03:00
parent 7faa070d83
commit b43e8fe8b8
15 changed files with 493 additions and 28 deletions
+14
View File
@@ -146,6 +146,9 @@ export class ClientLevel {
// the client data of blocks that have some, see BlockRef.set_client_data, by chunk_key then position_key "x,y,z"
block_data = new Map<number, Map<string, BlockData>>();
visuals = new Visuals(this);
// cracks on blocks machines are breaking, the stage to draw by position_key "x,y,z", see break_progress in
// common/protocol.ts
break_progress = new Map<string, { x: number; y: number; z: number; stage: number }>();
// server_blocks is the server's block id for each of its numeric ids
constructor(server_blocks: string[], view_distance: number) {
@@ -533,6 +536,17 @@ export class ClientLevel {
this.#dirty.delete(chunk);
this.block_data.delete(key);
this.visuals.unload(cx, cz);
for (const [position, { x, z }] of this.break_progress) {
if (Math.floor(x / CHUNK_SIZE) === cx && Math.floor(z / CHUNK_SIZE) === cz) {
this.break_progress.delete(position);
}
}
}
// see break_progress in common/protocol.ts
set_break_progress(x: number, y: number, z: number, stage: number | undefined) {
if (stage === undefined) this.break_progress.delete(`${x},${y},${z}`);
else this.break_progress.set(`${x},${y},${z}`, { x, y, z, stage });
}
// see block_data in common/protocol.ts
+10
View File
@@ -189,6 +189,16 @@ export class ClientPacketListener {
case "block_data":
level.set_block_data(message.x, message.y, message.z, message.data);
break;
case "break_progress":
level.set_break_progress(message.x, message.y, message.z, message.stage);
break;
case "push": {
const player = client.player;
player.vx += message.motion[0];
player.vy += message.motion[1];
player.vz += message.motion[2];
break;
}
case "chunk_batch_start":
this.#chunk_batch.on_batch_start();
break;
+10 -11
View File
@@ -31,7 +31,7 @@ import {
} from "$/client/renderer/mod.ts";
import { type BlockRegistry, EverythingRegistry, type RenderLayer } from "$/common/everything_registry.ts";
import { bake_block, type BakedQuad, FACE_INDEX, looks_by_state, type ModelJson } from "$/common/block_models.ts";
import { ID_MASK } from "$/common/constants.ts";
import { BREAK_STAGES, ID_MASK } from "$/common/constants.ts";
import { type Shape, shape_at } from "$/common/block_shapes.ts";
import type { BlockHitResult } from "$/client/level/client_level.ts";
import { FACE_GROUPS, UNALIGNED_GROUP } from "$/client/workers/chunk_messages.ts";
@@ -157,30 +157,29 @@ export class LevelRenderer {
draw_visuals(level.visuals, level.image);
}
// the cracks on the block being broken, drawn over its model's faces like minecraft's crumbling overlay, so a slab
// or a plant cracks where it is and not on a whole cube around it
// the cracks on the block being broken, and on the ones machines are breaking, drawn over their models' faces like
// minecraft's crumbling overlay, so a slab or a plant cracks where it is and not on a whole cube around it
render_destroy_progress(level: ClientLevel, game_mode: MultiPlayerGameMode) {
for (const { x, y, z, stage } of level.break_progress.values()) this.#draw_cracks(level, x, y, z, stage);
const block = game_mode.destroy_pos;
if (!block) {
return;
}
const progress = Math.max(0, Math.min(1, game_mode.destroy_progress / game_mode.destroy_time));
const stage = Math.round(progress * 8);
if (Number.isNaN(stage)) {
return;
}
const stage = Math.round(progress * BREAK_STAGES);
if (!Number.isNaN(stage)) this.#draw_cracks(level, block.x, block.y, block.z, stage);
}
const value = level.get_block_value(block.x, block.y, block.z);
#draw_cracks(level: ClientLevel, x: number, y: number, z: number, stage: number) {
const value = level.get_block_value(x, y, z);
const info = EverythingRegistry.get_by_id<BlockRegistry>("blocks", value & ID_MASK);
if (!info) {
return;
}
const tex = AssetManager.instance.get<Texture>("bworld:textures");
const region = get_sprite_region(`engine:break_${stage}`);
for (const quad of this.#destroy_quads(info, value)) {
push_decal_quad(tex, quad, block.x, block.y, block.z, region);
push_decal_quad(tex, quad, x, y, z, region);
}
}