R-Type 0.1
Loading...
Searching...
No Matches
game.hpp
1#pragma once
2
3#include "EngineContext.hpp"
4#include "Scene/serverGameScene.hpp"
5#include "Scene/serverLobbyScene.hpp"
6#include "Server.hpp"
7#include "collision/collisionBuffer.hpp"
8#include "factoryDataDriven/factoryDataDriven.hpp"
9#include "sceneManager/sceneManager.hpp"
10#include "serverScene.hpp"
11
12#include <array>
13#include <cmath>
14#include <cstdint>
15#include <unordered_map>
16
17namespace rtype::server {
29class Game {
30 friend class ServerLobbyScene;
31 friend class ServerGameScene;
32
33public:
38 explicit Game(std::uint16_t port = 4242);
39 ~Game();
40
46 void onLoad(rtk::EngineContext &context);
47
53 void onUpdate(rtk::EngineContext &context, float dt);
54
59 void onUnload(rtk::EngineContext &context);
60
65 bool shouldContinue() const;
66
70 void incrementServerTick() { _serverTick++; }
71
76 std::uint32_t getServerTick() const { return _serverTick; }
77
82 std::size_t getPlayerCount() const { return _players.size(); }
83
88 const std::unordered_map<std::uint32_t, std::size_t> &getPlayers() const {
89 return _players;
90 }
91
96 bool isGameActive() const { return _isGameActive; }
97
102 void setGameActive(bool active) { _isGameActive = active; }
103
108 _players.clear();
109 _serverTick = 0;
110 _isGameActive = false;
111 }
112
113private:
114 void registerComponents(rtk::EngineContext &context);
115 void registerBuilders(rtk::EngineContext &context);
116 void loadEntityBlueprints(rtk::EngineContext &context);
117 void subscribeEvents(rtk::EngineContext &context);
118 void addSystems(rtk::EngineContext &context);
119
120 void handleConnections(rtk::EngineContext &context);
121 void handleInputs(rtk::EngineContext &context);
122 void handleEntityQueries(rtk::EngineContext &context);
123 void cleanupOffscreenEnvironment(rtk::EngineContext &context);
124 void sendSnapshots(rtk::EngineContext &context);
125 void createBackground(rtk::EngineContext &context);
126
127 float createGate(rtk::EngineContext &context, float startX, bool symmetric);
128 float createRamp(rtk::EngineContext &context, float startX, bool symmetric);
129 float createReactor(rtk::EngineContext &context, float startX,
130 bool symmetric);
131 float createHighGround(rtk::EngineContext &context, float startX,
132 bool symmetric);
133 float createLowGround(rtk::EngineContext &context, float startX,
134 bool symmetric);
135 float createMediumGround(rtk::EngineContext &context, float startX,
136 bool symmetric);
137
138 void sendExistingEntities(std::uint32_t connectionId,
139 rtk::EngineContext &context);
140 void broadcastPlayerCreated(std::uint32_t connectionId,
141 rtk::EngineContext &context);
142 void broadcastPlayerDestroyed(std::uint32_t connectionId,
143 std::uint32_t networkId);
144
145 ServerDependency _serverContext;
146 ServerInput _serverInput;
147
150 _sceneManager;
151
152 Server _server;
153 std::unordered_map<std::uint32_t, std::size_t> _players;
154 std::array<std::uint16_t, 4> _playerArchetypes{};
155 std::uint32_t _serverTick = 0;
156 bool _running = true;
157 bool _isGameActive = false;
158 rtk::collision::CollisionBuffer _collisionBuffer;
159};
160
162} // namespace rtype::server
Holds the active scene and switches between a fixed set of scenes.
Definition sceneManager.hpp:32
Collisions detected during the current frame.
Definition collisionBuffer.hpp:24
Authoritative game simulation run by the server.
Definition game.hpp:29
void resetGameState()
Forgets every player and resets the tick and round state.
Definition game.hpp:107
bool shouldContinue() const
Tells the engine whether to keep running.
Definition game.cpp:366
void setGameActive(bool active)
Marks a round as started or stopped.
Definition game.hpp:102
std::size_t getPlayerCount() const
Returns the number of connected players.
Definition game.hpp:82
void onLoad(rtk::EngineContext &context)
Registers components, loads blueprints, adds the systems and starts the network server.
Definition game.cpp:40
std::uint32_t getServerTick() const
Returns the server tick.
Definition game.hpp:76
void onUnload(rtk::EngineContext &context)
Exits the scene and stops the network server.
Definition game.cpp:552
const std::unordered_map< std::uint32_t, std::size_t > & getPlayers() const
Returns the connected players.
Definition game.hpp:88
bool isGameActive() const
Tells whether a round is being played.
Definition game.hpp:96
Game(std::uint16_t port=4242)
Creates the game and its network server.
Definition game.cpp:33
void incrementServerTick()
Advances the server tick by one.
Definition game.hpp:70
void onUpdate(rtk::EngineContext &context, float dt)
Updates the active scene.
Definition game.cpp:362
Round in progress: simulates the stage and returns to the lobby when every player left.
Definition serverGameScene.hpp:17
Waiting room: starts the round with 2 players, or after 30 seconds with 1 player.
Definition serverLobbyScene.hpp:17
Game level layer over NetworkServer that decodes client messages and encodes server messages.
Definition Server.hpp:30
ServerSceneType
Identifiers of the server scenes.
Definition serverScene.hpp:14
Engine services handed to the game and its scenes.
Definition EngineContext.hpp:20
Object passed to every server scene constructor.
Definition serverScene.hpp:28
Input passed to server scenes, empty because inputs come from the network.
Definition serverScene.hpp:23