8#include <unordered_map>
9#include <unordered_set>
11#include "events/LevelEventSource.h"
14#include "ITerrainGenerator.h"
15#include "render/Material.h"
16#include "render/Renderable.h"
17#include "scene/Skybox.h"
18#include "scene/Updateable.h"
19#include "utility/ThreadPool.h"
36 World(std::unique_ptr<ITerrainGenerator> gen, uint32_t genThreads = 8);
37 World(std::unique_ptr<ITerrainGenerator> gen, glm::ivec3
chunkDims, uint32_t genThreads = 8);
42 glm::ivec3
chunkDims()
const {
return m_chunkDims; }
50 const glm::ivec3& from,
const glm::ivec3& to,
bool unloadRest =
false
55 virtual void unloadChunks(
const glm::ivec3& from,
const glm::ivec3& to);
67 std::shared_ptr<Chunk> getChunk(
const ChunkID&
id);
68 std::shared_ptr<const Chunk> getChunk(
const ChunkID&
id)
const;
78 std::shared_ptr<T> getChunkAs(
const ChunkID&
id);
87 std::shared_ptr<const T> getChunkAs(
const ChunkID&
id)
const;
89 const std::unordered_set<ChunkID>& loadedChunks()
const {
return m_loadedChunks; }
108 const glm::ivec3& pos,
109 bool fallbackToGenerator,
110 BlockState** state =
nullptr
115 const glm::ivec3& pos,
117 std::optional<BlockState> state = std::nullopt
119 void setBlock(
const ChunkID& chID,
const glm::ivec3& pos, MultiBlock&& multiBlock);
120 MultiBlock* getMultiBlock(
const ChunkID& chID,
const glm::ivec3& pos);
124 glm::ivec3 pos, BlockID blockID, std::optional<BlockState> state = std::nullopt
126 void setBlock(glm::ivec3 pos, MultiBlock&& multiBlock);
127 MultiBlock* getMultiBlock(glm::ivec3 pos);
135 BlockID
getBlockID(glm::vec3 pos,
bool fallbackToGenerator, BlockState** state =
nullptr);
140 const Material& getMaterial()
const {
return m_material; }
141 Skybox& getSkybox() {
return m_skybox; }
143 virtual void render(Engine& engine,
const Camera* camera,
int pass = 0)
override;
144 virtual void update(
float dt)
override;
151 const glm::ivec3& pos, BlockID blockID,
BlockState* state =
nullptr
156 std::unordered_map<ChunkID, std::shared_ptr<Chunk>> m_chunks;
157 std::unordered_set<ChunkID> m_loadedChunks;
158 std::unique_ptr<ITerrainGenerator> m_generator =
nullptr;
159 glm::ivec3 m_chunkDims;
178 bool canSeeFace(
const Block& curBlock, glm::vec3 pos, glm::ivec3 dir)
const;
185 mutable std::mutex m_mutex;
188 template <
typename T>
189 std::shared_ptr<T> World::getChunkAs(
const ChunkID&
id) {
190 static_assert(std::is_base_of<Chunk, T>::value,
"T must inherit from Chunk");
191 auto chunk = getChunk(
id);
192 return std::static_pointer_cast<T>(chunk);
195 template <
typename T>
196 std::shared_ptr<const T> World::getChunkAs(
const ChunkID&
id)
const {
197 static_assert(std::is_base_of<Chunk, T>::value,
"T must inherit from Chunk");
198 auto chunk = getChunk(
id);
199 return std::static_pointer_cast<const T>(chunk);
Definition ThreadPool.h:23
Definition BlockState.h:17
Definition ITerrainGenerator.h:11
Definition LevelEventSource.h:11
Definition Renderable.h:10
Definition Updateable.h:6
virtual void unloadChunks(const glm::ivec3 &from, const glm::ivec3 &to)
Unloads chunks in the given range.
Definition World.cpp:122
void unloadAllChunks(const std::vector< ChunkID > &except={})
Unloads all chunks except the given ones.
Definition World.cpp:155
BlockID getBlockID(const ChunkID &chID, const glm::ivec3 &pos, bool fallbackToGenerator, BlockState **state=nullptr)
Gets the block ID at the given position.
Definition World.cpp:224
void unloadChunksFromMemory(const std::vector< ChunkID > &ids)
Unloads given chunks from memory.
Definition World.cpp:194
virtual std::shared_ptr< Chunk > createChunk(const ChunkID &id)
Factory method for creating chunks. Override this to provide custom Chunk subclasses.
Definition World.cpp:477
glm::ivec3 chunkDims() const
Get the chunk dimensions for this world.
Definition World.h:42
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::A...
Definition World.cpp:299
virtual void afterBlockSet(const glm::ivec3 &pos, BlockID blockID, BlockState *state=nullptr)
Definition World.h:150
virtual std::future< void > loadChunks(const glm::ivec3 &from, const glm::ivec3 &to, bool unloadRest=false)
Loads chunks in the given range.
Definition World.cpp:54
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.
Definition World.cpp:253
void checkAndUpdateSurroundingChunks(const ChunkID &chID, const glm::ivec3 &pos)
Checks if the surrounding chunks need to be updated due to a block change.
Definition World.cpp:434
const ITerrainGenerator * getGenerator() const
Gets the terrain generator for this world.
Definition World.h:139