CoriEngine
Loading...
Searching...
No Matches
Cori::World::SceneHandle Class Reference

A handle for the scene, checks for scene validity before any call to the scene, if scene is invalid asserts. Will not keep the scene alive. More...

#include <SceneHandle.hpp>

Public Member Functions

 SceneHandle (const std::shared_ptr< Scene > &scene)
 Creates a handle for the scene.
Entity CreateBlankEntity ()
 Creates a blank Entity with no components attached.
template<typename... T>
Entity CreateEntity (const std::string &name)
 Creates an Entity with a default set of components.
void DestroyEntity (Entity entity)
 Destroys the entity.
std::expected< void, Core::CoriError<> > AddEntityToCache (Entity entity, const Utility::StringHash32 key)
 Adds entity to the local scene cache.
std::expected< Entity, Core::CoriError<> > GetEntityFromCache (const Utility::StringHash32 key)
 Retries the entity from a scene local entity cache.
void RemoveEntityFromCache (const Utility::StringHash32 key)
 Removes an entity from the scene local entity cache.
template<typename... T>
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.
template<typename... T>
auto StaticView ()
 Constructs a view of the entities that have a particular set of components. Variant without component exclusion.
template<typename... T, typename... ExcludeT>
auto StaticView (Exclude< ExcludeT... > excludeList)
 Constructs a static view of the entities that have a particular set of components. Variant with component exclusion.
DynamicEntityView DynamicView ()
 Constructs a dynamic view which can be configured at runtime.
template<typename T, typename... Args>
T & AddContextComponent (Args &&... args)
 Adds a component to the scene.
template<typename T>
T & GetContextComponent ()
 Retries the reference to the requested context component.
template<typename T>
const T & GetContextComponent () const
 Retries the reference to the requested context component. Const variant.
template<typename T>
bool HasContextComponent () const
 Checks if a scene has a specific context component.
template<typename T>
void RemoveContextComponent ()
 Removes a context component from the scene.
template<typename T, typename... Args>
requires IsSystem<T>
void RegisterSystem (Args &&... args)
 Registers the system for the scene.
template<typename T>
requires IsSystem<T>
void UnregisterSystem ()
 Unregisters the system from the scene.
template<typename T>
requires IsSystem<T>
std::expected< std::weak_ptr< T >, Core::CoriError<> > GetSystem ()
 Retries a registered system instance from the scene.
Graphics::CameraControllerGetActiveCamera ()
 Retrieves a reference to the CameraController associated with the current camera.
const Graphics::CameraControllerGetActiveCamera () const
 Retrieves a const reference to the CameraController associated with the current camera.
std::string_view GetName () const
 Retrieves the name of the scene.
entt::registry & GetRegistry ()
 Retrieves the EnTT registry.
bool IsValid () const
 Checks if the scene the handle points to is still valid.

Protected Member Functions

void BeginRender ()
void EndRender ()
void OnUpdate (Core::GameTimer &gameTimer)
void OnTickUpdate (Core::GameTimer &gameTimer)
void OnImGuiRender (Core::GameTimer &gameTimer)
bool OnUnbind ()
bool OnBind ()

Detailed Description

A handle for the scene, checks for scene validity before any call to the scene, if scene is invalid asserts. Will not keep the scene alive.

Definition at line 13 of file SceneHandle.hpp.

Constructor & Destructor Documentation

◆ SceneHandle()

Cori::World::SceneHandle::SceneHandle ( const std::shared_ptr< Scene > & scene)
inline

Creates a handle for the scene.

Parameters
sceneScene to create a handle for.

Definition at line 19 of file SceneHandle.hpp.

Member Function Documentation

◆ AddContextComponent()

template<typename T, typename... Args>
T & Cori::World::SceneHandle::AddContextComponent ( Args &&... args)
inline

Adds a component to the scene.

Template Parameters
TType of component to add.
ArgsDeduced automatically, no need to specify.
Parameters
argsArguments passed to the component constructor.
Returns
A reference to the newly created component.

Definition at line 155 of file SceneHandle.hpp.

◆ AddEntityToCache()

std::expected< void, Core::CoriError<> > Cori::World::SceneHandle::AddEntityToCache ( Entity entity,
const Utility::StringHash32 key )
inline

Adds entity to the local scene cache.

Parameters
entityHandle of the entity to add to cache.
key32bit FNV-1a hashed string, you can use ""_hs32 operator to create a compile time hash.
The only purpose of a scene local entity cache is to store entities that you plan to access very frequently but don't have a good place on the client side to store the handle.
Returns
Expected object with void on success or CoriError<> on failure.

Definition at line 64 of file SceneHandle.hpp.

◆ BeginRender()

void Cori::World::SceneHandle::BeginRender ( )
inlineprotected

Definition at line 292 of file SceneHandle.hpp.

◆ CreateBlankEntity()

Entity Cori::World::SceneHandle::CreateBlankEntity ( )
inlinenodiscard

Creates a blank Entity with no components attached.

Returns
A handle to the blank entity.
Warning
Be very carefully when using entities that don't have a default set of components, when you create an entity with CreateEntity it has <Name, Hierarchy, UUID, Transform> components by default.
Some engine systems expect an entity to have some of those components.
Entity::SetName() and Entity::GetName() expects Entity to have a Name component.
Anything connected to the Entity hierarchy system expects an Entity to have Hierarchy and Name components.
Be aware and use carefully, can cause a crash if used incorrectly.

Definition at line 30 of file SceneHandle.hpp.

◆ CreateEntity()

template<typename... T>
Entity Cori::World::SceneHandle::CreateEntity ( const std::string & name)
inlinenodiscard

Creates an Entity with a default set of components.

Parameters
nameName to assign to the Entity.
Template Parameters
TOptional set of components or tags that will be added to the entity at creation.
Returns
A handle to the created entity.

Unlike Scene::CreateBlankEntity() it is safe to use an Entity created with this method anywhere you like. Default set of components is <Name, Hierarchy, UUID, Transform>.

Definition at line 43 of file SceneHandle.hpp.

◆ DestroyEntity()

void Cori::World::SceneHandle::DestroyEntity ( Entity entity)
inline

Destroys the entity.

Parameters
entityHandle of the entity to destroy.

Definition at line 52 of file SceneHandle.hpp.

◆ DynamicView()

DynamicEntityView Cori::World::SceneHandle::DynamicView ( )
inlinenodiscard

Constructs a dynamic view which can be configured at runtime.

Use this when the exact set of components a view should have is not known at compile time.

Note
Dynamic views are slightly slower to iterate than static views.
Returns
An instance of dynamic entity view for the creator scene.

Definition at line 136 of file SceneHandle.hpp.

◆ EndRender()

void Cori::World::SceneHandle::EndRender ( )
inlineprotected

Definition at line 298 of file SceneHandle.hpp.

◆ FindEntity()

template<typename... T>
std::expected< Entity, Core::CoriError<> > Cori::World::SceneHandle::FindEntity ( const std::string & name)
inlinenodiscard

Conducts a scene wide search for the entity with a particular name and optionally a set of components.

Parameters
nameName of the entity you're searching for.
Template Parameters
TOptional set of components or tags the desired entity should be filtered by.
Note
As the entity names doesn't have to be unique, it returns the first entity with the given name and set of components it finds.
Returns
Expected object with an entity handle on success, or CoriError<> on failure.
Warning
This is pretty slow as it is a scene wide linear search and absolutely should not be used in a tight loop, each frame/tick or during gameplay!

Definition at line 97 of file SceneHandle.hpp.

◆ GetActiveCamera() [1/2]

Graphics::CameraController & Cori::World::SceneHandle::GetActiveCamera ( )
inlinenodiscard

Retrieves a reference to the CameraController associated with the current camera.

Returns
Reference to the CameraController.

Definition at line 246 of file SceneHandle.hpp.

◆ GetActiveCamera() [2/2]

const Graphics::CameraController & Cori::World::SceneHandle::GetActiveCamera ( ) const
inlinenodiscard

Retrieves a const reference to the CameraController associated with the current camera.

Returns
Const reference to the CameraController.

Definition at line 255 of file SceneHandle.hpp.

◆ GetContextComponent() [1/2]

template<typename T>
T & Cori::World::SceneHandle::GetContextComponent ( )
inlinenodiscard

Retries the reference to the requested context component.

Template Parameters
TType of context component to retrieve.
Note
Make sure you use receive by reference not by value.
Returns
Referenced to the requested context component.

Definition at line 167 of file SceneHandle.hpp.

◆ GetContextComponent() [2/2]

template<typename T>
const T & Cori::World::SceneHandle::GetContextComponent ( ) const
inlinenodiscard

Retries the reference to the requested context component. Const variant.

Template Parameters
TType of context component to retrieve.
Note
Make sure you use receive by const reference not by value.
Returns
Referenced to the requested context component.

Definition at line 179 of file SceneHandle.hpp.

◆ GetEntityFromCache()

std::expected< Entity, Core::CoriError<> > Cori::World::SceneHandle::GetEntityFromCache ( const Utility::StringHash32 key)
inlinenodiscard

Retries the entity from a scene local entity cache.

Parameters
keyKey you associated with an entity when adding it via AddEntityToCache method.
Returns
Expected object with an entity handle on success, or CoriError<> on failure.

Definition at line 74 of file SceneHandle.hpp.

◆ GetName()

std::string_view Cori::World::SceneHandle::GetName ( ) const
inlinenodiscard

Retrieves the name of the scene.

Returns
View to the name of the scene.

Definition at line 264 of file SceneHandle.hpp.

◆ GetRegistry()

entt::registry & Cori::World::SceneHandle::GetRegistry ( )
inlinenodiscard

Retrieves the EnTT registry.

Returns
Non-const reference to the EnTT registry.

For the most part this is needed if your system need some special behaviour/feature (listeners, reactive storage, etc.) from EnTT that I don't have abstraction over.
Making a feature complete wrapper over EnTT is out of the scope of this project.

Definition at line 275 of file SceneHandle.hpp.

◆ GetSystem()

template<typename T>
requires IsSystem<T>
std::expected< std::weak_ptr< T >, Core::CoriError<> > Cori::World::SceneHandle::GetSystem ( )
inlinenodiscard

Retries a registered system instance from the scene.

Template Parameters
TSystem to retrieve.
Returns
Weak pointer to the requested system instance.

Definition at line 237 of file SceneHandle.hpp.

◆ HasContextComponent()

template<typename T>
bool Cori::World::SceneHandle::HasContextComponent ( ) const
inlinenodiscard

Checks if a scene has a specific context component.

Template Parameters
TComponent type to check.
Returns
True the scene has this context component, false otherwise.

Definition at line 190 of file SceneHandle.hpp.

◆ IsValid()

bool Cori::World::SceneHandle::IsValid ( ) const
inlinenodiscard

Checks if the scene the handle points to is still valid.

Returns
True if scene is valid, false otherwise.

Definition at line 284 of file SceneHandle.hpp.

◆ OnBind()

bool Cori::World::SceneHandle::OnBind ( )
inlinenodiscardprotected

Definition at line 329 of file SceneHandle.hpp.

◆ OnImGuiRender()

void Cori::World::SceneHandle::OnImGuiRender ( Core::GameTimer & gameTimer)
inlineprotected

Definition at line 316 of file SceneHandle.hpp.

◆ OnTickUpdate()

void Cori::World::SceneHandle::OnTickUpdate ( Core::GameTimer & gameTimer)
inlineprotected

Definition at line 310 of file SceneHandle.hpp.

◆ OnUnbind()

bool Cori::World::SceneHandle::OnUnbind ( )
inlinenodiscardprotected

Definition at line 322 of file SceneHandle.hpp.

◆ OnUpdate()

void Cori::World::SceneHandle::OnUpdate ( Core::GameTimer & gameTimer)
inlineprotected

Definition at line 304 of file SceneHandle.hpp.

◆ RegisterSystem()

template<typename T, typename... Args>
requires IsSystem<T>
void Cori::World::SceneHandle::RegisterSystem ( Args &&... args)
inline

Registers the system for the scene.

Template Parameters
TSystem to register.
ArgsDeduced automatically, no need to specify.
Parameters
argsArguments that will be passed to Create method of your system class.

Scene has full control of the system lifetime, the system will be kept alive for as long as the scene is alive, but you can also explicitly unregister the system.

Note
If Create returns false, the system will not be registered.

Definition at line 215 of file SceneHandle.hpp.

◆ RemoveContextComponent()

template<typename T>
void Cori::World::SceneHandle::RemoveContextComponent ( )
inline

Removes a context component from the scene.

Template Parameters
TContext component type to remove.
Note
Removes the context components only if the scene has it, it's safe to try to remove a context component that the scene doesn't have.

Definition at line 201 of file SceneHandle.hpp.

◆ RemoveEntityFromCache()

void Cori::World::SceneHandle::RemoveEntityFromCache ( const Utility::StringHash32 key)
inline

Removes an entity from the scene local entity cache.

Parameters
keyKey you associated with an entity when adding it via AddEntityToCache method.

Definition at line 83 of file SceneHandle.hpp.

◆ StaticView() [1/2]

template<typename... T>
auto Cori::World::SceneHandle::StaticView ( )
inlinenodiscard

Constructs a view of the entities that have a particular set of components. Variant without component exclusion.

Template Parameters
TA set of components of the entities in the view.

Example usage: auto view = View<ComponentOne, ComponentTwo>();
This way the view will consist of entities that both have <ComponentOne, ComponentTwo> components.

Returns
Newly constructed view.

Definition at line 110 of file SceneHandle.hpp.

◆ StaticView() [2/2]

template<typename... T, typename... ExcludeT>
auto Cori::World::SceneHandle::StaticView ( Exclude< ExcludeT... > excludeList)
inlinenodiscard

Constructs a static view of the entities that have a particular set of components. Variant with component exclusion.

Template Parameters
TA set of components of the entities in the view.
ExcludeTComponent types to exclude from the view.
Parameters
excludeListInstance of Exclude with ExcludeT components.

Example usage: auto view = View<ComponentOne, ComponentTwo>(Exclude<FlagOne, FlagTwo>());
This way the view will consist of entities that both have <ComponentOne, ComponentTwo> components, and at the same time don't have either FlagOne or FlagTwo.

Returns
Newly constructed view.

Definition at line 125 of file SceneHandle.hpp.

◆ UnregisterSystem()

template<typename T>
requires IsSystem<T>
void Cori::World::SceneHandle::UnregisterSystem ( )
inline

Unregisters the system from the scene.

Template Parameters
TSystem to unregister.
Note
It is safe to call this method with a T system that is not registered.

Definition at line 226 of file SceneHandle.hpp.


The documentation for this class was generated from the following file: