Thrive
Classes | Public Types | Public Member Functions | List of all members
thrive::EntityFilter< ComponentTypes > Class Template Reference

Filters for entities that contain specific components. More...

#include <entity_filter.h>

Classes

struct  Implementation
 

Public Types

using ComponentGroup = std::tuple< typename detail::ExtractComponentType< ComponentTypes >::PointerType... >
 Typedef for the filter's component tuple. More...
 
using EntityMap = std::unordered_map< EntityId, ComponentGroup >
 Typedef for the filter's list of relevant entities.
 

Public Member Functions

 EntityFilter (bool recordChanges=false)
 Constructor. More...
 
 ~EntityFilter ()
 Destructor.
 
EntityMap & addedEntities ()
 Returns the entities added to this filter. More...
 
EntityMap::const_iterator begin () const
 Iterator. More...
 
void clearChanges ()
 Clears the lists for added and removed entities.
 
bool containsEntity (EntityId id) const
 Checks whether an entity id is contained in this filter. More...
 
EntityMap::const_iterator end () const
 Iterator. More...
 
const EntityMap & entities () const
 The relevant entities. More...
 
std::unordered_set< EntityId > & removedEntities ()
 Returns the entities removed from this filter. More...
 
EntityManager * entityManager ()
 Gets the current entiyManager. More...
 
void setEntityManager (EntityManager *entityManager)
 Sets the entity manager this filter applies to. More...
 

Detailed Description

template<typename... ComponentTypes>
class thrive::EntityFilter< ComponentTypes >

Filters for entities that contain specific components.

An entity filter helps a system in finding the entities that have exactly the right components to be relevant for the system.

Template Parameters
ComponentTypesThe component classes to watch for. You can wrap a class with the Optional template if you want to know if it's there, but it's not required.

Usage example:

class MySystem : public System {
private:
MyComponent,
Optional<SomeOtherComponent>
>
m_entities;
public:
void init(Engine* engine) override {
System::initNamed("MySystem", "engine);
m_entities.setEntityFilter(&engine->entityManager());
}
void update(int milliseconds) override {
for (auto& value : m_entities) {
EntityId entity = value.first;
MyComponent* myComponent = std::get<0>(value.second);
// Do something with myComponent
SomeOtherComponent* someOtherComponent = std::get<1>(value.second);
if (someOtherComponent) {
// Do something with someOtherComponent
}
}
}
void shutdown() overrde {
m_entities.setEntityManager(nullptr);
System::shutdown();
}
};

Member Typedef Documentation

template<typename... ComponentTypes>
using thrive::EntityFilter< ComponentTypes >::ComponentGroup = std::tuple< typename detail::ExtractComponentType<ComponentTypes>::PointerType... >

Typedef for the filter's component tuple.

All elements of the tuple are pointers. An optional component may be nullptr if not present.

Constructor & Destructor Documentation

template<typename... ComponentTypes>
thrive::EntityFilter< ComponentTypes >::EntityFilter ( bool  recordChanges = false)

Constructor.

Parameters
recordChangesIf true, you can query the added and removed entities through this filter.
Warning
If recordChanges is true, you are responsible for clearing the collections returned by addedEntities() and removedEntities(). If you don't clear them regularly, it's a memory leak. You can use EntityFilter::clearChanges() to clear both collections.

Member Function Documentation

template<typename... ComponentTypes>
EntityFilter< ComponentTypes... >::EntityMap & thrive::EntityFilter< ComponentTypes >::addedEntities ( )

Returns the entities added to this filter.

When you have processed the collection, please call clear() on it.

template<typename... ComponentTypes>
EntityFilter< ComponentTypes... >::EntityMap::const_iterator thrive::EntityFilter< ComponentTypes >::begin ( ) const

Iterator.

Equivalent to

entities().cbegin()
Returns
An iterator to the first relevant entity
template<typename... ComponentTypes>
bool thrive::EntityFilter< ComponentTypes >::containsEntity ( EntityId  id) const

Checks whether an entity id is contained in this filter.

Parameters
idThe entity to check for
Returns
true if this id can be found in this filter, false otherwise
template<typename... ComponentTypes>
EntityFilter< ComponentTypes... >::EntityMap::const_iterator thrive::EntityFilter< ComponentTypes >::end ( ) const

Iterator.

Equivalent to

entities().cend()
Returns
An iterator to the end of the relevant entities
template<typename... ComponentTypes>
const EntityFilter< ComponentTypes... >::EntityMap & thrive::EntityFilter< ComponentTypes >::entities ( ) const

The relevant entities.

The returned collection maps the entity id to the components required by this filter. Optional components may be nullptr.

template<typename... ComponentTypes>
EntityManager * thrive::EntityFilter< ComponentTypes >::entityManager ( )

Gets the current entiyManager.

Returns
template<typename... ComponentTypes>
std::unordered_set< EntityId > & thrive::EntityFilter< ComponentTypes >::removedEntities ( )

Returns the entities removed from this filter.

When you have processed the collection, please call clear() on it.

template<typename... ComponentTypes>
void thrive::EntityFilter< ComponentTypes >::setEntityManager ( EntityManager *  entityManager)

Sets the entity manager this filter applies to.

Parameters
entityManagerThe new entity manager to listen to. If nullptr, the filter stays empty.

The documentation for this class was generated from the following files: