3 #include "ChunkIndex.hpp"
4 #include "EntityCollision.hpp"
5 #include "WorldCollision.hpp"
6 #include "../app/Assets.hpp"
7 #include "../graphics/Format.hpp"
8 #include "../graphics/Viewport.hpp"
12 #include <glm/gtx/io.hpp>
13 #include <glm/gtx/transform.hpp>
18 World::World(const BlockTypeRegistry &types, const Config &config)
22 // TODO: set spawn base and extent from config
23 , spawn_index(chunks.MakeIndex(Chunk::Pos(0, 0, 0), 3))
26 , light_direction(config.light_direction)
27 , fog_density(config.fog_density) {
32 chunks.UnregisterIndex(spawn_index);
36 Player World::AddPlayer(const std::string &name) {
37 for (Player &p : players) {
38 if (p.entity->Name() == name) {
39 return { nullptr, nullptr };
42 Entity &entity = AddEntity();
44 // TODO: load from save file here
45 entity.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
46 entity.WorldCollidable(true);
47 entity.Position(config.spawn);
48 ChunkIndex *index = &chunks.MakeIndex(entity.ChunkCoords(), 6);
49 players.emplace_back(&entity, index);
50 return players.back();
53 Player World::AddPlayer(const std::string &name, std::uint32_t id) {
54 for (Player &p : players) {
55 if (p.entity->Name() == name) {
56 return { nullptr, nullptr };
59 Entity *entity = AddEntity(id);
61 return { nullptr, nullptr };
64 // TODO: load from save file here
65 entity->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
66 entity->WorldCollidable(true);
67 entity->Position(config.spawn);
68 ChunkIndex *index = &chunks.MakeIndex(entity->ChunkCoords(), 6);
69 players.emplace_back(entity, index);
70 return players.back();
73 Entity &World::AddEntity() {
74 if (entities.empty()) {
75 entities.emplace_back();
76 entities.back().ID(1);
77 return entities.back();
79 if (entities.back().ID() < std::numeric_limits<std::uint32_t>::max()) {
80 std::uint32_t id = entities.back().ID() + 1;
81 entities.emplace_back();
82 entities.back().ID(id);
83 return entities.back();
86 auto position = entities.begin();
87 auto end = entities.end();
88 while (position != end && position->ID() == id) {
92 auto entity = entities.emplace(position);
97 Entity *World::AddEntity(std::uint32_t id) {
98 if (entities.empty() || entities.back().ID() < id) {
99 entities.emplace_back();
100 entities.back().ID(id);
101 return &entities.back();
104 auto position = entities.begin();
105 auto end = entities.end();
106 while (position != end && position->ID() < id) {
109 if (position != end && position->ID() == id) {
112 auto entity = entities.emplace(position);
125 bool CandidateLess(const Candidate &a, const Candidate &b) {
126 return a.dist < b.dist;
129 std::vector<Candidate> candidates;
133 bool World::Intersection(
136 const Chunk::Pos &reference,
141 for (Chunk &cur_chunk : chunks) {
143 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
144 candidates.push_back({ &cur_chunk, cur_dist });
148 if (candidates.empty()) return false;
150 std::sort(candidates.begin(), candidates.end(), CandidateLess);
152 coll.chunk = nullptr;
154 coll.depth = std::numeric_limits<float>::infinity();
156 for (Candidate &cand : candidates) {
157 if (cand.dist > coll.depth) continue;
158 WorldCollision cur_coll;
159 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
160 if (cur_coll.depth < coll.depth) {
169 bool World::Intersection(
172 const Entity &reference,
173 EntityCollision &coll
175 coll.entity = nullptr;
176 coll.depth = std::numeric_limits<float>::infinity();
177 for (Entity &cur_entity : entities) {
178 if (&cur_entity == &reference) {
182 glm::vec3 cur_normal;
183 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
184 // TODO: fine grained check goes here? maybe?
185 if (cur_dist < coll.depth) {
186 coll.entity = &cur_entity;
187 coll.depth = cur_dist;
188 coll.normal = cur_normal;
196 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
197 AABB box = e.Bounds();
198 Chunk::Pos reference = e.ChunkCoords();
199 glm::mat4 M = e.Transform(reference);
201 for (Chunk &cur_chunk : chunks) {
202 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
203 // chunk is not one of the 3x3x3 surrounding the entity
204 // since there's no entity which can extent over 16 blocks, they can be skipped
207 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
217 std::vector<WorldCollision> col;
221 void World::Update(int dt) {
222 for (Entity &entity : entities) {
225 for (Entity &entity : entities) {
227 if (entity.WorldCollidable() && Intersection(entity, col)) {
228 // entity collides with the world
229 Resolve(entity, col);
232 for (Player &player : players) {
233 player.chunks->Rebase(player.entity->ChunkCoords());
235 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
236 if (iter->CanRemove()) {
237 iter = RemoveEntity(iter);
244 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
245 // determine displacement for each cardinal axis and move entity accordingly
246 glm::vec3 min_disp(0.0f);
247 glm::vec3 max_disp(0.0f);
248 for (const WorldCollision &c : col) {
249 if (!c.Blocks()) continue;
250 glm::vec3 local_disp(c.normal * c.depth);
251 // swap if neccessary (normal may point away from the entity)
252 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
255 min_disp = min(min_disp, local_disp);
256 max_disp = max(max_disp, local_disp);
259 // if only one direction is set, use that as the final
260 // if both directions are set, use average
261 glm::vec3 final_disp(0.0f);
262 for (int axis = 0; axis < 3; ++axis) {
263 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
264 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
265 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
267 final_disp[axis] = min_disp[axis];
269 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
270 final_disp[axis] = max_disp[axis];
276 World::EntityHandle World::RemoveEntity(EntityHandle &eh) {
278 for (auto player = players.begin(), end = players.end(); player != end;) {
279 if (player->entity == &*eh) {
280 chunks.UnregisterIndex(*player->chunks);
281 player = players.erase(player);
287 return entities.erase(eh);
291 void World::Render(Viewport &viewport) {
292 DirectionalLighting &entity_prog = viewport.EntityProgram();
293 entity_prog.SetLightDirection(light_direction);
294 entity_prog.SetFogDensity(fog_density);
296 for (Entity &entity : entities) {
297 entity.Render(entity.ChunkTransform(players[0].entity->ChunkCoords()), entity_prog);