void Client::Update(Player* _player, Player* _player2)
{
sf::Vector2f tempPosition;
//If the player has been assigned a number by the server
if (_player->getPlayerNumber() == 0 || _player->getPlayerNumber() == 1)
{
//Load the send packet with the player's position
float sendX, sendY, recieveX, recieveY;
sendX = _player->getPosition().x;
sendY = _player->getPosition().y;
sendPacket << sendX << sendY;
//If the time between messages has been reached
if (time.asMilliseconds() >= 50)
{
//Send the packet
if (socket.send(sendPacket) != sf::Socket::Done)
{
//error...
}
else
{
sendPacket.clear();
}
//Reset the timer
time = sf::Time::Zero;
}
//Attempt to recieve data from the server each frame
if (socket.receive(recievePacket) != sf::Socket::Done)
{
//error...
}
else
{
recievePacket >> recieveX >> recieveY;
recievePacket.clear();
tempPosition.x = recieveX;
tempPosition.y = recieveY;
_player2->setGoalPosition(tempPosition);
}
//Increment the timer with delta time
time += deltaClock.getElapsedTime();
deltaClock.restart();
}
}