Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Multiplayer.hpp"
- Multiplayer::Multiplayer(sf::RenderWindow &window, Gamestate &gamestate, sf::Vector2f &playableArea, bool host)
- : playableArea(playableArea),
- mainWindow(&window),
- gamestate(&gamestate),
- connectedString("Connected to: "),
- bulletsCount(0),
- maxBullets(1)
- {
- gameTexture = new sf::RenderTexture();
- gameView = new sf::View(sf::Vector2f(playableArea.x / 2, playableArea.y / 2), sf::Vector2f(700, 700));
- gameTexture->create(playableArea.x, playableArea.y);
- if (host) runServer();
- else connectToServer();
- player1 = new Player(sf::Color::Blue, sf::Vector2f(600, 400));
- player2 = new Player(sf::Color::Red, sf::Vector2f(600, 400));
- collision = new Collision();
- socket.setBlocking(false);
- focus = false;
- mainWindow->setTitle(title);
- std::cout << connectedString << std::endl;
- }
- Multiplayer::~Multiplayer()
- {
- delete gameTexture;
- delete gameView;
- delete player1, player2;
- delete collision;
- }
- sf::Vector2f Multiplayer::normalizeMousePosition()
- {
- sf::Vector2f playerCenter, mousePosition, aimDirection;
- playerCenter = player1->getCenter();
- mousePosition = sf::Vector2f(sf::Mouse::getPosition(*mainWindow));
- sf::Vector2f mouseWindowPosition = mainWindow->mapPixelToCoords(static_cast<sf::Vector2i>(mousePosition)); // fix mouse position
- aimDirection = mouseWindowPosition - playerCenter;
- sf::Vector2f aimDirectionNormalize = aimDirection / sqrt(pow(aimDirection.x, 2) + pow(aimDirection.y, 2));
- return aimDirectionNormalize;
- }
- void Multiplayer::runServer()
- {
- sf::IpAddress ip = sf::IpAddress::getLocalAddress();
- unsigned int port = 2000;
- if (listener.listen(port) != sf::Socket::Done) {
- std::cout << "Cannot create server!\n";
- return;
- }
- if (listener.accept(socket) != sf::Socket::Done) {
- std::cout << "Cannot accept client!\n";
- return;
- }
- connectedString += "client";
- title = "SERVER";
- }
- void Multiplayer::connectToServer()
- {
- sf::IpAddress ip = sf::IpAddress::getLocalAddress();
- unsigned int port = 2000;
- sf::Socket::Status status = socket.connect(ip, port);
- if (status != sf::Socket::Done) {
- std::cout << "Cannot connect to server!\n";
- exit(1);
- }
- connectedString += "server";
- title = "CLIENT";
- }
- void Multiplayer::start()
- {
- while (*gamestate == gamestate_multiplayergame)
- {
- processEvents();
- update();
- draw();
- }
- }
- void Multiplayer::processEvents()
- {
- sf::Event event;
- while (mainWindow->pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- *gamestate = gamestate_end;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
- *gamestate = gamestate_end;
- if (event.type == sf::Event::GainedFocus)
- focus = true;
- else if (event.type == sf::Event::LostFocus)
- focus = false;
- }
- }
- void Multiplayer::update()
- {
- gameTime = gameClock.restart();
- sf::Vector2f updateplayerPosition, updatebulletPosition,
- lastbulletPosition,
- lastplayerPosition = player1->getPosition();
- if (focus) {
- player1->update(gameTime.asSeconds());
- if (player1->isAttacking())
- if (bullets_p1.size() < 1) {
- Bullet bullet(800.f);
- bullet.setPosition(player1->getCenter());
- bullet.setVelocity(normalizeMousePosition());
- bullets_p1.push_back(bullet);
- }
- }
- if (bullets_p1.size() == 1)
- lastbulletPosition = bullets_p1[0].getPosition();
- for (int i = 0; i < bullets_p1.size(); i++)
- bullets_p1[i].update(gameTime.asSeconds());
- if (lastplayerPosition != player1->getPosition())
- {
- packetPosition << player1->getPosition().x << player1->getPosition().y;
- socket.send(packetPosition);
- }
- socket.receive(packetPosition);
- if (packetPosition >> updateplayerPosition.x >> updateplayerPosition.y)
- player2->setPosition(updateplayerPosition);
- if (bullets_p1.size() == 1)
- if (lastbulletPosition != bullets_p1[0].getPosition())
- {
- packetBullet << bullets_p1[0].getPosition().x << bullets_p1[0].getPosition().y;
- socket.send(packetBullet);
- }
- packetPosition.clear();
- socket.receive(packetBullet);
- if (packetBullet >> updatebulletPosition.x >> updatebulletPosition.y)
- {
- if (bullets_p1.size() == 0) {
- Bullet bullet(800.f);
- bullet.setPosition(updatebulletPosition);
- bullet.setVelocity(normalizeMousePosition());
- bullets_p2.push_back(bullet);
- }
- else if (bullets_p1.size() == 1)
- bullets_p2[0].setPosition(updatebulletPosition);
- }
- for (int i = 0; i < bullets_p1.size(); i++)
- if (collision->checkCollision(bullets_p1[i], *gameTexture))
- bullets_p1.erase(bullets_p1.begin() + i);
- packetBullet.clear();
- }
- void Multiplayer::draw()
- {
- mainWindow->clear(sf::Color::Yellow);
- gameTexture->clear(sf::Color::Black);
- mainWindow->setView(*gameView);
- gameView->setCenter(player1->getCenter());
- player1->draw(*gameTexture);
- player2->draw(*gameTexture);
- for (int i = 0; i < bullets_p1.size(); i++)
- bullets_p1[i].draw(*gameTexture);
- for (int i = 0; i < bullets_p2.size(); i++)
- bullets_p2[i].draw(*gameTexture);
- sf::Sprite gameTextureSprite(gameTexture->getTexture());
- mainWindow->draw(gameTextureSprite);
- gameTexture->display();
- mainWindow->display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement