105 template <
typename T>
107 if (!attached_component_) {
108 throw std::runtime_error(
"Behavior has no associated GameObject.");
120 template <
typename T>
122 if (!attached_component_) {
123 throw std::runtime_error(
"Behavior has no associated GameObject.");
135 template <
typename T>
137 if (!attached_component_) {
138 throw std::runtime_error(
"Behavior has no associated GameObject.");
156 template <
typename T>
158 if (!attached_component_) {
159 throw std::runtime_error(
"Behavior has no associated GameObject.");
171 template <
typename T>
173 if (!attached_component_) {
174 throw std::runtime_error(
"Behavior has no associated GameObject.");
195 template <
typename T>
197 if (!attached_component_) {
198 throw std::runtime_error(
"Behavior has no associated GameObject.");
201 std::vector<std::reference_wrapper<T>>
result;
224 std::optional<std::reference_wrapper<Component>> attached_component_;
Base class for defining logic that can be attached to Components, similar to Unity's MonoBehaviour.
Definition behavior.h:34
std::optional< std::reference_wrapper< T > > get_component()
Retrieves a component of type T attached to the same GameObject.
Definition behavior.h:106
GameObject & game_object() const
void attach(Component &component)
Attaches this Behavior to a Component.
virtual void on_serialize(std::vector< uint8_t > &) const
Serializes the behavior's state into a byte array.
Definition behavior.h:86
std::optional< std::reference_wrapper< T > > get_component_from_children()
Get a component of type T attached to this GameObject's children.
Definition behavior.h:136
Behavior & disable()
Disables the behavior.
Behavior & enable()
Enables the behavior.
std::vector< std::reference_wrapper< T > > get_components()
Retrieves all components of type T attached to the same GameObject.
Definition behavior.h:121
virtual void on_update(float dt)=0
Called once per frame.
std::vector< std::reference_wrapper< T > > get_components_in_parent()
Get all components of type T attached to this GameObject's parents.
Definition behavior.h:196
virtual void on_destroy()
Called when the Behavior or associated GameObject is destroyed.
Definition behavior.h:59
void destroy()
Destroy the parent game object.
void destroy(Component &component)
Destroy a specific component on the parent game object.
virtual void on_start()
Called before the first frame on_update().
Definition behavior.h:50
virtual ~Behavior()=default
virtual void on_awake()
Called when the Component is created. Runs before on_start().
Definition behavior.h:47
std::vector< std::reference_wrapper< T > > get_components_from_children()
Get all components of type T attached to this GameObject's children.
Definition behavior.h:157
std::optional< std::reference_wrapper< T > > get_component_in_parent()
Get a component of type T attached to this GameObject's parents.
Definition behavior.h:172
virtual void on_deserialize(const std::vector< uint8_t > &, size_t &)
Deserializes the behavior's state from a byte array.
Definition behavior.h:95
Transform & transform() const
void destroy(GameObject &game_object)
Destroy a specific game object in the scene.
Base class for all components that can be attached to GameObjects.
Definition component.h:24
Concept to constrain types to be derived from Component.
Definition gameObject.h:33
std::optional< std::reference_wrapper< GameObject > > parent() const
std::vector< std::reference_wrapper< GameObject > > & children()