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
Registry.cpp
Go to the documentation of this file.
1
7
8#include <entt/locator/locator.hpp>
9
13
14#include "Registry.hpp"
15
16namespace galaxy
17{
18 namespace core
19 {
21 {
22 // Handle on_* events.
23 /*m_entt.on_construct<components::RigidBody>().connect<&Registry::construct_rigidbody>(this);
24 m_entt.on_destroy<components::RigidBody>().connect<&Registry::destroy_rigidbody>(this);
25 m_entt.on_construct<components::Script>().connect<&Registry::construct_script>(this);
26 m_entt.on_destroy<components::Script>().connect<&Registry::destruct_script>(this);*/
27 m_entt.on_construct<flags::Disabled>().connect<&Registry::disable_entity>(this);
28 m_entt.on_destroy<flags::Disabled>().connect<&Registry::enable_entity>(this);
29 }
30
32 {
33 // this->m_bodies_to_construct = std::move(r.m_bodies_to_construct);
34 this->m_entt = std::move(r.m_entt);
35 }
36
38 {
39 if (this != &r)
40 {
41 // this->m_bodies_to_construct = std::move(r.m_bodies_to_construct);
42 this->m_entt = std::move(r.m_entt);
43 }
44
45 return *this;
46 }
47
51
52 entt::entity Registry::create()
53 {
54 const auto entity = m_entt.create();
55
56 m_entt.emplace<flags::Disabled>(entity);
57
58 auto& tag = m_entt.emplace<components::Tag>(entity);
59 tag.m_tag = "Untagged";
60
61 return entity;
62 }
63
64 /*entt::entity Registry::create_from_prefab(const std::string& name)
65 {
66 auto& prefabs = entt::locator<resource::Prefabs>::value();
67 if (prefabs.has(name))
68 {
69 return prefabs.get(name)->to_entity(m_entt);
70 }
71 else
72 {
73 GALAXY_LOG(GALAXY_WARNING, "Tried to load missing prefab '{0}'.", name);
74 return entt::null;
75 }
76 }*/
77
78 bool Registry::is_valid(const entt::entity entity)
79 {
80 auto& em = entt::locator<meta::EntityFactory>::value();
81
82 for (const auto& hash : em.get_validation_list())
83 {
84 if (!(em.get_validations().at(hash)(entity, m_entt)))
85 {
86 return false;
87 }
88 }
89
90 return true;
91 }
92
93 /*void Registry::update(b2World& b2World)
94 {
95 for (const auto& [rigidbody, transform] : m_bodies_to_construct)
96 {
97 b2BodyDef def {};
98 def.position.x = transform->m_tf.get_pos().x / GALAXY_WORLD_TO_BOX;
99 def.position.y = transform->m_tf.get_pos().y / GALAXY_WORLD_TO_BOX;
100 def.angle = glm::radians(transform->m_tf.get_rotation());
101 def.type = rigidbody->m_type;
102 def.fixedRotation = rigidbody->m_fixed_rotation;
103 def.bullet = rigidbody->m_bullet;
104
105 rigidbody->m_body = b2World.CreateBody(&def);
106
107 b2PolygonShape shape;
108 shape.SetAsBox(rigidbody->m_shape.x, rigidbody->m_shape.y);
109
110 b2FixtureDef fixture;
111 fixture.shape = &shape;
112 fixture.density = rigidbody->m_density;
113 fixture.friction = rigidbody->m_friction;
114 fixture.restitution = rigidbody->m_restitution;
115 fixture.restitutionThreshold = rigidbody->m_restitution_threshold;
116
117 rigidbody->m_body->CreateFixture(&fixture);
118 rigidbody->m_world = &b2World;
119 }
120
121 m_bodies_to_construct.clear();
122 }*/
123
125 {
126 // m_bodies_to_construct.clear();
127 m_entt.clear();
128 }
129
130 /*void Registry::construct_rigidbody(entt::registry& registry, entt::entity entity)
131 {
132 auto transform = registry.try_get<components::Transform>(entity);
133 if (!transform)
134 {
135 transform = &registry.emplace<components::Transform>(entity);
136 }
137
138 m_bodies_to_construct.emplace_back(&registry.get<components::RigidBody>(entity), transform);
139 }
140
141 void Registry::destroy_rigidbody(entt::registry& registry, entt::entity entity)
142 {
143 auto& rigidbody = registry.get<components::RigidBody>(entity);
144
145 if (rigidbody.m_body != nullptr)
146 {
147 rigidbody.m_world->DestroyBody(rigidbody.m_body);
148
149 rigidbody.m_body = nullptr;
150 rigidbody.m_world = nullptr;
151 }
152 }
153
154 void Registry::construct_script(entt::registry& registry, entt::entity entity)
155 {
156 auto& script = registry.get<components::Script>(entity);
157 auto& state = entt::locator<sol::state>::value();
158
159 auto result = state.load_file(script.file());
160 if (result.valid())
161 {
162 script.m_self = result.call();
163
164 if (script.m_self.valid())
165 {
166 script.m_update = script.m_self["update"];
167
168 if (!script.m_update.valid())
169 {
170 GALAXY_LOG(GALAXY_ERROR, "Update function not present in '{0}'.", script.file());
171 }
172
173 script.m_self["owner"] = std::ref(registry);
174 script.m_self["id"] = sol::readonly_property([entity] {
175 return entity;
176 });
177
178 sol::function init = script.m_self["construct"];
179 if (init.valid())
180 {
181 init(script.m_self);
182 }
183 }
184 else
185 {
186 GALAXY_LOG(GALAXY_ERROR, "Failed to validate script '{0}'. Make sure its in the correct format.", script.file());
187 }
188 }
189 else
190 {
191 GALAXY_LOG(GALAXY_ERROR, "Failed to load script '{0}' because '{1}'.", script.file(), magic_enum::enum_name(result.status()));
192 }
193 }
194
195 void Registry::destruct_script(entt::registry& registry, entt::entity entity)
196 {
197 auto& script = registry.get<components::Script>(entity);
198
199 if (script.m_self.valid())
200 {
201 sol::function destruct = script.m_self["destruct"];
202 if (destruct.valid())
203 {
204 destruct(script.m_self);
205 }
206
207 script.m_self.abandon();
208 }
209 }
210
211 void Registry::construct_nui(entt::registry& registry, entt::entity entity)
212 {
213 auto& ui = registry.get<components::GUI>(entity);
214 auto& state = entt::locator<sol::state>::value();
215 auto& nui = entt::locator<ui::NuklearUI>::value();
216 auto& fs = entt::locator<fs::VirtualFileSystem>::value();
217
218 const auto script = fs.read(ui.file());
219 if (!script.empty())
220 {
221 auto result = state.load(script);
222
223 if (result.valid())
224 {
225 ui.m_self = result.call();
226
227 if (ui.m_self.valid())
228 {
229 ui.m_self["ctx"] = nui.ctx();
230 ui.m_update = ui.m_self["do_ui"];
231
232 if (!ui.m_update.valid())
233 {
234 GALAXY_LOG(GALAXY_ERROR, "Update function not present in ui script '{0}'.", ui.file());
235 }
236 }
237 else
238 {
239 GALAXY_LOG(GALAXY_ERROR, "Failed to validate ui script '{0}'. Make sure its in the correct format.", ui.file());
240 }
241 }
242 else
243 {
244 GALAXY_LOG(GALAXY_ERROR, "Failed to load ui script '{0}' because '{1}'.", ui.file(), magic_enum::enum_name(result.status()));
245 }
246 }
247 else
248 {
249 GALAXY_LOG(GALAXY_ERROR, "Failed to read script '{0}'.", ui.file());
250 }
251 }
252
253 void Registry::destruct_nui(entt::registry& registry, entt::entity entity)
254 {
255 auto& ui = registry.get<components::GUI>(entity);
256 if (ui.m_self.valid())
257 {
258 ui.m_self.abandon();
259 }
260 }*/
261
262 void Registry::enable_entity(entt::registry& registry, entt::entity entity)
263 {
264 /*auto rb = registry.try_get<components::RigidBody>(entity);
265 if (rb)
266 {
267 rb->m_body->SetEnabled(true);
268 }*/
269 }
270
271 void Registry::disable_entity(entt::registry& registry, entt::entity entity)
272 {
273 /*auto rb = registry.try_get<components::RigidBody>(entity);
274 if (rb)
275 {
276 rb->m_body->SetEnabled(false);
277 }*/
278 }
279 } // namespace core
280} // namespace galaxy
Tag an entity.
Definition Tag.hpp:21
Wrapper around entt::registry to expand functionality.
Definition Registry.hpp:21
void clear()
Clear any pending data.
Definition Registry.cpp:124
Registry & operator=(Registry &&)
Move assignment operator.
Definition Registry.cpp:37
void disable_entity(entt::registry &registry, entt::entity entity)
Function that runs when an entity is disabled.
Definition Registry.cpp:271
entt::entity create()
Create an entity with some default components.
Definition Registry.cpp:52
~Registry()
Destructor.
Definition Registry.cpp:48
void enable_entity(entt::registry &registry, entt::entity entity)
Function that runs when an entity is enabled.
Definition Registry.cpp:262
Registry()
Typedef for creating b2 bodies from components.
Definition Registry.cpp:20
entt::registry m_entt
Scene entities.
Definition Registry.hpp:169
bool is_valid(const entt::entity entity)
Validate an entity to make sure all components have met their requirements as defined by register_dep...
Definition Registry.cpp:78
Used to stop an entity from being updated/rendered.
Definition Disabled.hpp:21
Timer.hpp galaxy.
Definition Async.hpp:17