From 56ec9e7fe2daeef3773aa532fb9cbf72247af21b Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 5 Nov 2021 18:54:21 +0100 Subject: [PATCH] feat: add upgrade tower (non gui) --- include/game/World.h | 1 + include/game/client/Client.h | 1 + include/game/client/WorldClient.h | 1 + include/game/server/ServerConnexion.h | 1 + include/protocol/PacketHandler.h | 1 + include/protocol/Protocol.h | 19 +++++++++++++++++++ src/game/Towers.cpp | 6 ++++-- src/game/World.cpp | 16 +++++++++++----- src/game/client/Client.cpp | 5 +++++ src/game/client/WorldClient.cpp | 6 ++++++ src/game/server/ServerConnexion.cpp | 7 +++++++ src/protocol/PacketFactory.cpp | 1 + src/protocol/Protocol.cpp | 15 +++++++++++++++ 13 files changed, 73 insertions(+), 7 deletions(-) diff --git a/include/game/World.h b/include/game/World.h index 908f0a2..ffe5251 100644 --- a/include/game/World.h +++ b/include/game/World.h @@ -185,6 +185,7 @@ public: const Team& getTeam(TeamColor team) const; const TowerList& getTowers() const { return m_Towers; }; + TowerPtr getTowerById(TowerID tower); // Archer Tower virtual void OnArrowShot(MobPtr target, Tower* shooter); diff --git a/include/game/client/Client.h b/include/game/client/Client.h index 96cba66..f68852b 100644 --- a/include/game/client/Client.h +++ b/include/game/client/Client.h @@ -42,6 +42,7 @@ public: void selectTeam(game::TeamColor team); void sendMobs(const std::vector& mobSends); + void upgradeTower(game::TowerID tower, game::TowerLevel level); }; } // namespace client diff --git a/include/game/client/WorldClient.h b/include/game/client/WorldClient.h index a583209..c9121e6 100644 --- a/include/game/client/WorldClient.h +++ b/include/game/client/WorldClient.h @@ -17,6 +17,7 @@ public: virtual void HandlePacket(protocol::WorldBeginDataPacket* packet) override; virtual void HandlePacket(protocol::WorldDataPacket* packet) override; virtual void HandlePacket(protocol::SpawnMobPacket* packet) override; + virtual void HandlePacket(protocol::UpgradeTowerPacket* packet) override; virtual void OnArrowShot(game::MobPtr target, game::Tower* shooter) override; }; diff --git a/include/game/server/ServerConnexion.h b/include/game/server/ServerConnexion.h index 27bb654..e97015b 100644 --- a/include/game/server/ServerConnexion.h +++ b/include/game/server/ServerConnexion.h @@ -39,6 +39,7 @@ public: virtual void HandlePacket(protocol::DisconnectPacket* packet); virtual void HandlePacket(protocol::PlaceTowerPacket* packet); virtual void HandlePacket(protocol::SendMobsPacket* packet); + virtual void HandlePacket(protocol::UpgradeTowerPacket* packet); std::uint8_t getID() const { return m_ID; } const game::Player* getPlayer() const { return m_Player; } diff --git a/include/protocol/PacketHandler.h b/include/protocol/PacketHandler.h index 74034f5..5470a41 100644 --- a/include/protocol/PacketHandler.h +++ b/include/protocol/PacketHandler.h @@ -37,6 +37,7 @@ public: virtual void HandlePacket(WorldAddTowerPacket* packet) {} virtual void HandlePacket(WorldRemoveTowerPacket* packet) {} virtual void HandlePacket(SendMobsPacket* packet) {} + virtual void HandlePacket(UpgradeTowerPacket* packet) {} }; } // namespace protocol diff --git a/include/protocol/Protocol.h b/include/protocol/Protocol.h index 93b7851..fc4a36c 100644 --- a/include/protocol/Protocol.h +++ b/include/protocol/Protocol.h @@ -36,6 +36,7 @@ enum class PacketType : std::uint8_t { // client <--> server KeepAlive, Disconnect, + UpgradeTower, }; struct WorldHeader { @@ -490,6 +491,24 @@ public: virtual PacketType getType() const { return PacketType::WorldRemoveTower; } }; +class UpgradeTowerPacket : public Packet { +private: + game::TowerID m_TowerID; + game::TowerLevel m_TowerLevel; +public: + UpgradeTowerPacket() {} + UpgradeTowerPacket(game::TowerID tower, game::TowerLevel level) : m_TowerID(tower), m_TowerLevel(level) {} + virtual ~UpgradeTowerPacket() {} + + virtual DataBuffer Serialize() const; + virtual void Deserialize(DataBuffer& data); + virtual void Dispatch(PacketHandler* handler); + + game::TowerID getTowerID() const { return m_TowerID; } + game::TowerLevel getTowerLevel() const { return m_TowerLevel; } + + virtual PacketType getType() const { return PacketType::UpgradeTower; } +}; } } \ No newline at end of file diff --git a/src/game/Towers.cpp b/src/game/Towers.cpp index 98e988d..5ab0809 100644 --- a/src/game/Towers.cpp +++ b/src/game/Towers.cpp @@ -109,7 +109,9 @@ const std::map, TowerStats> TowerConstants = { }; const TowerStats* getTowerStats(TowerType type, TowerLevel level) { - return &TowerConstants.at({ type, level }); + auto it = TowerConstants.find({type, level}); + if(it == TowerConstants.end()) return nullptr; + return &it->second; } @@ -238,7 +240,7 @@ void MageTower::tick(std::uint64_t delta, World* world) { bool wasTowerActive = false; for (MobPtr mob : world->getMobList()) { if (isMobInRange(mob)) { - mob->addEffect(EffectType::Fire, getLevel().getLevel() * 5, this); + mob->addEffect(EffectType::Fire, getLevel().getLevel() * 3, this); wasTowerActive = true; } } diff --git a/src/game/World.cpp b/src/game/World.cpp index 4f4850f..2b638e7 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -202,7 +202,7 @@ bool World::CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID playerID) co for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y); - if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower || getTower({worldPos.x + x, worldPos.y + y}) != nullptr) { + if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower || getTower({ worldPos.x + x, worldPos.y + y }) != nullptr) { return false; } } @@ -214,7 +214,7 @@ bool World::CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID playerID) co } bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const { - if(!CanPlaceLittleTower(worldPos, playerID)) return false; + if (!CanPlaceLittleTower(worldPos, playerID)) return false; TilePtr tile = getTile(worldPos.x, worldPos.y); const Player& player = m_Game->getPlayers()[playerID]; @@ -230,7 +230,7 @@ bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const for (int x = -2; x < 3; x++) { for (int y = -2; y < 3; y++) { game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y); - if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower || getTower({worldPos.x + x, worldPos.y + y}) != nullptr) { + if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower || getTower({ worldPos.x + x, worldPos.y + y }) != nullptr) { return false; } } @@ -259,7 +259,7 @@ void World::cleanDeadMobs() { } } -TowerPtr World::getTower(const glm::vec2& position) const{ +TowerPtr World::getTower(const glm::vec2& position) const { for (TowerPtr tower : m_Towers) { if (tower->getSize() == TowerSize::Big) { if (tower->getX() - 2 <= position.x && tower->getX() + 3 >= position.x && @@ -276,13 +276,19 @@ TowerPtr World::getTower(const glm::vec2& position) const{ return nullptr; } +TowerPtr World::getTowerById(TowerID towerID) { + auto it = std::find_if(m_Towers.begin(), m_Towers.end(), [towerID](TowerPtr tower) { return tower->getID() == towerID;}); + if(it == m_Towers.end()) return nullptr; + return *it; +} + void World::OnArrowShot(MobPtr target, Tower* shooter) { bool explosiveArrows = shooter->getLevel().getPath() == TowerPath::Bottom; if (explosiveArrows) { // aoe damage } else { target->damage(shooter->getStats()->getDamage()); - if(target->isDead()) + if (target->isDead()) target->setKillTower(shooter); } } diff --git a/src/game/client/Client.cpp b/src/game/client/Client.cpp index 6063825..c715008 100644 --- a/src/game/client/Client.cpp +++ b/src/game/client/Client.cpp @@ -54,5 +54,10 @@ void Client::sendMobs(const std::vector& mobSends){ m_Connexion.sendPacket(&packet); } +void Client::upgradeTower(game::TowerID tower, game::TowerLevel level){ + protocol::UpgradeTowerPacket packet(tower, level); + m_Connexion.sendPacket(&packet); +} + } // namespace client } // namespace td diff --git a/src/game/client/WorldClient.cpp b/src/game/client/WorldClient.cpp index ed11b9e..69f8004 100644 --- a/src/game/client/WorldClient.cpp +++ b/src/game/client/WorldClient.cpp @@ -25,6 +25,12 @@ void WorldClient::HandlePacket(protocol::SpawnMobPacket* packet) { packet->getMobX(), packet->getMobY(), packet->getMobDirection()); } +void WorldClient::HandlePacket(protocol::UpgradeTowerPacket* packet) { + game::TowerPtr tower = getTowerById(packet->getTowerID()); + if(tower == nullptr) return; // this should not happen but who knows ? + tower->upgrade(packet->getTowerLevel().getLevel(), packet->getTowerLevel().getPath()); +} + void WorldClient::OnArrowShot(game::MobPtr target, game::Tower* tower) { World::OnArrowShot(target, tower); } diff --git a/src/game/server/ServerConnexion.cpp b/src/game/server/ServerConnexion.cpp index 3bf52c8..990f9c6 100644 --- a/src/game/server/ServerConnexion.cpp +++ b/src/game/server/ServerConnexion.cpp @@ -40,6 +40,7 @@ void ServerConnexion::registerHandlers() { GetDispatcher()->RegisterHandler(protocol::PacketType::Disconnect, this); GetDispatcher()->RegisterHandler(protocol::PacketType::PlaceTower, this); GetDispatcher()->RegisterHandler(protocol::PacketType::SendMobs, this); + GetDispatcher()->RegisterHandler(protocol::PacketType::UpgradeTower, this); } bool ServerConnexion::updateSocket() { @@ -173,6 +174,12 @@ void ServerConnexion::HandlePacket(protocol::SendMobsPacket* packet) { } } +void ServerConnexion::HandlePacket(protocol::UpgradeTowerPacket* packet){ + //TODO: verify the packet + + m_Server->broadcastPacket(packet); +} + ServerConnexion::~ServerConnexion() { if (GetDispatcher() != nullptr) GetDispatcher()->UnregisterHandler(this); diff --git a/src/protocol/PacketFactory.cpp b/src/protocol/PacketFactory.cpp index 1fd53ce..f7d32bd 100644 --- a/src/protocol/PacketFactory.cpp +++ b/src/protocol/PacketFactory.cpp @@ -30,6 +30,7 @@ static std::map packets = { {PacketType::WorldAddTower, []() -> Packet* {return new WorldAddTowerPacket(); } }, {PacketType::WorldRemoveTower, []() -> Packet* {return new WorldRemoveTowerPacket(); } }, {PacketType::SendMobs, []() -> Packet* {return new SendMobsPacket(); } }, + {PacketType::UpgradeTower, []() -> Packet* {return new UpgradeTowerPacket(); } }, }; Packet* createPacket(PacketType type, DataBuffer& buffer) { diff --git a/src/protocol/Protocol.cpp b/src/protocol/Protocol.cpp index c82d0f7..406a0f5 100644 --- a/src/protocol/Protocol.cpp +++ b/src/protocol/Protocol.cpp @@ -514,6 +514,20 @@ void SendMobsPacket::Deserialize(DataBuffer& data) { } } +DataBuffer UpgradeTowerPacket::Serialize() const { + DataBuffer data; + data << getID() << m_TowerID << m_TowerLevel.getLevel() << m_TowerLevel.getPath(); + return data; +} + +void UpgradeTowerPacket::Deserialize(DataBuffer& data) { + std::uint8_t towerLevel; + game::TowerPath towerPath; + data >> m_TowerID >> towerLevel >> towerPath; + m_TowerLevel.setLevel(towerLevel); + m_TowerLevel.setPath(towerPath); +} + REGISTER_DISPATCH_CLASS(PlayerLoginPacket); REGISTER_DISPATCH_CLASS(WorldBeginDataPacket); REGISTER_DISPATCH_CLASS(WorldDataPacket); @@ -535,6 +549,7 @@ REGISTER_DISPATCH_CLASS(PlaceTowerPacket); REGISTER_DISPATCH_CLASS(WorldAddTowerPacket); REGISTER_DISPATCH_CLASS(WorldRemoveTowerPacket); REGISTER_DISPATCH_CLASS(SendMobsPacket); +REGISTER_DISPATCH_CLASS(UpgradeTowerPacket); } // namespace protocol } // namespace td \ No newline at end of file