summary refs log tree commit diff stats
path: root/src/entity_manager.h
blob: 74e758c8c7ed95be6b2ccd4e0e1c045d5e6dc8ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifndef ENTITY_MANAGER_H_C5832F11
#define ENTITY_MANAGER_H_C5832F11

#include <map>
#include <typeindex>
#include <set>
#include <cassert>
#include "component.h"

class EntityManager {
  private:
    struct EntityData {
      int parent = -1;
      std::map<std::type_index, std::unique_ptr<Component>> components;
    };
  
    std::map<int, EntityData> entities;
    std::map<int, std::set<int>> cachedChildren;
    std::map<std::set<std::type_index>, std::set<int>> cachedComponents;
  
    int nextEntityID = 0;
    
    bool ensureNoParentCycles(int entity, int parent)
    {
      EntityData& data = entities[parent];
      if (data.parent == entity)
      {
        return false;
      } else if (data.parent == -1)
      {
        return true;
      }
      
      return ensureNoParentCycles(entity, data.parent);
    }
    
    std::set<int> getEntitiesWithComponents(std::set<std::type_index>& componentTypes)
    {
      if (cachedComponents.count(componentTypes) == 1)
      {
        return cachedComponents[componentTypes];
      }
      
      std::set<int>& cache = cachedComponents[componentTypes];
      for (auto& entity : entities)
      {
        EntityData& data = entity.second;
        bool cacheEntity = true;
        
        for (auto& componentType : componentTypes)
        {
          if (data.components.count(componentType) == 0)
          {
            cacheEntity = false;
            break;
          }
        }
        
        if (cacheEntity)
        {
          cache.insert(entity.first);
        }
      }
      
      return cache;
    }
    
    template <class T, class... R> std::set<int> getEntitiesWithComponents(std::set<std::type_index>& componentTypes)
    {
      componentTypes.insert(typeid(T));
      
      return getEntitiesWithComponents<R...>(componentTypes);
    }
    
  public:
    EntityManager() = default;
    EntityManager(const EntityManager& copy) = delete;
    
    int emplaceEntity()
    {
      // Find a suitable entity ID
      while ((entities.count(nextEntityID) == 1) && (nextEntityID >= 0))
      {
        nextEntityID++;
      }
        
      if (nextEntityID < 0)
      {
        nextEntityID = 0;
        
        while ((entities.count(nextEntityID) == 1) && (nextEntityID >= 0))
        {
          nextEntityID++;
        }
        
        assert(nextEntityID >= 0);
      }
      
      // Initialize the data
      int id = nextEntityID++;
      entities[id];
      
      return id;
    }
    
    void deleteEntity(int entity)
    {
      assert(entities.count(entity) == 1);
      
      EntityData& data = entities[entity];
      
      // Destroy the children
      std::set<int> children = getChildren(entity);
      for (int child : children)
      {
        EntityData& childData = entities[child];
        childData.parent = -1;
        
        deleteEntity(child);
      }
      
      // Uncache children
      cachedChildren.erase(entity);
      
      if ((data.parent != -1) && (cachedChildren.count(data.parent) == 1))
      {
        cachedChildren[data.parent].erase(entity);
      }
      
      // Uncache components
      for (auto& cache : cachedComponents)
      {
        cache.second.erase(entity);
      }
      
      // Destroy the data
      entities.erase(entity);
    }
    
    std::set<int> getChildren(int parent)
    {
      assert(entities.count(parent) == 1);
      
      if (cachedChildren.count(parent) == 1)
      {
        return cachedChildren[parent];
      }
      
      std::set<int>& cache = cachedChildren[parent];
      for (auto& entity : entities)
      {
        EntityData& data = entity.second;
        if (data.parent == parent)
        {
          cache.insert(entity.first);
        }
      }
      
      return cache;
    }
    
    void setParent(int entity, int parent)
    {
      assert(entities.count(entity) == 1);
      assert(entities.count(parent) == 1);
      assert(ensureNoParentCycles(entity, parent));
      
      EntityData& data = entities[entity];
      
      // Remove from old parent
      if (data.parent != -1)
      {
        if (cachedChildren.count(data.parent) == 1)
        {
          cachedChildren[data.parent].erase(entity);
        }
      }
      
      data.parent = parent;
      
      // Cache new parent
      if (cachedChildren.count(parent) == 1)
      {
        cachedChildren[parent].insert(entity);
      }
    }
    
    void setNoParent(int entity)
    {
      assert(entities.count(entity) == 1);
      
      EntityData& data = entities[entity];
      
      // Remove from old parent
      if (data.parent != -1)
      {
        if (cachedChildren.count(data.parent) == 1)
        {
          cachedChildren[data.parent].erase(entity);
        }
      }
      
      data.parent = -1;
    }
    
    int getParent(int entity)
    {
      assert(entities.count(entity) == 1);
      
      EntityData& data = entities[entity];
      return data.parent;
    }
    
    template <class T, class... Args> T& emplaceComponent(int entity, Args&&... args)
    {
      assert(entities.count(entity) == 1);
      
      EntityData& data = entities[entity];
      std::type_index componentType = typeid(T);
      
      assert(data.components.count(componentType) == 0);
      
      // Initialize the component
      std::unique_ptr<T> ptr = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
      T& component = *ptr;
      data.components[componentType] = std::move(ptr);
      
      // Invalidate related caches
      std::remove_if(begin(cachedComponents), end(cachedComponents), [&] (std::pair<std::set<std::type_index>, int>& cache) {
        return cache.first.count(componentType) == 1;
      });
      
      return component;
    }
    
    template <class T> void removeComponent(int entity)
    {
      assert(entities.count(entity) == 1);
      
      EntityData& data = entities[entity];
      std::type_index componentType = typeid(T);
      
      assert(data.components.count(componentType) == 1);
      
      // Destroy the component
      data.components.erase(componentType);
      
      // Uncache the component
      for (auto& cache : cachedComponents)
      {
        if (cache.first.count(componentType) == 1)
        {
          cache.second.erase(entity);
        }
      }
    }
    
    template <class T> T& getComponent(int entity)
    {
      assert(entities.count(entity) == 1);
      
      EntityData& data = entities[entity];
      std::type_index componentType = typeid(T);
      
      assert(data.components.count(componentType) == 1);
      
      return *(data.components[componentType]);
    }
    
    template <class T, class... R> std::set<int> getEntitiesWithComponents()
    {
      std::set<std::type_index> componentTypes;
      componentTypes.insert(typeid(T));
      
      return getEntitiesWithComponents<R...>(componentTypes);
    }
};

#endif /* end of include guard: ENTITY_MANAGER_H_C5832F11 */