* Added Player class to represent players on the server. * Added Game class to manage the overall game state. * Server now adds new players to the game state when they connect. * Server now updates the players position in the game state on receiving a PlayerPosMessage.
34 lines
1.0 KiB
C++
34 lines
1.0 KiB
C++
#include <algorithm>
|
|
|
|
#include "game.h"
|
|
#include "network/socket.h"
|
|
|
|
void Game::add_player(BettolaLib::Network::Socket* socket) {
|
|
_players.emplace_back(socket);
|
|
}
|
|
|
|
void Game::remove_player(BettolaLib::Network::Socket* socket) {
|
|
/*
|
|
* TODO: We'll need a better way to ident players other than
|
|
* their socket. We'll match sockets for now though.. -- Falco
|
|
*/
|
|
_players.erase(
|
|
std::remove_if(_players.begin(), _players.end(),
|
|
[socket](const Player& player) {
|
|
/* this is a sh.t way to compare players. */
|
|
return &player.get_socket() == socket;
|
|
}),
|
|
_players.end());
|
|
}
|
|
|
|
void Game::update_player_pos(BettolaLib::Network::Socket* socket, float x, float y) {
|
|
auto it = std::find_if(_players.begin(), _players.end(),
|
|
[socket](const Player& player) {
|
|
return &player.get_socket() == socket;
|
|
});
|
|
if(it != _players.end()) {
|
|
it->set_position(x, y);
|
|
}
|
|
}
|
|
|