A 16^3 chunk of blocks. Used to represent a world's terrain.
Signature:
export default class Chunk extends EventRouter implements protocol.Serializable Extends: EventRouter
Implements: protocol.Serializable
Chunks make up the bulk of the terrain in a world. Chunks are fixed size, each containing 16^3 possible blocks as a 16x16x16 cube. Chunks can be spawned, despawned, have their unique blocks set or removed, and more. Chunks represent their internal block coordinates in local space, meaning only coordinates x: 0...15, y: 0...15, z: 0...15 are valid.
The Chunk follows a spawn and despawn lifecycle pattern. When you create a chunk, when you're ready to load it in your world you use .spawn(). To remove it, you use .despawn().
Use .setBlock() to set the block type id at a specific local cooridnate. Block type ids are ones that have been registered in the BlockTypeRegistry associated with the World the chunk belongs to. A block type id of 0 is used to represent no block. Removing a block is done by .setBlock(localCoordinate, 0).
This class is an EventRouter, and instances of it emit events with payloads listed under ChunkEventPayloads
// Assume we previously registered a stone block with type id of 10..
const chunk = new Chunk();
chunk.setBlock({ x: 0, y: 0, z: 0 }, 10); // Set the block at 0, 0, 0 to stone
chunk.spawn(world, { x: 16, y: 0, z: 16 }); // Spawn the chunk at global coordinate 16, 0, 16|
Constructor |
Modifiers |
Description |
|---|---|---|
|
Creates a new chunk instance. |
|
Property |
Modifiers |
Type |
Description |
|---|---|---|---|
|
|
Readonly<Uint8Array> |
The blocks in the chunk as a flat Uint8Array[4096], each index as 0 or a block type id. | |
|
|
boolean |
Whether the chunk is actively simulated in the internal physics engine. | |
|
|
boolean |
Whether the chunk has been spawned. | |
|
|
Vector3Like | undefined |
The origin coordinate of the chunk. | |
|
|
World | undefined |
The world the chunk belongs to. |
|
Method |
Modifiers |
Description |
|---|---|---|
|
|
Convert a block index to a local coordinate. | |
|
Despawn the chunk from the world. | ||
|
Get the block type id at a specific local coordinate. | ||
|
|
Convert a global coordinate to a local coordinate. | |
|
|
Convert a global coordinate to an origin coordinate. | |
|
Check if a block exists at a specific local coordinate. | ||
|
|
Check if an origin coordinate is valid. | |
|
Set the block at a specific local coordinate by block type id. | ||
|
Spawn the chunk in the world. |