VoxelEngine
 
Loading...
Searching...
No Matches
World.h
1#pragma once
2
3#include <future>
4#include <glm/glm.hpp>
5#include <memory>
6#include <mutex>
7#include <optional>
8#include <unordered_map>
9#include <unordered_set>
10
11#include "events/LevelEventSource.h"
12
13#include "Chunk.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"
20
21
22namespace gl {
23 class ShaderPipeline;
24}
25
26namespace engine {
27 class Chunk;
28 class Engine;
29 struct ChunkID;
30 struct RenderContext;
31
32 class World : public Renderable,
33 public Updateable,
34 public LevelEventSource {
35 public:
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);
38 ~World();
39
42 glm::ivec3 chunkDims() const { return m_chunkDims; }
43
45
49 virtual std::future<void> loadChunks(
50 const glm::ivec3& from, const glm::ivec3& to, bool unloadRest = false
51 );
52
55 virtual void unloadChunks(const glm::ivec3& from, const glm::ivec3& to);
56
60 void unloadAllChunks(const std::vector<ChunkID>& except = {});
61
65 void unloadChunksFromMemory(const std::vector<ChunkID>& ids);
66
67 std::shared_ptr<Chunk> getChunk(const ChunkID& id);
68 std::shared_ptr<const Chunk> getChunk(const ChunkID& id) const;
69
77 template <typename T>
78 std::shared_ptr<T> getChunkAs(const ChunkID& id);
79
86 template <typename T>
87 std::shared_ptr<const T> getChunkAs(const ChunkID& id) const;
88
89 const std::unordered_set<ChunkID>& loadedChunks() const { return m_loadedChunks; }
90
94 void checkAndUpdateSurroundingChunks(const ChunkID& chID, const glm::ivec3& pos);
95
97
106 BlockID getBlockID(
107 const ChunkID& chID,
108 const glm::ivec3& pos,
109 bool fallbackToGenerator,
110 BlockState** state = nullptr
111 );
112
113 void setBlock(
114 const ChunkID& chID,
115 const glm::ivec3& pos,
116 BlockID blockID,
117 std::optional<BlockState> state = std::nullopt
118 );
119 void setBlock(const ChunkID& chID, const glm::ivec3& pos, MultiBlock&& multiBlock);
120 MultiBlock* getMultiBlock(const ChunkID& chID, const glm::ivec3& pos);
121
122
123 void setBlock(
124 glm::ivec3 pos, BlockID blockID, std::optional<BlockState> state = std::nullopt
125 );
126 void setBlock(glm::ivec3 pos, MultiBlock&& multiBlock);
127 MultiBlock* getMultiBlock(glm::ivec3 pos);
128
135 BlockID getBlockID(glm::vec3 pos, bool fallbackToGenerator, BlockState** state = nullptr);
136
139 const ITerrainGenerator* getGenerator() const { return m_generator.get(); }
140 const Material& getMaterial() const { return m_material; }
141 Skybox& getSkybox() { return m_skybox; }
142
143 virtual void render(Engine& engine, const Camera* camera, int pass = 0) override;
144 virtual void update(float dt) override;
145
146
148
149
150 virtual void afterBlockSet(
151 const glm::ivec3& pos, BlockID blockID, BlockState* state = nullptr
152 ) {}
153 // virtual void multiBlockUpdated(const glm::ivec3& pos, MultiBlock* block) {};
154
155 protected:
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;
160
161 Material m_material;
162 Skybox m_skybox;
163
164 ThreadPool m_genPool;
165
171 virtual std::shared_ptr<Chunk> createChunk(const ChunkID& id);
172
178 bool canSeeFace(const Block& curBlock, glm::vec3 pos, glm::ivec3 dir) const;
179
180
181 friend class Chunk;
182
183 private:
184 void updateChunk(ChunkID id);
185 mutable std::mutex m_mutex;
186 };
187
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);
193 }
194
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);
200 }
201
202} // namespace engine
Definition ThreadPool.h:23
Definition BlockState.h:17
Definition Block.h:31
Definition Chunk.h:37
Definition ITerrainGenerator.h:11
Definition LevelEventSource.h:11
Definition Material.h:7
Definition Renderable.h:10
Definition Skybox.h:11
Definition Updateable.h:6
Definition World.h:34
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
Definition ChunkID.h:8