Hello everybody!
recently I started working with sfml and I am trying to make a simple network game.
1 player will be the server and the other one will be the client.
I made it work to each player able to move and see the other player moving too.
Now I want that each player will be able to shoot bullets.
it work nice on each separately but when I try it when they players are connected, it doesn't work quite well...
It send the bullet position to the other player but set it to to player position instead.
here is the code:
void Game :: update(sf::Time elapsedTime, sf::TcpSocket& socket)
{
sf::Packet playerPacket;
sf::Packet bulletPacket;
player.update(elapsedTime);
mOtherBullets = mBullets; //these are to std::vectors to hold each player bullets.
if(mBullets.size() > 0)
{
for(int i=0; i<mBullets.size(); ++i)
{
mOtherBullets[i].setSprite(mBullets[i].getSprite()); //don't know if this line is nessecery
bulletPacket << mBullets[i].getSprite().getPosition().x << mBullets[i].getSprite().getPosition().y;
}
if(socket.send(bulletPacket) == sf::Socket::Done)
{
//wrote here messege
}
}
if(mOtherBullets.size()>0)
{
if(socket.receive(bulletPacket) == sf::Socket::Done)
{
for (int j = 0; j < mOtherBullets.size(); ++j)
if( bulletPacket >> mNewBulletPosition.x >> mNewBulletPosition.y)
{
//code doesn't reach here
std::cout << "bullets successfully receied\n ";
mOtherBullets[j].getSprite().setPosition(mNewBulletPosition);
}
}
}
//this part works just fine but when one player shoots the other one see the player moving in the bullet direction
if(prevPlayerPosition != player.getSprite().getPosition())
{
playerPacket << player.getSprite().getPosition().x << player.getSprite().getPosition().y;
if(socket.send(playerPacket) == sf::Socket::Done)
{
}
}
if(socket.receive(playerPacket) == sf::Socket::Done)
{
if(playerPacket >> mNewPlayerPosition.x >> mNewPlayerPosition.y)
{
mSecondPlayerSprite.setPosition(mNewPlayerPosition);
}
}
}
I hope will get some help here.
Thanks.