Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. void Multiplayer::receivePackets()
  2. {
  3.     sf::Vector2f updateplayerPosition, updatebulletPosition;
  4.     sf::Packet packet;
  5.     int type;
  6.  
  7.     socket.receive(packet);
  8.  
  9.     if (packet >> type)
  10.         switch (type)
  11.         {
  12.             case static_cast<int>(PacketType::playerPos):
  13.             {
  14.                 if (packet >> updateplayerPosition.x >> updateplayerPosition.y)
  15.                     player2->setPosition(updateplayerPosition);
  16.                 break;
  17.             }
  18.  
  19.             case static_cast<int>(PacketType::bulletPos):
  20.             {
  21.                 if (packet >> updatebulletPosition.x >> updatebulletPosition.y)
  22.                     std::cout << "bullet position have been received\n";
  23.                 break;
  24.             }
  25.         }
  26.     packet.clear();
  27. }
  28.  
  29. void Multiplayer::playersUpdate()
  30. {
  31.     sf::Vector2f lastplayerPosition = player1->getPosition();
  32.     packetType1 = PacketType::playerPos;
  33.  
  34.     if (focus)
  35.         player1->update(gameTime.asSeconds());
  36.  
  37.     if (lastplayerPosition != player1->getPosition())
  38.     {
  39.         packet1 << static_cast<int>(packetType1) << player1->getPosition().x << player1->getPosition().y;
  40.         socket.send(packet1);
  41.     }  
  42. }
  43.  
  44. void Multiplayer::bulletsUpdate()
  45. {
  46.     sf::Vector2f lastbulletPosition;
  47.     packetType2 = PacketType::bulletPos;
  48.  
  49.     if (focus)
  50.         if (!isBullet)
  51.             if (player1->isAttacking()) {
  52.                 Bullet tbullet1(100.f), tbullet2(100.f);
  53.                 tbullet1.setPosition(player1->getCenter()), tbullet2.setPosition(player2->getCenter());
  54.                 tbullet1.setVelocity(normalizeMousePosition());
  55.                 vbullets1.push_back(tbullet1);
  56.                 vbullets2.push_back(tbullet2);
  57.                 isBullet = true;
  58.             }
  59.  
  60.     lastbulletPosition = (vbullets1.size() ? vbullets1[0].getPosition() : sf::Vector2f(0, 0));
  61.  
  62.     for (size_t i = 0; i < vbullets1.size(); i++)
  63.         vbullets1[i].update(gameTime.asSeconds());
  64.  
  65.     if (isBullet)
  66.         if (lastbulletPosition != vbullets1[0].getPosition()) {
  67.             packet2 << static_cast<int>(packetType2) << vbullets1[0].getPosition().x << vbullets1[0].getPosition().y;
  68.             socket.send(packet2);
  69.             std::cout << "bullet position has been sent\n";
  70.         }
  71.  
  72.     for (size_t i = 0; i < vbullets1.size(); i++)
  73.         if (collision->checkCollision(vbullets1[0], *gameTexture))
  74.             vbullets1.erase(vbullets1.begin() + i);
  75.     for (size_t i = 0; i < vbullets2.size(); i++)
  76.         if (collision->checkCollision(vbullets2[0], *gameTexture))
  77.             vbullets2.erase(vbullets2.begin() + i);
  78.  
  79.     if (vbullets1.size() < 1)
  80.         isBullet = false;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement