refactor: changed exp and money update
This commit is contained in:
@@ -18,12 +18,8 @@ private:
|
|||||||
std::uint8_t m_ID;
|
std::uint8_t m_ID;
|
||||||
|
|
||||||
std::uint8_t m_GoldPerSecond;
|
std::uint8_t m_GoldPerSecond;
|
||||||
|
|
||||||
bool m_GoldChanged;
|
|
||||||
bool m_ExpChanged;
|
|
||||||
public:
|
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), m_GoldPerSecond(5) {}
|
||||||
m_GoldChanged(false), m_ExpChanged(false) {}
|
|
||||||
|
|
||||||
const std::string& getName() const { return m_Name; }
|
const std::string& getName() const { return m_Name; }
|
||||||
void setName(const std::string& name) { m_Name = name; }
|
void setName(const std::string& name) { m_Name = name; }
|
||||||
@@ -35,20 +31,14 @@ public:
|
|||||||
void setGoldPerSecond(std::uint8_t goldPerSecond) { m_GoldPerSecond = goldPerSecond; }
|
void setGoldPerSecond(std::uint8_t goldPerSecond) { m_GoldPerSecond = goldPerSecond; }
|
||||||
|
|
||||||
std::uint32_t getGold() const { return m_Gold; }
|
std::uint32_t getGold() const { return m_Gold; }
|
||||||
void setGold(std::uint32_t gold) { m_GoldChanged = true; m_Gold = gold; }
|
void setGold(std::uint32_t gold) { m_Gold = gold; }
|
||||||
void addGold(std::uint32_t gold) { m_GoldChanged = true; m_Gold += gold; }
|
void addGold(std::uint32_t gold) { m_Gold += gold; }
|
||||||
void removeGold(std::uint32_t gold) { m_GoldChanged = true; m_Gold -= gold; }
|
void removeGold(std::uint32_t gold) { m_Gold -= gold; }
|
||||||
|
|
||||||
std::uint32_t getExp() const { return m_Exp; }
|
std::uint32_t getExp() const { return m_Exp; }
|
||||||
void setExp(std::uint32_t exp) { m_ExpChanged = true; m_Exp = exp; }
|
void setExp(std::uint32_t exp) { m_Exp = exp; }
|
||||||
void addExp(std::uint32_t exp) { m_ExpChanged = true; m_Exp += exp; }
|
void addExp(std::uint32_t exp) { m_Exp += exp; }
|
||||||
void removeExp(std::uint32_t exp) { m_ExpChanged = true; m_Exp -= exp; }
|
void removeExp(std::uint32_t exp) { m_Exp -= exp; }
|
||||||
|
|
||||||
bool hasGoldChanged() const { return m_GoldChanged; }
|
|
||||||
bool hasExpChanged() const { return m_ExpChanged; }
|
|
||||||
|
|
||||||
void updateGold() { m_GoldChanged = false; }
|
|
||||||
void updateExp() { m_ExpChanged = false; }
|
|
||||||
|
|
||||||
std::uint8_t getID() const { return m_ID; }
|
std::uint8_t getID() const { return m_ID; }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -37,25 +37,19 @@ void ServerGame::startGame() {
|
|||||||
|
|
||||||
void ServerGame::updatePlayerStats() {
|
void ServerGame::updatePlayerStats() {
|
||||||
m_GoldMineTimer.update();
|
m_GoldMineTimer.update();
|
||||||
for (auto& pair : m_Server->getPlayers()) {
|
|
||||||
game::Player& player = pair.second;
|
|
||||||
if (player.hasGoldChanged()) {
|
|
||||||
protocol::UpdateMoneyPacket packet(player.getGold());
|
|
||||||
m_Server->getConnexions()[player.getID()].sendPacket(&packet);
|
|
||||||
player.updateGold();
|
|
||||||
}
|
|
||||||
if (player.hasExpChanged()) {
|
|
||||||
protocol::UpdateExpPacket packet(player.getExp());
|
|
||||||
m_Server->getConnexions()[player.getID()].sendPacket(&packet);
|
|
||||||
player.updateExp();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerGame::updateGoldMines() {
|
void ServerGame::updateGoldMines() {
|
||||||
for (auto& pair : m_Server->getPlayers()) {
|
for (auto& pair : m_Server->getPlayers()) {
|
||||||
game::Player* player = &pair.second;
|
game::Player* player = &pair.second;
|
||||||
player->addGold(player->getGoldPerSecond());
|
player->addGold(player->getGoldPerSecond());
|
||||||
|
|
||||||
|
// Update player money and exp every second
|
||||||
|
protocol::UpdateMoneyPacket moneyPacket(player->getGold());
|
||||||
|
m_Server->getConnexions()[player->getID()].sendPacket(&moneyPacket);
|
||||||
|
|
||||||
|
protocol::UpdateExpPacket expPacket(player->getExp());
|
||||||
|
m_Server->getConnexions()[player->getID()].sendPacket(&expPacket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user