R-Type  2
Doom but in better
Loading...
Searching...
No Matches
game_manager.hpp
Go to the documentation of this file.
1#pragma once
2#include <unordered_map>
3#include <vector>
4#include <cstdint>
5#include <functional>
6#include "Game.hpp"
7#include "message.hpp"
8#include "memory"
9
15 uint32_t gameId;
16 std::unique_ptr<Game> game;
17 std::vector<uint32_t> clients;
18};
19
28public:
29 using BroadcastFunc = std::function<void(uint32_t, const Message&)>;
30
35 explicit GameManager(BroadcastFunc broadcastFunc);
36
40 std::forward_list<std::string> syncClientToGame(uint32_t clientId);
41
45 uint32_t assignClientToGame(uint32_t clientId);
46
50 void removeClientFromGame(uint32_t clientId);
51
55 uint32_t getGameIdForClient(uint32_t clientId) const;
56
60 void updateAllGames(float dt);
61
65 void handleGameMessage(uint32_t gameId, uint32_t clientId, const Message& msg);
66
67private:
71 uint32_t findOrCreateGame();
72
73 // The broadcast function to call: broadcastFunc_(clientId, msg).
74 BroadcastFunc broadcastFunc_;
75
76 std::unordered_map<uint32_t, GameInstance> games_;
77 std::unordered_map<uint32_t, uint32_t> clientToGame_;
78 uint32_t nextGameId_ = 1;
79
80 static const size_t MAX_PLAYERS_PER_GAME = 4; // Example capacity
81};
Manages multiple Game instances and routes messages/events.
uint32_t assignClientToGame(uint32_t clientId)
Assign a client to a new or existing game.
uint32_t getGameIdForClient(uint32_t clientId) const
Get the game ID for a client, or 0 if none.
void updateAllGames(float dt)
Update all games with the given deltaTime and broadcast events to clients.
std::forward_list< std::string > syncClientToGame(uint32_t clientId)
Gets the current game state for syncing.
void removeClientFromGame(uint32_t clientId)
Remove a client from whichever game they are in.
GameManager(BroadcastFunc broadcastFunc)
Constructor that takes a broadcast function (to send packets to clients).
std::function< void(uint32_t, const Message &)> BroadcastFunc
void handleGameMessage(uint32_t gameId, uint32_t clientId, const Message &msg)
Handle a message from a client in a specific game.
A single game session: a Game object plus the connected clients.
uint32_t gameId
std::unique_ptr< Game > game
std::vector< uint32_t > clients
Minimal network message with a 4-byte header (type + length) and a payload.
Definition message.hpp:9