implement nextEntityID save/load

This commit is contained in:
MihailRis
2024-07-05 23:41:55 +03:00
parent f4d3e62b3f
commit cf2faa4a01
5 changed files with 18 additions and 1 deletions
+2 -1
View File
@@ -85,7 +85,8 @@ std::shared_ptr<UINode> create_debug_panel(
L" visible: "+std::to_wstring(level->chunks->visible); L" visible: "+std::to_wstring(level->chunks->visible);
})); }));
panel->add(create_label([=]() { panel->add(create_label([=]() {
return L"entities: "+std::to_wstring(level->entities->size()); return L"entities: "+std::to_wstring(level->entities->size())+L" next: "+
std::to_wstring(level->entities->peekNextID());
})); }));
panel->add(create_label([=](){ panel->add(create_label([=](){
const auto& vox = player->selection.vox; const auto& vox = player->selection.vox;
+8
View File
@@ -192,9 +192,17 @@ public:
void despawn(entityid_t id); void despawn(entityid_t id);
dynamic::Value serialize(const Entity& entity); dynamic::Value serialize(const Entity& entity);
void setNextID(entityid_t id) {
nextID = id;
}
inline size_t size() const { inline size_t size() const {
return entities.size(); return entities.size();
} }
inline entityid_t peekNextID() const {
return nextID;
}
}; };
#endif // OBJECTS_ENTITIES_HPP_ #endif // OBJECTS_ENTITIES_HPP_
+1
View File
@@ -26,6 +26,7 @@ Level::Level(
entities(std::make_unique<Entities>(this)), entities(std::make_unique<Entities>(this)),
settings(settings) settings(settings)
{ {
entities->setNextID(this->world->nextEntityId);
auto inv = std::make_shared<Inventory>( auto inv = std::make_shared<Inventory>(
this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE
); );
+5
View File
@@ -9,10 +9,12 @@
#include "../files/WorldFiles.hpp" #include "../files/WorldFiles.hpp"
#include "../items/Inventories.hpp" #include "../items/Inventories.hpp"
#include "../objects/Player.hpp" #include "../objects/Player.hpp"
#include "../objects/Entities.hpp"
#include "../voxels/Chunk.hpp" #include "../voxels/Chunk.hpp"
#include "../voxels/Chunks.hpp" #include "../voxels/Chunks.hpp"
#include "../voxels/ChunksStorage.hpp" #include "../voxels/ChunksStorage.hpp"
#include "../world/WorldGenerators.hpp" #include "../world/WorldGenerators.hpp"
#include "Level.hpp"
#include <memory> #include <memory>
#include <glm/glm.hpp> #include <glm/glm.hpp>
@@ -52,6 +54,7 @@ void World::updateTimers(float delta) {
void World::write(Level* level) { void World::write(Level* level) {
const Content* content = level->content; const Content* content = level->content;
level->chunks->saveAll(); level->chunks->saveAll();
nextEntityId = level->entities->peekNextID();
wfile->write(this, content); wfile->write(this, content);
auto playerFile = dynamic::Map(); auto playerFile = dynamic::Map();
@@ -193,6 +196,7 @@ void World::deserialize(dynamic::Map* root) {
weatherobj->num("fog", fog); weatherobj->num("fog", fog);
} }
nextInventoryId = root->get("next-inventory-id", 2); nextInventoryId = root->get("next-inventory-id", 2);
nextEntityId = root->get("next-entity-id", 1);
} }
std::unique_ptr<dynamic::Map> World::serialize() const { std::unique_ptr<dynamic::Map> World::serialize() const {
@@ -215,5 +219,6 @@ std::unique_ptr<dynamic::Map> World::serialize() const {
weatherobj.put("fog", fog); weatherobj.put("fog", fog);
root->put("next-inventory-id", nextInventoryId); root->put("next-inventory-id", nextInventoryId);
root->put("next-entity-id", nextEntityId);
return root; return root;
} }
+2
View File
@@ -51,6 +51,8 @@ public:
/// @brief will be replaced with weather in future /// @brief will be replaced with weather in future
float fog = 0.0f; float fog = 0.0f;
entityid_t nextEntityId = 0;
World( World(
std::string name, std::string name,
std::string generator, std::string generator,