Elevate Engine 1
Loading...
Searching...
No Matches
GameObject.inl
Go to the documentation of this file.
1#pragma once
2#include "GameObject.h"
3
4#include <format>
5
6#include <entt/entt.hpp>
7
12
13namespace Elevate
14{
15 class Component;
16}
17
18#define EE_VALIDATE_COMPONENT_TYPE() EE_ASSERT((std::is_base_of<Component, T>::value), "EE_VALIDATE_COMPONENT_TYPE() %s : Type specifier must be a child of the Component class.", m_name);
19
20// TODO REPLACE ALL EE_LOGS TO EE_CORE_LOGS (they are in the engine so they should use this feature)
21namespace Elevate
22{
23 template<typename T, typename... Args>
24 T& GameObject::AddComponent(Args&&... args)
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 }
41
42 template <typename T>
43 T* GameObject::GetComponent(bool onlyReturnActive)
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 }
61
62 template <typename T>
64 {
66
67 return GetRegistryMap()[m_scene->m_registryId]->all_of<T>(entt::entity(m_entityId));
68 }
69
70 template <typename T>
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 }
82}
#define EE_VALIDATE_COMPONENT_TYPE()
#define EE_ERROR(...)
Definition Log.h:69
T & AddComponent(Args &&... args)
T * GetComponent(bool onlyReturnActive=false)
std::unordered_map< uint32_t, std::unique_ptr< entt::registry > > & GetRegistryMap()