Package com.rvandoosselaer.blocks
Class Chunk
- java.lang.Object
-
- com.rvandoosselaer.blocks.Chunk
-
public class Chunk extends java.lang.Object
A chunk holds an array ofBlock
elements. Blocks can be retrieved, added or removed using the appropriate methods. Each time the data structure of the chunk changes (when blocks are added or removed), theupdate()
method should be called to reevaluate the#isFull()
and#isEmpty()
flags. Make sure to call theclean()
method to properly dispose of the chunk.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Block
addBlock(int x, int y, int z, Block block)
Add a block to this chunk.Block
addBlock(@NonNull com.simsilica.mathd.Vec3i location, Block block)
Add a block to this chunk.void
clean()
static Chunk
createAt(@NonNull com.simsilica.mathd.Vec3i location)
com.jme3.scene.Mesh
createCollisionMesh(ChunkMeshGenerator strategy)
Creates and returns the collision mesh of the chunk with the givenChunkMeshGenerator
.com.jme3.scene.Node
createNode(ChunkMeshGenerator strategy)
Creates and returns the node of the chunk with the givenChunkMeshGenerator
.Block
getBlock(int x, int y, int z)
Retrieve the block at the given block coordinate in this chunk.Block
getBlock(@NonNull com.simsilica.mathd.Vec3i location)
Retrieve the block at the given block coordinate in this chunk.protected Block
getNeighbour(@NonNull com.simsilica.mathd.Vec3i location, @NonNull Direction direction)
Returns the neighbouring block at the given direction.com.jme3.math.Vector3f
getWorldLocation()
Calculates the world location of the chunk.protected boolean
isFaceVisible(@NonNull com.simsilica.mathd.Vec3i location, @NonNull Direction direction)
Checks if the face of the block is visible using the faceVisibleFunction and thus should be rendered.Block
removeBlock(int x, int y, int z)
Removes and returns the block at the given coordinate in this chunk.Block
removeBlock(@NonNull com.simsilica.mathd.Vec3i location)
Removes and returns the block at the given coordinate in this chunk.com.simsilica.mathd.Vec3i
toLocalLocation(@NonNull com.simsilica.mathd.Vec3i blockWorldLocation)
Calculates the local coordinate of the block inside this chunk, based on the world location of the block.void
update()
Updates the#isEmpty()
and#isFull()
values.
-
-
-
Method Detail
-
createAt
public static Chunk createAt(@NonNull @NonNull com.simsilica.mathd.Vec3i location)
-
addBlock
public Block addBlock(@NonNull @NonNull com.simsilica.mathd.Vec3i location, Block block)
Add a block to this chunk. If there was already a block at this location, it will be overwritten.- Parameters:
location
- local coordinate in the chunkblock
- the block to add- Returns:
- the previous block at the location or null
-
addBlock
public Block addBlock(int x, int y, int z, Block block)
Add a block to this chunk. If there was already a block at this location, it will be replaced.- Parameters:
x
- local x coordinate in the chunky
- local y coordinate in the chunkz
- local z coordinate in the chunkblock
- the block to add- Returns:
- the previous block at the location or null
-
getBlock
public Block getBlock(@NonNull @NonNull com.simsilica.mathd.Vec3i location)
Retrieve the block at the given block coordinate in this chunk.- Parameters:
location
- local coordinate in the chunk- Returns:
- block or null
-
getBlock
public Block getBlock(int x, int y, int z)
Retrieve the block at the given block coordinate in this chunk.- Parameters:
x
- local x coordinatey
- local y coordinatez
- local z coordinate- Returns:
- block or null
-
removeBlock
public Block removeBlock(@NonNull @NonNull com.simsilica.mathd.Vec3i location)
Removes and returns the block at the given coordinate in this chunk.- Parameters:
location
- local coordinate in the chunk- Returns:
- the removed block or null
-
removeBlock
public Block removeBlock(int x, int y, int z)
Removes and returns the block at the given coordinate in this chunk.- Parameters:
x
- local x coordinatey
- local y coordinatez
- local z coordinate- Returns:
- the removed block or null
-
createNode
public com.jme3.scene.Node createNode(ChunkMeshGenerator strategy)
Creates and returns the node of the chunk with the givenChunkMeshGenerator
.- Parameters:
strategy
- mesh generation strategy to use for constructing the node- Returns:
- the generated chunk node
-
createCollisionMesh
public com.jme3.scene.Mesh createCollisionMesh(ChunkMeshGenerator strategy)
Creates and returns the collision mesh of the chunk with the givenChunkMeshGenerator
.- Parameters:
strategy
- mesh generation strategy to use for creating the collision mesh- Returns:
- the generated collision mesh
-
update
public void update()
Updates the#isEmpty()
and#isFull()
values. This should be called whenever the block data has changed.
-
clean
public void clean()
-
toLocalLocation
public com.simsilica.mathd.Vec3i toLocalLocation(@NonNull @NonNull com.simsilica.mathd.Vec3i blockWorldLocation)
Calculates the local coordinate of the block inside this chunk, based on the world location of the block.- Parameters:
blockWorldLocation
- block location in the world- Returns:
- the local block coordinate
-
getWorldLocation
public com.jme3.math.Vector3f getWorldLocation()
Calculates the world location of the chunk.- Returns:
- the world location of the chunk
-
getNeighbour
protected Block getNeighbour(@NonNull @NonNull com.simsilica.mathd.Vec3i location, @NonNull @NonNull Direction direction)
Returns the neighbouring block at the given direction.- Parameters:
location
- block coordinatedirection
- neighbour direction- Returns:
- the neighbouring block or null
-
isFaceVisible
protected boolean isFaceVisible(@NonNull @NonNull com.simsilica.mathd.Vec3i location, @NonNull @NonNull Direction direction)
Checks if the face of the block is visible using the faceVisibleFunction and thus should be rendered.- Parameters:
location
- block coordinatedirection
- of the face- Returns:
- true if the face is visible
-
-