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