diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 11729085..e2d3a6ad 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -559,21 +559,6 @@ bool WorldFiles::readWorldInfo(World* world) { return true; } -void WorldFiles::writePlayer(std::shared_ptr player) { - files::write_json(getPlayerFile(), player->serialize().release()); -} - -bool WorldFiles::readPlayer(std::shared_ptr player) { - fs::path file = getPlayerFile(); - if (!fs::is_regular_file(file)) { - std::cerr << "warning: player.json does not exists" << std::endl; - return false; - } - - player->deserialize(files::read_json(file).get()); - return true; -} - void WorldFiles::addPack(const World* world, const std::string& id) { fs::path file = getPacksFile(); if (!fs::is_regular_file(file)) { diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 3269d4dd..c519c8bc 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -139,14 +139,9 @@ public: chunk_inventories_map fetchInventories(int x, int z); bool readWorldInfo(World* world); - bool readPlayer(std::shared_ptr player); void writeRegion(int x, int y, WorldRegion* entry, fs::path file, int layer); - /// @brief Write player data to world files - /// @param player target player - void writePlayer(std::shared_ptr player); - /// @brief Write all unsaved data to world files /// @param world target world /// @param content world content @@ -169,4 +164,4 @@ public: static const char* WORLD_FILE; }; -#endif /* FILES_WORLDFILES_H_ */ \ No newline at end of file +#endif /* FILES_WORLDFILES_H_ */ diff --git a/src/world/Level.cpp b/src/world/Level.cpp index afe7dbd4..4707908a 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -12,11 +12,6 @@ #include "../items/Inventory.h" #include "../items/Inventories.h" - -const float DEF_PLAYER_Y = 100.0f; -const float DEF_PLAYER_SPEED = 4.0f; -const int DEF_PLAYER_INVENTORY_SIZE = 40; - Level::Level(World* world, const Content* content, EngineSettings& settings) : world(world), content(content), @@ -25,7 +20,7 @@ Level::Level(World* world, const Content* content, EngineSettings& settings) events(std::make_unique()), settings(settings) { - auto inv = std::make_shared(0, DEF_PLAYER_INVENTORY_SIZE); + auto inv = std::make_shared(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE); auto player = spawnObject(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv); uint matrixSize = (settings.chunks.loadDistance + settings.chunks.padding) * 2; diff --git a/src/world/Level.h b/src/world/Level.h index d7c24638..7708921f 100644 --- a/src/world/Level.h +++ b/src/world/Level.h @@ -7,6 +7,10 @@ #include "../interfaces/Object.h" #include +const float DEF_PLAYER_Y = 100.0f; +const float DEF_PLAYER_SPEED = 4.0f; +const int DEF_PLAYER_INVENTORY_SIZE = 40; + class Content; class World; class Player; diff --git a/src/world/World.cpp b/src/world/World.cpp index 1607b5dd..800518df 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -64,11 +64,16 @@ void World::write(Level* level) { } wfile->write(this, content); - for (auto object : level->objects) { - if (std::shared_ptr player = std::dynamic_pointer_cast(object)) { - wfile->writePlayer(player); + auto playerFile = dynamic::Map(); + { + auto& players = playerFile.putList("players"); + for (auto object : level->objects) { + if (std::shared_ptr player = std::dynamic_pointer_cast(object)) { + players.put(player->serialize().release()); + } } } + files::write_json(wfile->getPlayerFile(), &playerFile); } Level* World::create(std::string name, @@ -98,10 +103,25 @@ Level* World::load(fs::path directory, } auto level = new Level(world.get(), content, settings); - for (auto object : level->objects) { - if (std::shared_ptr player = std::dynamic_pointer_cast(object)) { - wfile->readPlayer(player); - level->inventories->store(player->getInventory()); + { + fs::path file = wfile->getPlayerFile(); + if (!fs::is_regular_file(file)) { + std::cerr << "warning: player.json does not exists" << std::endl; + } else { + auto playerFile = files::read_json(file); + if (playerFile->has("players")) { + level->objects.clear(); + auto players = playerFile->list("players"); + for (size_t i = 0; i < players->size(); i++) { + auto player = level->spawnObject(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, level->inventories->create(DEF_PLAYER_INVENTORY_SIZE)); + player->deserialize(players->map(i)); + level->inventories->store(player->getInventory()); + } + } else { + auto player = level->getObject(0); + player->deserialize(playerFile.get()); + level->inventories->store(player->getInventory()); + } } } (void)world.release(); diff --git a/src/world/World.h b/src/world/World.h index 74728132..63c322d9 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -36,7 +36,7 @@ class World : Serializable { const Content* const content; std::vector packs; - int64_t nextInventoryId = 1; + int64_t nextInventoryId = 0; public: std::unique_ptr wfile;