29 #include <unordered_map> 33 #include <ht_platform.h> 34 #include <ht_transform.h> 41 #include <ht_component.h> 60 const Core::Guid& GetGuid(
void)
const;
65 const std::string& GetName(
void)
const;
76 bool GetEnabled(
void)
const;
82 void SetEnabled(
bool value);
117 GameObject* GetChildAtIndex(std::size_t index);
129 void RemoveChildAtIndex(std::size_t index);
154 void MarkForDestroy(
void);
166 template <
typename T>
167 bool AddComponent(T *component);
180 template <
typename T,
typename... Args>
181 bool AddComponent(Args&&... args);
190 template <
typename T>
191 bool RemoveComponent(
void);
198 template <
typename T>
199 bool HasComponent(
void)
const;
208 template <
typename T1,
typename T2,
typename... Args>
209 bool HasComponent(
void)
const;
217 template <
typename T>
218 T* GetComponent(
void);
226 template <
typename... Args>
227 std::tuple<Args*...> GetComponents(
void);
235 template <
typename T>
236 bool EnableComponent(
void);
244 template <
typename... Args>
247 return std::make_tuple(EnableComponent<Args>()...);
256 template <
typename T>
257 bool DisableComponent(
void);
264 template <
typename... Args>
267 return std::make_tuple(DisableComponent<Args>()...);
305 void OnEnabled(
void);
311 void OnDisabled(
void);
322 bool AddUninitializedComponent(T *component);
335 template <
typename T,
typename... Args>
336 bool AddUninitializedComponent(Args&&... args);
344 std::vector<GameObject*> m_children;
345 std::vector<Game::Component*> m_components;
346 std::unordered_map<Core::Guid, std::vector<Component*>::size_type> m_componentMap;
350 template <
typename T>
353 static_assert(std::is_base_of<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
355 Core::Guid component_id = Game::Component::template GetComponentId<T>();
356 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
357 if (iter != m_componentMap.cend())
360 m_componentMap.insert(std::make_pair(component_id, m_components.size()));
361 m_components.push_back(component);
363 component->SetOwner(
this);
365 component->VOnInit();
368 component->VOnEnable();
374 inline bool GameObject::AddComponent<Component>(
Component *component)
376 Core::Guid component_id = component->VGetComponentId();
377 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
378 if (iter != m_componentMap.cend())
381 m_componentMap.insert(std::make_pair(component_id, m_components.size()));
382 m_components.push_back(component);
384 component->SetOwner(
this);
386 component->VOnInit();
389 component->SetEnabled(
true);
394 template <
typename T,
typename... Args>
397 static_assert(std::is_base_of<Component, T>::value && !std::is_same<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
399 Core::Guid component_id = Game::Component:: template GetComponentId<T>();
400 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
401 if (iter != m_componentMap.cend())
404 T *component =
new T(std::forward<Args>(args)...);
405 m_componentMap.insert(std::make_pair(component_id, m_components.size()));
406 m_components.push_back(component);
408 component->SetOwner(
this);
410 component->VOnInit();
413 component->VOnEnable();
418 template <
typename T>
421 static_assert(std::is_base_of<Component, T>::value && !std::is_same<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
423 Core::Guid component_id = Game::Component:: template GetComponentId<T>();
424 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
425 if (iter == m_componentMap.cend())
428 std::vector<Component*>::size_type index = m_componentMap[component_id];
429 Component *component = m_components[index];
434 m_components[index] =
nullptr;
435 m_componentMap.erase(component_id);
441 template <
typename T>
444 static_assert(std::is_base_of<Component, T>::value && !std::is_same<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
445 Core::Guid component_id = Component:: template GetComponentId<T>();
446 return (m_componentMap.find(component_id) != m_componentMap.cend());
449 template <
typename T1,
typename T2,
typename... Args>
452 static_assert(std::is_base_of<Component, T1>::value && !std::is_same<Component, T1>::value,
"Must be a sub-class of Hatchit::Game::Component!");
453 return HasComponent<T1>() && HasComponent<T2, Args...>();
456 template <
typename T>
459 static_assert(std::is_base_of<Component, T>::value && !std::is_same<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
461 Core::Guid component_id = Component::GetComponentId<T>();
462 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
463 if (iter == m_componentMap.cend())
466 std::vector<Component*>::size_type index = m_componentMap[component_id];
467 return dynamic_cast<T*
>(m_components[index]);
470 template <
typename... Args>
473 return std::make_tuple(GetComponent<Args>()...);
476 template <
typename T>
479 static_assert(std::is_base_of<Component, T>::value && !std::is_same<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
481 if (!HasComponent<T>())
484 Core::Guid component_id = Component::GetComponentId<T>();
485 std::vector<Component*>::size_type index = m_componentMap[component_id];
486 Component *component = m_components[index];
495 template <
typename T>
498 static_assert(std::is_base_of<Component, T>::value && !std::is_same<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
500 if (!HasComponent<T>())
503 Core::Guid component_id = Component::GetComponentId<T>();
504 std::vector<Component*>::size_type index = m_componentMap[component_id];
505 Component *component = m_components[index];
515 inline bool GameObject::AddUninitializedComponent(T* component)
517 static_assert(std::is_base_of<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
519 Core::Guid component_id = Component::GetComponentId<T>();
520 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
521 if (iter != m_componentMap.cend())
524 m_componentMap.insert(std::make_pair(component_id, m_components.size()));
525 m_components.push_back(component);
527 component->SetOwner(
this);
533 inline bool GameObject::AddUninitializedComponent<Component>(
Component* component)
535 Core::Guid component_id = component->VGetComponentId();
536 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
537 if (iter != m_componentMap.cend())
540 m_componentMap.insert(std::make_pair(component_id, m_components.size()));
541 m_components.push_back(component);
543 component->SetOwner(
this);
548 template<
typename T,
typename ...Args>
549 inline bool GameObject::AddUninitializedComponent(Args && ...args)
551 static_assert(std::is_base_of<Component, T>::value && !std::is_same<Component, T>::value,
"Must be a sub-class of Hatchit::Game::Component!");
553 Core::Guid component_id = Game::Component:: template GetComponentId<T>();
554 std::unordered_map<Core::Guid, std::vector<Component*>::size_type>::const_iterator iter = m_componentMap.find(component_id);
555 if (iter != m_componentMap.cend())
558 T *component =
new T(std::forward<Args>(args)...);
559 m_componentMap.insert(std::make_pair(component_id, m_components.size()));
560 m_components.push_back(component);
562 component->SetOwner(
this);
auto DisableComponents(void) -> decltype(std::make_tuple(DisableComponent< Args >()...))
Disable Components of type Args...
Definition: ht_gameobject.h:265
void Disable(void)
An inline for SetEnabled(false);.
Definition: ht_gameobject.h:95
virtual void VOnDestroy(void)=0
Called when the GameObject is destroyed/deleted.
void Enable(void)
An inline for SetEnabled(true);.
Definition: ht_gameobject.h:87
void SetEnabled(bool value)
Setter that sets the value of m_enabled.
Definition: ht_component.cpp:36
Definition: ht_component.h:42
T * GetComponent(void)
Return a Component of type T attached to this GameObject.
Definition: ht_gameobject.h:457
Hatchit Engine Copyright(c) 2015-2016 Third-Degree.
Definition: ht_glfwkeyboard.h:21
Definition: ht_gameobject.h:48
bool AddComponent(T *component)
Attempts to attach a Component of type T.
Definition: ht_gameobject.h:351
std::tuple< Args *... > GetComponents(void)
Returns Components of type Args...
Definition: ht_gameobject.h:471
bool DisableComponent(void)
Disable a Component of type T attached to this GameObject.
Definition: ht_gameobject.h:496
bool EnableComponent(void)
Enable a Component of type T attached to this GameObject.
Definition: ht_gameobject.h:477
bool HasComponent(void) const
Test if a Component of type T is attached to this GameObject.
Definition: ht_gameobject.h:442
auto EnableComponents(void) -> decltype(std::make_tuple(EnableComponent< Args >()...))
Enable Components of type Args...
Definition: ht_gameobject.h:245
bool RemoveComponent(void)
Attempts to remove a Component of type T.
Definition: ht_gameobject.h:419
bool GetEnabled(void)
Getter that returns that value of m_enabled.
Definition: ht_component.cpp:31
Defines a scene.
Definition: ht_scene.h:44