2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 25.05.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Client.h"
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
#include <utility>
|
2021-10-09 13:41:12 +03:00
|
|
|
#include "engine/utils/Log.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
#include "Bonus.h"
|
2021-10-25 22:32:55 +03:00
|
|
|
#include "engine/animation/Timeline.h"
|
|
|
|
#include "engine/animation/ATranslateToPoint.h"
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
void Client::updatePacket() {
|
|
|
|
sf::Packet packet;
|
2021-10-26 09:40:35 +03:00
|
|
|
packet << MsgType::ClientUpdate << _player->position().x() << _player->position().y() << _player->position().z() << _player->angle().y() << _player->headAngle() << _player->playerName();
|
2021-09-13 15:53:43 +03:00
|
|
|
_socket.send(packet, _socket.serverId());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::processInit(sf::Packet& packet) {
|
|
|
|
sf::Uint16 targetId;
|
|
|
|
double buf[4];
|
2021-10-26 10:08:41 +03:00
|
|
|
int kills, deaths;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 10:08:41 +03:00
|
|
|
while (packet >> targetId >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> kills >> deaths)
|
2021-09-13 15:53:43 +03:00
|
|
|
{
|
|
|
|
if(targetId != _socket.ownId()) {
|
2021-10-18 18:30:02 +03:00
|
|
|
if(_spawnPlayerCallBack != nullptr)
|
|
|
|
_spawnPlayerCallBack(targetId);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
_players[targetId]->translateToPoint(Vec3D{ buf[0], buf[1], buf[2]});
|
2021-09-13 15:53:43 +03:00
|
|
|
_players[targetId]->setHealth(buf[3]);
|
2021-10-26 10:08:41 +03:00
|
|
|
_players[targetId]->setKills(kills);
|
|
|
|
_players[targetId]->setDeaths(deaths);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::processUpdate(sf::Packet& packet) {
|
|
|
|
sf::Uint16 targetId;
|
|
|
|
double buf[6];
|
2021-10-26 09:40:35 +03:00
|
|
|
std::string playerName;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-26 09:40:35 +03:00
|
|
|
while (packet >> targetId >> buf[0] >> buf[1] >> buf[2] >> buf[3] >> buf[4] >> buf[5] >> playerName) {
|
2021-09-13 15:53:43 +03:00
|
|
|
if (_players.count(targetId)) {
|
2021-10-26 09:40:35 +03:00
|
|
|
std::string tagName = "Player_" + std::to_string(targetId);
|
2021-10-25 22:32:55 +03:00
|
|
|
|
|
|
|
// 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;
|
2021-10-26 09:40:35 +03:00
|
|
|
Timeline::animate(AnimationListTag(tagName + "_linearTranslation"), new ATranslateToPoint(_players[targetId], Vec3D{buf[0], buf[1], buf[2]}, duration, Animation::LoopOut::None, Animation::InterpolationType::linear));
|
2021-10-25 22:32:55 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
_players[targetId]->setHealth(buf[3]);
|
2021-10-12 17:12:47 +03:00
|
|
|
_players[targetId]->rotateToAngle(Vec3D{0, buf[4], 0});
|
2021-10-26 09:40:35 +03:00
|
|
|
_players[targetId]->setPlayerName(playerName);
|
2021-09-14 13:47:53 +03:00
|
|
|
|
2021-10-18 18:30:02 +03:00
|
|
|
auto head = _players[targetId]->attached(ObjectNameTag("head"));
|
|
|
|
auto weapon = _players[targetId]->attached(ObjectNameTag("Weapon"));
|
|
|
|
|
|
|
|
head->rotateLeft(buf[5] - _players[targetId]->headAngle());
|
|
|
|
weapon->rotateLeft(-(buf[5] - _players[targetId]->headAngle()));
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
_players[targetId]->setHeadAngle(buf[5]);
|
|
|
|
} else if (targetId == _socket.ownId()) {
|
|
|
|
_player->setHealth(buf[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::processNewClient(sf::Packet& packet) {
|
|
|
|
sf::Uint16 targetId;
|
|
|
|
|
|
|
|
packet >> targetId;
|
|
|
|
|
2021-10-18 18:30:02 +03:00
|
|
|
if(_spawnPlayerCallBack != nullptr)
|
|
|
|
_spawnPlayerCallBack(targetId);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::processDisconnect(sf::Uint16 targetId) {
|
|
|
|
if (targetId != _socket.ownId() && _players.count(targetId)) {
|
|
|
|
_players.erase(targetId);
|
2021-10-02 20:36:07 +03:00
|
|
|
_removePlayerCallBack(targetId);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Client::processCustomPacket(MsgType type, sf::Packet& packet) {
|
|
|
|
sf::Uint16 buffId[2];
|
|
|
|
double dbuff[10];
|
|
|
|
std::string tmp, tmp2;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MsgType::Kill:
|
|
|
|
packet >> buffId[0] >> buffId[1];
|
2021-10-26 09:50:59 +03:00
|
|
|
_lastEvent = "";
|
|
|
|
if(buffId[1] == _socket.ownId()) {
|
|
|
|
_player->addKill();
|
|
|
|
SoundController::playSound(SoundTag("kill"), ShooterConsts::KILL_SOUND);
|
|
|
|
_lastEvent += _player->playerName();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_players[buffId[1]]->addKill();
|
|
|
|
_lastEvent += _players[buffId[1]]->playerName();
|
|
|
|
}
|
|
|
|
_lastEvent += " kill ";
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
if(buffId[0] == _socket.ownId()) {
|
|
|
|
_player->addDeath();
|
|
|
|
// respawn
|
2021-10-12 17:12:47 +03:00
|
|
|
_player->translateToPoint(Vec3D{50.0*(-1 + 2.0*(double)rand()/RAND_MAX),30.0*(double)rand()/RAND_MAX,50.0*(-1 + 2.0*(double)rand()/RAND_MAX)});
|
2021-09-13 15:53:43 +03:00
|
|
|
_player->initWeapons();
|
|
|
|
_player->setFullAbility();
|
2021-10-17 19:38:16 +03:00
|
|
|
SoundController::playSound(SoundTag("death"), ShooterConsts::DEATH_SOUND);
|
2021-10-26 09:50:59 +03:00
|
|
|
_lastEvent += _player->playerName();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
2021-10-26 09:50:59 +03:00
|
|
|
else {
|
2021-09-13 15:53:43 +03:00
|
|
|
_players[buffId[0]]->addDeath();
|
2021-10-26 09:50:59 +03:00
|
|
|
_lastEvent += _players[buffId[0]]->playerName();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MsgType::FireTrace:
|
|
|
|
packet >> dbuff[0] >> dbuff[1] >> dbuff[2] >> dbuff[3] >> dbuff[4] >> dbuff[5];
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
if(_addFireTraceCallBack != nullptr)
|
2021-10-12 17:12:47 +03:00
|
|
|
_addFireTraceCallBack(Vec3D(dbuff[0], dbuff[1], dbuff[2]), Vec3D(dbuff[3], dbuff[4], dbuff[5]));
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
case MsgType::InitBonuses:
|
|
|
|
while (packet >> tmp >> dbuff[0] >> dbuff[1] >> dbuff[2]) {
|
2021-10-02 20:36:07 +03:00
|
|
|
if(_addBonusCallBack != nullptr)
|
2021-10-12 17:12:47 +03:00
|
|
|
_addBonusCallBack(tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2]));
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MsgType::AddBonus:
|
|
|
|
packet >> tmp >> dbuff[0] >> dbuff[1] >> dbuff[2];
|
2021-10-02 20:36:07 +03:00
|
|
|
if(_addBonusCallBack != nullptr)
|
2021-10-12 17:12:47 +03:00
|
|
|
_addBonusCallBack(tmp, Vec3D(dbuff[0], dbuff[1], dbuff[2]));
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
case MsgType::RemoveBonus:
|
|
|
|
packet >> tmp;
|
2021-10-02 20:36:07 +03:00
|
|
|
if(_removeBonusCallBack != nullptr)
|
2021-10-17 10:21:10 +03:00
|
|
|
_removeBonusCallBack(ObjectNameTag(tmp));
|
2021-09-13 15:53:43 +03:00
|
|
|
break;
|
2021-10-18 18:30:02 +03:00
|
|
|
case MsgType::ChangeWeapon:
|
|
|
|
packet >> buffId[0] >> tmp;
|
|
|
|
|
|
|
|
if(_changeEnemyWeaponCallBack != nullptr)
|
|
|
|
_changeEnemyWeaponCallBack(tmp, buffId[0]);
|
|
|
|
break;
|
2021-10-17 10:21:10 +03:00
|
|
|
default:
|
2021-10-17 10:44:17 +03:00
|
|
|
return;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::processDisconnected() {
|
|
|
|
for (auto it = _players.begin(); it != _players.end();) {
|
2021-10-02 20:36:07 +03:00
|
|
|
processDisconnect(it++->first);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::damagePlayer(sf::Uint16 targetId, double damage) {
|
|
|
|
sf::Packet packet;
|
|
|
|
|
|
|
|
packet << MsgType::Damage << targetId << damage;
|
2021-10-18 18:30:02 +03:00
|
|
|
_socket.sendRely(packet, _socket.serverId());
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
Log::log("Client: damagePlayer " + std::to_string(targetId) + " ( -" + std::to_string(damage) + "hp )");
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Client::addTrace(const Vec3D& from, const Vec3D& to) {
|
2021-09-13 15:53:43 +03:00
|
|
|
sf::Packet packet;
|
|
|
|
|
|
|
|
packet << MsgType::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) {
|
|
|
|
sf::Packet packet;
|
|
|
|
|
|
|
|
packet << MsgType::RemoveBonus << bonusName;
|
2021-10-18 18:30:02 +03:00
|
|
|
_socket.sendRely(packet, _socket.serverId());
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
if(_removeBonusCallBack != nullptr)
|
2021-10-17 10:21:10 +03:00
|
|
|
_removeBonusCallBack(ObjectNameTag(bonusName));
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-18 18:30:02 +03:00
|
|
|
void Client::changeWeapon(const std::string &weaponName) {
|
|
|
|
sf::Packet packet;
|
|
|
|
|
|
|
|
packet << MsgType::ChangeWeapon << weaponName;
|
|
|
|
_socket.sendRely(packet, _socket.serverId());
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
void Client::addPlayer(sf::Uint16 id, std::shared_ptr<Player> player) {
|
|
|
|
_players.insert({ id, player });
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::setSpawnPlayerCallBack(std::function<void(sf::Uint16)> spawn) {
|
|
|
|
_spawnPlayerCallBack = std::move(spawn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::setRemovePlayerCallBack(std::function<void(sf::Uint16)> remove) {
|
|
|
|
_removePlayerCallBack = std::move(remove);
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Client::setAddFireTraceCallBack(std::function<void(const Vec3D &, const Vec3D &)> addTrace) {
|
2021-10-02 20:36:07 +03:00
|
|
|
_addFireTraceCallBack = std::move(addTrace);
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Client::setAddBonusCallBack(std::function<void(const std::string &, const Vec3D &)> addBonus) {
|
2021-10-02 20:36:07 +03:00
|
|
|
_addBonusCallBack = std::move(addBonus);
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
void Client::setRemoveBonusCallBack(std::function<void(const ObjectNameTag &)> removeBonus) {
|
2021-10-02 20:36:07 +03:00
|
|
|
_removeBonusCallBack = std::move(removeBonus);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
2021-10-18 18:30:02 +03:00
|
|
|
|
|
|
|
void Client::setChangeEnemyWeaponCallBack(std::function<void(const std::string&, sf::Uint16)> changeEnemyWeapon) {
|
|
|
|
_changeEnemyWeaponCallBack = std::move(changeEnemyWeapon);
|
|
|
|
}
|