galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
EntityFactory.cpp
Go to the documentation of this file.
1
7
8#include <nlohmann/json.hpp>
9
11
12#include "EntityFactory.hpp"
13
14#ifdef GALAXY_WIN_PLATFORM
17#endif
18
19namespace galaxy
20{
21 namespace meta
22 {
23 void EntityFactory::json_factory(const std::string& type, const entt::entity entity, entt::registry& registry, const nlohmann::json& json)
24 {
25 if (m_json_factory.contains(type))
26 {
27 m_json_factory[type](entity, registry, json);
28 }
29 else
30 {
31 GALAXY_LOG(GALAXY_FATAL, "Failed to create component {0} from json factory.", type);
32 }
33 }
34
35 entt::any EntityFactory::any_from_json(const std::string& type, const nlohmann::json& json)
36 {
37 return m_json_any_factory[type](json);
38 }
39
40 nlohmann::json EntityFactory::serialize_entity(const entt::entity entity, entt::registry& registry)
41 {
42 nlohmann::json json = nlohmann::json::object();
43 json["components"] = nlohmann::json::object();
44
45 for (auto&& [id, storage] : registry.storage())
46 {
47 if (storage.contains(entity))
48 {
49 SerializationData data = m_serialize_factory[get_type(id)](storage.value(entity));
50 if (!data.name.empty())
51 {
52 json["components"][data.name] = data.json;
53 }
54 }
55 }
56
57 return json;
58 }
59
60 entt::entity EntityFactory::deserialize_entity(const nlohmann::json& json, entt::registry& registry)
61 {
62 const auto entity = registry.create();
63 const auto& components = json.at("components");
64
65 if (!components.empty())
66 {
67 for (const auto& [key, value] : components.items())
68 {
69 // Use the assign function to create components for entities without having to know the type.
70 json_factory(key, entity, registry, value);
71 }
72 }
73
74 return entity;
75 }
76
77 const std::string& EntityFactory::get_type(const entt::id_type id)
78 {
79 return m_id_to_name.at(id);
80 }
81
82 entt::id_type EntityFactory::get_typeid(const std::string& name)
83 {
84 return m_name_to_id.at(name);
85 }
86
91
92 const std::vector<entt::id_type>& EntityFactory::get_validation_list() const
93 {
95 }
96 } // namespace meta
97} // namespace galaxy
98
99#ifdef GALAXY_WIN_PLATFORM
101#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_FATAL
Definition Log.hpp:26
#define GALAXY_DISABLE_WARNING_POP
Definition Pragma.hpp:44
#define GALAXY_DISABLE_WARNING(x)
Definition Pragma.hpp:45
#define GALAXY_DISABLE_WARNING_PUSH
Pragma.hpp galaxy.
Definition Pragma.hpp:43
entt::id_type get_typeid(const std::string &name)
Get an entity type id from a string.
std::vector< entt::id_type > m_validations_to_run
Validations to run upon request.
const std::string & get_type(const entt::id_type id)
Get a string representation of an entity type id.
entt::entity deserialize_entity(const nlohmann::json &json, entt::registry &registry)
Create an entity from a JSON object.
SerializeFactory m_serialize_factory
Serializes an entity from a void* pointer.
entt::any any_from_json(const std::string &type, const nlohmann::json &json)
Construct an entt::any from a json object.
ComponentJSONFactory m_json_factory
Used to allow for component creation from json.
void json_factory(const std::string &type, const entt::entity entity, entt::registry &registry, const nlohmann::json &json)
Construct component from json and assign to provided entity and registry.
ankerl::unordered_dense::map< std::string, entt::id_type > m_name_to_id
Maps a string name to an entt id.
Validations & get_validations()
Get a list of configurations.
const std::vector< entt::id_type > & get_validation_list() const
Get a list of validations to run.
ankerl::unordered_dense::map< entt::id_type, std::move_only_function< bool(const entt::entity, entt::registry &)> > Validations
ankerl::unordered_dense::map< entt::id_type, std::string > m_id_to_name
Maps an entt type id to a string.
AnyJSONFactory m_json_any_factory
Creates an entt::any from a json component data.
nlohmann::json serialize_entity(const entt::entity entity, entt::registry &registry)
Serialise a single entity.
Validations m_validations
Stores validation configurations.
Timer.hpp galaxy.
Definition Async.hpp:17
Data needed to serialize an entity.
std::string name
Name of the component.
nlohmann::json json
Component data as a json object.