CoriEngine
Loading...
Searching...
No Matches
SceneHandle.hpp
Go to the documentation of this file.
1#pragma once
2#include "Scene.hpp"
3
4namespace Cori {
5 namespace Core {
6 class Layer;
7 }
8
9 namespace World {
14 public:
19 SceneHandle(const std::shared_ptr<Scene>& scene) : m_SceneRaw(scene) {} //NOLINT
20
30 [[nodiscard]] Entity CreateBlankEntity() {
31 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
32 return m_SceneRaw.lock()->CreateBlankEntity();
33 }
34
42 template<typename... T>
43 [[nodiscard]] Entity CreateEntity(const std::string& name) {
44 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
45 return m_SceneRaw.lock()->CreateEntity<T...>(name);
46 }
47
52 void DestroyEntity(Entity entity) {
53 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
54 m_SceneRaw.lock()->DestroyEntity(entity);
55 }
56
64 std::expected<void, Core::CoriError<>> AddEntityToCache(Entity entity, const Utility::StringHash32 key) {
65 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
66 return m_SceneRaw.lock()->AddEntityToCache(entity, key);
67 }
68
74 [[nodiscard]] std::expected<Entity, Core::CoriError<>> GetEntityFromCache(const Utility::StringHash32 key) {
75 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
76 return m_SceneRaw.lock()->GetEntityFromCache(key);
77 }
78
84 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
85 m_SceneRaw.lock()->RemoveEntityFromCache(key);
86 }
87
96 template<typename... T>
97 [[nodiscard]] std::expected<Entity, Core::CoriError<>> FindEntity(const std::string& name) {
98 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
99 return m_SceneRaw.lock()->FindEntity<T...>(name);
100 }
101
109 template<typename... T>
110 [[nodiscard]] auto StaticView() {
111 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
112 return m_SceneRaw.lock()->StaticView<T...>();
113 }
114
124 template<typename... T, typename... ExcludeT>
125 [[nodiscard]] auto StaticView(Exclude<ExcludeT...> excludeList) {
126 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
127 return m_SceneRaw.lock()->StaticView<T...>(excludeList);
128 }
129
137 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
138 return m_SceneRaw.lock()->DynamicView();
139 }
140
141 //template<typename... T, typename Func>
142 //void ForEach(Func func) {
143 // CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
144 // m_SceneRaw.lock()->ForEach<T...>(func);
145 //}
146
154 template<typename T, typename... Args>
155 T& AddContextComponent(Args&&... args) {
156 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
157 return m_SceneRaw.lock()->AddContextComponent<T>(std::forward<Args>(args)...);
158 }
159
166 template<typename T>
167 [[nodiscard]] T& GetContextComponent() {
168 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
169 return m_SceneRaw.lock()->GetContextComponent<T>();
170 }
171
178 template<typename T>
179 [[nodiscard]] const T& GetContextComponent() const {
180 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
181 return m_SceneRaw.lock()->GetContextComponent<T>();
182 }
183
189 template<typename T>
190 [[nodiscard]] bool HasContextComponent() const {
191 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
192 return m_SceneRaw.lock()->HasContextComponent<T>();
193 }
194
200 template<typename T>
202 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
203 m_SceneRaw.lock()->RemoveContextComponent<T>();
204 }
205
214 template <typename T, typename... Args> requires IsSystem<T>
215 void RegisterSystem(Args&&... args) {
216 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
217 m_SceneRaw.lock()->RegisterSystem<T>(std::forward<Args>(args)...);
218 }
219
225 template <typename T> requires IsSystem<T>
227 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
228 m_SceneRaw.lock()->UnregisterSystem<T>();
229 }
230
236 template <typename T> requires IsSystem<T>
237 [[nodiscard]] std::expected<std::weak_ptr<T>, Core::CoriError<>> GetSystem() {
238 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
239 return m_SceneRaw.lock()->GetSystem<T>();
240 }
241
247 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
248 return m_SceneRaw.lock()->GetCameraController();
249 }
250
255 [[nodiscard]] const Graphics::CameraController& GetActiveCamera() const {
256 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
257 return m_SceneRaw.lock()->GetCameraController();
258 }
259
264 [[nodiscard]] std::string_view GetName() const {
265 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
266 return m_SceneRaw.lock()->GetName();
267 }
268
275 [[nodiscard]] entt::registry& GetRegistry() {
276 CORI_CORE_ASSERT(!m_SceneRaw.expired(), "No scene is currently bound.");
277 return m_SceneRaw.lock()->m_Registry;
278 }
279
284 [[nodiscard]] bool IsValid() const {
285 return !m_SceneRaw.expired();
286 }
287
288 protected:
289 friend Core::Layer;
290 friend Core::Application;
291
292 void BeginRender() {
293 if (!m_SceneRaw.expired()) {
294 m_SceneRaw.lock()->BeginRender();
295 }
296 }
297
298 void EndRender() {
299 if (!m_SceneRaw.expired()) {
300 m_SceneRaw.lock()->EndRender();
301 }
302 }
303
304 void OnUpdate(Core::GameTimer& gameTimer) {
305 if (!m_SceneRaw.expired()) {
306 m_SceneRaw.lock()->OnUpdate(gameTimer);
307 }
308 }
309
310 void OnTickUpdate(Core::GameTimer& gameTimer) {
311 if (!m_SceneRaw.expired()) {
312 m_SceneRaw.lock()->OnTickUpdate(gameTimer);
313 }
314 }
315
317 if (!m_SceneRaw.expired()) {
318 m_SceneRaw.lock()->OnImGuiRender(gameTimer);
319 }
320 }
321
322 [[nodiscard]] bool OnUnbind() {
323 if (!m_SceneRaw.expired()) {
324 return m_SceneRaw.lock()->OnUnbind();
325 }
326 return false;
327 }
328
329 [[nodiscard]] bool OnBind() {
330 if (!m_SceneRaw.expired()) {
331 return m_SceneRaw.lock()->OnBind();
332 }
333 return false;
334 }
335
336 private:
337 std::weak_ptr<Scene> m_SceneRaw;
338 };
339 }
340}
#define CORI_CORE_ASSERT(x,...)
Definition Logger.hpp:1029
Main Application object, there can only be one Application object. Basically a root of the program.
Custom error class mainly used in std::expected.
Definition Error.hpp:27
A GameTimer is responsible for managing everything that is connected with time, ticks,...
Definition Time.hpp:8
An abstract class that is ment to be used as a template for defining layers.
Definition Layer.hpp:13
A class that is used to manipulate Camera of the Scene. Each Scene has one of those.
A wrapper for an EnTT runtime view that provides an iterator to access Entity instances directly.
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
void OnTickUpdate(Core::GameTimer &gameTimer)
std::expected< std::weak_ptr< T >, Core::CoriError<> > GetSystem()
Retries a registered system instance from the scene.
void RegisterSystem(Args &&... args)
Registers the system for the scene.
void OnImGuiRender(Core::GameTimer &gameTimer)
entt::registry & GetRegistry()
Retrieves the EnTT registry.
std::string_view GetName() const
Retrieves the name of the scene.
void RemoveEntityFromCache(const Utility::StringHash32 key)
Removes an entity from the scene local entity cache.
void UnregisterSystem()
Unregisters the system from the scene.
void DestroyEntity(Entity entity)
Destroys the entity.
auto StaticView(Exclude< ExcludeT... > excludeList)
Constructs a static view of the entities that have a particular set of components....
Entity CreateEntity(const std::string &name)
Creates an Entity with a default set of components.
DynamicEntityView DynamicView()
Constructs a dynamic view which can be configured at runtime.
SceneHandle(const std::shared_ptr< Scene > &scene)
Creates a handle for the scene.
void RemoveContextComponent()
Removes a context component from the scene.
std::expected< void, Core::CoriError<> > AddEntityToCache(Entity entity, const Utility::StringHash32 key)
Adds entity to the local scene cache.
Entity CreateBlankEntity()
Creates a blank Entity with no components attached.
bool IsValid() const
Checks if the scene the handle points to is still valid.
bool HasContextComponent() const
Checks if a scene has a specific context component.
const T & GetContextComponent() const
Retries the reference to the requested context component. Const variant.
T & AddContextComponent(Args &&... args)
Adds a component to the scene.
void OnUpdate(Core::GameTimer &gameTimer)
T & GetContextComponent()
Retries the reference to the requested context component.
auto StaticView()
Constructs a view of the entities that have a particular set of components. Variant without component...
std::expected< Entity, Core::CoriError<> > GetEntityFromCache(const Utility::StringHash32 key)
Retries the entity from a scene local entity cache.
const Graphics::CameraController & GetActiveCamera() const
Retrieves a const reference to the CameraController associated with the current camera.
std::expected< Entity, Core::CoriError<> > FindEntity(const std::string &name)
Conducts a scene wide search for the entity with a particular name and optionally a set of components...
Graphics::CameraController & GetActiveCamera()
Retrieves a reference to the CameraController associated with the current camera.
Core systems of the engine are here.
entt::hashed_string::hash_type StringHash32
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
Helper to exclude certain components from a static view.