diff --git a/CMakeLists.txt b/CMakeLists.txt index 42c8c21..6446980 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,10 +10,10 @@ add_executable(shooter Source.cpp Player.cpp Player.h - Client.cpp - Client.h - Server.cpp - Server.h + ShooterClient.cpp + ShooterClient.h + ShooterServer.cpp + ShooterServer.h weapon/Weapon.cpp weapon/Weapon.h weapon/Ak47.cpp diff --git a/Shooter.cpp b/Shooter.cpp index 972f77d..c02e92e 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -11,7 +11,6 @@ #include "engine/animation/Timeline.h" #include "ShooterConsts.h" #include "engine/SoundController.h" -#include "engine/animation/ATranslateToPoint.h" using namespace std; @@ -60,7 +59,7 @@ void Shooter::InitNetwork() { client->connect(clientIp, clientPort); player->setPlayerNickName(playerName); - // TODO: encapsulate call backs inside Client + // TODO: encapsulate call backs inside ShooterClient client->setSpawnPlayerCallBack([this](sf::Uint16 id){ spawnPlayer(id); }); client->setRemovePlayerCallBack([this](sf::Uint16 id){ removePlayer(id); }); client->setAddFireTraceCallBack([this](const Vec3D& from, const Vec3D& to){ addFireTrace(from, to); }); @@ -98,8 +97,8 @@ void Shooter::start() { world->addBody(player); player->translate(Vec3D{0, 10, 0}); - client = std::make_shared(player); - server = std::make_shared(); + client = std::make_shared(player); + server = std::make_shared(); // connecting to the server InitNetwork(); diff --git a/Shooter.h b/Shooter.h index 23e631f..9041c9e 100644 --- a/Shooter.h +++ b/Shooter.h @@ -11,8 +11,8 @@ #include "PlayerController.h" #include "engine/gui/Window.h" -#include "Client.h" -#include "Server.h" +#include "ShooterClient.h" +#include "ShooterServer.h" class Shooter final : public Engine { @@ -24,8 +24,8 @@ private: Window mainMenu; - std::shared_ptr server; - std::shared_ptr client; + std::shared_ptr server; + std::shared_ptr client; bool inGame = false; diff --git a/Client.cpp b/ShooterClient.cpp similarity index 80% rename from Client.cpp rename to ShooterClient.cpp index bd67a9b..7d9d6eb 100644 --- a/Client.cpp +++ b/ShooterClient.cpp @@ -2,7 +2,7 @@ // Created by Иван Ильин on 25.05.2021. // -#include "Client.h" +#include "ShooterClient.h" #include #include "engine/utils/Log.h" @@ -10,13 +10,13 @@ #include "engine/animation/ATranslateToPoint.h" #include "ShooterMsgType.h" -void Client::updatePacket() { +void ShooterClient::updatePacket() { sf::Packet packet; packet << MsgType::ClientUpdate << _player->position().x() << _player->position().y() << _player->position().z() << _player->angle().y() << _player->headAngle() << _player->playerNickName(); _socket.send(packet, _socket.serverId()); } -void Client::processInit(sf::Packet& packet) { +void ShooterClient::processInit(sf::Packet& packet) { sf::Uint16 targetId; double buf[4]; int kills, deaths; @@ -36,7 +36,7 @@ void Client::processInit(sf::Packet& packet) { } } -void Client::processUpdate(sf::Packet& packet) { +void ShooterClient::processUpdate(sf::Packet& packet) { sf::Uint16 targetId; double buf[6]; std::string playerName; @@ -68,7 +68,7 @@ void Client::processUpdate(sf::Packet& packet) { } } -void Client::processNewClient(sf::Packet& packet) { +void ShooterClient::processNewClient(sf::Packet& packet) { sf::Uint16 targetId; packet >> targetId; @@ -78,7 +78,7 @@ void Client::processNewClient(sf::Packet& packet) { } } -void Client::processDisconnect(sf::Uint16 targetId) { +void ShooterClient::processDisconnect(sf::Uint16 targetId) { if (targetId != _socket.ownId() && _players.count(targetId)) { _players.erase(targetId); _removePlayerCallBack(targetId); @@ -86,7 +86,7 @@ void Client::processDisconnect(sf::Uint16 targetId) { } -void Client::processCustomPacket(sf::Packet& packet) { +void ShooterClient::processCustomPacket(sf::Packet& packet) { sf::Uint16 buffId[2]; double dbuff[10]; std::string tmp, tmp2; @@ -160,34 +160,34 @@ void Client::processCustomPacket(sf::Packet& packet) { } break; default: - Log::log("Client::processCustomPacket: unknown message type " + std::to_string(static_cast(type))); + Log::log("ShooterClient::processCustomPacket: unknown message type " + std::to_string(static_cast(type))); return; } } -void Client::processDisconnected() { +void ShooterClient::processDisconnected() { for (auto it = _players.begin(); it != _players.end();) { processDisconnect(it++->first); } } -void Client::damagePlayer(sf::Uint16 targetId, double damage) { +void ShooterClient::damagePlayer(sf::Uint16 targetId, double damage) { sf::Packet packet; packet << MsgType::Custom << ShooterMsgType::Damage << targetId << damage; _socket.sendRely(packet, _socket.serverId()); - Log::log("Client: damagePlayer " + std::to_string(targetId) + " ( -" + std::to_string(damage) + "hp )"); + Log::log("ShooterClient: damagePlayer " + std::to_string(targetId) + " ( -" + std::to_string(damage) + "hp )"); } -void Client::addTrace(const Vec3D& from, const Vec3D& to) { +void ShooterClient::addTrace(const Vec3D& from, const Vec3D& to) { sf::Packet packet; packet << MsgType::Custom << ShooterMsgType::FireTrace << from.x() << from.y() << from.z() << to.x() << to.y() << to.z(); _socket.send(packet, _socket.serverId()); } -void Client::takeBonus(const std::string& bonusName) { +void ShooterClient::takeBonus(const std::string& bonusName) { sf::Packet packet; packet << MsgType::Custom << ShooterMsgType::RemoveBonus << bonusName; @@ -198,37 +198,37 @@ void Client::takeBonus(const std::string& bonusName) { } } -void Client::changeWeapon(const std::string &weaponName) { +void ShooterClient::changeWeapon(const std::string &weaponName) { sf::Packet packet; packet << MsgType::Custom << ShooterMsgType::ChangeWeapon << weaponName; _socket.sendRely(packet, _socket.serverId()); } -void Client::addPlayer(sf::Uint16 id, std::shared_ptr player) { +void ShooterClient::addPlayer(sf::Uint16 id, std::shared_ptr player) { _players.insert({ id, player }); } -void Client::setSpawnPlayerCallBack(std::function spawn) { +void ShooterClient::setSpawnPlayerCallBack(std::function spawn) { _spawnPlayerCallBack = std::move(spawn); } -void Client::setRemovePlayerCallBack(std::function remove) { +void ShooterClient::setRemovePlayerCallBack(std::function remove) { _removePlayerCallBack = std::move(remove); } -void Client::setAddFireTraceCallBack(std::function addTrace) { +void ShooterClient::setAddFireTraceCallBack(std::function addTrace) { _addFireTraceCallBack = std::move(addTrace); } -void Client::setAddBonusCallBack(std::function addBonus) { +void ShooterClient::setAddBonusCallBack(std::function addBonus) { _addBonusCallBack = std::move(addBonus); } -void Client::setRemoveBonusCallBack(std::function removeBonus) { +void ShooterClient::setRemoveBonusCallBack(std::function removeBonus) { _removeBonusCallBack = std::move(removeBonus); } -void Client::setChangeEnemyWeaponCallBack(std::function changeEnemyWeapon) { +void ShooterClient::setChangeEnemyWeaponCallBack(std::function changeEnemyWeapon) { _changeEnemyWeaponCallBack = std::move(changeEnemyWeapon); } diff --git a/Client.h b/ShooterClient.h similarity index 91% rename from Client.h rename to ShooterClient.h index ae5b5f8..2a29247 100644 --- a/Client.h +++ b/ShooterClient.h @@ -2,13 +2,13 @@ // Created by Иван Ильин on 25.05.2021. // -#ifndef SHOOTER_CLIENT_H -#define SHOOTER_CLIENT_H +#ifndef SHOOTER_SHOOTERCLIENT_H +#define SHOOTER_SHOOTERCLIENT_H #include "engine/network/ClientUDP.h" #include "Player.h" -class Client final : public ClientUDP { +class ShooterClient final : public ClientUDP { private: std::string _lastEvent; @@ -22,7 +22,7 @@ private: std::function _removeBonusCallBack; std::function _changeEnemyWeaponCallBack; public: - explicit Client(std::shared_ptr player) : _player(player){}; + explicit ShooterClient(std::shared_ptr player) : _player(player){}; void updatePacket() override; diff --git a/Server.cpp b/ShooterServer.cpp similarity index 92% rename from Server.cpp rename to ShooterServer.cpp index 5b2c2e0..69481d3 100644 --- a/Server.cpp +++ b/ShooterServer.cpp @@ -2,11 +2,11 @@ // Created by Иван Ильин on 25.05.2021. // -#include "Server.h" +#include "ShooterServer.h" #include "engine/utils/Log.h" #include "ShooterMsgType.h" -void Server::broadcast() { +void ShooterServer::broadcast() { sf::Packet updatePacket; updatePacket << MsgType::ServerUpdate; @@ -20,7 +20,7 @@ void Server::broadcast() { } -void Server::processConnect(sf::Uint16 targetId) { +void ShooterServer::processConnect(sf::Uint16 targetId) { sf::Packet sendPacket1, sendPacket2; sf::Packet extraPacket; @@ -46,7 +46,7 @@ void Server::processConnect(sf::Uint16 targetId) { } -void Server::processClientUpdate(sf::Uint16 senderId, sf::Packet& packet) { +void ShooterServer::processClientUpdate(sf::Uint16 senderId, sf::Packet& packet) { double buf[5]; std::string playerName; @@ -57,7 +57,7 @@ void Server::processClientUpdate(sf::Uint16 senderId, sf::Packet& packet) { _players.at(senderId)->setPlayerNickName(playerName); } -void Server::processDisconnect(sf::Uint16 senderId) { +void ShooterServer::processDisconnect(sf::Uint16 senderId) { sf::Packet sendPacket; sendPacket << MsgType::Disconnect << senderId; @@ -68,7 +68,7 @@ void Server::processDisconnect(sf::Uint16 senderId) { } -void Server::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) { +void ShooterServer::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) { sf::Packet sendPacket; double dbuff[10]; sf::Uint16 targetId; @@ -137,17 +137,17 @@ void Server::processCustomPacket(sf::Packet& packet, sf::Uint16 senderId) { break; default: - Log::log("Server::processCustomPacket: unknown message type " + std::to_string(static_cast(type))); + Log::log("ShooterServer::processCustomPacket: unknown message type " + std::to_string(static_cast(type))); return; } } -void Server::processStop() { +void ShooterServer::processStop() { _players.clear(); _bonuses.clear(); } -void Server::generateBonuses() { +void ShooterServer::generateBonuses() { _bonuses.insert({"Bonus_gun_1", std::make_shared(BonusInfo{Vec3D(-10, -2, -15), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); _bonuses.insert({"Bonus_gun_2", std::make_shared(BonusInfo{Vec3D(10, -2, 15), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); @@ -170,7 +170,7 @@ void Server::generateBonuses() { _bonuses.insert({"Bonus_ability_2", std::make_shared(BonusInfo{Vec3D(-25, 18, 33), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})}); } -void Server::updateInfo() { +void ShooterServer::updateInfo() { for(auto& [bonusName, bonusInfo] : _bonuses) { if(!bonusInfo->onTheMap && std::abs(Time::time() - bonusInfo->lastTake) > ShooterConsts::BONUS_RECHARGE_TIME) { sf::Packet sendPacket; @@ -183,6 +183,6 @@ void Server::updateInfo() { } } -Server::~Server() { +ShooterServer::~ShooterServer() { processStop(); } diff --git a/Server.h b/ShooterServer.h similarity index 83% rename from Server.h rename to ShooterServer.h index 635b079..fd88fc2 100644 --- a/Server.h +++ b/ShooterServer.h @@ -2,8 +2,8 @@ // Created by Иван Ильин on 25.05.2021. // -#ifndef SHOOTER_SERVER_H -#define SHOOTER_SERVER_H +#ifndef SHOOTER_SHOOTERSERVER_H +#define SHOOTER_SHOOTERSERVER_H #include "engine/network/ServerUDP.h" #include "Player.h" @@ -14,12 +14,12 @@ struct BonusInfo final { const bool onTheMap = false; }; -class Server final : public ServerUDP { +class ShooterServer final : public ServerUDP { private: std::map> _players{}; std::map> _bonuses{}; public: - Server() = default; + ShooterServer() = default; void broadcast() override; @@ -35,7 +35,7 @@ public: void updateInfo() override; - ~Server() override; + ~ShooterServer() override; };