Package com.rvandoosselaer.blocks
Class ChunkManager
- java.lang.Object
-
- com.rvandoosselaer.blocks.ChunkManager
-
public class ChunkManager extends java.lang.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 methodsinitialize()
,update()
andcleanup()
should be called from the same thread. It is good practice to let the lifecycle of the ChunkManager be handled by theChunkManagerState
.A chunk can be retrieved by using the
getChunk(Vec3i)
method. When the chunk isn't available it can be requested with therequestChunk(Vec3i)
method. The ChunkManager will first try to load the requested chunk using theChunkRepository
. When this is not successful it will try to generate the chunk using theChunkGenerator
. When this also fails, an empty chunk will be created. The requested chunk is placed in the cache and can be retrieved with thegetChunk(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 Summary
Constructors Constructor Description ChunkManager()
ChunkManager(int cacheSize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBlock(com.jme3.math.Vector3f location, Block block)
void
addListener(@NonNull ChunkManagerListener listener)
void
cleanup()
java.util.Optional<Block>
getBlock(com.jme3.collision.CollisionResult collisionResult)
java.util.Optional<Block>
getBlock(com.jme3.math.Vector3f location)
static com.simsilica.mathd.Vec3i
getBlockLocation(@NonNull com.jme3.collision.CollisionResult collisionResult)
static com.simsilica.mathd.Vec3i
getBlockLocation(@NonNull com.jme3.math.Vector3f location)
java.util.Optional<Chunk>
getChunk(com.simsilica.mathd.Vec3i location)
static com.simsilica.mathd.Vec3i
getChunkLocation(@NonNull com.jme3.math.Vector3f location)
Calculate the location of the chunk that contains the given location.ChunkResolver
getChunkResolver()
java.util.Optional<Block>
getNeighbourBlock(com.jme3.collision.CollisionResult collisionResult)
java.util.Optional<Block>
getNeighbourBlock(com.jme3.math.Vector3f location, Direction direction)
static com.simsilica.mathd.Vec3i
getNeighbourBlockLocation(@NonNull com.jme3.collision.CollisionResult collisionResult)
static com.simsilica.mathd.Vec3i
getNeighbourBlockLocation(@NonNull com.jme3.math.Vector3f location, Direction direction)
void
initialize()
void
removeBlock(com.jme3.math.Vector3f location)
void
removeChunk(Chunk chunk)
Inform the ChunkManager that you no longer need access to the chunk and it can perform cleanup operations on it.void
removeChunk(com.simsilica.mathd.Vec3i location)
void
removeListener(@NonNull ChunkManagerListener listener)
void
requestChunk(com.simsilica.mathd.Vec3i location)
void
requestChunkMeshUpdate(Chunk chunk)
void
requestChunkMeshUpdate(com.simsilica.mathd.Vec3i location)
void
update()
-
-
-
Method Detail
-
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)
-
getChunk
public java.util.Optional<Chunk> getChunk(com.simsilica.mathd.Vec3i location)
-
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(Chunk)
-
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 java.util.Optional<Block> getBlock(com.jme3.math.Vector3f location)
-
getBlock
public java.util.Optional<Block> getBlock(com.jme3.collision.CollisionResult collisionResult)
-
getNeighbourBlock
public java.util.Optional<Block> getNeighbourBlock(com.jme3.math.Vector3f location, Direction direction)
-
getNeighbourBlock
public java.util.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()
-
-