small refactor

master
Vectozavr 2021-10-26 02:32:55 +07:00
parent 0cb7276943
commit 0f29f9c1c6
5 changed files with 28 additions and 10 deletions

View File

@ -7,6 +7,8 @@
#include <utility> #include <utility>
#include "engine/utils/Log.h" #include "engine/utils/Log.h"
#include "Bonus.h" #include "Bonus.h"
#include "engine/animation/Timeline.h"
#include "engine/animation/ATranslateToPoint.h"
void Client::updatePacket() { void Client::updatePacket() {
sf::Packet packet; 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]) { while (packet >> targetId >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> buf[4] >> buf[5]) {
if (_players.count(targetId)) { if (_players.count(targetId)) {
std::string name = "Player_" + std::to_string(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]->setHealth(buf[3]);
_players[targetId]->rotateToAngle(Vec3D{0, buf[4], 0}); _players[targetId]->rotateToAngle(Vec3D{0, buf[4], 0});

View File

@ -98,15 +98,6 @@ void Shooter::start() {
client = std::make_shared<Client>(player); client = std::make_shared<Client>(player);
server = std::make_shared<Server>(); server = std::make_shared<Server>();
// 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 // connecting to the server
InitNetwork(); InitNetwork();
// Waiting for connect and updating server if it's same window // Waiting for connect and updating server if it's same window
@ -122,6 +113,16 @@ void Shooter::start() {
inGame = false; inGame = false;
server->stop(); 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() { void Shooter::update() {

View File

@ -21,6 +21,7 @@ public:
class Timeline { class Timeline {
private: private:
// TODO: replace Animation* with shared_ptr<Animation> & check for possible memory leaks
std::map<AnimationListTag, std::list<Animation*>> _animations; std::map<AnimationListTag, std::list<Animation*>> _animations;
static Timeline* _instance; static Timeline* _instance;

View File

@ -28,6 +28,8 @@ bool ClientUDP::isWorking() const
void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port)
{ {
_ip = ip;
_port = port;
sf::Packet packet; sf::Packet packet;
packet << MsgType::Connect << Consts::NETWORK_VERSION; packet << MsgType::Connect << Consts::NETWORK_VERSION;
_working = _socket.bind(0); _working = _socket.bind(0);

View File

@ -15,6 +15,8 @@ protected:
UDPSocket _socket; UDPSocket _socket;
double _lastBroadcast; double _lastBroadcast;
bool _working; bool _working;
sf::Uint16 _port;
sf::IpAddress _ip;
bool process(); bool process();
bool timeout(sf::Uint16 id); bool timeout(sf::Uint16 id);
@ -29,6 +31,9 @@ public:
void disconnect(); void disconnect();
void update(); void update();
[[nodiscard]] sf::IpAddress ip() const { return _ip; }
[[nodiscard]] sf::Uint16 port() const { return _port; }
// virtual functions // virtual functions
virtual void updatePacket(){}; virtual void updatePacket(){};