summary refs log tree commit diff stats
path: root/src/entity_manager.h
blob: a36d720df08593baaae691225f5b7c78144b05b8 (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
#ifndef ENTITY_MANAGER_H_C5832F11
#define ENTITY_MANAGER_H_C5832F11

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

class EntityManager {
  private:
    struct EntityData {
      std::map<std::type_index, std::unique_ptr<Component>> components;
    };

    std::map<int, EntityData> entities;
    std::map<std::set<std::type_index>, std::set<int>> cachedComponents;

    int nextEntityID = 0;

    template <class T, class... R>
    std::set<int> getEntitiesWithComponentsHelper(std::set<std::type_index>& componentTypes)
    {
      componentTypes.insert(typeid(T));

      return getEntitiesWithComponents<R...>(componentTypes);
    }

    template <class... R>
    std::set<int> getEntitiesWithComponents(std::set<std::type_index>& componentTypes)
    {
      return getEntitiesWithComponentsHelper<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);

      // Uncache components
      for (auto& cache : cachedComponents)
      {
        cache.second.erase(entity);
      }

      // Destroy the data
      entities.erase(entity);
    }

    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
      erase_if(cachedComponents, [&componentType] (std::pair<const std::set<std::type_index>, std::set<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 *((T*)data.components[componentType].get());
    }

    template <class... R>
    std::set<int> getEntitiesWithComponents()
    {
      std::set<std::type_index> componentTypes;

      return getEntitiesWithComponentsHelper<R...>(componentTypes);
    }
};

template <>
std::set<int> EntityManager::getEntitiesWithComponents<>(std::set<std::type_index>& componentTypes);

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