Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
IEntity.h
Go to the documentation of this file.
1
6
7#pragma once
8#ifndef ECS__I_ENTITY_H__
9#define ECS__I_ENTITY_H__
10
11#include "util/Handle.h"
12
13namespace ECS
14{
16
18
20
21 struct CustomEvent;
22
23 template<class T, class ...P> T* AddComponent( ComponentManager*, EntityId, P&&... param );
24 template<class T> T* GetComponent( ComponentManager*, EntityId );
25 template<class T> void RemoveComponent( ComponentManager*, EntityId );
26
28 {
29 friend class EntityManager;
30
31 private:
32
33 // set on create; in EntityManager
35
36 protected:
37
39
40
41
42 // set on create; in EntityManager
44
45 // if false, entity won't be updated
47 static std::shared_mutex s_ComponentManagerLock;
48 public:
49
50 IEntity();
51 virtual ~IEntity();
52
53 template<class T>
54 T* GetComponent() const
55 {
56 std::shared_lock<std::shared_mutex> r_lock(s_ComponentManagerLock);
57 return ECS::GetComponent<T>( this->m_ComponentManagerInstance, this->m_EntityID );
58 }
59
60 template<class T, class ...P>
61 T* AddComponent(P&&... param)
62 {
63 std::scoped_lock<std::shared_mutex > r_lock(s_ComponentManagerLock);
64 return ECS::AddComponent<T>( this->m_ComponentManagerInstance, this->m_EntityID, std::forward<P>( param )... );
65 }
66
67 template<class T>
69 {
70 std::scoped_lock<std::shared_mutex> r_lock(s_ComponentManagerLock);
71 ECS::RemoveComponent<T>( this->m_ComponentManagerInstance, this->m_EntityID );
72 }
73
74 void RemoveAllComponents();
75
76 void PassDataToAllComponents(const ECS::CustomEvent& evt);
77 // COMPARE ENTITIES
78
79 inline bool operator==(const IEntity& rhs) const { return this->m_EntityID == rhs.m_EntityID; }
80 inline bool operator!=(const IEntity& rhs) const { return this->m_EntityID != rhs.m_EntityID; }
81 inline bool operator==(const IEntity* rhs) const { return this->m_EntityID == rhs->m_EntityID; }
82 inline bool operator!=(const IEntity* rhs) const { return this->m_EntityID != rhs->m_EntityID; }
83
84 // ACCESORS
86
87 inline EntityId GetEntityID() const { return this->m_EntityID; }
88
89 void SetActive(bool active);
90
91 inline bool IsActive() const { return this->m_Active; }
92
93 virtual void OnEnable() {}
94 virtual void OnDisable() {}
95 };
96
97} // namespace ECS
98
99#endif // ECS__I_ENTITY_H__
#define DECLARE_STATIC_LOGGER
Definition: LoggerMacro.h:16
#define ECS_API
Definition: Platform.h:16
bool m_Active
Definition: IEntity.h:46
T * GetComponent() const
Definition: IEntity.h:54
ComponentManager * m_ComponentManagerInstance
Definition: IEntity.h:34
void RemoveComponent()
Definition: IEntity.h:68
virtual void OnEnable()
Definition: IEntity.h:93
bool operator==(const IEntity *rhs) const
Definition: IEntity.h:81
bool operator!=(const IEntity &rhs) const
Definition: IEntity.h:80
DECLARE_STATIC_LOGGER EntityId m_EntityID
Definition: IEntity.h:43
bool operator!=(const IEntity *rhs) const
Definition: IEntity.h:82
bool IsActive() const
Definition: IEntity.h:91
static std::shared_mutex s_ComponentManagerLock
Definition: IEntity.h:47
virtual void OnDisable()
Definition: IEntity.h:94
bool operator==(const IEntity &rhs) const
Definition: IEntity.h:79
T * AddComponent(P &&... param)
Definition: IEntity.h:61
EntityId GetEntityID() const
Definition: IEntity.h:87
virtual EntityTypeId GetStaticEntityTypeID() const =0
Handle32 Handle64
Definition: Handle.h:102
TypeID EntityTypeId
Definition: IEntity.h:15
size_t TypeID
Definition: Platform.h:67
T * AddComponent(ComponentManager *mgr, const EntityId id, P &&... param)
T * GetComponent(ComponentManager *mgr, const EntityId id)
static const EntityId INVALID_ENTITY_ID
Definition: IEntity.h:19
void RemoveComponent(ComponentManager *mgr, const EntityId id)
static constexpr value_type INVALID_HANDLE
Definition: Handle.h:54