add entity 'save' property support
This commit is contained in:
parent
5b5abd1ab0
commit
51f6f0e629
@ -356,6 +356,24 @@ dynamic::Value Entities::serialize(const Entity& entity) {
|
||||
return root;
|
||||
}
|
||||
|
||||
dynamic::List_sptr Entities::serialize(const std::vector<Entity>& entities) {
|
||||
auto list = dynamic::create_list();
|
||||
for (auto& entity : entities) {
|
||||
if (!entity.getDef().save.enabled) {
|
||||
continue;
|
||||
}
|
||||
level->entities->onSave(entity);
|
||||
list->put(level->entities->serialize(entity));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
void Entities::despawn(std::vector<Entity> entities) {
|
||||
for (auto& entity : entities) {
|
||||
entity.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
void Entities::clean() {
|
||||
for (auto it = entities.begin(); it != entities.end();) {
|
||||
if (!registry.get<EntityId>(it->second).destroyFlag) {
|
||||
|
||||
@ -240,7 +240,9 @@ public:
|
||||
std::vector<Entity> getAllInside(AABB aabb);
|
||||
std::vector<Entity> getAllInRadius(glm::vec3 center, float radius);
|
||||
void despawn(entityid_t id);
|
||||
void despawn(std::vector<Entity> entities);
|
||||
dynamic::Value serialize(const Entity& entity);
|
||||
dynamic::List_sptr serialize(const std::vector<Entity>& entities);
|
||||
|
||||
void setNextID(entityid_t id) {
|
||||
nextID = id;
|
||||
|
||||
@ -743,18 +743,14 @@ void Chunks::save(Chunk* chunk) {
|
||||
)
|
||||
);
|
||||
auto entities = level->entities->getAllInside(aabb);
|
||||
auto root = dynamic::create_map();
|
||||
auto& list = root->putList("data");
|
||||
for (auto& entity : entities) {
|
||||
level->entities->onSave(entity);
|
||||
list.put(level->entities->serialize(entity));
|
||||
entity.destroy();
|
||||
}
|
||||
if (!entities.empty()) {
|
||||
auto root = dynamic::create_map();
|
||||
root->put("data", level->entities->serialize(entities));
|
||||
level->entities->despawn(std::move(entities));
|
||||
chunk->flags.entities = true;
|
||||
}
|
||||
worldFiles->getRegions().put(chunk, json::to_binary(root, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Chunks::saveAll() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user