From 0f29f9c1c63082f739eeb4f989f80b0219d048b4 Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Tue, 26 Oct 2021 02:32:55 +0700 Subject: [PATCH] small refactor --- Client.cpp | 11 ++++++++++- Shooter.cpp | 19 ++++++++++--------- engine/animation/Timeline.h | 1 + engine/network/ClientUDP.cpp | 2 ++ engine/network/ClientUDP.h | 5 +++++ 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Client.cpp b/Client.cpp index 20ed0e3..2491652 100644 --- a/Client.cpp +++ b/Client.cpp @@ -7,6 +7,8 @@ #include #include "engine/utils/Log.h" #include "Bonus.h" +#include "engine/animation/Timeline.h" +#include "engine/animation/ATranslateToPoint.h" void Client::updatePacket() { sf::Packet packet; @@ -37,7 +39,14 @@ void Client::processUpdate(sf::Packet& packet) { while (packet >> targetId >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> buf[4] >> buf[5]) { if (_players.count(targetId)) { std::string name = "Player_" + std::to_string(targetId); - _players[targetId]->translateToPoint(Vec3D{buf[0], buf[1], buf[2]}); + + // old approach (no animation): + //_players[targetId]->translateToPoint(Vec3D{buf[0], buf[1], buf[2]}); + + // new approach (linear extrapolational animations) + double duration = 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE; + Timeline::animate(AnimationListTag(name + "_linearTranslation"), new ATranslateToPoint(_players[targetId], Vec3D{buf[0], buf[1], buf[2]}, duration, Animation::LoopOut::None, Animation::InterpolationType::linear)); + _players[targetId]->setHealth(buf[3]); _players[targetId]->rotateToAngle(Vec3D{0, buf[4], 0}); diff --git a/Shooter.cpp b/Shooter.cpp index 33df19f..9840951 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -98,15 +98,6 @@ void Shooter::start() { client = std::make_shared(player); server = std::make_shared(); - // windows init: - mainMenu.title("Main menu"); - mainMenu.setBackgroundTexture(ShooterConsts::MAIN_MENU_BACK, 1.1, 1.1, screen->width(), screen->height()); - - mainMenu.addButton(screen->width()/2, 200, 200, 20, [this] () { this->play(); SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);}, "Play", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); - mainMenu.addButton(screen->width()/2, 350, 200, 20, [this] () { this->player->translateToPoint(Vec3D{0, 0, 0}); this->player->setVelocity({}); this->play(); SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);}, "Respawn", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); - - mainMenu.addButton(screen->width()/2, 500, 200, 20, [this] () { client->disconnect(); server->stop(); this->exit();}, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); - // connecting to the server InitNetwork(); // Waiting for connect and updating server if it's same window @@ -122,6 +113,16 @@ void Shooter::start() { inGame = false; server->stop(); } + + // windows init: + mainMenu.title("Main menu"); + mainMenu.setBackgroundTexture(ShooterConsts::MAIN_MENU_BACK, 1.1, 1.1, screen->width(), screen->height()); + + mainMenu.addButton(screen->width()/2, 200, 200, 20, [this] () { this->play(); SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);}, "Server: " + client->ip().toString(), 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); + mainMenu.addButton(screen->width()/2, 350, 200, 20, [this] () { this->player->translateToPoint(Vec3D{0, 0, 0}); this->player->setVelocity({}); this->play(); SoundController::playSound(SoundTag("click"), ShooterConsts::CLICK_SOUND);}, "Respawn", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); + + mainMenu.addButton(screen->width()/2, 500, 200, 20, [this] () { client->disconnect(); server->stop(); this->exit();}, "Exit", 5, 5, ShooterConsts::MAIN_MENU_GUI, {0, 66}, {0, 86}, {0, 46}, Consts::MEDIUM_FONT, {255, 255, 255}); + } void Shooter::update() { diff --git a/engine/animation/Timeline.h b/engine/animation/Timeline.h index ab1458f..445e956 100644 --- a/engine/animation/Timeline.h +++ b/engine/animation/Timeline.h @@ -21,6 +21,7 @@ public: class Timeline { private: + // TODO: replace Animation* with shared_ptr & check for possible memory leaks std::map> _animations; static Timeline* _instance; diff --git a/engine/network/ClientUDP.cpp b/engine/network/ClientUDP.cpp index 05ec623..370afb3 100644 --- a/engine/network/ClientUDP.cpp +++ b/engine/network/ClientUDP.cpp @@ -28,6 +28,8 @@ bool ClientUDP::isWorking() const void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) { + _ip = ip; + _port = port; sf::Packet packet; packet << MsgType::Connect << Consts::NETWORK_VERSION; _working = _socket.bind(0); diff --git a/engine/network/ClientUDP.h b/engine/network/ClientUDP.h index b21afcd..55258ab 100644 --- a/engine/network/ClientUDP.h +++ b/engine/network/ClientUDP.h @@ -15,6 +15,8 @@ protected: UDPSocket _socket; double _lastBroadcast; bool _working; + sf::Uint16 _port; + sf::IpAddress _ip; bool process(); bool timeout(sf::Uint16 id); @@ -29,6 +31,9 @@ public: void disconnect(); void update(); + [[nodiscard]] sf::IpAddress ip() const { return _ip; } + [[nodiscard]] sf::Uint16 port() const { return _port; } + // virtual functions virtual void updatePacket(){};