summary refs log tree commit diff stats
path: root/src/entity_manager.cpp
blob: 0aaaf8ec130e3d16cf2410802686ffbf7414bb33 (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
#ifndef ENTITY_MANAGER_CPP_42D78C22
#define ENTITY_MANAGER_CPP_42D78C22

#include "entity_manager.h"

template <>
std::set<EntityManager::id_type> EntityManager::getEntitiesWithComponents<>(
  std::set<std::type_index>& componentTypes) const
{
  if (cachedComponents.count(componentTypes) == 1)
  {
    return cachedComponents[componentTypes];
  }

  std::set<id_type>& cache = cachedComponents[componentTypes];
  for (id_type entity = 0; entity < entities.size(); entity++)
  {
    const EntityData& data = entities[entity];
    bool cacheEntity = true;

    for (auto& componentType : componentTypes)
    {
      if (data.components.count(componentType) == 0)
      {
        cacheEntity = false;
        break;
      }
    }

    if (cacheEntity)
    {
      cache.insert(entity);
    }
  }

  return cache;
}

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