Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. #include "Multiplayer.hpp"
  2.  
  3. Multiplayer::Multiplayer(sf::RenderWindow &window, Gamestate &gamestate, sf::Vector2f &playableArea, bool host)
  4.     :   playableArea(playableArea),
  5.         mainWindow(&window),
  6.         gamestate(&gamestate),
  7.         connectedString("Connected to: "),
  8.         bulletsCount(0),
  9.         maxBullets(1)
  10. {
  11.     gameTexture = new sf::RenderTexture();
  12.     gameView = new sf::View(sf::Vector2f(playableArea.x / 2, playableArea.y / 2), sf::Vector2f(700, 700));
  13.     gameTexture->create(playableArea.x, playableArea.y);
  14.  
  15.     if (host) runServer();
  16.     else connectToServer();
  17.  
  18.     player1 = new Player(sf::Color::Blue, sf::Vector2f(600, 400));
  19.     player2 = new Player(sf::Color::Red, sf::Vector2f(600, 400));
  20.     collision = new Collision();
  21.  
  22.     socket.setBlocking(false);
  23.     focus = false;
  24.  
  25.     mainWindow->setTitle(title);
  26.     std::cout << connectedString << std::endl;
  27. }
  28.  
  29. Multiplayer::~Multiplayer()
  30. {
  31.     delete gameTexture;
  32.     delete gameView;
  33.     delete player1, player2;
  34.     delete collision;
  35. }
  36.  
  37. sf::Vector2f Multiplayer::normalizeMousePosition()
  38. {
  39.     sf::Vector2f playerCenter, mousePosition, aimDirection;
  40.  
  41.     playerCenter = player1->getCenter();
  42.     mousePosition = sf::Vector2f(sf::Mouse::getPosition(*mainWindow));
  43.     sf::Vector2f mouseWindowPosition = mainWindow->mapPixelToCoords(static_cast<sf::Vector2i>(mousePosition)); // fix mouse position
  44.     aimDirection = mouseWindowPosition - playerCenter;
  45.     sf::Vector2f aimDirectionNormalize = aimDirection / sqrt(pow(aimDirection.x, 2) + pow(aimDirection.y, 2));
  46.  
  47.     return aimDirectionNormalize;
  48. }
  49.  
  50. void Multiplayer::runServer()
  51. {
  52.     sf::IpAddress ip = sf::IpAddress::getLocalAddress();
  53.     unsigned int port = 2000;
  54.  
  55.     if (listener.listen(port) != sf::Socket::Done) {
  56.         std::cout << "Cannot create server!\n";
  57.         return;
  58.     }
  59.  
  60.     if (listener.accept(socket) != sf::Socket::Done) {
  61.         std::cout << "Cannot accept client!\n";
  62.         return;
  63.     }
  64.  
  65.     connectedString += "client";
  66.     title = "SERVER";
  67.  
  68. }
  69.  
  70. void Multiplayer::connectToServer()
  71. {
  72.     sf::IpAddress ip = sf::IpAddress::getLocalAddress();
  73.     unsigned int port = 2000;
  74.  
  75.     sf::Socket::Status status = socket.connect(ip, port);
  76.     if (status != sf::Socket::Done) {
  77.         std::cout << "Cannot connect to server!\n";
  78.         exit(1);
  79.     }
  80.     connectedString += "server";
  81.     title = "CLIENT";
  82. }
  83.  
  84. void Multiplayer::start()
  85. {
  86.     while (*gamestate == gamestate_multiplayergame)
  87.     {
  88.         processEvents();
  89.         update();
  90.         draw();
  91.     }
  92. }
  93.  
  94. void Multiplayer::processEvents()
  95. {
  96.     sf::Event event;
  97.     while (mainWindow->pollEvent(event))
  98.     {
  99.         if (event.type == sf::Event::Closed)
  100.             *gamestate = gamestate_end;
  101.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  102.             *gamestate = gamestate_end;
  103.  
  104.         if (event.type == sf::Event::GainedFocus)
  105.             focus = true;
  106.         else if (event.type == sf::Event::LostFocus)
  107.             focus = false;
  108.     }
  109. }
  110.  
  111. void Multiplayer::update()
  112. {
  113.     gameTime = gameClock.restart();
  114.  
  115.     sf::Vector2f updateplayerPosition, updatebulletPosition,
  116.                  lastbulletPosition,
  117.                  lastplayerPosition = player1->getPosition();
  118.  
  119.     if (focus) {
  120.         player1->update(gameTime.asSeconds());
  121.             if (player1->isAttacking())
  122.                 if (bullets_p1.size() < 1) {
  123.                     Bullet bullet(800.f);
  124.                     bullet.setPosition(player1->getCenter());
  125.                     bullet.setVelocity(normalizeMousePosition());
  126.                     bullets_p1.push_back(bullet);
  127.                 }
  128.     }
  129.     if (bullets_p1.size() == 1)
  130.         lastbulletPosition = bullets_p1[0].getPosition();
  131.  
  132.     for (int i = 0; i < bullets_p1.size(); i++)
  133.         bullets_p1[i].update(gameTime.asSeconds());
  134.  
  135.     if (lastplayerPosition != player1->getPosition())
  136.     {
  137.         packetPosition << player1->getPosition().x << player1->getPosition().y;
  138.         socket.send(packetPosition);
  139.     }
  140.    
  141.     socket.receive(packetPosition);
  142.  
  143.     if (packetPosition >> updateplayerPosition.x >> updateplayerPosition.y)
  144.         player2->setPosition(updateplayerPosition);
  145.  
  146.     if (bullets_p1.size() == 1)
  147.         if (lastbulletPosition != bullets_p1[0].getPosition())
  148.         {
  149.             packetBullet << bullets_p1[0].getPosition().x << bullets_p1[0].getPosition().y;
  150.             socket.send(packetBullet);
  151.         }
  152.     packetPosition.clear();
  153.  
  154.     socket.receive(packetBullet);
  155.  
  156.    
  157.     if (packetBullet >> updatebulletPosition.x >> updatebulletPosition.y)
  158.     {
  159.         if (bullets_p1.size() == 0) {
  160.             Bullet bullet(800.f);
  161.             bullet.setPosition(updatebulletPosition);
  162.             bullet.setVelocity(normalizeMousePosition());
  163.             bullets_p2.push_back(bullet);
  164.         }
  165.         else if (bullets_p1.size() == 1)
  166.             bullets_p2[0].setPosition(updatebulletPosition);
  167.     }
  168.  
  169.     for (int i = 0; i < bullets_p1.size(); i++)
  170.         if (collision->checkCollision(bullets_p1[i], *gameTexture))
  171.             bullets_p1.erase(bullets_p1.begin() + i);
  172.  
  173.     packetBullet.clear();
  174. }
  175.  
  176. void Multiplayer::draw()
  177. {
  178.     mainWindow->clear(sf::Color::Yellow);
  179.     gameTexture->clear(sf::Color::Black);
  180.     mainWindow->setView(*gameView);
  181.     gameView->setCenter(player1->getCenter());
  182.  
  183.         player1->draw(*gameTexture);
  184.         player2->draw(*gameTexture);
  185.  
  186.         for (int i = 0; i < bullets_p1.size(); i++)
  187.             bullets_p1[i].draw(*gameTexture);
  188.         for (int i = 0; i < bullets_p2.size(); i++)
  189.             bullets_p2[i].draw(*gameTexture);
  190.  
  191.     sf::Sprite gameTextureSprite(gameTexture->getTexture());
  192.     mainWindow->draw(gameTextureSprite);
  193.  
  194.     gameTexture->display();
  195.     mainWindow->display();
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement