Unravel Engine C++ Reference
Loading...
Searching...
No Matches
scene.cpp
Go to the documentation of this file.
1#include "scene.h"
2#include "uuid/uuid.h"
10
13
16
19
22#include <engine/events.h>
24
25#include <logging/logging.h>
26
27namespace unravel
28{
29
30namespace
31{
32
33// auto clone_entity_impl(entt::registry& r, entt::handle entity) -> entt::handle
34// {
35// entt::handle object(r, r.create());
36
37// for(auto [id, storage] : r.storage())
38// {
39// auto name = storage.type().name();
40
41// if(name.find("edyn::") != std::string_view::npos)
42// {
43// continue;
44// }
45
46// if(name.find("bullet::") != std::string_view::npos)
47// {
48// continue;
49// }
50
51// if(storage.contains(entity) && !storage.contains(object))
52// {
53// storage.push(object, storage.value(entity));
54// }
55// }
56
57// return object;
58// }
59
60template<typename Registry>
61void remove_all_components(entt::basic_handle<Registry> handle)
62{
63 auto& registry = *handle.registry();
64 auto entity = handle.entity();
65
66 for(auto [id, storage] : registry.storage())
67 {
68 storage.remove(entity);
69 }
70}
71
72auto get_scene_registry_impl() -> std::vector<scene*>&
73{
74 static std::vector<scene*> scenes;
75 return scenes;
76}
77
78void register_scene(scene* scn)
79{
80 auto& scenes = get_scene_registry_impl();
81 scenes.emplace_back(scn);
82}
83
84void unregister_scene(scene* scn)
85{
86 auto& scenes = get_scene_registry_impl();
87 scenes.erase(std::remove(scenes.begin(), scenes.end(), scn), scenes.end());
88}
89
90
91template<typename ...Ts>
92void destroy_dependent_components(entt::registry& r, entt::entity e)
93{
94 r.remove<Ts...>(e);
95}
96
97template<typename ...Ts>
98void destroy_dependent_components_recursive(entt::registry& r, entt::entity e)
99{
100 if(!r.valid(e))
101 {
102 return;
103 }
104 destroy_dependent_components<Ts...>(r, e);
105
106
107 auto transform = r.try_get<transform_component>(e);
108 if(transform)
109 {
110 for(auto child : transform->get_children())
111 {
112 destroy_dependent_components_recursive<Ts...>(r, child);
113 }
114 }
115}
116
117} // namespace
118
119auto scene::get_all_scenes() -> const std::vector<scene*>&
120{
121 return get_scene_registry_impl();
122}
123
124scene::scene(const std::string& tag_name)
125 : tag(tag_name)
126{
127 register_scene(this);
128
129 registry = std::make_unique<entt::registry>();
130 unload();
131
132 registry->on_construct<root_component>().connect<&root_component::on_create_component>();
133 registry->on_update<root_component>().connect<&root_component::on_update_component>();
134 registry->on_destroy<root_component>().connect<&root_component::on_destroy_component>();
135
136 registry->on_construct<transform_component>().connect<&transform_component::on_create_component>();
137 registry->on_destroy<transform_component>().connect<&transform_component::on_destroy_component>();
138
139 registry->on_construct<model_component>().connect<&model_component::on_create_component>();
140 registry->on_destroy<model_component>().connect<&model_component::on_destroy_component>();
141
142 registry->on_construct<animation_component>().connect<&animation_system::on_create_component>();
143 registry->on_destroy<animation_component>().connect<&animation_system::on_destroy_component>();
144
145 registry->on_construct<physics_component>().connect<&physics_system::on_create_component>();
146 registry->on_destroy<physics_component>().connect<&physics_system::on_destroy_component>();
147
148
151
152 registry->on_destroy<prefab_component>().connect<&destroy_dependent_components_recursive<prefab_id_component>>();
153
154
155 registry->on_construct<script_component>().connect<&script_component::on_create_component>();
156 registry->on_destroy<script_component>().connect<&script_component::on_destroy_component>();
157
158 registry->on_construct<ui_document_component>().connect<&ui_system::on_create_component>();
159 registry->on_destroy<ui_document_component>().connect<&ui_system::on_destroy_component>();
160
161 registry->on_construct<particle_emitter_component>().connect<&particle_emitter_component::on_create_component>();
162 registry->on_destroy<particle_emitter_component>().connect<&particle_emitter_component::on_destroy_component>();
163
164}
165
167{
168 unload();
169 unregister_scene(this);
170}
171
173{
174 registry->clear();
175 auto reserved_entity = registry->create();
176 source = {};
177}
178
180{
181 if(load_from_prefab(pfb, *this))
182 {
183 source = pfb;
184 return true;
185 }
186
187 return false;
188}
189auto scene::instantiate_out(const asset_handle<prefab>& pfb, entt::handle& e) -> bool
190{
191 bool result = load_from_prefab_out(pfb, *registry, e);
192 return result;
193}
194
195auto scene::instantiate(const asset_handle<prefab>& pfb) -> entt::handle
196{
197 auto e = load_from_prefab(pfb, *registry);
198 return e;
199}
200
201auto scene::instantiate(const asset_handle<prefab>& pfb, entt::handle parent) -> entt::handle
202{
203 auto e = load_from_prefab(pfb, *registry);;
204 if(parent)
205 {
206 auto trans_comp = e.get<transform_component>();
207 trans_comp.set_parent(parent, true);
208 }
209
210 return e;
211}
212
213auto scene::create_entity(entt::registry& r, const std::string& name, entt::handle parent) -> entt::handle
214{
215 entt::handle ent(r, r.create());
216 ent.emplace<tag_component>().name = !name.empty() ? name : "Entity";
217 ent.emplace<layer_component>();
218
219 auto& transform = ent.emplace<transform_component>();
220 if(parent)
221 {
222 transform.set_parent(parent, false);
223 }
224
225 return ent;
226}
227
228auto scene::create_entity(const std::string& tag, entt::handle parent) -> entt::handle
229{
230 return create_entity(*registry, tag, parent);
231}
232
233void scene::clone_entity(entt::handle& clone_to, entt::handle clone_from, bool keep_parent)
234{
235 // APPLOG_TRACE_PERF(std::chrono::microseconds);
236
237 auto* reg = clone_from.registry();
238 clone_entity_from_stream(clone_from, clone_to);
239 if(keep_parent)
240 {
241 // get cloned from transform
242 auto& clone_from_component = clone_from.get<transform_component>();
243
244 // // get cloned to transform
245 auto& clone_to_component = clone_to.get<transform_component>();
246
247 // set parent from original
248 auto parent = clone_from_component.get_parent();
249 if(parent)
250 {
251 clone_to_component.set_parent(parent, false);
252 }
253 }
254
255 // auto clone_to = clone_entity_impl(*registry, clone_from);
256
257 // // get cloned to transform
258 // auto& clone_to_component = clone_to.get<transform_component>();
259
260 // // clear parent and children which were copied.
261 // clone_to_component._clear_relationships();
262
263 // // get cloned from transform
264 // auto& clone_from_component = clone_from.get<transform_component>();
265
266 // // clone children as well
267 // const auto& children = clone_from_component.get_children();
268 // for(const auto& child : children)
269 // {
270 // auto cloned_child = clone_entity(child, false);
271 // auto& comp = cloned_child.get<transform_component>();
272 // comp.set_parent(clone_to);
273 // }
274
275 // if(keep_parent)
276 // {
277 // // set parent from original
278 // auto parent = clone_from_component.get_parent();
279 // if(parent)
280 // {
281 // clone_to_component.set_parent(parent);
282 // }
283 // }
284}
285
286auto scene::clone_entity(entt::handle clone_from, bool keep_parent) -> entt::handle
287{
288 // APPLOG_TRACE_PERF(std::chrono::microseconds);
289
290 auto* reg = clone_from.registry();
291 entt::handle clone_to(*reg, reg->create());
292 clone_entity(clone_to, clone_from, keep_parent);
293 return clone_to;
294}
295
296void scene::clone_scene(const scene& src_scene, scene& dst_scene)
297{
298 clone_scene_from_stream(src_scene, dst_scene);
299}
300
301void scene::clear_entity(entt::handle& handle)
302{
303 remove_all_components(handle);
304}
305
306
307auto scene::create_handle(entt::entity e) -> entt::handle
308{
309 entt::handle handle(*registry, e);
310 return handle;
311}
312
313auto scene::create_handle(entt::entity e) const -> entt::const_handle
314{
315 entt::const_handle handle(*registry, e);
316 return handle;
317}
318
319auto scene::find_entity_by_prefab_uuid(entt::handle entity, const hpp::uuid& target_uuid) -> entt::handle
320{
321 if (!entity)
322 {
323 return {};
324 }
325
326 auto* id_comp = entity.try_get<prefab_id_component>();
327 if (id_comp && id_comp->id == target_uuid)
328 {
329 return entity;
330 }
331
332 // Search children
333 auto* transform = entity.try_get<transform_component>();
334 if (transform)
335 {
336 for (auto child : transform->get_children())
337 {
338 auto found = find_entity_by_prefab_uuid(child, target_uuid);
339 if (found)
340 {
341 return found;
342 }
343 }
344 }
345
346 return {};
347}
348} // namespace unravel
Class that contains core data for meshes.
static void on_destroy_component(entt::registry &r, entt::entity e)
static void on_create_component(entt::registry &r, entt::entity e)
Component that wraps particle system emitter functionality.
Component that handles physics properties and behaviors.
Class that contains core data for audio listeners. There can only be one instance of it per scene.
Component that handles transformations (position, rotation, scale, etc.) in the ACE framework.
auto set_parent(const entt::handle &parent, bool global_stays=true) -> bool
Sets the parent entity.
auto get_parent() const noexcept -> entt::handle
RELATIONSHIP.
std::string name
Definition hub.cpp:27
std::string tag
Definition hub.cpp:26
bgfx::Transform transform
Definition graphics.h:40
void clone_scene_from_stream(const scene &src_scene, scene &dst_scene)
Definition entity.cpp:1253
auto load_from_prefab_out(const asset_handle< prefab > &pfb, entt::registry &registry, entt::handle &obj) -> bool
Definition entity.cpp:934
auto load_from_prefab(const asset_handle< prefab > &pfb, entt::registry &registry) -> entt::handle
Definition entity.cpp:985
void clone_entity_from_stream(entt::const_handle src_obj, entt::handle &dst_obj)
Definition entity.cpp:1056
entt::entity entity
Represents a handle to an asset, providing access and management functions.
Component that provides a layer mask for an entity.
Component that holds a reference to a prefab asset and tracks property overrides.
Component that provides a unique identifier (UUID) for a prefab.
Root component structure for the ACE framework, serves as the base component.
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:21
void clear_entity(entt::handle &handle)
Definition scene.cpp:301
asset_handle< scene_prefab > source
The source prefab asset handle for the scene.
Definition scene.h:112
auto load_from(const asset_handle< scene_prefab > &pfb) -> bool
Loads a scene from a prefab asset.
Definition scene.cpp:179
auto instantiate_out(const asset_handle< prefab > &pfb, entt::handle &e) -> bool
Instantiates a prefab in the scene.
Definition scene.cpp:189
static void clone_scene(const scene &src_scene, scene &dst_scene)
Clones the entities from one scene to another.
Definition scene.cpp:296
auto instantiate(const asset_handle< prefab > &pfb) -> entt::handle
Definition scene.cpp:195
~scene()
Destroys the scene and cleans up resources.
Definition scene.cpp:166
static auto get_all_scenes() -> const std::vector< scene * > &
Definition scene.cpp:119
auto create_entity(const std::string &tag={}, entt::handle parent={}) -> entt::handle
Creates an entity in the scene with an optional tag and parent.
Definition scene.cpp:228
auto clone_entity(entt::handle clone_from, bool keep_parent=true) -> entt::handle
Clones an existing entity in the scene.
Definition scene.cpp:286
void unload()
Unloads the scene, removing all entities.
Definition scene.cpp:172
std::unique_ptr< entt::registry > registry
The registry that manages all entities in the scene.
Definition scene.h:117
scene(const std::string &tag_name)
Constructs a new scene.
Definition scene.cpp:124
auto create_handle(entt::entity e) -> entt::handle
Creates an entity in the scene.
Definition scene.cpp:307
static auto find_entity_by_prefab_uuid(entt::handle root_entity, const hpp::uuid &target_uuid) -> entt::handle
Finds an entity by UUID in the scene.
Definition scene.cpp:319
Component that provides a tag (name or label) for an entity.
Component that holds a reference to a UI document for RmlUi rendering.
gfx::uniform_handle handle
Definition uniform.cpp:9