SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
GameObject.h
1#pragma once
2
3#include <algorithm>
4
5namespace spark::core
6{
7 template <typename T, typename... Args> requires std::is_base_of_v<GameObject, T>
8 T* GameObject::Instantiate(std::string name, GameObject* parent, Args&&... args)
9 {
10 T* object = new T(std::move(name), parent, std::forward<Args>(args)...);
11 if (parent)
12 if (parent->m_initialized)
13 static_cast<AbstractGameObject*>(object)->onSpawn();
14 return object;
15 }
16
17 template <typename T, typename... Args> requires std::is_base_of_v<Component, T>
18 void GameObject::addComponent(Args&&... args)
19 {
20 addComponent(new T(this, std::forward<Args>(args)...), true);
21 }
22
23 template <typename T> requires std::is_base_of_v<Component, T>
28
29 template <typename T> requires std::is_base_of_v<Component, T>
31 {
32 return component<T>() != nullptr;
33 }
34
35 template <typename T> requires std::is_base_of_v<Component, T>
36 std::vector<T*> GameObject::componentsInChildren() const
37 {
38 std::vector<T*> components;
39 for (const auto* child : children())
40 {
41 // On the object
42 auto* component = child->component<T>();
43 if (component != nullptr)
44 components.push_back(component);
45
46 // On the children
47 auto child_components = child->componentsInChildren<T>();
48 std::copy(child_components.begin(), child_components.end(), std::back_inserter(components));
49 }
50 return components;
51 }
52
53 template <typename T> requires std::is_base_of_v<Component, T>
55 {
56 const auto it = m_components.find(&T::classRtti());
57 if (it == m_components.cend())
58 return nullptr;
59 return static_cast<T*>(it->second.first);
60 }
61
62 template <typename T> requires std::is_base_of_v<Component, T>
64 {
65 for (const auto* child : children())
66 {
67 auto* component = child->component<T>();
68 if (component != nullptr)
69 return component;
70 }
71 return nullptr;
72 }
73
74 template <typename T> requires std::is_base_of_v<Component, T>
76 {
77 return parent()->component<T>();
78 }
79}
A GameObject is any object in the game. It contains a list of components that provides functionality ...
Definition GameObject.h:26
static T * Instantiate(std::string name, GameObject *parent, Args &&... args)
Instantiates a new GameObject.
Definition GameObject.h:8
const std::string & name() const
Gets the name of the GameObject.
Definition GameObject.cpp:58
virtual void onSpawn()
Method called when the GameObject is spawned in the scene.
Definition GameObject.h:178
void addComponent(Component *component, bool managed=false)
Adds a component to the GameObject.
Definition GameObject.cpp:68
void removeComponent()
Removes a component of the given type from the GameObject.
Definition GameObject.h:24
std::vector< T * > componentsInChildren() const
Gets a list of components of type T in any direct child of the GameObject.
Definition GameObject.h:36
bool hasComponent() const
Checks if the GameObject has a component of type T.
Definition GameObject.h:30
T * componentInParent() const
Gets a component of type T in any direct parent of the GameObject.
Definition GameObject.h:75
T * component() const
Gets a component of type T.
Definition GameObject.h:54
std::vector< Component * > components() const
Gets all the components for this GameObject.
Definition GameObject.cpp:87
T * componentInChildren() const
Gets a component of type T in any direct child of the GameObject.
Definition GameObject.h:63