Class ChunkManager

java.lang.Object
com.rvandoosselaer.blocks.ChunkManager

public class ChunkManager extends Object
The ChunkManager is responsible for the administration of chunks and the maintenance of the underlying ChunkCache. The ChunkManager is thread safe, but the lifecycle methods initialize(), update() and cleanup() should be called from the same thread. It is good practice to let the lifecycle of the ChunkManager be handled by the ChunkManagerState.

A chunk can be retrieved by using the getChunk(Vec3i) method. When the chunk isn't available it can be requested with the requestChunk(Vec3i) method. The ChunkManager will first try to load the requested chunk using the ChunkRepository. When this is not successful it will try to generate the chunk using the ChunkGenerator. When this also fails, an empty chunk will be created. The requested chunk is placed in the cache and can be retrieved with the getChunk(Vec3i) method.

Applications can register a ChunkManagerListener to the ChunkManager to get notified when a chunk is available in the cache or when a chunk is updated.

  • Constructor Details

    • ChunkManager

      public ChunkManager()
    • ChunkManager

      public ChunkManager(int cacheSize)
  • Method Details

    • getChunkLocation

      public static com.simsilica.mathd.Vec3i getChunkLocation(@NonNull @NonNull com.jme3.math.Vector3f location)
      Calculate the location of the chunk that contains the given location.
      Parameters:
      location -
      Returns:
      location of the chunk
    • getBlockLocation

      public static com.simsilica.mathd.Vec3i getBlockLocation(@NonNull @NonNull com.jme3.math.Vector3f location)
    • getBlockLocation

      public static com.simsilica.mathd.Vec3i getBlockLocation(@NonNull @NonNull com.jme3.collision.CollisionResult collisionResult)
    • getNeighbourBlockLocation

      public static com.simsilica.mathd.Vec3i getNeighbourBlockLocation(@NonNull @NonNull com.jme3.math.Vector3f location, Direction direction)
    • getNeighbourBlockLocation

      public static com.simsilica.mathd.Vec3i getNeighbourBlockLocation(@NonNull @NonNull com.jme3.collision.CollisionResult collisionResult)
    • getBlockCenterLocation

      public static com.jme3.math.Vector3f getBlockCenterLocation(@NonNull @NonNull com.simsilica.mathd.Vec3i location)
      Calculate the center location of a block.
      Parameters:
      location -
      Returns:
      center of the block
    • getChunk

      public Optional<Chunk> getChunk(com.simsilica.mathd.Vec3i location)
    • setChunk

      public void setChunk(Chunk chunk)
    • requestChunk

      public void requestChunk(com.simsilica.mathd.Vec3i location)
    • requestChunkMeshUpdate

      public void requestChunkMeshUpdate(com.simsilica.mathd.Vec3i location)
    • requestChunkMeshUpdate

      public void requestChunkMeshUpdate(Chunk chunk)
    • removeChunk

      public void removeChunk(com.simsilica.mathd.Vec3i location)
      Parameters:
      location - of the chunk
      See Also:
    • removeChunk

      public void removeChunk(Chunk chunk)
      Inform the ChunkManager that you no longer need access to the chunk and it can perform cleanup operations on it.
      Parameters:
      chunk -
    • addBlock

      public void addBlock(com.jme3.math.Vector3f location, Block block)
    • removeBlock

      public void removeBlock(com.jme3.math.Vector3f location)
    • getBlock

      public Optional<Block> getBlock(com.jme3.math.Vector3f location)
    • getBlock

      public Optional<Block> getBlock(com.jme3.collision.CollisionResult collisionResult)
    • getNeighbourBlock

      public Optional<Block> getNeighbourBlock(com.jme3.math.Vector3f location, Direction direction)
    • getNeighbourBlock

      public Optional<Block> getNeighbourBlock(com.jme3.collision.CollisionResult collisionResult)
    • initialize

      public void initialize()
    • update

      public void update()
    • cleanup

      public void cleanup()
    • addListener

      public void addListener(@NonNull @NonNull ChunkManagerListener listener)
    • removeListener

      public void removeListener(@NonNull @NonNull ChunkManagerListener listener)
    • getChunkResolver

      public ChunkResolver getChunkResolver()