hytopia
Version:
The HYTOPIA SDK makes it easy for developers to create massively multiplayer games using JavaScript or TypeScript.
359 lines (161 loc) • 5.28 kB
Markdown
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [server](./server.md) > [Chunk](./server.chunk.md)
## Chunk class
A 16^3 chunk of blocks. Used to represent a world's terrain.
**Signature:**
```typescript
export default class Chunk extends EventRouter implements protocol.Serializable
```
**Extends:** [EventRouter](./server.eventrouter.md)
**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](./server.blocktyperegistry.md) associated with the [World](./server.world.md) 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).
<h2>Events</h2>
This class is an EventRouter, and instances of it emit events with payloads listed under [ChunkEventPayloads](./server.chunkeventpayloads.md)
## Example
```typescript
// 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
<table><thead><tr><th>
Constructor
</th><th>
Modifiers
</th><th>
Description
</th></tr></thead>
<tbody><tr><td>
[(constructor)()](./server.chunk._constructor_.md)
</td><td>
</td><td>
Creates a new chunk instance.
</td></tr>
</tbody></table>
## Properties
<table><thead><tr><th>
Property
</th><th>
Modifiers
</th><th>
Type
</th><th>
Description
</th></tr></thead>
<tbody><tr><td>
[blocks](./server.chunk.blocks.md)
</td><td>
`readonly`
</td><td>
Readonly<Uint8Array>
</td><td>
The blocks in the chunk as a flat Uint8Array\[4096\], each index as 0 or a block type id.
</td></tr>
<tr><td>
[isSimulated](./server.chunk.issimulated.md)
</td><td>
`readonly`
</td><td>
boolean
</td><td>
Whether the chunk is actively simulated in the internal physics engine.
</td></tr>
<tr><td>
[isSpawned](./server.chunk.isspawned.md)
</td><td>
`readonly`
</td><td>
boolean
</td><td>
Whether the chunk has been spawned.
</td></tr>
<tr><td>
[originCoordinate](./server.chunk.origincoordinate.md)
</td><td>
`readonly`
</td><td>
[Vector3Like](./server.vector3like.md) \| undefined
</td><td>
The origin coordinate of the chunk.
</td></tr>
<tr><td>
[world](./server.chunk.world.md)
</td><td>
`readonly`
</td><td>
[World](./server.world.md) \| undefined
</td><td>
The world the chunk belongs to.
</td></tr>
</tbody></table>
## Methods
<table><thead><tr><th>
Method
</th><th>
Modifiers
</th><th>
Description
</th></tr></thead>
<tbody><tr><td>
[blockIndexToLocalCoordinate(index)](./server.chunk.blockindextolocalcoordinate.md)
</td><td>
`static`
</td><td>
Convert a block index to a local coordinate.
</td></tr>
<tr><td>
[despawn()](./server.chunk.despawn.md)
</td><td>
</td><td>
Despawn the chunk from the world.
</td></tr>
<tr><td>
[getBlockId(localCoordinate)](./server.chunk.getblockid.md)
</td><td>
</td><td>
Get the block type id at a specific local coordinate.
</td></tr>
<tr><td>
[globalCoordinateToLocalCoordinate(globalCoordinate)](./server.chunk.globalcoordinatetolocalcoordinate.md)
</td><td>
`static`
</td><td>
Convert a global coordinate to a local coordinate.
</td></tr>
<tr><td>
[globalCoordinateToOriginCoordinate(globalCoordinate)](./server.chunk.globalcoordinatetoorigincoordinate.md)
</td><td>
`static`
</td><td>
Convert a global coordinate to an origin coordinate.
</td></tr>
<tr><td>
[hasBlock(localCoordinate)](./server.chunk.hasblock.md)
</td><td>
</td><td>
Check if a block exists at a specific local coordinate.
</td></tr>
<tr><td>
[isValidOriginCoordinate(coordinate)](./server.chunk.isvalidorigincoordinate.md)
</td><td>
`static`
</td><td>
Check if an origin coordinate is valid.
</td></tr>
<tr><td>
[setBlock(localCoordinate, blockTypeId)](./server.chunk.setblock.md)
</td><td>
</td><td>
Set the block at a specific local coordinate by block type id.
</td></tr>
<tr><td>
[spawn(world, originCoordinate)](./server.chunk.spawn.md)
</td><td>
</td><td>
Spawn the chunk in the world.
</td></tr>
</tbody></table>