6 #include <lazarus/ECS/Entity.h>
7 #include <lazarus/ECS/EventListener.h>
8 #include <lazarus/ECS/Updateable.h>
57 template <
typename... Types>
70 template <
typename... Types>
72 typename std::common_type<std::function<
void(
Entity*, Types*...)>>::type&& func,
73 bool includeDeleted=
false);
84 template <
typename EventType>
90 template <
typename EventType>
96 template <
typename EventType>
97 void emit(
const EventType& event);
117 void garbageCollect();
120 std::unordered_map<Identifier, std::shared_ptr<Entity>> entities;
121 std::vector<Updateable*> updateables;
123 std::unordered_map<std::type_index,
124 std::vector<__lz::BaseEventListener*>> subscribers;
127 template <
typename... Types>
130 std::vector<Entity*> result;
133 result.push_back(ent);
139 template <
typename... Types>
141 typename std::common_type<std::function<
void(
Entity*, Types*...)>>::type&& func,
144 for (
auto it = entities.begin(); it != entities.end(); ++it)
148 if (!includeDeleted && entity->
isDeleted())
151 if (entity->
has<Types...>())
152 func(entity, entity->
get<Types>()...);
156 template <
typename EventType>
159 std::type_index typeId = __lz::getTypeIndex<EventType>();
160 auto found = subscribers.find(typeId);
161 if (found == subscribers.end())
164 std::vector<__lz::BaseEventListener*> vec;
165 vec.push_back(eventListener);
166 subscribers[typeId] = vec;
171 found->second.push_back(eventListener);
175 template <
typename EventType>
178 auto found = subscribers.find(__lz::getTypeIndex<EventType>());
179 if (found != subscribers.end())
181 auto eventListeners = found->second;
182 for (
auto it = eventListeners.begin(); it != eventListeners.end(); ++it)
184 if (*it == eventListener)
187 eventListeners.erase(it);
193 std::stringstream msg;
194 msg <<
"ECS engine was not subscribed to the event ";
195 msg <<
typeid(EventType).name();
199 template <
typename EventType>
203 auto found = subscribers.find(__lz::getTypeIndex<EventType>());
204 if (found != subscribers.end())
206 auto eventListeners = found->second;
207 for (
auto it = eventListeners.begin(); it != eventListeners.end(); ++it)
210 listener->
receive(*
this, event);
virtual void update()
Updates all the updateable objects in the engine.
Definition: ECSEngine.cpp:32
void applyToEach(typename std::common_type< std::function< void(Entity *, Types *...)>>::type &&func, bool includeDeleted=false)
Applies a function to each of the entities from the collection that have the specified component type...
Definition: ECSEngine.h:140
virtual void receive(ECSEngine &engine, const EventType &event)=0
Called when the EventListener receives an event from the ECS engine.
void registerUpdateable(Updateable *updateable)
Adds an updateable object to the engine.
Definition: ECSEngine.cpp:27
Entity * addEntity()
Adds a new entity to the collection and returns a pointer to it.
Definition: ECSEngine.cpp:5
std::vector< Entity * > entitiesWithComponents(bool includeDeleted=false)
Returns a vector with the entities that have the specified components.
Definition: ECSEngine.h:128
void unsubscribe(EventListener< EventType > *eventListener)
Unsubscribes the event listener from the list of listeners of that event type.
Definition: ECSEngine.h:176
Entity * getEntity(Identifier entityId)
Gets a pointer to the entity from the collection with the given ID, or a nullptr if an entity with su...
Definition: ECSEngine.cpp:19
Interface for objects that react to events of a certain type.
Definition: EventListener.h:32
bool isDeleted() const
Returns whether this entity is marked for deletion upon the next pass of the garbage collector...
Definition: Entity.h:111
Component * get()
Returns a pointer to the entity's component of the specified type.
Definition: Entity.h:189
Interface for objects that can be updated by the ECS engine when it ticks.
Definition: Updateable.h:15
bool has() const
Returns whether the entity has a component of type T.
Definition: Entity.h:144
An Entity is a collection of components with a unique ID.
Definition: Entity.h:50
void subscribe(EventListener< EventType > *eventListener)
Subscribes the event listener to the list of listeners of that event type.
Definition: ECSEngine.h:157
Main driver to work with entities, components and systems.
Definition: ECSEngine.h:27
void emit(const EventType &event)
Emit an event to all listeners of that type of event.
Definition: ECSEngine.h:200