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

#include "entity_manager.h"

template <>
std::set<int> EntityManager::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;
}

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