Jetpack documentation 1.0.0
Loading...
Searching...
No Matches
Client Class Reference

A class that represents the client. More...

#include <Client.hpp>

Public Types

typedef std::tuple< CmdID, int, std::string > response_t
 

Public Member Functions

 Client (int ac, char **av)
 Constructor for Client class.
 
 ~Client ()
 Destructor for Client class.
 
 Client (const Client &)=delete
 Deleted copy constructor.
 
Clientoperator= (const Client &)=delete
 Deleted assignment operator.
 
void sendCommand (int fd, const std::vector< std::string > &command)
 Sends a command to the server.
 
std::pair< int, std::string > getResponse ()
 Receives a response from the server.
 
response_t getJetpackResponse ()
 Receives a response from the server.
 
void run ()
 Runs the client.
 

Static Public Member Functions

static void closeJetpackClient (int signal)
 Closes the client.
 

Static Public Attributes

static std::mutex isRunningMutex
 
static bool isRunning = false
 

Protected Member Functions

void executeExit (std::vector< std::string > &command)
 Executes the exit command.
 
void handleCommandId (const int &code, const std::string &message)
 Handles the response of the command ID.
 
void handleCommandMap (const int &code, const std::string &message)
 Handles the response of the command MAP.
 
void handleCommandPseudo (const int &code, const std::string &message)
 Handles the response of the command PSEUDO.
 
void handleCommandMove (const int &code, const std::string &message)
 Handles the response of the command MOVE.
 
void handleCommandClientsNb (const int &code, const std::string &message)
 Handles the response of the command CLIENT_NB.
 
void handleCommandClientsStats (const int &code, const std::string &message)
 Handles the response of the command CLIENTS_STATS.
 
void handleCommandClientsPositions (const int &code, const std::string &message)
 Handles the response of the command CLIENTS_POSITIONS.
 

Protected Attributes

std::unordered_map< std::string, std::function< void(std::vector< std::string > &command)> > _commands
 

Private Types

enum class  MusicState { NONE , PLAYING , PAUSED , STOPPED }
 
enum  GameState { MAIN_MENU , GAME , END }
 

Private Member Functions

void _checkArgs (int ac, char **av)
 Check the arguments passed to the client.
 
void _initCommands ()
 Initializes the commands of the client.
 
void _initGameInformation ()
 Initializes the game information.
 
void _initClient ()
 Initializes the client.
 
void _initFx ()
 Initializes the FX for the game.
 
void _initMusic ()
 Initializes the music for the game.
 
void _initElements ()
 Initializes the elements of the game.
 
void _initSfml ()
 Initializes the SFML window and all game elements.
 
void _promptThread ()
 Thread for handling user input.
 
void _communicationThread ()
 Thread for handling communication with the server.
 
void _gameThread ()
 Thread for handling the game loop.
 
void _updateFromResponse ()
 Updates the game from the server responses.
 
void _updateAnimation (float deltaTime)
 Updates the animation.
 
void _updateMenu (float deltaTime)
 Updates the menu.
 
void _updateGame (float deltaTime)
 Updates the game.
 
void _update (float deltaTime)
 Updates the client.
 
void _drawMenu ()
 Draws the elements of the menu.
 
void _drawGame ()
 Draws the elements of the game.
 
void _drawEnd ()
 Draws the elements of the end.
 
void _draw ()
 Draws the elements on the screen depending on the game state.
 
void _analyseEvent ()
 Analyses the events of the game.
 

Private Attributes

std::string _ip
 
std::string _port
 
std::unique_ptr< Socket_client_socket
 
struct sockaddr_in _client
 
std::mutex _commands_queue_mutex
 
std::queue< std::vector< std::string > > _commands_queue
 
std::mutex _response_queue_mutex
 
std::queue< response_t > _response_queue
 
GameState _gameState = MAIN_MENU
 
std::size_t _id
 
std::vector< std::string > _map
 
std::size_t _nb_players = 0
 
std::map< size_t, std::unique_ptr< Player > > _players
 
std::string _winner
 
sf::RenderWindow _window
 
sf::Font _font
 
sf::Text _text
 
sf::Music _menuMusic
 
MusicState _menuMusicState = MusicState::NONE
 
sf::Music _gameMusic
 
MusicState _gameMusicState = MusicState::NONE
 
sf::SoundBuffer _jetpackSoundStartBuffer
 
sf::Sound _jetpackSoundStart
 
sf::SoundBuffer _jetpackSoundStopBuffer
 
sf::Sound _jetpackSoundStop
 
sf::SoundBuffer _jetpackSoundDownBuffer
 
sf::Sound _jetpackSoundDown
 
sf::SoundBuffer _jetpackSoundUpBuffer
 
sf::Sound _jetpackSoundUp
 
sf::SoundBuffer _coinSoundBuffer
 
sf::Sound _coinSound
 
sf::SoundBuffer _zappedSoundBuffer
 
sf::Sound _zappedSound
 
sf::SoundBuffer _firedSoundBuffer
 
sf::Sound _firedSound
 
sf::Texture _background_texture
 
sf::Sprite _background_sprite
 
float _background_size
 
float _background_pos = 0.f
 
sf::Texture _player_texture
 
sf::Sprite _player_sprite
 
bool _jetpack = false
 
Player::PlayerState _curent_state = Player::PlayerState::NORMAL
 
sf::Texture _coin_texture
 
sf::Sprite _coin_sprite
 
Animation _coin_animation
 
std::unordered_map< std::array< std::size_t, 2 >, std::unique_ptr< Coin >, PositionHash_coins
 
std::unordered_map< std::array< std::size_t, 2 >, std::unique_ptr< Trap >, PositionHash_traps
 
sf::Texture _zapper_texture
 
sf::Sprite _zapper_sprite
 
Animation _zapper_animation
 
sf::Texture _laser_texture
 
sf::Sprite _laser_sprite
 
Animation _laser_animation
 
sf::Texture _missile_texture
 
sf::Sprite _missile_sprite
 
Animation _missile_animation
 

Static Private Attributes

static constexpr float WINDOW_WIDTH = 1280.0f
 
static constexpr float WINDOW_HEIGHT = 720.0f
 
static constexpr float BACKGROUND_SPEED = 100.0f
 

Detailed Description

A class that represents the client.

Constructor & Destructor Documentation

◆ Client() [1/2]

Client ( int ac,
char ** av )

Constructor for Client class.

Parameters
acNumber of command line arguments
avArray of command line arguments

◆ Client() [2/2]

Client ( const Client & )
delete

Deleted copy constructor.

Parameters
otherThe other Client object to copy from

Member Function Documentation

◆ _checkArgs()

void _checkArgs ( int ac,
char ** av )
private

Check the arguments passed to the client.

Parameters
acThe number of arguments
avThe arguments
Exceptions
JetpackErrorIf the arguments are invalid
JetpackUsageIf the help flag is passed

◆ _initClient()

void _initClient ( )
private

Initializes the client.

Exceptions
JetpackErrorIf the connection fails

◆ _initElements()

void _initElements ( )
private

Initializes the elements of the game.

Exceptions
JetpackErrorIf the elements initialization fails

◆ _initFx()

void _initFx ( )
private

Initializes the FX for the game.

Exceptions
JetpackErrorIf the FX initialization fails

◆ _initGameInformation()

void _initGameInformation ( )
private

Initializes the game information.

Exceptions
JetpackErrorIf the ID or MAP command fails

◆ _initMusic()

void _initMusic ( )
private

Initializes the music for the game.

Exceptions
JetpackErrorIf the music initialization fails

◆ _initSfml()

void _initSfml ( )
private

Initializes the SFML window and all game elements.

Exceptions
JetpackErrorIf the SFML initialization fails

◆ _update()

void _update ( float deltaTime)
private

Updates the client.

Parameters
deltaTimeThe time since the last update

◆ _updateAnimation()

void _updateAnimation ( float deltaTime)
private

Updates the animation.

Updates the animation of the game.

Parameters
deltaTimeThe time since the last update
deltaTimeThe time since the last frame

◆ _updateGame()

void _updateGame ( float deltaTime)
private

Updates the game.

Parameters
deltaTimeThe time since the last update

◆ _updateMenu()

void _updateMenu ( float deltaTime)
private

Updates the menu.

Parameters
deltaTimeThe time since the last update

◆ closeJetpackClient()

void closeJetpackClient ( int signal)
static

Closes the client.

Note
This function is a signal handler for SIGINT
Parameters
signalThe signal to close the client

◆ executeExit()

void executeExit ( std::vector< std::string > & command)
protected

Executes the exit command.

Parameters
commandThe command to execute

◆ getJetpackResponse()

Client::response_t getJetpackResponse ( )

Receives a response from the server.

Returns
A tuple containing the command ID, the code and the message

◆ getResponse()

std::pair< int, std::string > getResponse ( )

Receives a response from the server.

Returns
A pair containing the code and the message

◆ handleCommandClientsNb()

void handleCommandClientsNb ( const int & code,
const std::string & message )
protected

Handles the response of the command CLIENT_NB.

Parameters
codeThe command code
messageThe message to handle

◆ handleCommandClientsPositions()

void handleCommandClientsPositions ( const int & code,
const std::string & message )
protected

Handles the response of the command CLIENTS_POSITIONS.

Parameters
codeThe command code
messageThe message to handle

◆ handleCommandClientsStats()

void handleCommandClientsStats ( const int & code,
const std::string & message )
protected

Handles the response of the command CLIENTS_STATS.

Parameters
codeThe command code
messageThe message to handle

◆ handleCommandId()

void handleCommandId ( const int & code,
const std::string & message )
protected

Handles the response of the command ID.

Parameters
codeThe command code
messageThe message to handle

◆ handleCommandMap()

void handleCommandMap ( const int & code,
const std::string & message )
protected

Handles the response of the command MAP.

Parameters
codeThe command code
messageThe message to handle

◆ handleCommandMove()

void handleCommandMove ( const int & code,
const std::string & message )
protected

Handles the response of the command MOVE.

Parameters
codeThe command code
messageThe message to handle

◆ handleCommandPseudo()

void handleCommandPseudo ( const int & code,
const std::string & message )
protected

Handles the response of the command PSEUDO.

Parameters
codeThe command code
messageThe message to handle

◆ operator=()

Client & operator= ( const Client & )
delete

Deleted assignment operator.

Parameters
otherThe other Client object to assign from
Returns
Reference to the current object

◆ run()

void run ( )

Runs the client.

Note
This function is the main loop of the client

◆ sendCommand()

void sendCommand ( int fd,
const std::vector< std::string > & command )

Sends a command to the server.

Parameters
fdThe file descriptor of the socket
commandThe command to send

The documentation for this class was generated from the following files: