X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2FWorld.cpp;h=6ad30ff0761b445a19dc6757203019b6b727d662;hb=1812e1a29378526a59a346caa2348df3e7522049;hp=7ac9e18a6287e80ef989657f57b10d71c35b9ce3;hpb=e872614d387c4bfc3afb04bcc7cba3d9b8f3954b;p=blank.git diff --git a/src/world/World.cpp b/src/world/World.cpp index 7ac9e18..6ad30ff 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -3,10 +3,10 @@ #include "EntityCollision.hpp" #include "WorldCollision.hpp" #include "../app/Assets.hpp" -#include "../app/TextureIndex.hpp" #include "../graphics/Format.hpp" #include "../graphics/Viewport.hpp" +#include #include #include #include @@ -14,33 +14,100 @@ namespace blank { -World::World(const Assets &assets, const Config &config, const WorldSave &save) -: block_type() -, block_tex() +World::World(const BlockTypeRegistry &types, const Config &config, const WorldSave &save) +: config(config) +, block_type(types) , generate(config.gen) -, chunks(config.load, block_type, generate, save) -, player() +, chunks(config.load, types, generate, save) +, players() , entities() , light_direction(config.light_direction) , fog_density(config.fog_density) { - TextureIndex tex_index; - assets.LoadBlockTypes("default", block_type, tex_index); - - block_tex.Bind(); - assets.LoadTextures(tex_index, block_tex); - block_tex.FilterNearest(); - generate.Space(0); generate.Light(13); generate.Solids({ 1, 4, 7, 10 }); +} + + +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; +} - player = &AddEntity(); - player->Name("player"); +Entity *World::AddPlayer(const std::string &name, std::uint32_t id) { + for (Entity *e : players) { + if (e->Name() == name) { + return nullptr; + } + } + Entity *player = AddEntity(id); + if (!player) { + return nullptr; + } + 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; +} + +Entity &World::AddEntity() { + if (entities.empty()) { + entities.emplace_back(); + entities.back().ID(1); + return entities.back(); + } + if (entities.back().ID() < std::numeric_limits::max()) { + std::uint32_t id = entities.back().ID() + 1; + entities.emplace_back(); + entities.back().ID(id); + return entities.back(); + } + std::uint32_t id = 1; + auto position = entities.begin(); + auto end = entities.end(); + while (position != end && position->ID() == id) { + ++id; + ++position; + } + auto entity = entities.emplace(position); + entity->ID(id); + return *entity; +} + +Entity *World::AddEntity(std::uint32_t id) { + if (entities.empty() || entities.back().ID() < id) { + entities.emplace_back(); + entities.back().ID(id); + return &entities.back(); + } + + auto position = entities.begin(); + auto end = entities.end(); + while (position != end && position->ID() < id) { + ++position; + } + if (position != end && position->ID() == id) { + return nullptr; + } + auto entity = entities.emplace(position); + entity->ID(id); + return &*entity; } @@ -51,6 +118,10 @@ struct Candidate { float dist; }; +bool CandidateLess(const Candidate &a, const Candidate &b) { + return a.dist < b.dist; +} + std::vector candidates; } @@ -72,6 +143,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(); @@ -135,11 +208,6 @@ bool World::Intersection(const Entity &e, std::vector &col) { } -Chunk &World::PlayerChunk() { - return chunks.ForceLoad(player->ChunkCoords()); -} - - namespace { std::vector col; @@ -164,7 +232,8 @@ void World::Update(int dt) { ++iter; } } - chunks.Rebase(player->ChunkCoords()); + // TODO: make flexible + chunks.Rebase(players[0]->ChunkCoords()); chunks.Update(dt); } @@ -202,27 +271,12 @@ void World::Resolve(Entity &e, std::vector &col) { void World::Render(Viewport &viewport) { - viewport.WorldPosition(player->Transform(player->ChunkCoords())); - - BlockLighting &chunk_prog = viewport.ChunkProgram(); - chunk_prog.SetTexture(block_tex); - chunk_prog.SetFogDensity(fog_density); - - for (Chunk &chunk : chunks.Loaded()) { - glm::mat4 m(chunk.Transform(player->ChunkCoords())); - chunk_prog.SetM(m); - glm::mat4 mvp(chunk_prog.GetVP() * m); - if (!CullTest(Chunk::Bounds(), mvp)) { - chunk.Draw(); - } - } - DirectionalLighting &entity_prog = viewport.EntityProgram(); entity_prog.SetLightDirection(light_direction); 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); } }