GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Entity.hpp
Go to the documentation of this file.
1 #ifndef RADIX_ENTITY_HPP
2 #define RADIX_ENTITY_HPP
3 
4 #include <array>
5 #include <memory>
6 #include <type_traits>
7 
10 #include <radix/env/Util.hpp>
11 
12 namespace radix {
13 
14 class EntityManager;
15 
16 using EntityId = uint32_t;
17 
23 class Entity {
24 private:
25  friend class EntityManager;
26 
27  Entity(Entity&) = delete;
28  Entity& operator=(Entity&) = delete;
29  Entity(Entity&&) = delete;
30  Entity& operator=(Entity&&) = delete;
31 
34 
38  std::string name;
39 
40 public:
41  struct ComponentAddedEvent : public Event {
42  static constexpr StaticEventTypeName TypeName = "radix/Entity:ComponentAdded";
43  const EventTypeName getTypeName() const {
44  return TypeName;
45  }
46  static constexpr StaticEventType Type = TypeNameHash(TypeName);
47  const EventType getType() const {
48  return Type;
49  }
50 
54  entity(e), component(c) {}
55  };
56  struct ComponentRemovedEvent : public Event {
57  static constexpr StaticEventTypeName TypeName = "radix/Entity:ComponentRemoved";
58  const EventTypeName getTypeName() const {
59  return TypeName;
60  }
61  static constexpr StaticEventType Type = TypeNameHash(TypeName);
62  const EventType getType() const {
63  return Type;
64  }
65 
69  entity(e), component(c) {}
70  };
71  struct NameChangedEvent : public Event {
72  static constexpr StaticEventTypeName TypeName = "radix/Entity:NameChanged";
73  const EventTypeName getTypeName() const {
74  return TypeName;
75  }
76  static constexpr StaticEventType Type = TypeNameHash(TypeName);
77  const EventType getType() const {
78  return Type;
79  }
80 
82  const std::string oldName;
83  NameChangedEvent(Entity &e, const std::string &o) :
84  entity(e), oldName(o) {}
85  };
86 
87 
89  Entity(EntityManager &manager, EntityId id) :
90  manager(manager), id(id) {
91  }
92 
93  ~Entity() {
95  }
96 
97 
104 
105  inline bool operator==(const Entity &o) const {
106  return id == o.id;
107  }
108  inline bool operator!=(const Entity &o) const {
109  return id != o.id;
110  }
111 
112 
113  std::string getName() const {
114  return name;
115  }
116  void setName(const std::string&);
117 
118 
119  std::array<std::unique_ptr<Component>, Component::MaxId> components;
120 
121  template<typename T, typename... TArgs>
122  T& addComponent(TArgs&&... mArgs) {
123  static_assert(std::is_base_of<Component, T>::value, "T must be a Component");
124  if (hasComponent<T>()) {
125  Util::Log(Warning) << "Overwriting a " << typeid(T).name() << " component";
126  }
127  T* result(new T(*this, std::forward<TArgs>(mArgs)...));
128  Component::TypeId id = Component::getTypeId<T>();
129  addComponent(id, result);
130  return *result;
131  }
132 
133  template<typename T>
134  inline bool hasComponent() const {
135  static_assert(std::is_base_of<Component, T>::value, "T must be a Component");
136  return components[Component::getTypeId<T>()].get() != nullptr;
137  }
138 
139  template<typename T>
140  inline T& getComponent() const {
141  static_assert(std::is_base_of<Component, T>::value, "T must be a Component");
142  return *reinterpret_cast<T*>(components[Component::getTypeId<T>()].get());
143  }
144 
145  template<class T>
147  removeComponent(Component::getTypeId<T>());
148  }
149 
150  void clearComponents();
151 };
152 
153 } /* namespace radix */
154 
155 #endif /* RADIX_ENTITY_HPP */
std::string name
Entity's name.
Definition: Entity.hpp:38
EntityId id
Entity (instance) identifier.
Definition: Entity.hpp:103
std::size_t ComponentTypeId
Definition: Component.hpp:11
Definition: GameController.hpp:7
Entity & entity
Definition: Entity.hpp:51
Definition: Entity.hpp:41
Definition: Logger.hpp:12
Definition: Event.hpp:15
const std::string oldName
Definition: Entity.hpp:82
Entity(Entity &)=delete
void clearComponents()
Definition: Entity.cpp:27
static constexpr StaticEventType Type
Definition: Entity.hpp:76
static _Log Log
Definition: Util.hpp:25
bool hasComponent() const
Definition: Entity.hpp:134
ECS entity, Component container It is created like this:
Definition: Entity.hpp:23
Component & component
Definition: Entity.hpp:52
T & getComponent() const
Definition: Entity.hpp:140
Entity(EntityManager &manager, EntityId id)
Definition: Entity.hpp:89
Definition: Entity.hpp:71
std::array< std::unique_ptr< Component >, Component::MaxId > components
Definition: Entity.hpp:119
static constexpr StaticEventTypeName TypeName
Definition: Entity.hpp:57
void addComponent(ComponentTypeId, Component *)
Definition: Entity.cpp:17
const EventTypeName getTypeName() const
Definition: Entity.hpp:43
const uint32_t StaticEventType
Definition: Event.hpp:10
ComponentRemovedEvent(Entity &e, Component &c)
Definition: Entity.hpp:68
Base class to create entity components.
Definition: Component.hpp:25
T & addComponent(TArgs &&...mArgs)
Definition: Entity.hpp:122
std::string EventTypeName
Definition: Event.hpp:13
const EventType getType() const
Definition: Entity.hpp:62
static constexpr StaticEventTypeName TypeName
Definition: Entity.hpp:72
Definition: Entity.hpp:56
uint32_t EntityId
Definition: Entity.hpp:16
ComponentAddedEvent(Entity &e, Component &c)
Definition: Entity.hpp:53
Manager and container of entities.
Definition: EntityManager.hpp:16
Component & component
Definition: Entity.hpp:67
static constexpr StaticEventType Type
Definition: Entity.hpp:61
static constexpr StaticEventTypeName TypeName
Definition: Entity.hpp:42
static constexpr TypeId MaxId
Definition: Component.hpp:36
ComponentTypeId TypeId
Definition: Component.hpp:35
bool operator!=(const Entity &o) const
Definition: Entity.hpp:108
const EventType getType() const
Definition: Entity.hpp:77
void removeComponent()
Definition: Entity.hpp:146
const EventTypeName getTypeName() const
Definition: Entity.hpp:58
static constexpr EventType TypeNameHash(StaticEventTypeName etn)
Definition: Event.hpp:16
EntityManager & manager
Definition: Entity.hpp:88
bool operator==(const Entity &o) const
Definition: Entity.hpp:105
~Entity()
Definition: Entity.hpp:93
void setName(const std::string &)
Definition: Entity.cpp:8
NameChangedEvent(Entity &e, const std::string &o)
Definition: Entity.hpp:83
const EventTypeName getTypeName() const
Definition: Entity.hpp:73
uint32_t EventType
Definition: Event.hpp:11
const char *const StaticEventTypeName
Definition: Event.hpp:12
const EventType getType() const
Definition: Entity.hpp:47
Entity & operator=(Entity &)=delete
std::string getName() const
Definition: Entity.hpp:113
Entity & entity
Definition: Entity.hpp:66
static constexpr StaticEventType Type
Definition: Entity.hpp:46
Entity & entity
Definition: Entity.hpp:81