small refactor
parent
0cb7276943
commit
0f29f9c1c6
11
Client.cpp
11
Client.cpp
|
@ -7,6 +7,8 @@
|
|||
#include <utility>
|
||||
#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});
|
||||
|
||||
|
|
19
Shooter.cpp
19
Shooter.cpp
|
@ -98,15 +98,6 @@ void Shooter::start() {
|
|||
client = std::make_shared<Client>(player);
|
||||
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
|
||||
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() {
|
||||
|
|
|
@ -21,6 +21,7 @@ public:
|
|||
|
||||
class Timeline {
|
||||
private:
|
||||
// TODO: replace Animation* with shared_ptr<Animation> & check for possible memory leaks
|
||||
std::map<AnimationListTag, std::list<Animation*>> _animations;
|
||||
|
||||
static Timeline* _instance;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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(){};
|
||||
|
||||
|
|
Loading…
Reference in New Issue