R-Type  2
Doom but in better
Loading...
Searching...
No Matches
EventSystem.cpp
Go to the documentation of this file.
1#include <sstream>
2#include "EventSystem.hpp"
3#include "SpawnSystem.hpp"
4#include "MovementSystem.hpp"
5#include "WeaponSystem.hpp"
7
9{
10 while (true) {
11 if (r.queue->isEmpty()) {
12 break;
13 }
14
15 auto optionalEvent = r.queue->pop();
16 if (!optionalEvent.has_value()) {
17 continue;
18 }
19
20 const GameMessage& event = optionalEvent.value();
21
22 if (validateAction(event, r)) {
23 performAction(event, r);
24 } else {
25 std::cerr << "Invalid action received: " << event.type << std::endl;
26 }
27 }
28}
29
31{
32 return true;
33}
34
35bool performAction(const GameMessage& event, Registry& r)
36{
37 std::size_t id;
38 switch (event.type)
39 {
40 case P_CONNECT:
41 spawn_player(r, 120.f, 540.f, event.msg.cli_id, event.msg.username);
42 break;
43 case P_DISCONNECT:
44 id = getIdByClientId(r, event.msg.cli_id);
45 r.kill_entity(Entity(id));
46 r.dispatcher->notify({P_KILL, id, { 0, 0, 0, "", {0, 0} }});
47 break;
48 case P_MOVE:
49 move_player(r, event.id, event.msg.coords.x, event.msg.coords.y);
50 break;
51 case P_SHOOT:
52 make_shot(r, event.id);
53 break;
54 default:
55 return false;
56 }
57 return true;
58}
bool performAction(const GameMessage &event, Registry &r)
Performs the action associated with the event.
bool validateAction(const GameMessage &event, Registry &r)
Validates the action associated with the event.
void event_system(Registry &r)
Processes events from the queue and performs actions.
@ P_SHOOT
Represents a player shooting event.
@ P_CONNECT
Represents a connecting player event.
@ P_KILL
Represents a kill event.
@ P_MOVE
Represents a movement event.
@ P_DISCONNECT
Represents a disconnecting player event.
void move_player(Registry &r, std::size_t id, float x, float y)
Moves a player to a specified position.
std::size_t getIdByClientId(Registry &r, const unsigned int client_id)
Gets entity id by the player's client id.
void spawn_player(Registry &r, const float pos_x, const float pos_y, const uint32_t client_id, const std::string &username)
Spawns a player with the specified username at the given position.
Registry * r
Definition Tests.cpp:4
void make_shot(Registry &r, std::size_t id)
Changes the state of the weapon.
Represents an entity in an Entity-Component-System (ECS) architecture.
Definition Entity.hpp:12
void notify(const GameMessage &event)
Notifies the subscribed listener of an event.
bool isEmpty()
Check if the queue is empty.
Definition Queue.cpp:25
std::optional< GameMessage > pop()
Try to pop a GameMessage item from the queue.
Definition Queue.cpp:9
Manages entities and their associated components, enabling the creation, deletion,...
Definition Registry.hpp:23
void kill_entity(const Entity &entity)
Deletes an entity and removes all its components.
Definition Registry.cpp:35
EventDispatcher * dispatcher
Definition Registry.hpp:191
Queue * queue
Definition Registry.hpp:192
A structure representing a game message.
messageType type
The type of message (e.g., SPAWN, MOVE, etc.).
messageInfo msg
Additional information about the event (status, asset ID, and coordinates).
std::size_t id
The unique identifier for the entity or object involved in the event.
float x
The X coordinate.
float y
The Y coordinate.
uint32_t cli_id
Id of the player's client.
coord coords
Coordinates associated with the event, such as the position of an entity.
char username[9]
Username of player connecting.