From 7509cc1bf285c80d6effe7abcf3c932e44c68379 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 27 Apr 2022 18:37:07 +0200 Subject: [PATCH] feat: add player upgrades --- include/game/Player.h | 12 ++++++------ include/game/PlayerUpgrades.h | 34 ++++++++++++++++++++++++++++++++++ src/game/server/ServerGame.cpp | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 include/game/PlayerUpgrades.h diff --git a/include/game/Player.h b/include/game/Player.h index 1c54f42..f073529 100644 --- a/include/game/Player.h +++ b/include/game/Player.h @@ -4,6 +4,7 @@ #include #include "game/Team.h" +#include "game/PlayerUpgrades.h" namespace td { namespace game { @@ -11,17 +12,16 @@ namespace game { class Player { private: game::TeamColor m_TeamColor; + PlayerUpgrades m_Upgrades; std::uint32_t m_Gold; std::uint32_t m_Exp; std::string m_Name; PlayerID m_ID; - std::uint8_t m_GoldPerSecond; - public: - Player(std::uint8_t id = 0) : m_TeamColor(game::TeamColor::None), m_Gold(0), m_Exp(0), m_ID(id), m_GoldPerSecond(5) {} + Player(std::uint8_t id = 0) : m_TeamColor(game::TeamColor::None), m_Gold(0), m_Exp(0), m_ID(id) {} const std::string& GetName() const { return m_Name; } void SetName(const std::string& name) { m_Name = name; } @@ -29,9 +29,6 @@ public: game::TeamColor GetTeamColor() const { return m_TeamColor; } void SetTeamColor(game::TeamColor teamColor) { m_TeamColor = teamColor; } - std::uint8_t GetGoldPerSecond() const { return m_GoldPerSecond; } - void SetGoldPerSecond(std::uint8_t goldPerSecond) { m_GoldPerSecond = goldPerSecond; } - std::uint32_t GetGold() const { return m_Gold; } void SetGold(std::uint32_t gold) { m_Gold = gold; } void AddGold(std::uint32_t gold) { m_Gold += gold; } @@ -42,6 +39,9 @@ public: void AddExp(std::uint32_t exp) { m_Exp += exp; } void RemoveExp(std::uint32_t exp) { m_Exp -= exp; } + const PlayerUpgrades& getUpgrades() const { return m_Upgrades; } + PlayerUpgrades& getUpgrades() { return m_Upgrades; } + PlayerID GetID() const { return m_ID; } }; diff --git a/include/game/PlayerUpgrades.h b/include/game/PlayerUpgrades.h new file mode 100644 index 0000000..f3dd88c --- /dev/null +++ b/include/game/PlayerUpgrades.h @@ -0,0 +1,34 @@ +#pragma once + +#include "Mobs.h" + +namespace td { +namespace game { + +class PlayerUpgrades { +private: + std::uint8_t m_ClickerLevel; + std::uint8_t m_GoldPerSecond; + std::array(MobType::MOB_COUNT)> m_MobsUpgradeLevel; +public: + static const int MAX_MOB_LEVEL = 5; + static const int MAX_CLICKER_LEVEL = 3; + + PlayerUpgrades() : m_ClickerLevel(1), m_GoldPerSecond(5) {} + + std::uint8_t GetClickerLevel() const { return m_ClickerLevel; } + std::uint8_t GetMobUpgradeLevel(MobType mob) const { return m_MobsUpgradeLevel.at(static_cast(mob)); } + std::uint8_t GetGoldPerSecond() const { return m_GoldPerSecond; } + + + void UpgradeMob(MobType mob) { + std::uint8_t& mobLevel = m_MobsUpgradeLevel.at(static_cast(mob)); + mobLevel = std::min(mobLevel + 1, MAX_MOB_LEVEL); + } + + void UpgradeClicker() { m_ClickerLevel = std::min(m_ClickerLevel + 1, MAX_CLICKER_LEVEL); } + void SetGoldPerSecond(std::uint8_t goldPerSecond) { m_GoldPerSecond = goldPerSecond; } +}; + +} // namespace game +} // namespace td diff --git a/src/game/server/ServerGame.cpp b/src/game/server/ServerGame.cpp index c45d6a8..3250f0d 100644 --- a/src/game/server/ServerGame.cpp +++ b/src/game/server/ServerGame.cpp @@ -42,7 +42,7 @@ void ServerGame::UpdatePlayerStats() { void ServerGame::UpdateGoldMines() { for (auto& pair : m_Server->GetPlayers()) { game::Player* player = &pair.second; - player->AddGold(player->GetGoldPerSecond()); + player->AddGold(player->getUpgrades().GetGoldPerSecond()); // Update player money and exp every second protocol::UpdateMoneyPacket moneyPacket(player->GetGold());