R-Type
Event.hpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-Type
4 ** File description:
5 ** Event
6 */
7 
8 #ifndef EVENT_HPP_
9 #define EVENT_HPP_
10 #include <any>
11 #include <functional>
12 #include <unordered_map>
13 #include <vector>
14 #include <type_traits>
15 #include <algorithm>
16 #include <iterator>
17 #include <map>
18 #include <string>
19 
20 namespace GameEngine
21 {
23  using EventType = std::size_t;
25  struct NoEventData
26  {
27  };
28 
30  enum class Event : EventType {
33  QuitEvent,
34  PollEvent,
40  GetTexture,
43  GetEntity,
44  GetDestroy,
45  SendInput,
46  GetEndGame,
48  GetScore,
57  };
58 
59  static const std::map<EventType, std::string> InternalEventNames{
60  {static_cast<EventType>(Event::WindowIsOpen), "WindowIsOpen"},
61  {static_cast<EventType>(Event::WindowCloseEvent), "WindowCloseEvent"},
62  {static_cast<EventType>(Event::PollEvent), "PollEvent"},
63  {static_cast<EventType>(Event::GetWorldMousePos), "GetWorldMousePos"},
64  {static_cast<EventType>(Event::WindowSetView), "WindowSetView"},
65  {static_cast<EventType>(Event::SetFpsLimitEvent), "SetFpsLimitEvent"},
66  {static_cast<EventType>(Event::GetTransform), "GetTransform"},
67  {static_cast<EventType>(Event::GetCollision), "GetCollision"},
68  {static_cast<EventType>(Event::GetTexture), "GetTexture"},
69  {static_cast<EventType>(Event::GetControllable), "GetControllable"},
70  {static_cast<EventType>(Event::GetNewEntity), "GetNewEntity"},
71  {static_cast<EventType>(Event::GetEntity), "GetEntity"},
72  {static_cast<EventType>(Event::SendInput), "SendInput"},
73  {static_cast<EventType>(Event::DeleteEntity), "DeleteEntity"},
74  {static_cast<EventType>(Event::EnemiesSpawnedEvent), "EnemiesSpawnedEvent"},
75  {static_cast<EventType>(Event::EnemiesMoveEvent), "EnemiesMoveEvent"},
76  {static_cast<EventType>(Event::EnemiesDieEvent), "EnemiesDieEvent"},
77  {static_cast<EventType>(Event::PlayerMoveEvent), "PlayerMoveEvent"},
78  {static_cast<EventType>(Event::PlayerSpawnedEvent), "PlayerSpawnedEvent"},
79  {static_cast<EventType>(Event::PlayersDieEvent), "PlayersDieEvent"},
80  {static_cast<EventType>(Event::PlayerShootEvent), "PlayerShootEvent"},
81  };
82 #ifdef DEBUG
83  static const int DEFAULT_MAX_EVENT_LOG_LENGTH = 100000;
84 #endif
85 
88  template <typename EventData = NoEventData>
90  {
91  public:
92 #ifdef DEBUG
93  EventHandler(std::vector<EventType> &eventLog, const EventType eventType)
94  : _eventLog(eventLog), _eventType(eventType){};
95 #else
96  EventHandler(const EventType eventType) : _eventType(eventType){};
97 #endif
100  void publish(EventData eventData) { _publish(eventData); }
102  void publish() { _publish(NoEventData{}); }
105  template <typename Function>
106  void subscribe(const Function &function)
107  {
108  std::function<void(EventData)> callback = function;
109  _callbacks.push_back(function);
110  }
111 
112  private:
113 #ifdef DEBUG
114  std::vector<EventType> &_eventLog;
115 #endif
116  const EventType _eventType;
119  void _publish(EventData eventData)
120  {
121 #ifdef DEBUG
122  if (_eventLog.size() >= DEFAULT_MAX_EVENT_LOG_LENGTH)
123  _eventLog.clear();
124  _eventLog.push_back(_eventType);
125 #endif
126  for (std::function<void(EventData)> callback : _callbacks)
127  callback(eventData);
128  }
130  std::vector<std::function<void(EventData)>> _callbacks;
131  };
132 
135  {
136  public:
141  template <class EventData = NoEventData>
143  {
144 #ifdef DEBUG
145  _handlers.insert({eventType, EventHandler<EventData>(eventLog, eventType)});
146 #else
147  _handlers.insert({eventType, EventHandler<EventData>(eventType)});
148 #endif
149  return getHandler<EventData>(eventType);
150  }
153  void removeHandler(const EventType eventType) { _handlers.erase(eventType); }
158  template <class EventData = NoEventData>
159  void publish(const EventType eventType, EventData eventData)
160  {
161  getHandler<EventData>(eventType).publish(eventData);
162  }
167  template <class EventData = NoEventData>
169  {
170  return std::any_cast<EventHandler<EventData> &>(_handlers[eventType]);
171  }
172 
173 #ifdef DEBUG
174  std::vector<EventType> eventLog;
175 #endif
176 
177  private:
179  std::unordered_map<EventType, std::any> _handlers;
180  };
181 
182 } // namespace GameEngine
183 
184 #endif /* !EVENT_HPP_ */
class that store all the callback functions for a specific event type
Definition: Event.hpp:90
void publish(EventData eventData)
call all the subscribed functions with the given eventData
Definition: Event.hpp:100
void publish()
call all the subscribed functions with no data (use when EventData == NoEventData)
Definition: Event.hpp:102
EventHandler(const EventType eventType)
Definition: Event.hpp:96
void subscribe(const Function &function)
register the given function to be called when the event is published
Definition: Event.hpp:106
class to help create and manage all the event handlers
Definition: Event.hpp:135
EventHandler< EventData > & addHandler(EventType eventType)
create a new event handler for the given event type
Definition: Event.hpp:142
void removeHandler(const EventType eventType)
remove an event handler
Definition: Event.hpp:153
void publish(const EventType eventType, EventData eventData)
call all the subscribed functions with the given eventData
Definition: Event.hpp:159
EventHandler< EventData > & getHandler(const EventType eventType)
return an event handler by its type
Definition: Event.hpp:168
Definition: AssetManager.hpp:15
std::size_t EventType
value to describe the event type
Definition: Event.hpp:23
Event
enum of all event types
Definition: Event.hpp:30
empty struct to describe an event that dosent provide data to subscribed functions
Definition: Event.hpp:26