X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2FWorld.cpp;fp=src%2Fworld%2FWorld.cpp;h=6ad30ff0761b445a19dc6757203019b6b727d662;hb=4e3d9c23940c4511623b5bf328cbbe42641c4b30;hp=06ccb62cfea1f97f0341ac313c3848f9ee81e9a6;hpb=2b31a783f1bac06c7f6d7adbe5e13e4148c7a815;p=blank.git diff --git a/src/world/World.cpp b/src/world/World.cpp index 06ccb62..6ad30ff 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -46,6 +46,70 @@ Entity *World::AddPlayer(const std::string &name) { return &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; +} + namespace {