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 });
+}
+
+
+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<std::uint32_t>::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;
}
}
-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);
}
}