Unravel Engine C++ Reference
Loading...
Searching...
No Matches
scene.h
Go to the documentation of this file.
1#pragma once
2#include <engine/engine_export.h>
3
4#include <uuid/uuid.h>
5
6#include "prefab.h"
7#include <base/basetypes.hpp>
8#include <context/context.hpp>
10#include <entt/entt.hpp>
11#include <entt/signal/sigh.hpp>
12#include <functional>
13
14using namespace entt::literals;
15
16namespace unravel
17{
18
19template<class T>
21{
22 using sink_t = typename entt::sink<entt::sigh<void(entt::registry&, entt::entity)>>;
23 entt::sigh<void(entt::registry&, entt::entity)> sig;
24};
25
26template<class T>
27inline auto on_load(entt::registry& reg) -> typename on_load_bus<T>::sink_t
28{
29 auto& ctx = reg.ctx();
30 if(!ctx.contains<on_load_bus<T>>())
31 {
32 return typename on_load_bus<T>::sink_t(ctx.emplace<on_load_bus<T>>().sig);
33 }
34 return typename on_load_bus<T>::sink_t(ctx.get<on_load_bus<T>>().sig);
35}
36
37template<class T>
38inline auto on_construct(entt::registry& reg) -> decltype(auto)
39{
40 return reg.on_construct<T>();
41}
42
43template<class T>
44inline auto on_update(entt::registry& reg) -> decltype(auto)
45{
46 return reg.on_update<T>();
47}
48
49template<class T>
50inline auto on_destroy(entt::registry& reg) -> decltype(auto)
51{
52 return reg.on_destroy<T>();
53}
54
55template<class T>
56inline void emit_on_load(entt::registry& reg, entt::entity e)
57{
58 if (auto* bus = reg.ctx().find<on_load_bus<T>>())
59 {
60 bus->sig.publish(reg, e);
61 }
62}
63
64#define ENTT_TAG(name) entt::tag<name##_hs>
69struct scene
70{
74 scene(const std::string& tag_name);
75
79 ~scene();
80
81 static auto get_all_scenes() -> const std::vector<scene*>&;
87 auto load_from(const asset_handle<scene_prefab>& pfb, bool call_callbacks = true) -> bool;
88
93 void reload(bool call_callbacks = true);
97 void unload();
98
104 auto instantiate_out(const asset_handle<prefab>& pfb, entt::handle&, bool call_callbacks = true) -> bool;
105 static auto instantiate_out(entt::registry& reg, const asset_handle<prefab>& pfb, entt::handle&, bool call_callbacks = true) -> bool;
106 auto instantiate(const asset_handle<prefab>& pfb, bool call_callbacks = true) -> entt::handle;
107 auto instantiate(const asset_handle<prefab>& pfb, entt::handle parent, bool call_callbacks = true) -> entt::handle;
108
109 void clear_entity(entt::handle& handle);
115 auto create_handle(entt::entity e) -> entt::handle;
116
122 auto create_handle(entt::entity e) const -> entt::const_handle;
123
130 auto create_entity(const std::string& tag = {}, entt::handle parent = {}) -> entt::handle;
131
138 auto clone_entity(entt::handle clone_from, bool keep_parent = true, bool call_callbacks = true) -> entt::handle;
139 void clone_entity(entt::handle& clone_to, entt::handle clone_from, bool keep_parent = true, bool call_callbacks = true);
140
148 static auto create_entity(entt::registry& r, const std::string& name = {}, entt::handle parent = {}) -> entt::handle;
149
155 static void clone_scene(const scene& src_scene, scene& dst_scene, bool call_callbacks = true);
156
162 static auto find_entity_by_prefab_uuid(entt::handle root_entity, const hpp::uuid& target_uuid) -> entt::handle;
163
169 auto find_entity_by_uuid(const hpp::uuid& target_uuid) const -> entt::handle;
170
176 static auto get_scene(entt::handle entity) -> scene*;
177
178
183
187 std::unique_ptr<entt::registry> registry{};
188
189
190 std::string tag{};
191};
192
193
194#define TAG_COMPONENT(name) entt::tag<name##_hs>
195
199ENGINE_EXPORT auto find_entity_by_uuid(entt::registry& registry, const hpp::uuid& target_uuid) -> entt::handle;
200
201
202} // namespace unravel
203
204namespace entt
205{
207{
208 uhandle() = default;
209 uhandle(entt::handle handle);
210 uhandle(entt::registry& registry, const hpp::uuid& uuid) : registry(&registry), uuid(uuid) {}
211 uhandle(const uhandle& other) = default;
212 uhandle(uhandle&& other) = default;
213 auto operator=(const uhandle& other) -> uhandle& = default;
214 auto operator=(uhandle&& other) -> uhandle& = default;
215 ~uhandle() = default;
216 auto resolve() const -> entt::handle;
217 auto operator==(const uhandle& other) const -> bool
218 {
219 return registry == other.registry && uuid == other.uuid;
220 }
221 auto operator!=(const uhandle& other) const -> bool
222 {
223 return !(*this == other);
224 }
225 entt::registry* registry{};
226 hpp::uuid uuid{};
227};
228
229inline auto make_uhandle(entt::handle handle) -> uhandle
230{
231 return uhandle(handle);
232}
233}
std::string name
Definition hub.cpp:33
const aiScene * scene
auto make_uhandle(entt::handle handle) -> uhandle
Definition scene.h:229
auto on_destroy(entt::registry &reg) -> decltype(auto)
Definition scene.h:50
auto on_update(entt::registry &reg) -> decltype(auto)
Definition scene.h:44
auto on_construct(entt::registry &reg) -> decltype(auto)
Definition scene.h:38
auto on_load(entt::registry &reg) -> typename on_load_bus< T >::sink_t
Definition scene.h:27
auto find_entity_by_uuid(entt::registry &registry, const hpp::uuid &target_uuid) -> entt::handle
Finds an entity by id_component UUID in a registry (no scene wrapper required).
Definition scene.cpp:321
void emit_on_load(entt::registry &reg, entt::entity e)
Definition scene.h:56
entt::handle entity
Thread-safe handle to an asset.
auto operator=(const uhandle &other) -> uhandle &=default
entt::registry * registry
Definition scene.h:225
auto resolve() const -> entt::handle
Definition scene.cpp:486
hpp::uuid uuid
Definition scene.h:226
uhandle(uhandle &&other)=default
auto operator=(uhandle &&other) -> uhandle &=default
uhandle(entt::registry &registry, const hpp::uuid &uuid)
Definition scene.h:210
uhandle(const uhandle &other)=default
~uhandle()=default
auto operator!=(const uhandle &other) const -> bool
Definition scene.h:221
uhandle()=default
entt::sigh< void(entt::registry &, entt::entity)> sig
Definition scene.h:23
typename entt::sink< entt::sigh< void(entt::registry &, entt::entity)> > sink_t
Definition scene.h:22
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:70
auto find_entity_by_uuid(const hpp::uuid &target_uuid) const -> entt::handle
Finds an entity by UUID in the scene.
Definition scene.cpp:338
void clear_entity(entt::handle &handle)
Definition scene.cpp:422
auto instantiate_out(const asset_handle< prefab > &pfb, entt::handle &, bool call_callbacks=true) -> bool
Instantiates a prefab in the scene.
Definition scene.cpp:238
asset_handle< scene_prefab > source
The source prefab asset handle for the scene.
Definition scene.h:182
~scene()
Destroys the scene and cleans up resources.
Definition scene.cpp:194
auto load_from(const asset_handle< scene_prefab > &pfb, bool call_callbacks=true) -> bool
Loads a scene from a prefab asset.
Definition scene.cpp:219
static auto get_all_scenes() -> const std::vector< scene * > &
Definition scene.cpp:143
auto clone_entity(entt::handle clone_from, bool keep_parent=true, bool call_callbacks=true) -> entt::handle
Clones an existing entity in the scene.
Definition scene.cpp:398
auto instantiate(const asset_handle< prefab > &pfb, bool call_callbacks=true) -> entt::handle
Definition scene.cpp:258
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:360
std::string tag
Definition scene.h:190
static auto get_scene(entt::handle entity) -> scene *
Gets the scene from an entity handle.
Definition scene.cpp:343
void unload()
Unloads the scene, removing all entities.
Definition scene.cpp:200
static void clone_scene(const scene &src_scene, scene &dst_scene, bool call_callbacks=true)
Clones the entities from one scene to another.
Definition scene.cpp:408
std::unique_ptr< entt::registry > registry
The registry that manages all entities in the scene.
Definition scene.h:187
auto create_handle(entt::entity e) -> entt::handle
Creates an entity in the scene.
Definition scene.cpp:428
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:440
gfx::uniform_handle handle
Definition uniform.cpp:9
std::string tag_name