#include "../graphics/Format.hpp"
#include "../graphics/Viewport.hpp"
+#include <algorithm>
#include <limits>
#include <glm/gtx/io.hpp>
#include <glm/gtx/transform.hpp>
namespace blank {
World::World(const BlockTypeRegistry &types, const Config &config, const WorldSave &save)
-: block_type(types)
+: config(config)
+, block_type(types)
, generate(config.gen)
, chunks(config.load, types, generate, save)
-, player()
+, players()
, entities()
, light_direction(config.light_direction)
, fog_density(config.fog_density) {
generate.Space(0);
generate.Light(13);
generate.Solids({ 1, 4, 7, 10 });
+}
- player = &AddEntity();
- player->Name("player");
- player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
- player->WorldCollidable(true);
- player->Position(config.spawn);
- chunks.QueueSurrounding(player->ChunkCoords());
+Entity *World::AddPlayer(const std::string &name) {
+ for (Entity *e : players) {
+ if (e->Name() == name) {
+ return nullptr;
+ }
+ }
+ Entity &player = AddEntity();
+ player.Name(name);
+ // TODO: load from save file here
+ player.Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
+ player.WorldCollidable(true);
+ player.Position(config.spawn);
+ players.push_back(&player);
+ chunks.QueueSurrounding(player.ChunkCoords());
+ return &player;
}
float dist;
};
+bool CandidateLess(const Candidate &a, const Candidate &b) {
+ return a.dist < b.dist;
+}
+
std::vector<Candidate> candidates;
}
if (candidates.empty()) return false;
+ std::sort(candidates.begin(), candidates.end(), CandidateLess);
+
coll.chunk = nullptr;
coll.block = -1;
coll.depth = std::numeric_limits<float>::infinity();
}
-Chunk &World::PlayerChunk() {
- return chunks.ForceLoad(player->ChunkCoords());
-}
-
-
namespace {
std::vector<WorldCollision> col;
++iter;
}
}
- chunks.Rebase(player->ChunkCoords());
+ // TODO: make flexible
+ chunks.Rebase(players[0]->ChunkCoords());
chunks.Update(dt);
}
entity_prog.SetFogDensity(fog_density);
for (Entity &entity : entities) {
- entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);
+ entity.Render(entity.ChunkTransform(players[0]->ChunkCoords()), entity_prog);
}
}