Proper cutting board

This commit is contained in:
2026-09-29 00:04:56 -03:00
parent a51adba46f
commit 445148e812
26 changed files with 549 additions and 16 deletions
+21
View File
@@ -19,6 +19,7 @@ import { crosses_planes } from "../workers/translucent_sort.ts";
import { Camera } from "../camera.ts";
import type { Entity } from "../entity/entity.ts";
import type { ModelJson } from "$/common/block_models.ts";
import type { ItemStack } from "$/common/inventory.ts";
export interface Block {
id: string;
@@ -106,6 +107,8 @@ export class ClientLevel {
// every entity this client knows about, the local player included, by id
entities = new Map<string, Entity>();
// what blocks with item_display show, by chunk_key then position_key "x,y,z"
block_items = new Map<number, Map<string, { x: number; y: number; z: number; item: ItemStack }>>();
// server_blocks is the server's block id for each of its numeric ids
constructor(server_blocks: string[], view_distance: number) {
@@ -390,6 +393,24 @@ export class ClientLevel {
}
this.delete_chunk_mesh(chunk);
this.chunks.delete(key);
this.block_items.delete(key);
}
// see set_block_item in common/protocol.ts
set_block_item(x: number, y: number, z: number, item: ItemStack | undefined) {
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
let items = this.block_items.get(key);
if (item) {
if (!this.chunks.has(key)) return;
if (!items) {
items = new Map();
this.block_items.set(key, items);
}
items.set(`${x},${y},${z}`, { x, y, z, item });
} else {
items?.delete(`${x},${y},${z}`);
if (items?.size === 0) this.block_items.delete(key);
}
}
// a chunk is only meshed once all its neighbors exist, otherwise its border faces and light would be