X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2FWorld.cpp;h=06ccb62cfea1f97f0341ac313c3848f9ee81e9a6;hb=dbfcb12348b80e2582f710acb1e4ed0011889ba2;hp=592558b303a943e655fac9eec3f43f5dc7471755;hpb=ad7cf72ed47c39640d5588ba53386e090289b4d1;p=blank.git diff --git a/src/world/World.cpp b/src/world/World.cpp index 592558b..06ccb62 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -6,6 +6,7 @@ #include "../graphics/Format.hpp" #include "../graphics/Viewport.hpp" +#include #include #include #include @@ -14,24 +15,35 @@ 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; } @@ -42,6 +54,10 @@ struct Candidate { float dist; }; +bool CandidateLess(const Candidate &a, const Candidate &b) { + return a.dist < b.dist; +} + std::vector candidates; } @@ -63,6 +79,8 @@ bool World::Intersection( if (candidates.empty()) return false; + std::sort(candidates.begin(), candidates.end(), CandidateLess); + coll.chunk = nullptr; coll.block = -1; coll.depth = std::numeric_limits::infinity(); @@ -126,11 +144,6 @@ bool World::Intersection(const Entity &e, std::vector &col) { } -Chunk &World::PlayerChunk() { - return chunks.ForceLoad(player->ChunkCoords()); -} - - namespace { std::vector col; @@ -155,7 +168,8 @@ void World::Update(int dt) { ++iter; } } - chunks.Rebase(player->ChunkCoords()); + // TODO: make flexible + chunks.Rebase(players[0]->ChunkCoords()); chunks.Update(dt); } @@ -198,7 +212,7 @@ void World::Render(Viewport &viewport) { 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); } }