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"
8
11
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}
162
164{
165 unload();
166 unregister_scene(this);
167}
168
170{
171 registry->clear();
172 auto reserved_entity = registry->create();
173 source = {};
174}
175
177{
178 if(load_from_prefab(pfb, *this))
179 {
180 source = pfb;
181 return true;
182 }
183
184 return false;
185}
186auto scene::instantiate_out(const asset_handle<prefab>& pfb, entt::handle& e) -> bool
187{
188 bool result = load_from_prefab_out(pfb, *registry, e);
189 return result;
190}
191
192auto scene::instantiate(const asset_handle<prefab>& pfb) -> entt::handle
193{
194 auto e = load_from_prefab(pfb, *registry);
195 return e;
196}
197
198auto scene::instantiate(const asset_handle<prefab>& pfb, entt::handle parent) -> entt::handle
199{
200 auto e = load_from_prefab(pfb, *registry);;
201 if(parent)
202 {
203 auto trans_comp = e.get<transform_component>();
204 trans_comp.set_parent(parent, true);
205 }
206
207 return e;
208}
209
210auto scene::create_entity(entt::registry& r, const std::string& name, entt::handle parent) -> entt::handle
211{
212 entt::handle ent(r, r.create());
213 ent.emplace<tag_component>().name = !name.empty() ? name : "Entity";
214 ent.emplace<layer_component>();
215
216 auto& transform = ent.emplace<transform_component>();
217 if(parent)
218 {
219 transform.set_parent(parent, false);
220 }
221
222 return ent;
223}
224
225auto scene::create_entity(const std::string& tag, entt::handle parent) -> entt::handle
226{
227 return create_entity(*registry, tag, parent);
228}
229
230void scene::clone_entity(entt::handle& clone_to, entt::handle clone_from, bool keep_parent)
231{
232 // APPLOG_TRACE_PERF(std::chrono::microseconds);
233
234 auto* reg = clone_from.registry();
235 clone_entity_from_stream(clone_from, clone_to);
236 if(keep_parent)
237 {
238 // get cloned from transform
239 auto& clone_from_component = clone_from.get<transform_component>();
240
241 // // get cloned to transform
242 auto& clone_to_component = clone_to.get<transform_component>();
243
244 // set parent from original
245 auto parent = clone_from_component.get_parent();
246 if(parent)
247 {
248 clone_to_component.set_parent(parent, false);
249 }
250 }
251
252 // auto clone_to = clone_entity_impl(*registry, clone_from);
253
254 // // get cloned to transform
255 // auto& clone_to_component = clone_to.get<transform_component>();
256
257 // // clear parent and children which were copied.
258 // clone_to_component._clear_relationships();
259
260 // // get cloned from transform
261 // auto& clone_from_component = clone_from.get<transform_component>();
262
263 // // clone children as well
264 // const auto& children = clone_from_component.get_children();
265 // for(const auto& child : children)
266 // {
267 // auto cloned_child = clone_entity(child, false);
268 // auto& comp = cloned_child.get<transform_component>();
269 // comp.set_parent(clone_to);
270 // }
271
272 // if(keep_parent)
273 // {
274 // // set parent from original
275 // auto parent = clone_from_component.get_parent();
276 // if(parent)
277 // {
278 // clone_to_component.set_parent(parent);
279 // }
280 // }
281}
282
283auto scene::clone_entity(entt::handle clone_from, bool keep_parent) -> entt::handle
284{
285 // APPLOG_TRACE_PERF(std::chrono::microseconds);
286
287 auto* reg = clone_from.registry();
288 entt::handle clone_to(*reg, reg->create());
289 clone_entity(clone_to, clone_from, keep_parent);
290 return clone_to;
291}
292
293void scene::clone_scene(const scene& src_scene, scene& dst_scene)
294{
295 clone_scene_from_stream(src_scene, dst_scene);
296}
297
298void scene::clear_entity(entt::handle& handle)
299{
300 remove_all_components(handle);
301}
302
303
304auto scene::create_handle(entt::entity e) -> entt::handle
305{
306 entt::handle handle(*registry, e);
307 return handle;
308}
309
310auto scene::create_handle(entt::entity e) const -> entt::const_handle
311{
312 entt::const_handle handle(*registry, e);
313 return handle;
314}
315
316auto scene::find_entity_by_prefab_uuid(entt::handle entity, const hpp::uuid& target_uuid) -> entt::handle
317{
318 if (!entity)
319 {
320 return {};
321 }
322
323 auto* id_comp = entity.try_get<prefab_id_component>();
324 if (id_comp && id_comp->id == target_uuid)
325 {
326 return entity;
327 }
328
329 // Search children
330 auto* transform = entity.try_get<transform_component>();
331 if (transform)
332 {
333 for (auto child : transform->get_children())
334 {
335 auto found = find_entity_by_prefab_uuid(child, target_uuid);
336 if (found)
337 {
338 return found;
339 }
340 }
341 }
342
343 return {};
344}
345} // 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 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:298
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:176
auto instantiate_out(const asset_handle< prefab > &pfb, entt::handle &e) -> bool
Instantiates a prefab in the scene.
Definition scene.cpp:186
static void clone_scene(const scene &src_scene, scene &dst_scene)
Clones the entities from one scene to another.
Definition scene.cpp:293
auto instantiate(const asset_handle< prefab > &pfb) -> entt::handle
Definition scene.cpp:192
~scene()
Destroys the scene and cleans up resources.
Definition scene.cpp:163
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:225
auto clone_entity(entt::handle clone_from, bool keep_parent=true) -> entt::handle
Clones an existing entity in the scene.
Definition scene.cpp:283
void unload()
Unloads the scene, removing all entities.
Definition scene.cpp:169
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:304
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:316
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