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
Prefab.cpp
Go to the documentation of this file.
1
7
8#include "galaxy/core/ServiceLocator.hpp"
9#include "galaxy/core/ServiceLocator.hpp"
12#include "galaxy/math/ZLib.hpp"
13#include "galaxy/meta/EntityMeta.hpp"
15
16#include "Prefab.hpp"
17
18#ifdef GALAXY_WIN_PLATFORM
21#endif
22
23namespace galaxy
24{
25 namespace core
26 {
28 {
29 }
30
31 Prefab::Prefab(entt::entity entity, entt::registry& registry)
32 {
33 from_entity(entity, registry);
34 }
35
36 Prefab::Prefab(const nlohmann::json& json)
37 {
38 from_json(json);
39 }
40
42 {
43 this->m_json = p.m_json;
44 }
45
47 {
48 this->m_json = std::move(p.m_json);
49 }
50
52 {
53 if (this != &p)
54 {
55 this->m_json = p.m_json;
56 }
57
58 return *this;
59 }
60
62 {
63 if (this != &p)
64 {
65 this->m_json = std::move(p.m_json);
66 }
67
68 return *this;
69 }
70
72 {
73 }
74
75 bool Prefab::load(const std::string& file)
76 {
77 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
78
79 const auto data = fs.read(file);
80 if (!data.empty())
81 {
82 const auto base64 = math::decode_zlib(data);
83 const auto decompressed = math::decode_base64(base64);
84
85 from_json(nlohmann::json::parse(decompressed));
86 return true;
87 }
88 else
89 {
90 GALAXY_LOG(GALAXY_ERROR, "Attempted to read empty prefab '{0}'.", file);
91 return false;
92 }
93 }
94
95 void Prefab::from_entity(entt::entity entity, entt::registry& registry)
96 {
97 auto& em = ServiceLocator<meta::EntityMeta>::ref();
98
99 if (registry.valid(entity))
100 {
101 m_json = em.serialize_entity(entity, registry);
102 }
103 }
104
105 void Prefab::from_json(const nlohmann::json& json)
106 {
107 if (json.contains("components"))
108 {
109 m_json = json;
110 }
111 }
112
113 entt::entity Prefab::to_entity(entt::registry& registry) const
114 {
115 auto& em = ServiceLocator<meta::EntityMeta>::ref();
116 return em.deserialize_entity(m_json, registry);
117 }
118
119 const nlohmann::json& Prefab::to_json() const
120 {
121 return m_json;
122 }
123 } // namespace core
124} // namespace galaxy
125
126#ifdef GALAXY_WIN_PLATFORM
128#endif
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
#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
Preconstructed entity definition that can be loaded at any time.
Definition Prefab.hpp:22
entt::entity to_entity(entt::registry &registry) const
Creates an entity from this prefab.
Definition Prefab.cpp:113
const nlohmann::json & to_json() const
Gets the json representation of this prefab.
Definition Prefab.cpp:119
nlohmann::json m_json
Prefab as json data.
Definition Prefab.hpp:114
bool load(const std::string &file)
Loads from a json file on disk.
Definition Prefab.cpp:75
Prefab()
Constructor.
Definition Prefab.cpp:27
~Prefab()
Destructor.
Definition Prefab.cpp:71
void from_json(const nlohmann::json &json)
Creates the prefab from a json object.
Definition Prefab.cpp:105
Prefab & operator=(const Prefab &)
Copy assignment operator.
Definition Prefab.cpp:51
void from_entity(entt::entity entity, entt::registry &registry)
Creates the prefab from an entity.
Definition Prefab.cpp:95
std::string decode_base64(const std::string &input)
Decompresses string into Base64.
Definition Base64.cpp:105
std::string decode_zlib(const std::string &input)
Decompresses string into ZLib.
Definition ZLib.cpp:276
Timer.hpp galaxy.
Definition Async.hpp:17