Send block data to clients
This commit is contained in:
@@ -31,7 +31,6 @@ import { Camera } from "../camera.ts";
|
||||
import type { Entity } from "../entity/entity.ts";
|
||||
import type { ModelJson } from "$/common/block_models.ts";
|
||||
import { clip_shape, collision_shape_at, FULL_SHAPE, type Shape, shape_at } from "$/common/block_shapes.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { BlockPredictions } from "./block_predictions.ts";
|
||||
|
||||
export interface Block {
|
||||
@@ -98,6 +97,14 @@ export interface BlockHitResult {
|
||||
hit: [number, number, number];
|
||||
}
|
||||
|
||||
// a block's client data, see ClientLevel.block_data
|
||||
export interface BlockData {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
data: Record<string, unknown>;
|
||||
}
|
||||
|
||||
const NEIGHBOR_OFFSETS = [[-1, 0], [1, 0], [0, -1], [0, 1]] as const;
|
||||
// light spreads diagonally too, so meshing and lighting need all 8
|
||||
const ALL_NEIGHBOR_OFFSETS = [...NEIGHBOR_OFFSETS, [-1, -1], [1, -1], [-1, 1], [1, 1]] as const;
|
||||
@@ -133,8 +140,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 }>>();
|
||||
// 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>>();
|
||||
|
||||
// server_blocks is the server's block id for each of its numeric ids
|
||||
constructor(server_blocks: string[], view_distance: number) {
|
||||
@@ -517,24 +524,34 @@ export class ClientLevel {
|
||||
this.delete_chunk_mesh(chunk);
|
||||
this.chunks.delete(key);
|
||||
this.#dirty.delete(chunk);
|
||||
this.block_items.delete(key);
|
||||
this.block_data.delete(key);
|
||||
}
|
||||
|
||||
// see set_block_item in common/protocol.ts
|
||||
set_block_item(x: number, y: number, z: number, item: ItemStack | undefined) {
|
||||
// see block_data in common/protocol.ts
|
||||
set_block_data(x: number, y: number, z: number, changed: Record<string, unknown>) {
|
||||
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);
|
||||
if (!this.chunks.has(key)) return;
|
||||
let blocks = this.block_data.get(key);
|
||||
const position = `${x},${y},${z}`;
|
||||
const data = { ...blocks?.get(position)?.data };
|
||||
for (const [field, value] of Object.entries(changed)) {
|
||||
if (value === null) delete data[field];
|
||||
else data[field] = value;
|
||||
}
|
||||
if (Object.keys(data).length > 0) {
|
||||
if (!blocks) {
|
||||
blocks = new Map();
|
||||
this.block_data.set(key, blocks);
|
||||
}
|
||||
blocks.set(position, { x, y, z, data });
|
||||
} else if (blocks?.delete(position) && blocks.size === 0) {
|
||||
this.block_data.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
get_block_data(x: number, y: number, z: number): Readonly<Record<string, unknown>> | undefined {
|
||||
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
|
||||
return this.block_data.get(key)?.get(`${x},${y},${z}`)?.data;
|
||||
}
|
||||
|
||||
// a chunk is only meshed once all its neighbors exist, otherwise its border faces and light would be
|
||||
|
||||
Reference in New Issue
Block a user