Huge refactoring v3
parent
870842b682
commit
875f9f85ec
|
@ -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
|
||||
|
|
|
@ -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<Client>(player);
|
||||
server = std::make_shared<Server>();
|
||||
client = std::make_shared<ShooterClient>(player);
|
||||
server = std::make_shared<ShooterServer>();
|
||||
|
||||
// connecting to the server
|
||||
InitNetwork();
|
||||
|
|
|
@ -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> server;
|
||||
std::shared_ptr<Client> client;
|
||||
std::shared_ptr<ShooterServer> server;
|
||||
std::shared_ptr<ShooterClient> client;
|
||||
|
||||
bool inGame = false;
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
// Created by Иван Ильин on 25.05.2021.
|
||||
//
|
||||
|
||||
#include "Client.h"
|
||||
#include "ShooterClient.h"
|
||||
|
||||
#include <utility>
|
||||
#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<int>(type)));
|
||||
Log::log("ShooterClient::processCustomPacket: unknown message type " + std::to_string(static_cast<int>(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> player) {
|
||||
void ShooterClient::addPlayer(sf::Uint16 id, std::shared_ptr<Player> player) {
|
||||
_players.insert({ id, player });
|
||||
}
|
||||
|
||||
void Client::setSpawnPlayerCallBack(std::function<void(sf::Uint16)> spawn) {
|
||||
void ShooterClient::setSpawnPlayerCallBack(std::function<void(sf::Uint16)> spawn) {
|
||||
_spawnPlayerCallBack = std::move(spawn);
|
||||
}
|
||||
|
||||
void Client::setRemovePlayerCallBack(std::function<void(sf::Uint16)> remove) {
|
||||
void ShooterClient::setRemovePlayerCallBack(std::function<void(sf::Uint16)> remove) {
|
||||
_removePlayerCallBack = std::move(remove);
|
||||
}
|
||||
|
||||
void Client::setAddFireTraceCallBack(std::function<void(const Vec3D &, const Vec3D &)> addTrace) {
|
||||
void ShooterClient::setAddFireTraceCallBack(std::function<void(const Vec3D &, const Vec3D &)> addTrace) {
|
||||
_addFireTraceCallBack = std::move(addTrace);
|
||||
}
|
||||
|
||||
void Client::setAddBonusCallBack(std::function<void(const std::string &, const Vec3D &)> addBonus) {
|
||||
void ShooterClient::setAddBonusCallBack(std::function<void(const std::string &, const Vec3D &)> addBonus) {
|
||||
_addBonusCallBack = std::move(addBonus);
|
||||
}
|
||||
|
||||
void Client::setRemoveBonusCallBack(std::function<void(const ObjectNameTag &)> removeBonus) {
|
||||
void ShooterClient::setRemoveBonusCallBack(std::function<void(const ObjectNameTag &)> removeBonus) {
|
||||
_removeBonusCallBack = std::move(removeBonus);
|
||||
}
|
||||
|
||||
void Client::setChangeEnemyWeaponCallBack(std::function<void(const std::string&, sf::Uint16)> changeEnemyWeapon) {
|
||||
void ShooterClient::setChangeEnemyWeaponCallBack(std::function<void(const std::string&, sf::Uint16)> changeEnemyWeapon) {
|
||||
_changeEnemyWeaponCallBack = std::move(changeEnemyWeapon);
|
||||
}
|
|
@ -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<void(const ObjectNameTag&)> _removeBonusCallBack;
|
||||
std::function<void(const std::string&, sf::Uint16)> _changeEnemyWeaponCallBack;
|
||||
public:
|
||||
explicit Client(std::shared_ptr<Player> player) : _player(player){};
|
||||
explicit ShooterClient(std::shared_ptr<Player> player) : _player(player){};
|
||||
|
||||
void updatePacket() override;
|
||||
|
|
@ -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<int>(type)));
|
||||
Log::log("ShooterServer::processCustomPacket: unknown message type " + std::to_string(static_cast<int>(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>(BonusInfo{Vec3D(-10, -2, -15), -2*ShooterConsts::BONUS_RECHARGE_TIME, true})});
|
||||
_bonuses.insert({"Bonus_gun_2", std::make_shared<BonusInfo>(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>(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();
|
||||
}
|
|
@ -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<sf::Uint16, std::shared_ptr<Player>> _players{};
|
||||
std::map<std::string, std::shared_ptr<BonusInfo>> _bonuses{};
|
||||
public:
|
||||
Server() = default;
|
||||
ShooterServer() = default;
|
||||
|
||||
void broadcast() override;
|
||||
|
||||
|
@ -35,7 +35,7 @@ public:
|
|||
|
||||
void updateInfo() override;
|
||||
|
||||
~Server() override;
|
||||
~ShooterServer() override;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue