Package com.rvandoosselaer.blocks
Class Chunk
java.lang.Object
com.rvandoosselaer.blocks.Chunk
A chunk holds an array of
Block
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), the update()
method
should be called to reevaluate the #isFull()
and #isEmpty()
flags.
Make sure to call the cleanup()
method to properly dispose of the chunk.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdd a block to this chunk.Add a block to this chunk.void
cleanup()
boolean
containsLocation
(com.simsilica.mathd.Vec3i blockWorldLocation) checks if the block location in the world is part of this chunk.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
.getBlock
(int x, int y, int z) Retrieve the block at the given block coordinate in this chunk.getBlock
(@NonNull com.simsilica.mathd.Vec3i location) Retrieve the block at the given block coordinate in this chunk.getNeighbour
(@NonNull com.simsilica.mathd.Vec3i location, @NonNull Direction direction) Returns the neighbouring block at the given direction.com.jme3.math.Vector3f
Calculates the world location of the chunk.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.removeBlock
(int x, int y, int z) Removes and returns the block at the given coordinate in this chunk.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.
-
Constructor Details
-
Chunk
public Chunk(@NonNull @NonNull com.simsilica.mathd.Vec3i location)
-
-
Method Details
-
createAt
-
addBlock
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
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
Retrieve the block at the given block coordinate in this chunk.- Parameters:
location
- local coordinate in the chunk- Returns:
- block or null
-
getBlock
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
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
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
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
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. -
cleanup
public void cleanup() -
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
-
containsLocation
public boolean containsLocation(com.simsilica.mathd.Vec3i blockWorldLocation) checks if the block location in the world is part of this chunk.- Parameters:
blockWorldLocation
- block location in the world- Returns:
- true if this chunk contains the location, false otherwise
-
getWorldLocation
public com.jme3.math.Vector3f getWorldLocation()Calculates the world location of the chunk.- Returns:
- the world location of the chunk
-
getNeighbour
public Block getNeighbour(@NonNull @NonNull com.simsilica.mathd.Vec3i location, @NonNull @NonNull Direction direction) Returns the neighbouring block at the given direction. When aChunkResolver
is set, the block from a neighbouring chunk is retrieved.- Parameters:
location
- block coordinatedirection
- neighbour direction- Returns:
- the neighbouring block or null
-
isFaceVisible
public 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
-