Elevate Engine 1
Loading...
Searching...
No Matches
Elevate::GameObject Class Reference

#include <GameObject.h>

Inheritance diagram for Elevate::GameObject:
Elevate::ITransformable

Public Member Functions

 GameObject (std::string name, std::shared_ptr< Scene > scene, std::shared_ptr< GameObject > parent=nullptr)
 
 ~GameObject ()
 
template<typename T , typename... Args>
T & AddComponent (Args &&... args)
 
template<typename T >
T * GetComponent (bool onlyReturnActive=false)
 
std::vector< Component * > GetComponents ()
 
template<typename T >
bool HasComponent ()
 
template<typename T >
void RemoveComponent ()
 
std::string & GetName ()
 
void SetName (std::string newName)
 
void SetParent (std::shared_ptr< GameObject > newParent)
 
void Destroy ()
 
void RemoveChild (std::shared_ptr< GameObject > child)
 
uint32_t GetEntityId ()
 
uint32_t GetObjectId ()
 
const bool HasChild () const
 
std::set< std::shared_ptr< GameObject > > GetChilds () const
 
SceneGetScene ()
 
glm::mat4 GenGlobalMatrix () const
 
void SetFromGlobalMatrix (const glm::mat4 &newWorld)
 
glm::vec3 GetGlobalPosition ()
 
- Public Member Functions inherited from Elevate::ITransformable
TransformGetTransform ()
 
const TransformGetTransform () const
 
void SetPosition (glm::vec3 pos)
 
void SetRotation (glm::vec3 rotation)
 
void SetScale (glm::vec3 scale)
 
glm::vec3 & GetPosition ()
 
glm::vec3 & GetRotation ()
 
glm::vec3 & GetScale ()
 
const glm::mat4 & GetModelMatrix () const
 

Static Public Member Functions

static std::shared_ptr< GameObjectCreate (std::string name, std::shared_ptr< Scene > scene, std::shared_ptr< GameObject > parent=nullptr)
 

Protected Member Functions

void Update ()
 
void Render ()
 
void Notify (Event &event)
 
void OnSetPosition () override
 
void OnSetRotation () override
 
void OnSetScale () override
 
void RenderInEditor ()
 
void RenderWhenSelected ()
 
void AddChild (std::shared_ptr< GameObject > child)
 

Friends

class Scene
 
class ComponentRegistry
 
class Editor::EditorLayer
 

Additional Inherited Members

- Protected Attributes inherited from Elevate::ITransformable
Transform m_Transform
 

Detailed Description

Definition at line 28 of file GameObject.h.

Constructor & Destructor Documentation

◆ GameObject()

Elevate::GameObject::GameObject ( std::string  name,
std::shared_ptr< Scene scene,
std::shared_ptr< GameObject parent = nullptr 
)

Definition at line 22 of file GameObject.cpp.

23 : m_name(name), m_scene(scene.get()), m_parent(parent)
24 {
25 m_goId = s_goIdCount;
26 s_goIdCount++;
27 }

◆ ~GameObject()

Elevate::GameObject::~GameObject ( )

Definition at line 210 of file GameObject.cpp.

211{
213
214 //if (m_scene)
215 //{
216 // m_scene->m_registryId.destroy(entt::entity(m_entityId));
217 //} else EE_CORE_ERROR("Object '{0}' must be destroyed from an existing scene!", m_name);
218}
static void UnregisterGameObject(GameObject *obj)

Member Function Documentation

◆ AddChild()

void Elevate::GameObject::AddChild ( std::shared_ptr< GameObject child)
protected

Definition at line 314 of file GameObject.cpp.

315 {
316 if (child)
317 {
318 child->m_parent = shared_from_this();
319 m_childs.emplace(child);
320 m_scene->RemoveFromRoot(child);
321 }
322 }

◆ AddComponent()

template<typename T , typename... Args>
T & Elevate::GameObject::AddComponent ( Args &&...  args)

Definition at line 24 of file GameObject.inl.

25 {
27
28 // We can't add a second component of the same type
29 if (GetRegistryMap()[m_scene->m_registryId]->all_of<T>(entt::entity(m_entityId)))
30 {
31 EE_ERROR("Error: Tried to add an already existing component to the %s GameObject", m_name);
32 return GetRegistryMap()[m_scene->m_registryId]->get<T>(entt::entity(m_entityId));
33 }
34
35 auto& comp = GetRegistryMap()[m_scene->m_registryId]->emplace<T>(entt::entity(m_entityId), std::forward<Args>(args)...);
36 comp.gameObject = this;
37 comp.Init();
38
39 return comp;
40 }
#define EE_VALIDATE_COMPONENT_TYPE()
#define EE_ERROR(...)
Definition Log.h:69
std::unordered_map< uint32_t, std::unique_ptr< entt::registry > > & GetRegistryMap()

◆ Create()

std::shared_ptr< GameObject > Elevate::GameObject::Create ( std::string  name,
std::shared_ptr< Scene scene,
std::shared_ptr< GameObject parent = nullptr 
)
static

Definition at line 329 of file GameObject.cpp.

330 {
331 std::shared_ptr<GameObject> obj = std::make_shared<GameObject>(name, scene, parent);
332 obj->Initialize();
333 obj->SetPosition({ 0.0f, 0.0f, 0.0f });
334 return obj;
335 }

◆ Destroy()

void Elevate::GameObject::Destroy ( )

Definition at line 293 of file GameObject.cpp.

294 {
295 if (m_parent) {
296 m_parent->RemoveChild(shared_from_this());
297 }
298 else {
299 m_scene->RemoveFromRoot(shared_from_this());
300 }
301
302 auto childsCopy = m_childs;
303 for (const auto& child : childsCopy)
304 {
305 if (child)
306 {
307 child->Destroy();
308 }
309 }
310 m_childs.clear();
311 m_parent.reset();
312 }

◆ GenGlobalMatrix()

glm::mat4 Elevate::GameObject::GenGlobalMatrix ( ) const

Definition at line 338 of file GameObject.cpp.

339 {
340 if (m_parent)
341 {
342 return m_parent->GenGlobalMatrix() * GetModelMatrix();
343 }
344 else
345 {
346 return GetModelMatrix();
347 }
348 }
const glm::mat4 & GetModelMatrix() const

◆ GetChilds()

std::set< std::shared_ptr< GameObject > > Elevate::GameObject::GetChilds ( ) const
inline

Definition at line 60 of file GameObject.h.

60{ return m_childs; }

◆ GetComponent()

template<typename T >
T * Elevate::GameObject::GetComponent ( bool  onlyReturnActive = false)

Definition at line 43 of file GameObject.inl.

44 {
46
47 T* component = GetRegistryMap()[m_scene->m_registryId]->try_get<T>(entt::entity(m_entityId));
48
49 if (!component)
50 {
51 return nullptr;
52 }
53
54 if (onlyReturnActive && !component->IsActive())
55 {
56 return nullptr;
57 }
58
59 return component;
60 }

◆ GetComponents()

std::vector< Component * > Elevate::GameObject::GetComponents ( )

Definition at line 220 of file GameObject.cpp.

221 {
222 std::vector<Component*> components;
223 if (!m_scene) return components;
224
225 for (auto& [type, entry] : ComponentRegistry::GetEntries()) {
226 if (Component* component = entry.getter(weak_from_this())) {
227 components.push_back(component);
228 }
229 }
230
231 return components;
232 }
friend class ComponentRegistry
Definition GameObject.h:107

◆ GetEntityId()

uint32_t Elevate::GameObject::GetEntityId ( )
inline

Definition at line 56 of file GameObject.h.

56{ return m_entityId; }

◆ GetGlobalPosition()

glm::vec3 Elevate::GameObject::GetGlobalPosition ( )

Definition at line 47 of file GameObject.cpp.

48 {
49 glm::mat4 global = GenGlobalMatrix();
50 return glm::vec3(global[3]);
51 }
glm::mat4 GenGlobalMatrix() const

◆ GetName()

std::string & Elevate::GameObject::GetName ( )
inline

Definition at line 48 of file GameObject.h.

48{ return m_name; }

◆ GetObjectId()

uint32_t Elevate::GameObject::GetObjectId ( )
inline

Definition at line 57 of file GameObject.h.

57{ return m_goId; }

◆ GetScene()

Scene * Elevate::GameObject::GetScene ( )
inline

Definition at line 64 of file GameObject.h.

64{ return m_scene; }

◆ HasChild()

const bool Elevate::GameObject::HasChild ( ) const
inline

Definition at line 59 of file GameObject.h.

59{ return m_childs.size() > 0; }

◆ HasComponent()

template<typename T >
bool Elevate::GameObject::HasComponent ( )

Definition at line 63 of file GameObject.inl.

64 {
66
67 return GetRegistryMap()[m_scene->m_registryId]->all_of<T>(entt::entity(m_entityId));
68 }

◆ Notify()

void Elevate::GameObject::Notify ( Event event)
protected

Definition at line 87 of file GameObject.cpp.

88 {
89 for (Component* comp : GetComponents())
90 {
91 if (comp->IsActive())
92 {
93 comp->OnNotify(e);
94 }
95 }
96
97 for (std::shared_ptr<GameObject> child : m_childs)
98 {
99 child->Notify(e);
100 }
101 }
std::vector< Component * > GetComponents()

◆ OnSetPosition()

void Elevate::GameObject::OnSetPosition ( )
overrideprotectedvirtual

Reimplemented from Elevate::ITransformable.

Definition at line 103 of file GameObject.cpp.

104 {
105 if (m_entityId != EE_INVALID_ENTITY_ID && m_isInitialized)
106 {
108 }
109
110 for (Component* comp : GetComponents())
111 {
112 if (comp->IsActive())
113 {
114 comp->OnSetPosition();
115 }
116 }
117
118 for (std::shared_ptr<GameObject> child : m_childs)
119 {
120 child->OnSetPosition();
121 }
122 }
#define EE_INVALID_ENTITY_ID
Definition GameObject.h:14
static void UpdatePosition(GameObject *obj)

◆ OnSetRotation()

void Elevate::GameObject::OnSetRotation ( )
overrideprotectedvirtual

Reimplemented from Elevate::ITransformable.

Definition at line 124 of file GameObject.cpp.

125 {
126 for (Component* comp : GetComponents())
127 {
128 if (comp->IsActive())
129 {
130 comp->OnSetRotation();
131 }
132 }
133
134 for (std::shared_ptr<GameObject> child : m_childs)
135 {
136 child->OnSetRotation();
137 }
138 }

◆ OnSetScale()

void Elevate::GameObject::OnSetScale ( )
overrideprotectedvirtual

Reimplemented from Elevate::ITransformable.

Definition at line 140 of file GameObject.cpp.

141 {
142 for (Component* comp : GetComponents())
143 {
144 if (comp->IsActive())
145 {
146 comp->OnSetScale();
147 }
148 }
149
150 for (std::shared_ptr<GameObject> child : m_childs)
151 {
152 child->OnSetScale();
153 }
154 }

◆ RemoveChild()

void Elevate::GameObject::RemoveChild ( std::shared_ptr< GameObject child)

Definition at line 324 of file GameObject.cpp.

325 {
326 m_childs.erase(child);
327 }

◆ RemoveComponent()

template<typename T >
void Elevate::GameObject::RemoveComponent ( )

Definition at line 71 of file GameObject.inl.

72 {
74
75 if (HasComponent<T>())
76 {
77 GetComponent<T>()->Destroy();
78 GetRegistryMap()[m_scene->m_registryId]->remove<T>(entt::entity(m_entityId));
79 }
80 else EE_ERROR("Trying to remove a missing component. You need to add the component before removing it.");
81 }

◆ Render()

void Elevate::GameObject::Render ( )
protected

Definition at line 70 of file GameObject.cpp.

71 {
72 // TODO MAKE GETCOMPONENTS ONLY RETURN ACTIVE COMOPNENTS TO PREVENT THE CHECK
73 for (Component* comp : GetComponents())
74 {
75 if (comp->IsActive())
76 {
77 comp->Render();
78 }
79 }
80
81 for (std::shared_ptr<GameObject> child : m_childs)
82 {
83 child->Render();
84 }
85 }

◆ RenderInEditor()

void Elevate::GameObject::RenderInEditor ( )
protected

Definition at line 234 of file GameObject.cpp.

235 {
236 // TODO MAKE GETCOMPONENTS ONLY RETURN ACTIVE COMOPNENTS TO PREVENT THE CHECK
237 for (Component* comp : GetComponents())
238 {
239 if (comp->IsActive())
240 {
241 comp->RenderInEditor();
242 }
243 }
244
245 for (std::shared_ptr<GameObject> child : m_childs)
246 {
247 child->RenderInEditor();
248 }
249 }

◆ RenderWhenSelected()

void Elevate::GameObject::RenderWhenSelected ( )
protected

Definition at line 251 of file GameObject.cpp.

252 {
253 // TODO MAKE GETCOMPONENTS ONLY RETURN ACTIVE COMOPNENTS TO PREVENT THE CHECK
254 for (Component* comp : GetComponents())
255 {
256 if (comp->IsActive())
257 {
258 comp->RenderWhenSelected();
259 }
260 }
261
262 for (std::shared_ptr<GameObject> child : m_childs)
263 {
264 child->RenderWhenSelected();
265 }
266 }

◆ SetFromGlobalMatrix()

void Elevate::GameObject::SetFromGlobalMatrix ( const glm::mat4 &  newWorld)

Definition at line 29 of file GameObject.cpp.

30 {
31 glm::mat4 newLocal = m_parent ? glm::inverse(m_parent->GenGlobalMatrix()) * newWorld : newWorld;
32
33 glm::vec3 scale;
34 glm::quat rotationQuat;
35 glm::vec3 position;
36 glm::vec3 skew;
37 glm::vec4 perspective;
38
39 glm::decompose(newLocal, scale, rotationQuat, position, skew, perspective);
40 glm::vec3 rotationEuler = glm::degrees(glm::eulerAngles(rotationQuat));
41
42 SetScale(scale);
43 SetRotation(rotationEuler);
44 SetPosition(position);
45 }
void SetRotation(glm::vec3 rotation)
void SetPosition(glm::vec3 pos)
void SetScale(glm::vec3 scale)

◆ SetName()

void Elevate::GameObject::SetName ( std::string  newName)
inline

Definition at line 49 of file GameObject.h.

49{ m_name = newName; }

◆ SetParent()

void Elevate::GameObject::SetParent ( std::shared_ptr< GameObject newParent)

Definition at line 268 of file GameObject.cpp.

269 {
270 if (newParent == m_parent)
271 return;
272
273 if (m_parent)
274 {
275 m_parent->RemoveChild(shared_from_this());
276 }
277
278 this->m_parent = newParent;
279
280 if (newParent)
281 {
282 newParent->AddChild(shared_from_this());
283 if (m_scene)
284 {
285 m_scene->RemoveFromRoot(shared_from_this());
286 }
287 }
288 else {
289 m_scene->AddRootObject(shared_from_this());
290 }
291 }

◆ Update()

void Elevate::GameObject::Update ( )
protected

Definition at line 53 of file GameObject.cpp.

54 {
55 // TODO MAKE GETCOMPONENTS ONLY RETURN ACTIVE COMOPNENTS TO PREVENT THE CHECK
56 for (Component* comp : GetComponents())
57 {
58 if (comp->IsActive())
59 {
60 comp->Update();
61 }
62 }
63
64 for (std::shared_ptr<GameObject> child : m_childs)
65 {
66 child->Update();
67 }
68 }

Friends And Related Symbol Documentation

◆ ComponentRegistry

friend class ComponentRegistry
friend

Definition at line 107 of file GameObject.h.

◆ Editor::EditorLayer

friend class Editor::EditorLayer
friend

Definition at line 108 of file GameObject.h.

◆ Scene

friend class Scene
friend

Definition at line 106 of file GameObject.h.


The documentation for this class was generated from the following files: