Skip to content

Commit

Permalink
Deathmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sebimih13 committed Jan 29, 2025
1 parent dcde870 commit 92d84a5
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 22 deletions.
9 changes: 8 additions & 1 deletion DeadZone/source/Client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ void Client::handleReceivedPacket()
// clientName
Game::get().updateRemotePlayerClientName(clientKey, playerData["clientName"].get<std::string>());

// team
Game::get().updateRemotePlayerTeam(clientKey, playerData["team"].get<int>());

// outfitColor
glm::vec3 outfitColor = glm::vec3(
playerData["outfitColor"]["x"].get<double>(),
Expand Down Expand Up @@ -218,6 +221,7 @@ void Client::handleReceivedPacket()
0.3, 0.3,
bulletData["textureName2D"].get<std::string>(),
0.0,
clientKey,
1.0,
bulletData["damage"].get<double>(),
15.0,
Expand All @@ -233,7 +237,8 @@ void Client::handleReceivedPacket()
bulletData["speed"].get<double>(),
0.3, 0.3,
bulletData["textureName2D"].get<std::string>(),
bulletData["damage"].get<double>()
bulletData["damage"].get<double>(),
clientKey
));
}
}
Expand Down Expand Up @@ -325,6 +330,8 @@ void Client::handleReceivedPacket()
// self
if (jsonData.contains("player"))
{
Player::get().setTeam(jsonData["player"]["team"].get<int>());

Player::get().setOutfitColor(glm::vec3(
jsonData["player"]["outfitColor"]["x"].get<double>(),
jsonData["player"]["outfitColor"]["y"].get<double>(),
Expand Down
47 changes: 47 additions & 0 deletions DeadZone/source/CollisionManager/CollisionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ void CollisionManager::handleCollisions(std::vector<std::shared_ptr<Entity>>& en
if (!std::dynamic_pointer_cast<CollidableEntity>(entities[i])->getCollisionActive())
continue;

if (std::dynamic_pointer_cast<Bullet>(entities[i]))
{
continue;
}

glm::vec2 overlap = Player::get().isInCollision(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]));

if (overlap.x > 0.0 && overlap.y > 0.0)
Expand All @@ -102,6 +107,48 @@ void CollisionManager::handleCollisions(std::vector<std::shared_ptr<Entity>>& en
std::dynamic_pointer_cast<CollidableEntity>(entities[i])->onCollide(Player::get(), overlap);
}
}

// Player vs. Bullets
for (int i = 0; i < entities.size(); ++i)
{
if (std::dynamic_pointer_cast<Bullet>(entities[i]) == nullptr)
{
continue;
}

if (Game::get().getGameMode() == Game::GameMode::TeamDeathMatch)
{
if (std::dynamic_pointer_cast<Bullet>(entities[i])->getOwner() != "player")
{
std::string bulletOwner = std::dynamic_pointer_cast<Bullet>(entities[i])->getOwner();
int bulletTeam = Game::get().getRemotePlayerTeam(bulletOwner);

glm::vec2 overlap = Player::get().isInCollision(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]));
if (overlap.x > 0.0 && overlap.y > 0.0)
{
if (Player::get().getTeam() != bulletTeam)
{
Player::get().onCollide(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]), overlap);
std::dynamic_pointer_cast<CollidableEntity>(entities[i])->onCollide(Player::get(), overlap);
}
else
{
// Player::get().onCollide(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]), overlap);
std::dynamic_pointer_cast<CollidableEntity>(entities[i])->onCollide(Player::get(), overlap);
}
}
}
}
else
{
glm::vec2 overlap = Player::get().isInCollision(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]));
if (overlap.x > 0.0 && overlap.y > 0.0)
{
Player::get().onCollide(*std::dynamic_pointer_cast<CollidableEntity>(entities[i]), overlap);
std::dynamic_pointer_cast<CollidableEntity>(entities[i])->onCollide(Player::get(), overlap);
}
}
}
}

// Other entities vs. everything
Expand Down
5 changes: 3 additions & 2 deletions DeadZone/source/Entity/Bullet/Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include "../../GlobalClock/GlobalClock.h"
#include "BulletBlast.h"

Bullet::Bullet(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage)
Bullet::Bullet(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage, const std::string& owner)
: Entity(x, y, drawWidth, drawHeight, rotateAngle, speed)
, CollidableEntity(x, y, drawWidth, drawHeight, rotateAngle, speed, collideWidth, collideHeight)
, TexturableEntity(x, y, drawWidth, drawHeight, rotateAngle, speed, textureName2D), damage(damage)
, TexturableEntity(x, y, drawWidth, drawHeight, rotateAngle, speed, textureName2D)
, damage(damage), owner(owner)
{

}
Expand Down
4 changes: 3 additions & 1 deletion DeadZone/source/Entity/Bullet/Bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ class Bullet : public virtual CollidableEntity, public virtual TexturableEntity
{
protected:
double damage;
std::string owner;

public:
Bullet(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage);
Bullet(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage, const std::string& owner);
virtual ~Bullet() = default;

virtual void onCollide(CollidableEntity& other, glm::vec2 overlap) override;
virtual void update() override;

inline double getDamage() const { return this->damage; }
inline std::string getOwner() const { return owner; }
};

4 changes: 2 additions & 2 deletions DeadZone/source/Entity/Bullet/ThrownGrenade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

const double ThrownGrenade::maximumSizeIncreaseAnimation = 3.0;

ThrownGrenade::ThrownGrenade(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage, double timeUntilExplosion, double explosionDamage, double explosionScale, double explosionDuration)
ThrownGrenade::ThrownGrenade(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage, const std::string& owner, double timeUntilExplosion, double explosionDamage, double explosionScale, double explosionDuration)
: Entity(x, y, drawWidth, drawHeight, rotateAngle, speed)
, CollidableEntity(x, y, drawWidth, drawHeight, rotateAngle, speed, collideWidth, collideHeight)
, TexturableEntity(x, y, drawWidth, drawHeight, rotateAngle, speed, textureName2D)
, Bullet(x, y, drawWidth, drawHeight, rotateAngle, speed, collideWidth, collideHeight, textureName2D, damage)
, Bullet(x, y, drawWidth, drawHeight, rotateAngle, speed, collideWidth, collideHeight, textureName2D, damage, owner)
, timeUntilExplosion(timeUntilExplosion), explosionDamage(explosionDamage), timeThrown(GlobalClock::get().getCurrentTime())
, originalDrawWidth(drawWidth), originalDrawHeight(drawHeight), originalCollideWidth(collideWidth), originalCollideHeight(collideHeight)
, explosionScale(explosionScale), explosionDuration(explosionDuration)
Expand Down
2 changes: 1 addition & 1 deletion DeadZone/source/Entity/Bullet/ThrownGrenade.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ThrownGrenade : public virtual Bullet
double explosionDuration;

public:
ThrownGrenade(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage, double timeUntilExplosion, double explosionDamage, double explosionScale, double explosionDurating);
ThrownGrenade(double x, double y, double drawWidth, double drawHeight, double rotateAngle, double speed, double collideWidth, double collideHeight, const std::string& textureName2D, double damage, const std::string& owner, double timeUntilExplosion, double explosionDamage, double explosionScale, double explosionDurating);
virtual ~ThrownGrenade();

virtual void update() override;
Expand Down
2 changes: 1 addition & 1 deletion DeadZone/source/Entity/Human/Human.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void Human::onCollide(CollidableEntity& other, glm::vec2 overlap)
}
}
else if (dynamic_cast<Bullet*>(&other) != nullptr)
{
{
if (overlap.x < overlap.y)
{
if (this->x < other.getX())
Expand Down
13 changes: 7 additions & 6 deletions DeadZone/source/Entity/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ Player::Player(double x, double y, double drawWidth, double drawHeight, double r
// Load player save.json
load();

if (Map::get().getHasBeenLoaded())
{
const std::pair<int, int> pos = Map::getRandomAccesiblePosition();
this->y = pos.first + 0.5f;
this->x = pos.second + 0.5f;
}
// TODO: repara
//if (Map::get().getHasBeenLoaded())
//{
// const std::pair<int, int> pos = Map::getRandomAccesiblePosition();
// this->y = pos.first + 0.5f;
// this->x = pos.second + 0.5f;
//}
}

Player& Player::get()
Expand Down
2 changes: 1 addition & 1 deletion DeadZone/source/Entity/Player/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Player : public virtual Human // singleton
inline std::map<Weapon::WeaponType, double> getBulletPrices() const { return this->bulletPrices; }
inline double getBulletPrice(Weapon::WeaponType weaponType) { return this->bulletPrices[weaponType]; }

inline int getTeam() { return team; }
inline int getTeam() const { return team; }
inline void setTeam(int _team) { team = _team; }

static void deleteInstance();
Expand Down
12 changes: 6 additions & 6 deletions DeadZone/source/Entity/Weapon/Weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,17 @@ void Weapon::onClick()
{
case WeaponType::REVOLVER:
SoundManager::get().play("revolver_01", false);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 10.0, 0.3, 0.3, "bullet0", this->damage);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 10.0, 0.3, 0.3, "bullet0", this->damage, "player");
break;

case WeaponType::SHOTGUN:
SoundManager::get().play("shotgun_01", false);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 10.0, 0.3, 0.3, "bullet1", this->damage);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 10.0, 0.3, 0.3, "bullet1", this->damage, "player");
break;

case WeaponType::AK47:
SoundManager::get().play("ak47_01", false);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 20.0, 0.3, 0.3, "bullet3", this->damage);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 20.0, 0.3, 0.3, "bullet3", this->damage, "player");
break;

case WeaponType::M4:
Expand Down Expand Up @@ -246,13 +246,13 @@ void Weapon::onClick()
break;
}

bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 20.0, 0.3, 0.3, "bullet3", this->damage);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 20.0, 0.3, 0.3, "bullet3", this->damage, "player");
}
break;

case WeaponType::MINIGUN:
SoundManager::get().play("minigun_01", false);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 20.0, 0.3, 0.3, "bullet0", this->damage);
bullet = std::make_shared<Bullet>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 20.0, 0.3, 0.3, "bullet0", this->damage, "player");
break;

case WeaponType::GRENADE:
Expand All @@ -272,7 +272,7 @@ void Weapon::onClick()
break;
}

bullet = std::make_shared<ThrownGrenade>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 3.0, 0.3, 0.3, "grenade0", 0.0, 1.0, this->damage, 15.0, 1.0); // durata aruncare grenada, damage, scale explozie si durata explozie (ultimii 4 parametrii)
bullet = std::make_shared<ThrownGrenade>(static_cast<double>(bulletLocation.x), static_cast<double>(bulletLocation.y), 0.3, 0.3, Player::get().getRotateAngle(), 3.0, 0.3, 0.3, "grenade0", 0.0, "player", 1.0, this->damage, 15.0, 1.0); // durata aruncare grenada, damage, scale explozie si durata explozie (ultimii 4 parametrii)

if (this->numBullets == 0)
{
Expand Down
16 changes: 16 additions & 0 deletions DeadZone/source/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ void Game::updateRemotePlayerClientName(const std::string& clientKey, const std:
remotePlayers[clientKey]->setClientName(name);
}

void Game::updateRemotePlayerTeam(const std::string& clientKey, int newTeam)
{
remotePlayers[clientKey]->setTeam(newTeam);
}

void Game::updateRemotePlayerOutfitColor(const std::string& clientKey, const glm::vec3& color)
{
remotePlayers[clientKey]->setOutfitColor(color);
Expand Down Expand Up @@ -462,6 +467,17 @@ void Game::applyRemotePlayerCloseRangeDamage(const std::string& clientKey, doubl
}
}

int Game::getRemotePlayerTeam(const std::string& clientKey)
{
if (remotePlayers.find(clientKey) == remotePlayers.end())
{
std::cout << "Could not find the remote player - " << clientKey << std::endl;
return -1;
}

return remotePlayers[clientKey]->getTeam();
}

void Game::establishConnection()
{
// Load JSON
Expand Down
3 changes: 3 additions & 0 deletions DeadZone/source/Game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ class Game
void spawnRemotePlayer(const std::string& clientKey);
void removeRemotePlayer(const std::string& clientKey);
void updateRemotePlayerClientName(const std::string& clientKey, const std::string& name);
void updateRemotePlayerTeam(const std::string& clientKey, int newTeam);
void updateRemotePlayerOutfitColor(const std::string& clientKey, const glm::vec3& color);
void updateRemotePlayerPosition(const std::string& clientKey, double x, double y);
void updateRemotePlayerRotateAngle(const std::string& clientKey, double angle);
void updateRemotePlayerStatuses(const std::string& clientKey, const std::vector<AnimatedEntity::EntityStatus>& statuses);
void applyRemotePlayerCloseRangeDamage(const std::string& clientKey, double damage, double shortRangeAttackRadius);

int getRemotePlayerTeam(const std::string& clientKey);

inline bool getIsServer() const { return isServer; }
void establishConnection();
void stopConnection();
Expand Down
6 changes: 5 additions & 1 deletion DeadZone/source/Server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ void Server::handleReceivedPacket()
0.3, 0.3,
jsonData["bullet"]["textureName2D"].get<std::string>(),
0.0,
clientKey,
1.0,
jsonData["bullet"]["damage"].get<double>(),
15.0,
Expand All @@ -233,7 +234,8 @@ void Server::handleReceivedPacket()
jsonData["bullet"]["speed"].get<double>(),
0.3, 0.3,
jsonData["bullet"]["textureName2D"].get<std::string>(),
jsonData["bullet"]["damage"].get<double>()
jsonData["bullet"]["damage"].get<double>(),
clientKey
);
}
}
Expand Down Expand Up @@ -419,6 +421,7 @@ void Server::update()
if (connectedClient.second.updateSelf)
{
nlohmann::json jsonData;
jsonData["player"]["team"] = connectedClient.second.remotePlayerData.getTeam();
jsonData["player"]["outfitColor"]["x"] = connectedClient.second.remotePlayerData.getOutfitColor().x;
jsonData["player"]["outfitColor"]["y"] = connectedClient.second.remotePlayerData.getOutfitColor().y;
jsonData["player"]["outfitColor"]["z"] = connectedClient.second.remotePlayerData.getOutfitColor().z;
Expand Down Expand Up @@ -449,6 +452,7 @@ void Server::update()

// remotePlayers
jsonData["remotePlayers"][otherConnectedClient.first]["clientName"] = otherConnectedClient.second.remotePlayerData.getClientName();
jsonData["remotePlayers"][otherConnectedClient.first]["team"] = otherConnectedClient.second.remotePlayerData.getTeam();
jsonData["remotePlayers"][otherConnectedClient.first]["outfitColor"]["x"] = otherConnectedClient.second.remotePlayerData.getOutfitColor().x;
jsonData["remotePlayers"][otherConnectedClient.first]["outfitColor"]["y"] = otherConnectedClient.second.remotePlayerData.getOutfitColor().y;
jsonData["remotePlayers"][otherConnectedClient.first]["outfitColor"]["z"] = otherConnectedClient.second.remotePlayerData.getOutfitColor().z;
Expand Down

0 comments on commit 92d84a5

Please sign in to comment.