Room

The Room class represents a room in the game. It contains and manages all the entities in the room and the clients connected to it. It also contains the game loop that refreshes the entities and send the positions of the entities to the clients. The game loop runs in its own thread that is started when the room is created and stopped when the room is deleted.

Each room has a unique id and a boolean that indicates if the room is private or not. A private room is a room that can only be joined by the client that created it. It also has a maximum number of players that can be in it.

A room cannot be copied or moved.

Public member functions

// Construct a new Room object
Room(u_int id, std::shared_ptr<Client> client, Levels &levels, const std::vector<std::shared_ptr<Client>> &allClients, bool privateRoom = false);
// Get the state of the room (WAIT | RUN | END | STOPPED)
State getState() const;
// Get the id of the room
u_int getId() const;
// Get the number of players in the room
unsigned int getNbPlayer() const;
// Get the progress of the game
unsigned int getProgress() const;
// Get max number of players allowed in the room
unsigned int getMaxPlayer() const;
// Add a new player to the room
void addPlayer(std::shared_ptr<Client> client);
// remove a player from the room
void removePlayer(std::shared_ptr<Client> client);
// Move a player in the room
void movePlayer(std::shared_ptr<Client> client, char move, char nbr = 1);
// Checks if a client is in the room
bool isClientInRoom(std::shared_ptr<Client> client);
// Get the player associated with a client
Player &getPlayer(std::shared_ptr<Client> client);
// Send a packet to all players in the room
void sendToAll(const Stream &stream);
// Get the id of the next missile to create
u_int &getMissilesIds();
// Add a monster in the room
void addMonster(IEntity::Type type, int x, int y);
// Get the position of the nearest player from an entity
std::pair<short, short> getNearestPlayerPos(const IEntity &entity);
// Check if the room is private
bool isPrivate() const;
// Check if the it remain monsters in the room
bool isMonster() const;
// Get the id of the next Bomb to create
size_t &getBombIds();
// Get the id of the next Laser to create
size_t &getLaserIds();
// Get the id of the next Ray to create
size_t &getRayIds();
// Apply damage to all monsters in a zone
void degInZone(float x, float y, size_t radius, Player &player);
// Get the Players object
std::vector<std::unique_ptr<Player>> &getPlayers();
// Send a message in the chat
void sendChat(std::shared_ptr<Client> client, const std::string &message);

Public member attributes

// The mutex that lock the players vector
std::mutex _playersMutex;

Member functions documentation

Constructor

The constructor of the Room class.

Room(u_int id, std::shared_ptr<Client> client, Levels &levels, const std::vector<std::shared_ptr<Client>> &allClients, bool privateRoom = false);

Parameters

  • id : The id of the room

  • client : The client that created the room

  • levels : The levels of the gameplay

  • allClients : The list of all the clients connected to the server

  • privateRoom : A boolean that indicates if the room is private or not (default: false)

getState

Get the state of the room.

State getState() const;

Return value

The State of the room. (WAIT | RUN | END | STOPPED)

getId

Get the id of the room.

u_int getId() const;

Return value

An unsigned int containing the id of the room.

getNbPlayer

Get the number of players in the room.

unsigned int getNbPlayer() const;

Return value

An unsigned int containing the number of players in the room.

getProgress

Get the progress of the game.

unsigned int getProgress() const;

Return value

An unsigned int containing the progress of the game.

getMaxPlayer

Get max number of players allowed in the room.

unsigned int getMaxPlayer() const;

Return value

An unsigned int containing the max number of players allowed in the room.

addPlayer

Add a new player to the room.

void addPlayer(std::shared_ptr<Client> client);

Parameters

  • client : A shared pointer of the client to add to the room

removePlayer

Remove a player from the room.

void removePlayer(std::shared_ptr<Client> client);

Parameters

  • client : A shared pointer of the client to remove from the room

movePlayer

Move a player in the room.

void movePlayer(std::shared_ptr<Client> client, char move, char nbr = 1);

Parameters

  • client : A shared pointer of the client to move

  • move : The direction to move the player (UP | DOWN | LEFT | RIGHT)

  • nbr : The number of times to move the player (default: 1)

isClientInRoom

Checks if a client is in the room.

bool isClientInRoom(std::shared_ptr<Client> client);

Parameters

  • client : A shared pointer of the client to check

Return value

A boolean that indicates if the client is in the room or not.

getPlayer

Get the player associated with a client.

Player &getPlayer(std::shared_ptr<Client> client);

Parameters

  • client : A shared pointer of the client to get the player from

Return value

A reference to the player associated with the client.

sendToAll

Send a packet to all players in the room.

void sendToAll(const Stream &stream);

Parameters

  • stream : The stream to send to all players

getMissilesIds

Get the id of the next missile to create.

u_int &getMissilesIds();

Return value

A reference to the id of the next missile to create.

addMonster

Add a monster in the room.

void addMonster(IEntity::Type type, int x, int y);

Parameters

  • type : The type of the monster to add

  • x : The x position of the monster

  • y : The y position of the monster

getNearestPlayerPos

Get the position of the nearest player from an entity.

std::pair<short, short> getNearestPlayerPos(const IEntity &entity);

Parameters

  • entity : The entity to get the nearest player from

Return value

A pair of short containing the x and y position of the nearest player.

isPrivate

Check if the room is private.

bool isPrivate() const;

Return value

A boolean that indicates if the room is private or not.

isMonster

Check if the it remain monsters in the room.

bool isMonster() const;

Return value

A boolean that indicates if the it remain monsters in the room.

getBombIds

Get the id of the next Bomb to create.

size_t &getBombIds();

Return value

A reference to the id of the next Bomb to create.

getLaserIds

Get the id of the next Laser to create.

size_t &getLaserIds();

Return value

A reference to the id of the next Laser to create.

getRayIds

Get the id of the next Ray to create.

size_t &getRayIds();

Return value

A reference to the id of the next Ray to create.

degInZone

Apply damage to all monsters in a zone.

void degInZone(float x, float y, size_t radius, Player &player);

Parameters

  • x : The x position of the center of the zone

  • y : The y position of the center of the zone

  • radius : The radius of the zone

  • player : The player that apply the damage

getPlayers

Get the Players object.

std::vector<std::unique_ptr<Player>> &getPlayers();

Return value

A reference to the Players object.

sendChat

Send a message in the chat.

void sendChat(std::shared_ptr<Client> client, const std::string &message);

Parameters

  • client : A shared pointer of the client that send the message

  • message : The message to send

Last updated