SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
spark::core::GameObject Class Reference

A GameObject is any object in the game. It contains a list of components that provides functionality to the GameObject. More...

#include <GameObject.h>

Inheritance diagram for spark::core::GameObject:
spark::rtti::HasRtti spark::core::details::AbstractGameObject< GameObject > spark::patterns::Composite< GameObject, GameObjectDeleter >

Public Member Functions

 GameObject (std::string name, GameObject *parent=nullptr)
 Instantiates a new GameObject.
 
 GameObject (const GameObject &other)=delete
 
 GameObject (GameObject &&other) noexcept=default
 
GameObjectoperator= (const GameObject &other)=delete
 
GameObjectoperator= (GameObject &&other) noexcept=default
 
const lib::Uuiduuid () const
 Gets the UUID of the GameObject.
 
const std::string & name () const
 Gets the name of the GameObject.
 
components::Transformtransform () const
 Gets the transform component of the GameObject.
 
void addComponent (Component *component, bool managed=false)
 Adds a component to the GameObject.
 
template<typename T , typename... Args>
requires std::is_base_of_v<Component, T>
void addComponent (Args &&... args)
 Adds a component of type T to the GameObject.
 
void removeComponent (Component *component)
 Removes a component from the GameObject.
 
template<typename T >
requires std::is_base_of_v<Component, T>
void removeComponent ()
 Removes a component of the given type from the GameObject.
 
template<typename T >
requires std::is_base_of_v<Component, T>
bool hasComponent () const
 Checks if the GameObject has a component of type T.
 
std::vector< Component * > components () const
 Gets all the components for this GameObject.
 
template<typename T >
requires std::is_base_of_v<Component, T>
std::vector< T * > componentsInChildren () const
 Gets a list of components of type T in any direct child of the GameObject.
 
template<typename T >
requires std::is_base_of_v<Component, T>
T * component () const
 Gets a component of type T.
 
template<typename T >
requires std::is_base_of_v<Component, T>
T * componentInChildren () const
 Gets a component of type T in any direct child of the GameObject.
 
template<typename T >
requires std::is_base_of_v<Component, T>
T * componentInParent () const
 Gets a component of type T in any direct parent of the GameObject.
 
virtual void onSpawn ()
 Method called when the GameObject is spawned in the scene.
 
virtual void onUpdate (float dt)
 Method called on every frame.
 
virtual void onDestroyed ()
 Method called when the GameObject is destroyed.
 
- Public Member Functions inherited from spark::rtti::HasRtti
 HasRtti (const HasRtti &other)=default
 
 HasRtti (HasRtti &&other) noexcept=default
 
HasRttioperator= (const HasRtti &other)=default
 
HasRttioperator= (HasRtti &&other) noexcept=default
 
virtual RttiBaserttiInstance () const =0
 
- Public Member Functions inherited from spark::core::details::AbstractGameObject< GameObject >
 AbstractGameObject (GameObject *parent=nullptr)
 
 AbstractGameObject (const AbstractGameObject &other)=delete
 
 AbstractGameObject (AbstractGameObject &&other) noexcept=default
 
AbstractGameObjectoperator= (const AbstractGameObject &other)=delete
 
AbstractGameObjectoperator= (AbstractGameObject &&other) noexcept=default
 
void onSpawn ()
 Method calling the corresponding method on the implementation, the children and components.
 
void onUpdate (float dt)
 Method calling the corresponding method on the implementation, the children and components.
 
void onDestroyed ()
 Method calling the corresponding method on the implementation, the children and components.
 
- Public Member Functions inherited from spark::patterns::Composite< GameObject, GameObjectDeleter >
 Composite (GameObject *parent)
 
 Composite (const Composite &other)=delete
 
 Composite (Composite &&other) noexcept=default
 
Compositeoperator= (const Composite &other)=delete
 
Compositeoperator= (Composite &&other) noexcept=default
 
std::vector< GameObject * > children () const
 
GameObject * parent ()
 
const GameObject * parent () const
 
GameObject * root ()
 
const GameObject * root () const
 

Static Public Member Functions

template<typename T = GameObject, typename... Args>
requires std::is_base_of_v<GameObject, T>
static T * Instantiate (std::string name, GameObject *parent, Args &&... args)
 Instantiates a new GameObject.
 
static void Destroy (GameObject *object, bool immediate=false)
 Destroys the current GameObject and all its children from the current scene.
 
static GameObjectFindById (GameObject *root, const lib::Uuid &uuid)
 Finds a GameObject by its UUID.
 
static GameObjectFindByName (GameObject *root, const std::string &name)
 Finds a GameObject by its name.
 

Public Attributes

bool isShown = true
 A boolean indicating if the GameObject is shown in the scene.
 

Detailed Description

A GameObject is any object in the game. It contains a list of components that provides functionality to the GameObject.

GameObjects can be parented to other GameObjects. When a GameObject is destroyed, all its children are destroyed as well with their components.

Constructor & Destructor Documentation

◆ GameObject()

spark::core::GameObject::GameObject ( std::string name,
GameObject * parent = nullptr )
explicit

Instantiates a new GameObject.

Parameters
nameThe name of the GameObject.
parentThe parent of the GameObject. If nullptr, the GameObject is parented to the root GameObject.

Use GameObject::Instantiate to instantiate a GameObject externally.

Member Function Documentation

◆ addComponent() [1/2]

template<typename T , typename... Args>
requires std::is_base_of_v<Component, T>
void spark::core::GameObject::addComponent ( Args &&... args)

Adds a component of type T to the GameObject.

Template Parameters
TThe type of component to add.
ArgsThe types of the arguments to pass to the constructor of the component.
Parameters
argsThe arguments to pass to the constructor of the component.

◆ addComponent() [2/2]

void spark::core::GameObject::addComponent ( Component * component,
bool managed = false )

Adds a component to the GameObject.

Parameters
componentA pointer to the component to add.
managedA boolean indicating if the component is managed by the GameObject. If true, the component will be destroyed when the GameObject is destroyed.

◆ component()

template<typename T >
requires std::is_base_of_v<Component, T>
T * spark::core::GameObject::component ( ) const
nodiscard

Gets a component of type T.

Template Parameters
TThe type of component to get.
Returns
A pointer to the component of type T, or nullptr if no component is not found.

◆ componentInChildren()

template<typename T >
requires std::is_base_of_v<Component, T>
T * spark::core::GameObject::componentInChildren ( ) const
nodiscard

Gets a component of type T in any direct child of the GameObject.

Template Parameters
TThe type of component to get.
Returns
A pointer to the component of type T, or nullptr if no component is not found.

◆ componentInParent()

template<typename T >
requires std::is_base_of_v<Component, T>
T * spark::core::GameObject::componentInParent ( ) const
nodiscard

Gets a component of type T in any direct parent of the GameObject.

Template Parameters
TThe type of component to get.
Returns
A pointer to the component of type T, or nullptr if no component is not found.

◆ components()

std::vector< Component * > spark::core::GameObject::components ( ) const
nodiscard

Gets all the components for this GameObject.

Returns
A vector of pointers to the components.

◆ componentsInChildren()

template<typename T >
requires std::is_base_of_v<Component, T>
std::vector< T * > spark::core::GameObject::componentsInChildren ( ) const
nodiscard

Gets a list of components of type T in any direct child of the GameObject.

Template Parameters
TThe type of component to get.
Returns
A vector of pointers to the component of type T, or an empty vector if no component was found.

◆ Destroy()

void spark::core::GameObject::Destroy ( GameObject * object,
bool immediate = false )
static

Destroys the current GameObject and all its children from the current scene.

Parameters
objectThe object to destroy.
immediatetrue to destroy the object immediately, false to destroy it at the end of the frame. Default is false.

◆ FindById()

GameObject * spark::core::GameObject::FindById ( GameObject * root,
const lib::Uuid & uuid )
static

Finds a GameObject by its UUID.

Parameters
rootThe root GameObject to start the search from.
uuidThe UUID of the GameObject to find.
Returns
A pointer to the GameObject if found, nullptr otherwise.

◆ FindByName()

GameObject * spark::core::GameObject::FindByName ( GameObject * root,
const std::string & name )
static

Finds a GameObject by its name.

Parameters
rootThe root GameObject to start the search from.
nameThe name of the GameObject to find.
Returns
A pointer to the GameObject if found, nullptr otherwise.

◆ hasComponent()

template<typename T >
requires std::is_base_of_v<Component, T>
bool spark::core::GameObject::hasComponent ( ) const
nodiscard

Checks if the GameObject has a component of type T.

Template Parameters
TThe type of component to check.
Returns
A boolean indicating if the GameObject has a component of type T.

◆ Instantiate()

template<typename T , typename... Args>
requires std::is_base_of_v<GameObject, T>
T * spark::core::GameObject::Instantiate ( std::string name,
GameObject * parent,
Args &&... args )
static

Instantiates a new GameObject.

Template Parameters
TThe type of GameObject to instantiate
Parameters
nameThe name of the GameObject
parentThe parent GameObject in the scene
argsThe arguments to pass to the constructor of the GameObject
Returns
A pointer to the instantiated GameObject

This method should only be called during the game runtime, not in editor. Use GameObject::GameObject for this.

◆ name()

const std::string & spark::core::GameObject::name ( ) const
nodiscard

Gets the name of the GameObject.

Returns
A const reference to the name of this GameObject.

◆ onUpdate()

virtual void spark::core::GameObject::onUpdate ( float dt)
inlinevirtual

Method called on every frame.

Parameters
dtThe time in seconds since the last frame.

◆ removeComponent() [1/2]

template<typename T >
requires std::is_base_of_v<Component, T>
void spark::core::GameObject::removeComponent ( )

Removes a component of the given type from the GameObject.

Template Parameters
TThe type of component to remove.

◆ removeComponent() [2/2]

void spark::core::GameObject::removeComponent ( Component * component)

Removes a component from the GameObject.

Parameters
componentA pointer to the component to remove.

◆ transform()

components::Transform * spark::core::GameObject::transform ( ) const
nodiscard

Gets the transform component of the GameObject.

Returns
A pointer to the transform component of the GameObject.

◆ uuid()

const lib::Uuid & spark::core::GameObject::uuid ( ) const
nodiscard

Gets the UUID of the GameObject.

Returns
A const reference to the UUID of this GameObject.