Thrive
entity_filter.h
1 #pragma once
2 
3 #include "engine/entity_manager.h"
4 #include "engine/component_collection.h"
5 
6 #include <assert.h>
7 #include <forward_list>
8 #include <functional>
9 #include <tuple>
10 #include <unordered_map>
11 
12 #include <iostream>
13 
14 namespace thrive {
15 
21 template<typename ComponentType>
22 struct Optional {};
23 
24 namespace detail {
25 
26 template<typename ComponentType>
27 struct ExtractComponentType {
28 
29  using PointerType = ComponentType*;
30 
31  using Type = ComponentType;
32 
33 };
34 
35 template<typename ComponentType>
36 struct ExtractComponentType<Optional<ComponentType>> {
37 
38  using PointerType = ComponentType*;
39 
40  using Type = ComponentType;
41 
42 };
43 
44 } // namespace detail
45 
46 
96 template<typename... ComponentTypes>
97 class EntityFilter {
98 
99 public:
100 
107  using ComponentGroup = std::tuple<
108  typename detail::ExtractComponentType<ComponentTypes>::PointerType...
109  >;
110 
114  using EntityMap = std::unordered_map<EntityId, ComponentGroup>;
115 
129  EntityFilter(
130  bool recordChanges = false
131  );
132 
137 
145  EntityMap&
146  addedEntities();
147 
158  typename EntityMap::const_iterator
159  begin() const;
160 
164  void
165  clearChanges();
166 
176  bool
178  EntityId id
179  ) const;
180 
191  typename EntityMap::const_iterator
192  end() const;
193 
201  const EntityMap&
202  entities() const;
203 
211  std::unordered_set<EntityId>&
212  removedEntities();
213 
220  entityManager();
221 
229  void
232  );
233 
234 private:
235 
236  struct Implementation;
237  std::unique_ptr<Implementation> m_impl;
238 };
239 
240 template<>
241 class EntityFilter<> {
242 };
243 
244 }
245 
246 #include "engine/entity_filter.cpp"
EntityMap::const_iterator begin() const
Iterator.
Definition: entity_filter.cpp:248
EntityFilter(bool recordChanges=false)
Constructor.
Definition: entity_filter.cpp:231
std::unordered_map< EntityId, ComponentGroup > EntityMap
Typedef for the filter's list of relevant entities.
Definition: entity_filter.h:114
Marker for optional components in a filter.
Definition: entity_filter.h:22
Manages entities and their components.
Definition: entity_manager.h:23
Filters for entities that contain specific components.
Definition: entity_filter.h:97
~EntityFilter()
Destructor.
Definition: entity_filter.h:136
void setEntityManager(EntityManager *entityManager)
Sets the entity manager this filter applies to.
Definition: entity_filter.cpp:299
Definition: bullet_ogre_conversion.h:6
EntityMap::const_iterator end() const
Iterator.
Definition: entity_filter.cpp:272
EntityMap & addedEntities()
Returns the entities added to this filter.
Definition: entity_filter.cpp:240
bool containsEntity(EntityId id) const
Checks whether an entity id is contained in this filter.
Definition: entity_filter.cpp:263
void clearChanges()
Clears the lists for added and removed entities.
Definition: entity_filter.cpp:255
const EntityMap & entities() const
The relevant entities.
Definition: entity_filter.cpp:279
std::tuple< typename detail::ExtractComponentType< ComponentTypes >::PointerType... > ComponentGroup
Typedef for the filter's component tuple.
Definition: entity_filter.h:109
std::unordered_set< EntityId > & removedEntities()
Returns the entities removed from this filter.
Definition: entity_filter.cpp:286
EntityManager * entityManager()
Gets the current entiyManager.
Definition: entity_filter.cpp:293