R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Game.cpp
Go to the documentation of this file.
1#include "Game.hpp"
2#include "Registry.hpp"
3#include "EventDispatcher.hpp"
5#include "Queue.hpp"
6
8{
9 _dispatcher = new EventDispatcher();
10 _queue = new Queue();
11 _dispatcher->subscribe([this](const GameMessage& event) {
12 this->onGameEventReceived(event);
13 });
14 _game = new Registry(_dispatcher, _queue);
15}
16
18{
19 delete _game;
20 delete _dispatcher;
21 delete _queue;
22}
23
24void Game::update(float _deltaTime)
25{
26 Time::deltaTime = _deltaTime;
27 _game->run_systems();
28}
29
30std::forward_list<std::string> Game::getGameEvents(void)
31{
32 std::forward_list<std::string> serializedMessages;
33 for (const auto& event : _eventsFromGame) {
34 std::ostringstream oss;
35 serialize(event, oss);
36 serializedMessages.push_front(oss.str());
37 }
38 _eventsFromGame.clear();
39 return serializedMessages;
40}
41
43{
44 _eventsFromGame.push_front(event);
45}
46
47std::forward_list<std::string> Game::getCurrentGameState(void)
48{
49 std::forward_list<std::string> serializedMessages;
50 for (const auto& event : _game->sync_game()) {
51 std::ostringstream oss;
52 serialize(event, oss);
53 serializedMessages.push_front(oss.str());
54 }
55 return serializedMessages;
56}
57
58void Game::onServerEventReceived(std::string &event)
59{
60 std::istringstream iss(event);
61 GameMessage loadedMessage = deserialize(iss);
62 _eventsFromServer.push_front(loadedMessage);
63 _queue->push(loadedMessage);
64}
GameMessage deserialize(std::istringstream &is)
Deserializes a GameMessage object from a binary stream.
void serialize(const GameMessage &message, std::ostringstream &os)
Serializes a GameMessage object into a binary stream.
A utility class for dispatching and handling game events.
void subscribe(Callback callback)
Subscribes a listener to game events.
std::forward_list< std::string > getGameEvents(void)
Retrieves game messages.
Definition Game.cpp:30
void onGameEventReceived(const GameMessage &event)
Handles an event received during gameplay.
Definition Game.cpp:42
void update(float _deltaTime)
Updates the game state for the current frame.
Definition Game.cpp:24
Game()
Constructs a new Game object.
Definition Game.cpp:7
std::forward_list< std::string > getCurrentGameState(void)
Gets the current game state (syncing)
Definition Game.cpp:47
~Game()
Destroys the Game object.
Definition Game.cpp:17
void onServerEventReceived(std::string &event)
Handles an event received from server.
Definition Game.cpp:58
A thread-safe queue for storing and managing GameMessage objects.
Definition Queue.hpp:17
void push(const GameMessage &item)
Push a GameMessage item into the queue.
Definition Queue.cpp:3
Manages entities and their associated components, enabling the creation, deletion,...
Definition Registry.hpp:23
void run_systems()
Executes all registered systems.
Definition Registry.cpp:11
std::vector< GameMessage > sync_game()
Creates a list of GameMessages that syncs the player to the game.
Definition Registry.cpp:18
float deltaTime
The time difference between the current frame and the last frame.
Definition Time.cpp:4
A structure representing a game message.