Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
GameObject Class Reference

Concept to constrain types to be derived from Component. More...

#include <gameObject.h>

Inheritance diagram for GameObject:
[legend]

Public Member Functions

 GameObject (Scene &scene)
 
virtual ~GameObject ()=default
 
 GameObject (const GameObject &)=delete
 
GameObjectoperator= (const GameObject &)=delete
 
 GameObject (GameObject &&)=default
 
GameObjectoperator= (GameObject &&)=default
 
std::optional< std::reference_wrapper< GameObject > > parent () const
 
GameObjectparent (GameObject &parent)
 
GameObjectparent (std::nullopt_t null_opt)
 
std::string id () const noexcept
 
bool is_active_in_world () const noexcept
 Retrieves whether the GameObject is active in the world.
 
bool is_active () const noexcept
 Retrieves whether the GameObject is active.
 
void set_inactive () noexcept
 
void set_active () noexcept
 
void set_active_in_world () noexcept
 
void set_inactive_in_world () noexcept
 
GameObjectmark_for_deletion () noexcept
 Marks the GameObject for deletion.
 
bool marked_for_deletion () const noexcept
 
void mark_dont_destroy_on_load (bool dont_destroy) noexcept
 Sets whether the GameObject should not be destroyed on scene load.
 
bool dont_destroy_on_load () const noexcept
 
GameObjectname (const std::string &name)
 
const std::string & name () const
 
GameObjecttag (const std::string &tag)
 
const std::string & tag () const
 
GameObjectprefab_type_id (const std::string &id)
 
const std::string & prefab_type_id () const
 
GameObjectlayer (int layer)
 
int layer () const
 
GameObjecttransform (Transform transform)
 
Transformtransform ()
 
const Transformtransform () const
 
Scenescene () const noexcept
 
void scene (Scene &scene) noexcept
 
std::vector< std::reference_wrapper< GameObject > > & children ()
 
GameObjectadd_child (GameObject &child)
 
GameObjectremove_child (GameObject &child)
 
template<IsComponent T>
std::optional< std::reference_wrapper< T > > get_component () const noexcept
 Get the first component of type T attached to this GameObject.
 
template<IsComponent T>
std::vector< std::reference_wrapper< T > > get_components () const
 Get all components of type T attached to this GameObject.
 
template<IsComponent BS, typename B >
std::optional< std::reference_wrapper< B > > get_script () const noexcept
 Get the first script component of base type BS with behavior of type B.
 
std::vector< std::reference_wrapper< Component > > get_components_all () const
 Get all components attached to this GameObject.
 
template<IsComponent T>
std::vector< std::reference_wrapper< T > > get_components_from_children () const
 Get all components of type T attached to this GameObject and its children.
 
template<IsComponent T, typename... Args>
Tadd_component (Args &&... args)
 Adds a component of type T to this GameObject.
 
template<IsComponent T>
void remove_component (T &component)
 Removes a component of type T from this GameObject.
 
void remove_all_components ()
 
void serialize (std::vector< uint8_t > &out) const
 Serialize this GameObject's own state and its components into out. The format produced is: uint16_t name_len, name bytes uint16_t tag_len, tag bytes uint8_t is_active int32_t layer uint16_t component_count for each component: uint16_t type_name_len, type_name bytes uint32_t payload_len, payload bytes.
 
void deserialize (const std::vector< uint8_t > &data, size_t &offset)
 Deserialize this GameObject's state and dispatch component payloads from data starting at offset. Implementations should advance offset by the number of bytes consumed. Subclasses overriding this method should call GameObject::deserialize first to keep base behavior.
 

Detailed Description

Concept to constrain types to be derived from Component.

Template Parameters
TThe type to check. GameObjects can have multiple components attached to them to define their behavior and properties.

Each GameObject has a unique identifier, a name, a tag, a layer, and a transform representing its position, rotation, and scale in the scene. GameObjects can also have parent-child relationships to form a hierarchy.

GameObjects can be marked as active or inactive, which affects whether they and their components are updated and rendered.

GameObjects can be marked for deletion, which will remove them from the scene at the end of the current update cycle.

GameObjects can be marked as "don't destroy on load", which prevents them from being destroyed when loading a new scene.

Constructor & Destructor Documentation

◆ GameObject() [1/3]

GameObject::GameObject ( Scene scene)
explicit

◆ ~GameObject()

virtual GameObject::~GameObject ( )
virtualdefault

◆ GameObject() [2/3]

GameObject::GameObject ( const GameObject )
delete

◆ GameObject() [3/3]

GameObject::GameObject ( GameObject &&  )
default

Member Function Documentation

◆ add_child()

GameObject & GameObject::add_child ( GameObject child)

◆ add_component()

template<IsComponent T, typename... Args>
T & GameObject::add_component ( Args &&...  args)
inline

Adds a component of type T to this GameObject.

Template Parameters
TThe type of the component to add. Must be derived from Component
Parameters
argsThe arguments to forward to the component's constructor.
Returns
A reference to the added component.

◆ children()

std::vector< std::reference_wrapper< GameObject > > & GameObject::children ( )

◆ deserialize()

void GameObject::deserialize ( const std::vector< uint8_t > &  data,
size_t offset 
)

Deserialize this GameObject's state and dispatch component payloads from data starting at offset. Implementations should advance offset by the number of bytes consumed. Subclasses overriding this method should call GameObject::deserialize first to keep base behavior.

◆ dont_destroy_on_load()

bool GameObject::dont_destroy_on_load ( ) const
noexcept

◆ get_component()

template<IsComponent T>
std::optional< std::reference_wrapper< T > > GameObject::get_component ( ) const
inlinenoexcept

Get the first component of type T attached to this GameObject.

Template Parameters
TThe type of the component to get. Must be derived from Component
Returns
An optional reference to the component if found, std::nullopt otherwise.

◆ get_components()

template<IsComponent T>
std::vector< std::reference_wrapper< T > > GameObject::get_components ( ) const
inline

Get all components of type T attached to this GameObject.

Template Parameters
TThe type of the component to get. Must be derived from Component
Returns
A vector of references to the components found.

◆ get_components_all()

std::vector< std::reference_wrapper< Component > > GameObject::get_components_all ( ) const

Get all components attached to this GameObject.

Returns
A vector of references to all components.

◆ get_components_from_children()

template<IsComponent T>
std::vector< std::reference_wrapper< T > > GameObject::get_components_from_children ( ) const
inline

Get all components of type T attached to this GameObject and its children.

Template Parameters
TThe type of the component to get. Must be derived from Component
Returns
A vector of references to the components found.

◆ get_script()

template<IsComponent BS, typename B >
std::optional< std::reference_wrapper< B > > GameObject::get_script ( ) const
inlinenoexcept

Get the first script component of base type BS with behavior of type B.

Template Parameters
BSThe base type of the script component. Must be derived from Component
BThe type of the behavior.
Returns
An optional reference to the behavior if found, std::nullopt otherwise.

◆ id()

std::string GameObject::id ( ) const
noexcept

◆ is_active()

bool GameObject::is_active ( ) const
noexcept

Retrieves whether the GameObject is active.

Returns
wether the GameObject is active.

◆ is_active_in_world()

bool GameObject::is_active_in_world ( ) const
noexcept

Retrieves whether the GameObject is active in the world.

Returns
wether the GameObject is active in the world.

◆ layer() [1/2]

int GameObject::layer ( ) const

◆ layer() [2/2]

GameObject & GameObject::layer ( int  layer)

◆ mark_dont_destroy_on_load()

void GameObject::mark_dont_destroy_on_load ( bool  dont_destroy)
noexcept

Sets whether the GameObject should not be destroyed on scene load.

Parameters
dont_destroytrue to prevent destruction on scene load, false to
Note
only parent GameObjects can be marked as don't destroy on load. allow destruction.
Returns
whether the GameObject is marked as don't destroy on load.

◆ mark_for_deletion()

GameObject & GameObject::mark_for_deletion ( )
noexcept

Marks the GameObject for deletion.

Note
The GameObject will be deleted at the end of the current scene update cycle.
Warning
Once marked for deletion, all child GameObjects and components will also be marked for deletion.
Returns
wether the GameObject is marked for deletion.

◆ marked_for_deletion()

bool GameObject::marked_for_deletion ( ) const
noexcept

◆ name() [1/2]

const std::string & GameObject::name ( ) const

◆ name() [2/2]

GameObject & GameObject::name ( const std::string &  name)

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ parent() [1/3]

std::optional< std::reference_wrapper< GameObject > > GameObject::parent ( ) const

◆ parent() [2/3]

GameObject & GameObject::parent ( GameObject parent)

◆ parent() [3/3]

GameObject & GameObject::parent ( std::nullopt_t  null_opt)

◆ prefab_type_id() [1/2]

const std::string & GameObject::prefab_type_id ( ) const

◆ prefab_type_id() [2/2]

GameObject & GameObject::prefab_type_id ( const std::string &  id)

◆ remove_all_components()

void GameObject::remove_all_components ( )

◆ remove_child()

GameObject & GameObject::remove_child ( GameObject child)

◆ remove_component()

template<IsComponent T>
void GameObject::remove_component ( T component)
inline

Removes a component of type T from this GameObject.

Template Parameters
TThe type of the component to remove. Must be derived from Component
Parameters
componentThe component to remove. @CAUTION: This will call the component's on_detach method before removing it.
Returns
void

◆ scene() [1/2]

Scene & GameObject::scene ( ) const
noexcept

◆ scene() [2/2]

void GameObject::scene ( Scene scene)
noexcept

◆ serialize()

void GameObject::serialize ( std::vector< uint8_t > &  out) const

Serialize this GameObject's own state and its components into out. The format produced is: uint16_t name_len, name bytes uint16_t tag_len, tag bytes uint8_t is_active int32_t layer uint16_t component_count for each component: uint16_t type_name_len, type_name bytes uint32_t payload_len, payload bytes.

◆ set_active()

void GameObject::set_active ( )
noexcept

◆ set_active_in_world()

void GameObject::set_active_in_world ( )
noexcept

◆ set_inactive()

void GameObject::set_inactive ( )
noexcept

◆ set_inactive_in_world()

void GameObject::set_inactive_in_world ( )
noexcept

◆ tag() [1/2]

const std::string & GameObject::tag ( ) const

◆ tag() [2/2]

GameObject & GameObject::tag ( const std::string &  tag)

◆ transform() [1/3]

Transform & GameObject::transform ( )

◆ transform() [2/3]

const Transform & GameObject::transform ( ) const

◆ transform() [3/3]

GameObject & GameObject::transform ( Transform  transform)

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