Class Chunk

java.lang.Object
com.rvandoosselaer.blocks.Chunk

public class Chunk extends Object
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
    Constructor
    Description
    Chunk(@NonNull com.simsilica.mathd.Vec3i location)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    addBlock(int x, int y, int z, Block block)
    Add a block to this chunk.
    addBlock(@NonNull com.simsilica.mathd.Vec3i location, Block block)
    Add a block to this chunk.
    void
     
    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
    Creates and returns the collision mesh of the chunk with the given ChunkMeshGenerator.
    com.jme3.scene.Node
    Creates and returns the node of the chunk with the given ChunkMeshGenerator.
    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
    Updates the #isEmpty() and #isFull() values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Chunk

      public Chunk(@NonNull @NonNull com.simsilica.mathd.Vec3i location)
  • Method Details

    • 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 chunk
      block - 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 chunk
      y - local y coordinate in the chunk
      z - local z coordinate in the chunk
      block - 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 coordinate
      y - local y coordinate
      z - 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 coordinate
      y - local y coordinate
      z - 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 given ChunkMeshGenerator.
      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 given ChunkMeshGenerator.
      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 a ChunkResolver is set, the block from a neighbouring chunk is retrieved.
      Parameters:
      location - block coordinate
      direction - 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 coordinate
      direction - of the face
      Returns:
      true if the face is visible