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

Base class for defining logic that can be attached to Components, similar to Unity's MonoBehaviour. More...

#include <behavior.h>

Public Member Functions

 Behavior ()
 
virtual ~Behavior ()=default
 
void attach (Component &component)
 Attaches this Behavior to a Component.
 
virtual void on_awake ()
 Called when the Component is created. Runs before on_start().
 
virtual void on_start ()
 Called before the first frame on_update().
 
virtual void on_update (float dt)=0
 Called once per frame.
 
virtual void on_destroy ()
 Called when the Behavior or associated GameObject is destroyed.
 
GameObjectgame_object () const
 
Transformtransform () const
 
bool enabled () const
 
Behaviorenable ()
 Enables the behavior.
 
Behaviordisable ()
 Disables the behavior.
 
virtual void on_serialize (std::vector< uint8_t > &) const
 Serializes the behavior's state into a byte array.
 
virtual void on_deserialize (const std::vector< uint8_t > &, size_t &)
 Deserializes the behavior's state from a byte array.
 
template<typename T >
std::optional< std::reference_wrapper< T > > get_component ()
 Retrieves a component of type T attached to the same GameObject.
 
template<typename T >
std::vector< std::reference_wrapper< T > > get_components ()
 Retrieves all components of type T attached to the same GameObject.
 
template<typename T >
std::optional< std::reference_wrapper< T > > get_component_from_children ()
 Get a component of type T attached to this GameObject's children.
 
template<typename T >
std::vector< std::reference_wrapper< T > > get_components_from_children ()
 Get all components of type T attached to this GameObject's children.
 
template<typename T >
std::optional< std::reference_wrapper< T > > get_component_in_parent ()
 Get a component of type T attached to this GameObject's parents.
 
template<typename T >
std::vector< std::reference_wrapper< T > > get_components_in_parent ()
 Get all components of type T attached to this GameObject's parents.
 
void destroy ()
 Destroy the parent game object.
 
void destroy (Component &component)
 Destroy a specific component on the parent game object.
 
void destroy (GameObject &game_object)
 Destroy a specific game object in the scene.
 

Detailed Description

Base class for defining logic that can be attached to Components, similar to Unity's MonoBehaviour.

Behaviors provide a structured lifecycle for game logic:

A Behavior operates on the Component it is attached to and provides utility methods for accessing the GameObject, Transform, and other Components. See it as a gateway to implement game logic that interacts with the engine's objects.

Note
Users should derive from this class to implement custom behavior.
Warning
on_update() must be overridden.

Usage:

Constructor & Destructor Documentation

◆ Behavior()

Behavior::Behavior ( )

◆ ~Behavior()

virtual Behavior::~Behavior ( )
virtualdefault

Member Function Documentation

◆ attach()

void Behavior::attach ( Component component)

Attaches this Behavior to a Component.

Parameters
componentThe component this behavior should operate on.
Exceptions
std::runtime_errorif already attached.

◆ destroy() [1/3]

void Behavior::destroy ( )

Destroy the parent game object.

◆ destroy() [2/3]

void Behavior::destroy ( Component component)

Destroy a specific component on the parent game object.

◆ destroy() [3/3]

void Behavior::destroy ( GameObject game_object)

Destroy a specific game object in the scene.

◆ disable()

Behavior & Behavior::disable ( )

Disables the behavior.

◆ enable()

Behavior & Behavior::enable ( )

Enables the behavior.

◆ enabled()

bool Behavior::enabled ( ) const
Returns
Whether the behavior is currently enabled.

◆ game_object()

GameObject & Behavior::game_object ( ) const
Returns
Reference to the GameObject this Behavior operates on.
Exceptions
std::runtime_errorif not attached.

◆ get_component()

template<typename T >
std::optional< std::reference_wrapper< T > > Behavior::get_component ( )
inline

Retrieves a component of type T attached to the same GameObject.

Template Parameters
TThe type of the component to retrieve.
Returns
An optional reference to the component if found, std::nullopt otherwise.
Exceptions
std::runtime_errorif not attached.

◆ get_component_from_children()

template<typename T >
std::optional< std::reference_wrapper< T > > Behavior::get_component_from_children ( )
inline

Get a component of type T attached to this GameObject's children.

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

◆ get_component_in_parent()

template<typename T >
std::optional< std::reference_wrapper< T > > Behavior::get_component_in_parent ( )
inline

Get a component of type T attached to this GameObject's parents.

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

◆ get_components()

template<typename T >
std::vector< std::reference_wrapper< T > > Behavior::get_components ( )
inline

Retrieves all components of type T attached to the same GameObject.

Template Parameters
TThe type of the components to retrieve.
Returns
A vector of references to the components found.
Exceptions
std::runtime_errorif not attached.

◆ get_components_from_children()

template<typename T >
std::vector< std::reference_wrapper< T > > Behavior::get_components_from_children ( )
inline

Get all components of type T attached to this GameObject's children.

Template Parameters
TThe type of the component to get.
Returns
A vector of references to the components found.

◆ get_components_in_parent()

template<typename T >
std::vector< std::reference_wrapper< T > > Behavior::get_components_in_parent ( )
inline

Get all components of type T attached to this GameObject's parents.

Template Parameters
TThe type of the component to get.
Returns
A vector of references to the components found.

◆ on_awake()

virtual void Behavior::on_awake ( )
inlinevirtual

Called when the Component is created. Runs before on_start().

◆ on_deserialize()

virtual void Behavior::on_deserialize ( const std::vector< uint8_t > &  ,
size_t  
)
inlinevirtual

Deserializes the behavior's state from a byte array.

Parameters
dataThe byte array containing the serialized state.
offsetThe current offset in the byte array from which to start reading. This offset will be updated as bytes are read.
Returns
void

◆ on_destroy()

virtual void Behavior::on_destroy ( )
inlinevirtual

Called when the Behavior or associated GameObject is destroyed.

◆ on_serialize()

virtual void Behavior::on_serialize ( std::vector< uint8_t > &  ) const
inlinevirtual

Serializes the behavior's state into a byte array.

Parameters
outA vector to which the serialized bytes will be appended.
Returns
void

◆ on_start()

virtual void Behavior::on_start ( )
inlinevirtual

Called before the first frame on_update().

◆ on_update()

virtual void Behavior::on_update ( float  dt)
pure virtual

Called once per frame.

Parameters
dtTime delta since last frame.

◆ transform()

Transform & Behavior::transform ( ) const
Returns
Reference to the Transform of the attached GameObject.

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