Loading...
Searching...
No Matches
SceneSerializer.h
Go to the documentation of this file.
1//
2// Created by denzel on 03/12/2025.
3//
4
5#pragma once
6#include "hellfire/ecs/ComponentRegistry.h"
7#include "hellfire/scene/Scene.h"
8#include "hellfire/serializers/Serializer.h"
9
10namespace hellfire {
11 template<>
12 struct Serializer<Scene> {
14
15 static bool serialize(std::ostream& output, const Scene* scene) {
16 if (scene == nullptr) return false;
17
18 nlohmann::ordered_json j;
19 j["name"] = scene->get_name();
20 j["default_camera"] = scene->get_default_camera_entity_id();
21 j["entities"] = nlohmann::ordered_json::array();
22
23 for (const EntityID root_id : scene->get_root_entities()) {
24 serialize_entity_recursive(*scene, root_id, j["entities"]);
25 }
26
27 output << j.dump(2);
28 return output.good();
29 }
30
31 static bool deserialize(std::istream& input, Scene* scene) {
32 try {
33 nlohmann::json j;
34 input >> j;
35
36 scene->set_name(j.at("name"));
37
38 Remap id_remap;
39 for (const auto& entity_json : j.at("entities")) {
40 create_entity_recursive(*scene, entity_json, INVALID_ENTITY, id_remap);
41 }
42
43 if (j.contains("default_camera")) {
44 EntityID old_id = j.at("default_camera");
45 if (id_remap.contains(old_id)) {
46 scene->set_default_camera(id_remap.at(old_id));
47 }
48 }
49
50 return true;
51 } catch (...) {
52 return false;
53 }
54 }
55
56 private:
57 static void serialize_entity_recursive(const Scene& scene, EntityID id, nlohmann::ordered_json& out) {
58 const Entity* entity = scene.get_entity(id);
59 if (!entity) return;
60
61 nlohmann::ordered_json entity_json;
62 entity_json["id"] = id;
63 entity_json["name"] = entity->get_name();
64 entity_json["components"] = ComponentRegistry::instance().serialize_all_components(*entity);
65 entity_json["children"] = nlohmann::json::array();
66
67 for (EntityID child_id : scene.get_children(id)) {
68 serialize_entity_recursive(scene, child_id, entity_json["children"]);
69 }
70
71 out.push_back(entity_json);
72 }
73
74 static void create_entity_recursive(Scene& scene, const nlohmann::json& entity_json, EntityID parent_id, Remap& id_remap) {
75 EntityID old_id = entity_json.at("id");
76 EntityID new_id = scene.create_entity(entity_json.at("name"));
77 id_remap[old_id] = new_id;
78
79 if (parent_id != INVALID_ENTITY) {
80 scene.set_parent(new_id, parent_id);
81 }
82
83 Entity* entity = scene.get_entity(new_id);
84 if (entity_json.contains("components")) {
85 ComponentRegistry::instance().deserialize_all_components(*entity, entity_json.at("components"));
86 }
87
88 if (entity_json.contains("children")) {
89 for (const auto& child_json : entity_json.at("children")) {
90 create_entity_recursive(scene, child_json, new_id, id_remap);
91 }
92 }
93
94
95 }
96 };
97}
static constexpr hellfire::EntityID INVALID_ENTITY
Definition Scene.h:10
std::shared_ptr< Mesh > get_mesh(AssetID id)
std::shared_ptr< Material > get_material(AssetID id)
static ComponentRegistry & instance()
const std::string & get_name() const
Definition Entity.h:46
uint32_t get_id() const
Definition Entity.h:45
void resolve_entity(Entity &entity)
SceneAssetResolver(AssetManager &assets)
void destroy_scene(Scene *scene)
CameraComponent * get_active_camera() const
bool save_scene(Scene *scene)
Scene * create_scene(const std::string &name="GameScene")
Scene * load_scene(const std::filesystem::path &filename)
std::optional< AssetID > get_scene_asset_id(Scene *scene) const
void set_active_camera(EntityID camera) const
std::optional< AssetID > get_active_scene_asset_id() const
SceneActivatedCallback scene_activated_callback_
Scene * get_active_scene() const
bool save_scene_as(const std::string &filename, Scene *scene)
std::vector< Scene * > scenes_
std::vector< Scene * > get_scenes()
std::unordered_map< Scene *, AssetID > scene_asset_ids_
EntityID find_entity_by_name(const std::string &name)
std::vector< EntityID > get_camera_entities() const
Scene * load_scene(AssetID asset_id, const std::filesystem::path &filename)
void update(float delta_time)
Manages a collection of entities and their hierarchical relationships.
Definition Scene.h:24
Entity * find_entity_by_name(const std::string &name)
Finds an entity by its name.
Definition Scene.cpp:174
Scene(std::string name="Unnamed")
Constructs a new Scene with an optional name.
Definition Scene.cpp:9
const Entity * get_entity(EntityID id) const
Retrieves an entity by its ID (const version)
Definition Scene.cpp:86
void set_playing(bool active)
Definition Scene.h:173
const std::string & get_name() const
Definition Scene.h:170
virtual void update(float delta_time)
Updates all entities in the scene.
Definition Scene.cpp:161
void set_default_camera(EntityID camera_id)
Sets the default camera for the scene.
Definition Scene.cpp:182
CameraComponent * get_default_camera() const
Definition Scene.cpp:189
void set_parent(EntityID child_id, EntityID parent_id)
Sets the parent of an entity.
Definition Scene.cpp:105
void set_source_filename(const std::filesystem::path &filename)
Definition Scene.h:174
const std::filesystem::path & get_source_filename() const
Definition Scene.h:175
Entity * get_entity(EntityID id)
Retrieves an entity by its ID.
Definition Scene.cpp:81
EntityID get_default_camera_entity_id() const
Definition Scene.h:159
void set_name(const std::string &name)
Definition Scene.h:171
EntityID create_entity(const std::string &name="GameObject")
Creates a new entity in the scene.
Definition Scene.cpp:29
void register_all_components()
constexpr AssetID INVALID_ASSET_ID
static void create_entity_recursive(Scene &scene, const nlohmann::json &entity_json, EntityID parent_id, Remap &id_remap)
static bool serialize(std::ostream &output, const Scene *scene)
static bool deserialize(std::istream &input, Scene *scene)
static void serialize_entity_recursive(const Scene &scene, EntityID id, nlohmann::ordered_json &out)