Skip to content

Latest commit

 

History

History
358 lines (161 loc) · 5.16 KB

File metadata and controls

358 lines (161 loc) · 5.16 KB

Home > server > Chunk

Chunk class

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

Remarks

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).

Events

This class is an EventRouter, and instances of it emit events with payloads listed under ChunkEventPayloads

Example

// 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

Constructors

Constructor

Modifiers

Description

(constructor)()

Creates a new chunk instance.

Properties

Property

Modifiers

Type

Description

blocks

readonly

Readonly<Uint8Array>

The blocks in the chunk as a flat Uint8Array[4096], each index as 0 or a block type id.

isSimulated

readonly

boolean

Whether the chunk is actively simulated in the internal physics engine.

isSpawned

readonly

boolean

Whether the chunk has been spawned.

originCoordinate

readonly

Vector3Like | undefined

The origin coordinate of the chunk.

world

readonly

World | undefined

The world the chunk belongs to.

Methods

Method

Modifiers

Description

blockIndexToLocalCoordinate(index)

static

Convert a block index to a local coordinate.

despawn()

Despawn the chunk from the world.

getBlockId(localCoordinate)

Get the block type id at a specific local coordinate.

globalCoordinateToLocalCoordinate(globalCoordinate)

static

Convert a global coordinate to a local coordinate.

globalCoordinateToOriginCoordinate(globalCoordinate)

static

Convert a global coordinate to an origin coordinate.

hasBlock(localCoordinate)

Check if a block exists at a specific local coordinate.

isValidOriginCoordinate(coordinate)

static

Check if an origin coordinate is valid.

setBlock(localCoordinate, blockTypeId)

Set the block at a specific local coordinate by block type id.

spawn(world, originCoordinate)

Spawn the chunk in the world.