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.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <algorithm>
7#include <optional>
8#include <ranges>
9#include <string>
10#include <vector>
11
12class Scene;
13
34 private:
35 std::string id_;
36
37 std::vector<std::unique_ptr<Component>> components_;
38 std::vector<std::reference_wrapper<GameObject>> children_;
39
40 std::optional<std::reference_wrapper<GameObject>> parent_;
41 bool is_active_{true};
42 bool is_active_in_world_{true};
43
44 std::string name_;
45 std::string tag_;
46 int layer_{};
47 std::reference_wrapper<Scene> scene_;
48 Transform transform_;
49
50 bool marked_for_deletion_{false};
51 bool dont_destroy_on_load_{false};
52 std::string prefab_type_id_{}; // Identifier for prefab type used during
53 // network replication
54
55 public:
57 virtual ~GameObject() = default;
58
59 GameObject(const GameObject&) = delete;
60 GameObject& operator=(const GameObject&) = delete;
61
62 GameObject(GameObject&&) = default;
64
65 std::optional<std::reference_wrapper<GameObject>> parent() const; // NOLINT
67 GameObject& parent(std::nullopt_t null_opt);
68
69 [[nodiscard]] std::string id() const noexcept;
70
76
82
87
100
110
112 [[nodiscard]] const std::string& name() const;
113
115 [[nodiscard]] const std::string& tag() const;
116
119
122
126
129
133
143 for (const auto& component : components_) {
144 if (auto* casted = dynamic_cast<T*>(component.get())) {
145 return std::ref(*casted);
146 }
147 }
148 return std::nullopt;
149 }
150
156 template <IsComponent T>
157 [[nodiscard]] std::vector<std::reference_wrapper<T>> get_components() const {
158 auto filtered =
159 components_ |
160 std::views::filter([](auto& c) { return dynamic_cast<T*>(c.get()); }) |
161 std::views::transform([](auto& c) -> std::reference_wrapper<T> {
162 return std::ref(*static_cast<T*>(c.get()));
163 });
164
165 return std::vector<std::reference_wrapper<T>>(filtered.begin(),
166 filtered.end());
167 }
168
178 template <IsComponent BS, typename B>
179 [[nodiscard]] std::optional<std::reference_wrapper<B>> get_script()
182
183 for (const auto& script : scripts) {
184 if (auto& behavior = script.get().behavior();
185 dynamic_cast<B*>(&behavior)) {
186 return std::ref(static_cast<B&>(behavior));
187 }
188 }
189
190 return std::nullopt;
191 }
192
197 [[nodiscard]] std::vector<std::reference_wrapper<Component>>
199
206 template <IsComponent T>
207 std::vector<std::reference_wrapper<T>> get_components_from_children() const {
208 std::vector<std::reference_wrapper<T>> result{};
209
210 for (const auto& child : children_) {
211 auto child_components = child.get().get_components_from_children<T>();
212 result.insert(result.end(), child_components.begin(),
213 child_components.end());
214 }
215
216 return result;
217 }
218
225 template <IsComponent T, typename... Args>
227 auto component = std::make_unique<T>(std::forward<Args>(args)...);
228 T& ref = *component;
229
230 component->parent(std::ref(*this));
231 component->on_attach();
232
233 components_.emplace_back(std::move(component));
234 return ref;
235 }
236
246 template <IsComponent T>
248 component.on_detach();
249 component.parent(std::nullopt);
250
251 components_.erase(
252 std::remove_if(components_.begin(), components_.end(),
253 [&component](const std::unique_ptr<Component>& c) {
254 return c.get() == &component;
255 }),
256 components_.end());
257 }
258
260
273 void serialize(std::vector<uint8_t>& out) const;
274
281 void deserialize(const std::vector<uint8_t>& data, size_t& offset);
282};
Concept to constrain types to be derived from Component.
Definition gameObject.h:33
const std::string & prefab_type_id() const
GameObject & operator=(const GameObject &)=delete
GameObject & mark_for_deletion() noexcept
Marks the GameObject for deletion.
const std::string & name() const
Transform & transform()
GameObject & remove_child(GameObject &child)
void remove_component(T &component)
Removes a component of type T from this GameObject.
Definition gameObject.h:247
void set_inactive() noexcept
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 n...
bool is_active_in_world() const noexcept
Retrieves whether the GameObject is active in the world.
std::optional< std::reference_wrapper< GameObject > > parent() const
std::vector< std::reference_wrapper< GameObject > > & children()
const std::string & tag() const
bool is_active() const noexcept
Retrieves whether the GameObject is active.
GameObject & add_child(GameObject &child)
virtual ~GameObject()=default
void set_active_in_world() noexcept
T & add_component(Args &&... args)
Adds a component of type T to this GameObject.
Definition gameObject.h:226
void set_inactive_in_world() noexcept
bool dont_destroy_on_load() const noexcept
GameObject & parent(GameObject &parent)
void remove_all_components()
std::vector< std::reference_wrapper< T > > get_components_from_children() const
Get all components of type T attached to this GameObject and its children.
Definition gameObject.h:207
GameObject(const GameObject &)=delete
GameObject(Scene &scene)
int layer() const
void set_active() noexcept
std::optional< std::reference_wrapper< B > > get_script() const noexcept
Get the first script component of base type BS with behavior of type B.
Definition gameObject.h:179
GameObject & parent(std::nullopt_t null_opt)
std::optional< std::reference_wrapper< T > > get_component() const noexcept
Get the first component of type T attached to this GameObject.
Definition gameObject.h:141
Scene & scene() const noexcept
bool marked_for_deletion() const noexcept
std::vector< std::reference_wrapper< Component > > get_components_all() const
Get all components attached to this GameObject.
void mark_dont_destroy_on_load(bool dont_destroy) noexcept
Sets whether the GameObject should not be destroyed on scene load.
GameObject(GameObject &&)=default
std::vector< std::reference_wrapper< T > > get_components() const
Get all components of type T attached to this GameObject.
Definition gameObject.h:157
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....
GameObject & operator=(GameObject &&)=default
std::string id() const noexcept
Represents a scene within the engine. A scene contains multiple GameObjects and manages their lifecyc...
Definition scene.h:19
Represents the position, rotation, and scale of a GameObject in 3D space. The Transform class encapsu...
Definition transform.h:22
Definition component.h:122