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.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <functional>
7#include <optional>
8#include <stdexcept>
9
34class Behavior {
35 public:
37 virtual ~Behavior() = default;
38
44 void attach(Component& component);
45
47 virtual void on_awake(){};
48
50 virtual void on_start(){};
51
56 virtual void on_update(float dt) = 0;
57
59 virtual void on_destroy(){};
60
65 [[nodiscard]] GameObject& game_object() const;
66
70 [[nodiscard]] Transform& transform() const;
71
73 [[nodiscard]] bool enabled() const;
74
77
80
86 virtual void on_serialize(std::vector<uint8_t>& /*out*/) const {};
87
95 virtual void on_deserialize(const std::vector<uint8_t>& /*data*/,
96 size_t& /*offset*/){};
97
105 template <typename T>
106 std::optional<std::reference_wrapper<T>> get_component() {
107 if (!attached_component_) {
108 throw std::runtime_error("Behavior has no associated GameObject.");
109 }
110
111 return game_object().template get_component<T>();
112 }
113
120 template <typename T>
121 std::vector<std::reference_wrapper<T>> get_components() {
122 if (!attached_component_) {
123 throw std::runtime_error("Behavior has no associated GameObject.");
124 }
125
126 return game_object().template get_components<T>();
127 }
128
135 template <typename T>
136 std::optional<std::reference_wrapper<T>> get_component_from_children() {
137 if (!attached_component_) {
138 throw std::runtime_error("Behavior has no associated GameObject.");
139 }
140
141 for (auto& child : game_object().children()) {
142 auto comp = child.get().template get_component<T>();
143 if (comp) {
144 return comp;
145 }
146 }
147
148 return std::nullopt;
149 }
150
156 template <typename T>
157 std::vector<std::reference_wrapper<T>> get_components_from_children() {
158 if (!attached_component_) {
159 throw std::runtime_error("Behavior has no associated GameObject.");
160 }
161
163 }
164
171 template <typename T>
172 std::optional<std::reference_wrapper<T>> get_component_in_parent() {
173 if (!attached_component_) {
174 throw std::runtime_error("Behavior has no associated GameObject.");
175 }
176
178
179 while (current_parent) {
180 auto comp = current_parent->get().template get_component<T>();
181 if (comp) {
182 return comp;
183 }
184 current_parent = current_parent->get().parent();
185 }
186
187 return std::nullopt;
188 }
189
195 template <typename T>
196 std::vector<std::reference_wrapper<T>> get_components_in_parent() {
197 if (!attached_component_) {
198 throw std::runtime_error("Behavior has no associated GameObject.");
199 }
200
201 std::vector<std::reference_wrapper<T>> result;
203
204 while (current_parent) {
205 auto comps = current_parent->get().template get_components<T>();
206 result.insert(result.end(), comps.begin(), comps.end());
207
208 current_parent = current_parent->get().parent();
209 }
210
211 return result;
212 }
213
215 void destroy();
216
219
222
223 private:
224 std::optional<std::reference_wrapper<Component>> attached_component_;
225 bool enabled_{true};
226};
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
bool enabled() const
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()
Represents the position, rotation, and scale of a GameObject in 3D space. The Transform class encapsu...
Definition transform.h:22