Jetpack documentation 1.0.0
Loading...
Searching...
No Matches
Client.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** jetpack
4** File description:
5** The Client.hpp
6*/
13#ifndef CLIENT_HPP_
14 #define CLIENT_HPP_
15
16 #include "client/Player.hpp"
17 #include "client/Coin.hpp"
18 #include "client/Trap.hpp"
19 #include "utils/PositionHash.hpp"
20
21namespace Jetpack {
26 class Client
27 {
28 public:
29 /* Constructor and destructor */
30
36 Client(int ac, char **av);
40 ~Client();
45 Client(const Client &) = delete;
51 Client &operator=(const Client &) = delete;
52
53
54
55 /* Protocol functions */
56
57 typedef std::tuple<CmdID, int, std::string> response_t; /* A tuple containing the command ID, the code and the message */
58
64 void sendCommand(int fd, const std::vector<std::string> &command);
69 std::pair<int, std::string> getResponse();
74 response_t getJetpackResponse();
75
76
77
78 /* Client functions */
79
80 static std::mutex isRunningMutex; /* Mutex for isRunning */
81 static bool isRunning; /* The running status of the client (used to stop the client) */
82
88 static void closeJetpackClient(int signal);
93 void run();
94
95
96
97
98 private:
99 /* The state of the music */
100 enum class MusicState {
101 NONE,
102 PLAYING,
103 PAUSED,
104 STOPPED,
105 };
106
107
108
109 /* Initialization functions */
110
118 void _checkArgs(int ac, char **av);
122 void _initCommands();
132 void _initClient();
137 void _initFx();
142 void _initMusic();
147 void _initElements();
152 void _initSfml();
153
154
155
156 /* Thread functions */
157
161 void _promptThread();
169 void _gameThread();
170
171
172
173 /* Game functions */
174
178 void _updateFromResponse();
183 void _updateAnimation(float deltaTime);
188 void _updateMenu(float deltaTime);
193 void _updateGame(float deltaTime);
198 void _update(float deltaTime);
202 void _drawMenu();
206 void _drawGame();
210 void _drawEnd();
214 void _draw();
218 void _analyseEvent();
219
220
221
222 protected:
223 /* Command functions */
224
225 std::unordered_map<std::string, std::function<void(std::vector<std::string> &command)>> _commands; /* The map of commands (command name, function) */
226
231 void executeExit(std::vector<std::string> &command);
232
233
234
235 /* Response handler functions */
236
242 void handleCommandId(const int &code, const std::string &message);
248 void handleCommandMap(const int &code, const std::string &message);
254 void handleCommandPseudo(const int &code, const std::string &message);
260 void handleCommandMove(const int &code, const std::string &message);
266 void handleCommandClientsNb(const int &code, const std::string &message);
272 void handleCommandClientsStats(const int &code, const std::string &message);
278 void handleCommandClientsPositions(const int &code, const std::string &message);
279
280
281
282 private:
283 /* Client information */
284
285 std::string _ip; /* The IP address of the server */
286 std::string _port; /* The port of the server */
287 std::unique_ptr<Socket> _client_socket; /* The socket of the client */
288 struct sockaddr_in _client; /* The address of the client */
289 std::mutex _commands_queue_mutex; /* Mutex for the commands queue */
290 std::queue<std::vector<std::string>> _commands_queue; /* The queue of commands to send to the server */
291 std::mutex _response_queue_mutex; /* Mutex for the response queue */
292 std::queue<response_t> _response_queue; /* The queue of responses from the server */
293
294
295
296 /* Game information */
297
298 enum GameState {
299 MAIN_MENU,
300 GAME,
301 END
302 };
303
304 GameState _gameState = MAIN_MENU; /* The game state */
305 std::size_t _id; /* The ID of the client */
306 std::vector<std::string> _map; /* The map of the game */
307 std::size_t _nb_players = 0; /* The number of players */
308 std::map<size_t, std::unique_ptr<Player>> _players; /* The map of players (ID, Player) */
309 std::string _winner; /* The winner of the game */
310
311
312
313 /* Graphical elements */
314
315 static constexpr float WINDOW_WIDTH = 1280.0f; /* The width of the window */
316 static constexpr float WINDOW_HEIGHT = 720.0f; /* The height of the window */
317 sf::RenderWindow _window; /* The window of the game */
318 #define NB_LINES _map.size() /* The number of lines in the map */
319 #define RATIO (WINDOW_HEIGHT / static_cast<float>(NB_LINES)) /* The size of a tile */
320
321
322 /* Text */
323
324 sf::Font _font; /* The font of the text */
325 sf::Text _text; /* The text to display */
326
327
328 /* Music */
329
330 sf::Music _menuMusic; /* The menu music */
331 MusicState _menuMusicState = MusicState::NONE; /* The state of the menu music */
332
333 sf::Music _gameMusic; /* The game music */
334 MusicState _gameMusicState = MusicState::NONE; /* The state of the game music */
335
336
337 /* Fx */
338
339 sf::SoundBuffer _jetpackSoundStartBuffer; /* The jetpack sound start buffer */
340 sf::Sound _jetpackSoundStart; /* The jetpack sound start */
341
342 sf::SoundBuffer _jetpackSoundStopBuffer; /* The jetpack sound stop buffer */
343 sf::Sound _jetpackSoundStop; /* The jetpack sound stop */
344
345 sf::SoundBuffer _jetpackSoundDownBuffer; /* The jetpack sound down buffer */
346 sf::Sound _jetpackSoundDown; /* The jetpack sound down */
347
348 sf::SoundBuffer _jetpackSoundUpBuffer; /* The jetpack sound up buffer */
349 sf::Sound _jetpackSoundUp; /* The jetpack sound up */
350
351 sf::SoundBuffer _coinSoundBuffer; /* The coin sound buffer */
352 sf::Sound _coinSound; /* The coin sound */
353
354 sf::SoundBuffer _zappedSoundBuffer; /* The zapped sound buffer */
355 sf::Sound _zappedSound; /* The zapped sound */
356
357 sf::SoundBuffer _firedSoundBuffer; /* The fired sound buffer */
358 sf::Sound _firedSound; /* The fired sound */
359
360
361 /* Background */
362
363 sf::Texture _background_texture; /* The background texture */
364 sf::Sprite _background_sprite; /* The background sprite */
365 float _background_size; /* The size of the background */
366 float _background_pos = 0.f; /* The position of the background */
367 static constexpr float BACKGROUND_SPEED = 100.0f; /* The speed of the background */
368
369
370 /* Player */
371
372 sf::Texture _player_texture; /* The player texture */
373 sf::Sprite _player_sprite; /* The player sprite */
374 bool _jetpack = false; /* The jetpack status */
375 Player::PlayerState _curent_state = Player::PlayerState::NORMAL; /* The current state of the player */
376
377
378 /* Coin */
379
380 sf::Texture _coin_texture; /* The coin texture */
381 sf::Sprite _coin_sprite; /* The coin sprite */
382 Animation _coin_animation; /* The coin animation */
383 std::unordered_map<std::array<std::size_t, 2>, std::unique_ptr<Coin>, PositionHash> _coins; /* The map of coins (position, Coin) */
384
385
386 /* Traps */
387 std::unordered_map<std::array<std::size_t, 2>, std::unique_ptr<Trap>, PositionHash> _traps; /* The map of traps (position, Trap) */
388
389 /* Zapper */
390 sf::Texture _zapper_texture; /* The zapper texture */
391 sf::Sprite _zapper_sprite; /* The zapper sprite */
392 Animation _zapper_animation; /* The zapper animation */
393
394 /* Lazer */
395 sf::Texture _laser_texture; /* The laser texture */
396 sf::Sprite _laser_sprite; /* The laser sprite */
397 Animation _laser_animation; /* The laser animation */
398
399 /* Missile */
400 sf::Texture _missile_texture; /* The missile texture */
401 sf::Sprite _missile_sprite; /* The missile sprite */
402 Animation _missile_animation; /* The missile animation */
403 };
404}
405
406#endif /* CLIENT_HPP_ */
The Coin.hpp.
The Player.hpp.
The PositionHash.hpp.
The Trap.hpp.
A class that represents the client.
Definition Client.hpp:27
void _initElements()
Initializes the elements of the game.
Definition Game.cpp:95
void handleCommandId(const int &code, const std::string &message)
Handles the response of the command ID.
Definition Id.cpp:21
void _initSfml()
Initializes the SFML window and all game elements.
Definition Game.cpp:161
void handleCommandClientsNb(const int &code, const std::string &message)
Handles the response of the command CLIENT_NB.
Definition ClientsNb.cpp:21
void run()
Runs the client.
Definition Client.cpp:320
static void closeJetpackClient(int signal)
Closes the client.
Definition Client.cpp:24
Client & operator=(const Client &)=delete
Deleted assignment operator.
~Client()
Destructor for Client class.
Definition Client.cpp:258
void handleCommandMap(const int &code, const std::string &message)
Handles the response of the command MAP.
Definition Map.cpp:22
Client(int ac, char **av)
Constructor for Client class.
Definition Client.cpp:247
void _updateMenu(float deltaTime)
Updates the menu.
Definition Game.cpp:263
void _initCommands()
Initializes the commands of the client.
Definition Client.cpp:215
void _initGameInformation()
Initializes the game information.
Definition Client.cpp:192
void _analyseEvent()
Analyses the events of the game.
Definition Game.cpp:538
void _initFx()
Initializes the FX for the game.
Definition Game.cpp:20
void handleCommandMove(const int &code, const std::string &message)
Handles the response of the command MOVE.
Definition Move.cpp:22
std::pair< int, std::string > getResponse()
Receives a response from the server.
Definition Client.cpp:95
void _updateAnimation(float deltaTime)
Updates the animation.
Definition Game.cpp:230
void _updateGame(float deltaTime)
Updates the game.
Definition Game.cpp:284
void _draw()
Draws the elements on the screen depending on the game state.
Definition Game.cpp:514
void _promptThread()
Thread for handling user input.
Definition Client.cpp:266
void handleCommandClientsStats(const int &code, const std::string &message)
Handles the response of the command CLIENTS_STATS.
Definition ClientsStats.cpp:21
void _gameThread()
Thread for handling the game loop.
Definition Game.cpp:591
void _updateFromResponse()
Updates the game from the server responses.
Definition Game.cpp:195
Client(const Client &)=delete
Deleted copy constructor.
void handleCommandPseudo(const int &code, const std::string &message)
Handles the response of the command PSEUDO.
Definition Pseudo.cpp:22
response_t getJetpackResponse()
Receives a response from the server.
Definition Client.cpp:142
void _drawEnd()
Draws the elements of the end.
Definition Game.cpp:487
void _initMusic()
Initializes the music for the game.
Definition Game.cpp:71
void executeExit(std::vector< std::string > &command)
Executes the exit command.
Definition Exit.cpp:20
void _initClient()
Initializes the client.
Definition Client.cpp:225
void _drawGame()
Draws the elements of the game.
Definition Game.cpp:402
void _checkArgs(int ac, char **av)
Check the arguments passed to the client.
Definition Client.cpp:38
void _update(float deltaTime)
Updates the client.
Definition Game.cpp:361
void handleCommandClientsPositions(const int &code, const std::string &message)
Handles the response of the command CLIENTS_POSITIONS.
Definition ClientsPosition.cpp:21
void sendCommand(int fd, const std::vector< std::string > &command)
Sends a command to the server.
Definition Client.cpp:74
void _communicationThread()
Thread for handling communication with the server.
Definition Client.cpp:297
void _drawMenu()
Draws the elements of the menu.
Definition Game.cpp:385