Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
component_manager.hpp
1#pragma once
2
3#include <memory>
4#include <typeinfo>
5#include <unordered_map>
6#include <vector>
7
8#include "compact_list.hpp"
9#include "sparse_list.hpp"
10
11template <typename T, typename... Types>
13class Editor;
16class Entity {
17 public:
18 friend ComponentManager;
19 friend ComponentIterator;
20 private:
21 explicit Entity(const unsigned i) : id_(i) {}
22 explicit operator unsigned() { return id_; }
23 unsigned id_ = 0;
24};
25
26struct RenderTreeCmp;
27
30 public:
31 //friend Editor;
34 ~ComponentManager() = default;
40
57 template <typename... T>
58 Entity addEntity(Entity* parent, const std::string& editor_name,
59 const T&... args) {
60 Entity e = addEntityEmpty(parent, editor_name);
61 // Loops through the template arguments executing the lambda.
62 ([&] { setComponent(e, args); }(), ...);
63 return e;
64 }
65
70 void deleteEntity(Entity& entity);
71
81 template <typename T>
82 void addComponentClass(ComponentListType t = ComponentListType::kCompact) {
83 if (t == ComponentListType::kSparse) {
84 component_lists_.emplace(typeid(T).hash_code(),
85 std::make_unique<ComponentListSparse<T>>());
86 } else {
87 component_lists_.emplace(typeid(T).hash_code(),
88 std::make_unique<ComponentListCompact<T>>());
89 }
90 }
91
96 template <typename T>
97 void setComponent(const Entity& entity, const T& component) {
98 auto comp_base = component_lists_.find(typeid(T).hash_code());
99 if (comp_base->second.get()->type_ == ComponentListType::kSparse) {
100 ComponentListSparse<T>* component_vector =
101 static_cast<ComponentListSparse<T>*>(comp_base->second.get());
102 component_vector->components_[entity.id_ - 1].emplace(component);
103
104 } else {
105 ComponentListCompact<T>* component_vector =
106 static_cast<ComponentListCompact<T>*>(comp_base->second.get());
107 component_vector->setComponent(entity.id_, component);
108 }
109 }
110
116 template <typename T>
117 T* getComponent(Entity entity) const{
118 auto comp_base = component_lists_.find(typeid(T).hash_code());
119 if (comp_base->second.get()->type_ == ComponentListType::kSparse) {
120 ComponentListSparse<T>* component_vector =
121 static_cast<ComponentListSparse<T>*>(comp_base->second.get());
122 return &component_vector->components_[entity.id_ - 1].value();
123
124 } else {
125 ComponentListCompact<T>* component_vector =
126 static_cast<ComponentListCompact<T>*>(comp_base->second.get());
127 return component_vector->getComp(static_cast<unsigned>(entity));
128 }
129 }
130
136 template <typename T>
137 T* getComponent(unsigned entity) const{
138 auto comp_base = component_lists_.find(typeid(T).hash_code());
139 if (comp_base->second.get()->type_ == ComponentListType::kSparse) {
140 ComponentListSparse<T>* component_vector =
141 static_cast<ComponentListSparse<T>*>(comp_base->second.get());
142 if(component_vector->components_[entity - 1].has_value()) {
143 return &component_vector->components_[entity - 1].value();
144 }
145 return nullptr;
146
147 } else {
148 ComponentListCompact<T>* component_vector =
149 static_cast<ComponentListCompact<T>*>(comp_base->second.get());
150 return component_vector->getComp(entity);
151 }
152 }
153
160 template <typename T>
161 ComponentListCompact<T>& getComponentList() const{
162 auto comp_base = component_lists_.find(typeid(T).hash_code());
163 ComponentListCompact<T>* component_vector =
164 static_cast<ComponentListCompact<T>*>(comp_base->second.get());
165 return *component_vector;
166 }
167
177 template <typename T>
178 ComponentListSparse<T>& getSparseList() const{
179 auto comp_base = component_lists_.find(typeid(T).hash_code());
180 ComponentListSparse<T>* component_vector =
181 static_cast<ComponentListSparse<T>*>(comp_base->second.get());
182 return *component_vector;
183 }
184
185 private:
189 Entity addEntityEmpty(Entity* parent, const std::string& editor_name);
190
191 void innerDelete(unsigned i);
192
193 RenderTreeCmp* getRender(unsigned ent) const;
194
195 // map containint all component lists
196 std::unordered_map<std::size_t, std::unique_ptr<ComponentListBase>>
197 component_lists_;
198
199 // marks the positions where entities have been freed so that they can be
200 // filled with new components to avoid blanks in the compoent vectors
201 std::vector<unsigned> freed_entities_;
202
203 // biggest entity id or position in the array
204 unsigned current_entity_;
205};
Helper to iterate several component lists simultaneously.
Definition: lists_iterator.hpp:9
Handles anything related to entities and components in the engine.
Definition: component_manager.hpp:29
void setComponent(const Entity &entity, const T &component)
Adds a component to the specified entity.
Definition: component_manager.hpp:97
ComponentListCompact< T > & getComponentList() const
Retrieves the list of all the components of the same type.
Definition: component_manager.hpp:161
void addComponentClass(ComponentListType t=ComponentListType::kCompact)
Defines a list for a custom component and makes it ready to use with the engine.
Definition: component_manager.hpp:82
Entity addEntity(Entity *parent, const std::string &editor_name, const T &... args)
Creates an entity and assigns the specified components to it.
Definition: component_manager.hpp:58
ComponentManager & operator=(const ComponentManager &)=delete
Copy assignment operator (deleted).
ComponentListSparse< T > & getSparseList() const
Retrieves the list of all the components of the same type.
Definition: component_manager.hpp:178
void deleteEntity(Entity &entity)
Deletes an entity from the system and all its children.
T * getComponent(Entity entity) const
Retrieves a component for a specific entity.
Definition: component_manager.hpp:117
ComponentManager(const ComponentManager &)=delete
Copy constructor (deleted).
ComponentManager()
Default constructor.
Display and allows to modify entity information in real time.
Definition: editor.hpp:14
Identifies an entity. This class can only be modified by the engine.
Definition: component_manager.hpp:16
ComponentListType
type of container to be used for storing componets
Definition: component_lists.hpp:8