Skip to content

Commit

Permalink
Multiplayer kills
Browse files Browse the repository at this point in the history
  • Loading branch information
sebimih13 committed Jan 30, 2025
1 parent f42ac0f commit d42ceab
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions DeadZone/source/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ void Client::handleReceivedPacket()
));
}

// goldRewarded
if (jsonData.contains("goldRewarded"))
{
Player::get().addGold(jsonData["goldRewarded"].get<int>());
}

enet_packet_destroy(this->eNetEvent.packet);
}

Expand Down Expand Up @@ -538,3 +544,11 @@ void Client::sendDisconnect()
enet_host_flush(this->client);
}
}

void Client::sendConfirmedKill(const std::string& clientKey)
{
nlohmann::json jsonData;
jsonData["confirmedKill"] = clientKey;

this->sendMessageUnsafe(jsonData.dump(), this->lastTimeSentPing);
}
1 change: 1 addition & 0 deletions DeadZone/source/Client/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Client
void sendOpenedDoor(int id);
void sendCloseRangeDamage(const double damage, const double shortRangeAttackRadius);
void sendDisconnect();
void sendConfirmedKill(const std::string& clientKey);

// Getters
inline bool getWorkingServerConnection() const { return this->workingServerConnection; }
Expand Down
15 changes: 15 additions & 0 deletions DeadZone/source/CollisionManager/CollisionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../Entity/Enemy/Enemy.h"
#include "../Entity/Bullet/ThrownGrenade.h"
#include "../Entity/Explosion/Explosion.h"
#include "../Client/Client.h"

#include <iostream>
#include <memory>
Expand Down Expand Up @@ -128,8 +129,15 @@ void CollisionManager::handleCollisions(std::vector<std::shared_ptr<Entity>>& en
{
if (Player::get().getTeam() != bulletTeam)
{
bool notDeadBefore = !Player::get().isDead();

Player::get().onCollide(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]), overlap);
std::dynamic_pointer_cast<CollidableEntity>(entities[i])->onCollide(Player::get(), overlap);

if (Player::get().isDead() && notDeadBefore)
{
Client::get().sendConfirmedKill(bulletOwner);
}
}
else
{
Expand Down Expand Up @@ -169,8 +177,15 @@ void CollisionManager::handleCollisions(std::vector<std::shared_ptr<Entity>>& en
{
if (Player::get().getTeam() != explosionTeam)
{
bool notDeadBefore = !Player::get().isDead();

Player::get().onCollide(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]), overlap);
std::dynamic_pointer_cast<CollidableEntity>(entities[i])->onCollide(Player::get(), overlap);

if (Player::get().isDead() && notDeadBefore)
{
Client::get().sendConfirmedKill(explosionOwner);
}
}
else
{
Expand Down
1 change: 1 addition & 0 deletions DeadZone/source/Entity/Player/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class Player : public virtual Human // singleton
inline int getGold() const { return this->gold; }
inline int getGoldCap() const { return this->goldCap; }

inline void addGold(int gold) { this->gold += gold; }
inline void setGold(int gold) { this->gold = gold; }

inline bool getInteractUsed() const { return this->interactUsed; }
Expand Down
14 changes: 14 additions & 0 deletions DeadZone/source/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

const glm::vec3 Server::COLOR_TEAM_1 = glm::vec3(0.0f, 0.0f, 1.0f);
const glm::vec3 Server::COLOR_TEAM_2 = glm::vec3(1.0f, 0.0f, 0.0f);
const int Server::GOLD_PER_KILL = 100;

ReplicatedSound::ReplicatedSound(const std::string& name, bool paused)
: name(name)
Expand Down Expand Up @@ -274,6 +275,19 @@ void Server::handleReceivedPacket()
disconnectPlayer(clientKey);
}

if (jsonData.contains("confirmedKill"))
{
std::string clientKeyKiller = jsonData["confirmedKill"].get<std::string>();

if (connectedClients.find(clientKeyKiller) != connectedClients.end())
{
nlohmann::json killJsonData;
killJsonData["goldRewarded"] = GOLD_PER_KILL;

connectedClients[clientKeyKiller].sendMessageUnsafe(killJsonData.dump());
}
}

// TODO: de pus in if-uri? excluzand "ping"
updateClients = true;

Expand Down
1 change: 1 addition & 0 deletions DeadZone/source/Server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Server
int sizeTeam2;
static const glm::vec3 COLOR_TEAM_1;
static const glm::vec3 COLOR_TEAM_2;
static const int GOLD_PER_KILL;

// Atentie aici la unicitatea cheii
inline std::string getClientKey(const ENetAddress& address) const { return std::to_string(address.host) + ":" + std::to_string(address.port); }
Expand Down

0 comments on commit d42ceab

Please sign in to comment.