37 std::vector<std::unique_ptr<Component>> components_;
38 std::vector<std::reference_wrapper<GameObject>> children_;
40 std::optional<std::reference_wrapper<GameObject>> parent_;
41 bool is_active_{
true};
42 bool is_active_in_world_{
true};
47 std::reference_wrapper<Scene> scene_;
50 bool marked_for_deletion_{
false};
51 bool dont_destroy_on_load_{
false};
52 std::string prefab_type_id_{};
65 std::optional<std::reference_wrapper<GameObject>>
parent()
const;
143 for (
const auto&
component : components_) {
156 template <IsComponent T>
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()));
165 return std::vector<std::reference_wrapper<T>>(
filtered.begin(),
178 template <IsComponent BS,
typename B>
184 if (
auto& behavior =
script.get().behavior();
185 dynamic_cast<B*
>(&behavior)) {
186 return std::ref(
static_cast<B&
>(behavior));
197 [[
nodiscard]] std::vector<std::reference_wrapper<Component>>
206 template <IsComponent T>
208 std::vector<std::reference_wrapper<T>>
result{};
210 for (
const auto&
child : children_) {
227 auto component = std::make_unique<T>(std::forward<Args>(
args)...);
233 components_.emplace_back(std::move(
component));
246 template <IsComponent T>
252 std::remove_if(components_.begin(), components_.end(),
253 [&
component](
const std::unique_ptr<Component>&
c) {
254 return c.get() == &component;
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
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
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
Definition component.h:122