VoxelEngine
 
Loading...
Searching...
No Matches
engine::World Class Reference
Inheritance diagram for engine::World:
engine::Renderable engine::Updateable engine::LevelEventSource engine::EventSource

Public Member Functions

 World (std::unique_ptr< ITerrainGenerator > gen, uint32_t genThreads=8)
 
 World (std::unique_ptr< ITerrainGenerator > gen, glm::ivec3 chunkDims, uint32_t genThreads=8)
 
glm::ivec3 chunkDims () const
 Get the chunk dimensions for this world.
 
virtual std::future< void > loadChunks (const glm::ivec3 &from, const glm::ivec3 &to, bool unloadRest=false)
 Loads chunks in the given range.
 
virtual void unloadChunks (const glm::ivec3 &from, const glm::ivec3 &to)
 Unloads chunks in the given range.
 
void unloadAllChunks (const std::vector< ChunkID > &except={})
 Unloads all chunks except the given ones.
 
void unloadChunksFromMemory (const std::vector< ChunkID > &ids)
 Unloads given chunks from memory.
 
std::shared_ptr< ChunkgetChunk (const ChunkID &id)
 
std::shared_ptr< const ChunkgetChunk (const ChunkID &id) const
 
template<typename T >
std::shared_ptr< T > getChunkAs (const ChunkID &id)
 
template<typename T >
std::shared_ptr< const T > getChunkAs (const ChunkID &id) const
 Gets a const chunk and casts it to the specified custom chunk type.
 
const std::unordered_set< ChunkID > & loadedChunks () const
 
void checkAndUpdateSurroundingChunks (const ChunkID &chID, const glm::ivec3 &pos)
 Checks if the surrounding chunks need to be updated due to a block change.
 
BlockID getBlockID (const ChunkID &chID, const glm::ivec3 &pos, bool fallbackToGenerator, BlockState **state=nullptr)
 Gets the block ID at the given position.
 
void setBlock (const ChunkID &chID, const glm::ivec3 &pos, BlockID blockID, std::optional< BlockState > state=std::nullopt)
 
void setBlock (const ChunkID &chID, const glm::ivec3 &pos, MultiBlock &&multiBlock)
 
MultiBlockgetMultiBlock (const ChunkID &chID, const glm::ivec3 &pos)
 
void setBlock (glm::ivec3 pos, BlockID blockID, std::optional< BlockState > state=std::nullopt)
 
void setBlock (glm::ivec3 pos, MultiBlock &&multiBlock)
 
MultiBlockgetMultiBlock (glm::ivec3 pos)
 
BlockID getBlockID (glm::vec3 pos, bool fallbackToGenerator, BlockState **state=nullptr)
 Gets the block ID at the given position.
 
const ITerrainGeneratorgetGenerator () const
 Gets the terrain generator for this world.
 
const MaterialgetMaterial () const
 
SkyboxgetSkybox ()
 
virtual void render (Engine &engine, const Camera *camera, int pass=0) override
 Engine will call this method before rendering the object. You have to set at the very least the gl::Attributes instance and material. Model matrix is optional, but recommended.
 
virtual void update (float dt) override
 
virtual void afterBlockSet (const glm::ivec3 &pos, BlockID blockID, BlockState *state=nullptr)
 
- Public Member Functions inherited from engine::EventSource
void subscribe (EventSite *site)
 
void unsubscribe (EventSite *site)
 
template<typename TEventSite , typename TEvent >
void fireEvent (void(TEventSite::*fire)(TEvent *event), TEvent *event)
 

Protected Member Functions

virtual std::shared_ptr< ChunkcreateChunk (const ChunkID &id)
 Factory method for creating chunks. Override this to provide custom Chunk subclasses.
 
bool canSeeFace (const Block &curBlock, glm::vec3 pos, glm::ivec3 dir) const
 Checks if the face of current block facing given direction can be seen and thus should be rendered.
 
- Protected Member Functions inherited from engine::LevelEventSource
void fireChunkUnloadEvent (ChunkUnloadEvent *pEvent)
 
void fireChunkBeforeLoadEvent (ChunkBeforeLoadEvent *pEvent)
 

Protected Attributes

std::unordered_map< ChunkID, std::shared_ptr< Chunk > > m_chunks
 
std::unordered_set< ChunkIDm_loadedChunks
 
std::unique_ptr< ITerrainGeneratorm_generator = nullptr
 
glm::ivec3 m_chunkDims
 
Material m_material
 
Skybox m_skybox
 
ThreadPool m_genPool
 

Friends

class Chunk
 

Member Function Documentation

◆ afterBlockSet()

virtual void engine::World::afterBlockSet ( const glm::ivec3 &  pos,
BlockID  blockID,
BlockState state = nullptr 
)
inlinevirtual

Events

◆ canSeeFace()

bool World::canSeeFace ( const Block curBlock,
glm::vec3  pos,
glm::ivec3  dir 
) const
protected

Checks if the face of current block facing given direction can be seen and thus should be rendered.

Parameters
curBlockThe current block.
posThe position of the current block.
dirThe direction from which the face is being checked.
Returns
true if the face can be seen (face should be rendered), false otherwise.

◆ checkAndUpdateSurroundingChunks()

void World::checkAndUpdateSurroundingChunks ( const ChunkID chID,
const glm::ivec3 &  pos 
)

Checks if the surrounding chunks need to be updated due to a block change.

Parameters
chIDThe chunk that was changed.
posThe position of the block that was changed.

◆ chunkDims()

glm::ivec3 engine::World::chunkDims ( ) const
inline

Get the chunk dimensions for this world.

Returns
The dimensions of chunks in this world.

◆ createChunk()

std::shared_ptr< Chunk > World::createChunk ( const ChunkID id)
protectedvirtual

Factory method for creating chunks. Override this to provide custom Chunk subclasses.

Parameters
idThe chunk coordinates to create the chunk at.
Returns
A shared pointer to the newly created chunk.
Note
The default implementation creates a standard Chunk. Custom World subclasses can override this to return custom Chunk types.

◆ getBlockID() [1/2]

BlockID World::getBlockID ( const ChunkID chID,
const glm::ivec3 &  pos,
bool  fallbackToGenerator,
BlockState **  state = nullptr 
)

Gets the block ID at the given position.

access

Parameters
chIDThe chunk to query the block from
posThe position of the block within the chunk (0 to Chunk::Dims)
fallbackToGeneratorIf true, the generator will be used to get the block ID if the chunk is not generated
state[out] The state of the block. If nullptr, the state will not be returned.
Returns
the block ID or engine::InvalidBlockID if:
  • The position is out of bounds (eg less than or greater than Chunk::Dims)
  • The chunk is not generated and fallbackToGenerator is false

◆ getBlockID() [2/2]

BlockID World::getBlockID ( glm::vec3  pos,
bool  fallbackToGenerator,
BlockState **  state = nullptr 
)

Gets the block ID at the given position.

Parameters
posThe position of the block in world space
fallbackToGeneratorIf true, the generator will be used to get the block ID if the chunk is not generated
state[out] The state of the block. If nullptr, the state will not be returned.
Returns
the block ID or engine::InvalidBlockID if:
  • The chunk is not generated and fallbackToGenerator is false

◆ getChunkAs()

template<typename T >
std::shared_ptr< const T > engine::World::getChunkAs ( const ChunkID id) const

Gets a const chunk and casts it to the specified custom chunk type.

Template Parameters
TThe custom chunk type (must inherit from Chunk).
Parameters
idThe chunk ID to retrieve.
Returns
A shared_ptr to the const chunk cast to type T.
Note
This is a convenience method that performs a static_pointer_cast internally. If the chunk doesn't exist, returns nullptr.

◆ getGenerator()

const ITerrainGenerator * engine::World::getGenerator ( ) const
inline

Gets the terrain generator for this world.

Returns
Pointer to the terrain generator.

◆ loadChunks()

std::future< void > World::loadChunks ( const glm::ivec3 &  from,
const glm::ivec3 &  to,
bool  unloadRest = false 
)
virtual

Loads chunks in the given range.

management

Parameters
unloadRestIf true, all chunks outside the range will be unloaded.
Returns
A shared_future that becomes ready when all chunks are loaded and generated

◆ render()

void World::render ( Engine engine,
const Camera camera,
int  pass = 0 
)
overridevirtual

Engine will call this method before rendering the object. You have to set at the very least the gl::Attributes instance and material. Model matrix is optional, but recommended.

Parameters
EngineThe render engine to which you can push RenderContext to schedule rendering.
passNumber indicating the pass, if you need multiple passes for rendering.
Note
The render pass has to be set by the caller of this method.
The render context has to be filled with the necessary data for rendering.

Implements engine::Renderable.

◆ unloadAllChunks()

void World::unloadAllChunks ( const std::vector< ChunkID > &  except = {})

Unloads all chunks except the given ones.

Parameters
exceptChunks to keep loaded.
Note
This function fires a ChunkUnloadEvent for each chunk that is unloaded.

◆ unloadChunks()

void World::unloadChunks ( const glm::ivec3 &  from,
const glm::ivec3 &  to 
)
virtual

Unloads chunks in the given range.

Note
This function fires a ChunkUnloadEvent for each chunk that is unloaded.

◆ unloadChunksFromMemory()

void World::unloadChunksFromMemory ( const std::vector< ChunkID > &  ids)

Unloads given chunks from memory.

Parameters
idsChunks to unload.
Note
This function does not fire any events.

◆ update()

void World::update ( float  dt)
overridevirtual

Implements engine::Updateable.


The documentation for this class was generated from the following files: