CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EventListener.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "Event.hpp"
13
15
17
18namespace CeresEngine {
19
22 template<CEntityEvent Event> class EntityEventListener {
23 static_assert(isEntityEvent<Event>, "Event must be a event type");
24
25 private:
28
33 virtual void when(Entity& entity, const Event& event) = 0;
34
35 protected:
36 virtual ~EntityEventListener() = default;
37
38 public:
42
44 void unsubscribe();
45 };
46
47} // namespace CeresEngine
48
49#include "EventManager.hpp"
50
51namespace CeresEngine {
52
54 mConnection = eventManager.subscribe<Event>([this](Entity entity, const Event& event) { when(entity, event); });
55 }
56
57 template<CEntityEvent Event> void EntityEventListener<Event>::unsubscribe() { mConnection.disconnect(); }
58
59} // namespace CeresEngine
A event listener that listens for events.
Definition EventListener.hpp:22
void subscribe(EntityEventManager &eventManager)
Subscribe the listener to the event in the given eventManager.
Definition EventListener.hpp:53
virtual ~EntityEventListener()=default
EventConnection mConnection
A vector of all active signal connections.
Definition EventListener.hpp:27
virtual void when(Entity &entity, const Event &event)=0
A callback called whenever the event Event gets emitted by a entity.
void unsubscribe()
Unsubscribes the listener.
Definition EventListener.hpp:57
Definition EventManager.hpp:24
The base entity class.
Definition Entity.hpp:41
Scoped connection class.
Definition Event.hpp:104
Base template for the event class.
Definition Event.hpp:27
Definition Application.hpp:19
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25