feat: add cooldown timer

This commit is contained in:
2021-11-09 19:18:24 +01:00
parent f71f69cfc2
commit 9a256a4c66
4 changed files with 39 additions and 28 deletions

View File

@@ -93,7 +93,7 @@ private:
TowerLevel m_Level{};
PlayerID m_Builder;
protected:
utils::Timer m_Timer;
utils::CooldownTimer m_Timer;
public:
Tower(TowerID id, TowerType type, std::uint16_t x, std::uint16_t y, PlayerID builder) : m_ID(id), m_Type(type), m_X(x), m_Y(y), m_Builder(builder),
m_Timer(getStats()->getDamageRate() * 1000) { // converting seconds to millis
@@ -107,7 +107,7 @@ public:
void upgrade(std::uint8_t level, TowerPath path) {
m_Level.setLevel(level);
m_Level.setPath(path);
m_Timer.setInterval(getStats()->getDamageRate() * 1000); // converting seconds to millis
m_Timer.setCooldown(getStats()->getDamageRate() * 1000); // converting seconds to millis
m_Timer.reset();
}

View File

@@ -41,21 +41,36 @@ class Timer {
private:
std::uint64_t m_Interval;
std::uint64_t m_InternalTime = 0;
bool m_Waiting = true;
public:
Timer() : m_Interval(0) {}
Timer(std::uint64_t interval) : m_Interval(interval) {}
bool update(std::uint64_t delta);
void wait(); // let update return true on the next tick (if it was not used)
void reset();
void setInterval(std::uint64_t newInterval) { m_Interval = newInterval; }
std::uint64_t getInterval() const { return m_Interval; }
};
// utililty class to call function at regular period of time with a cooldown (used for towers)
class CooldownTimer {
private:
std::uint64_t m_Cooldown;
std::uint64_t m_CooldownTime;
public:
CooldownTimer() : m_Cooldown(0), m_CooldownTime(0) {}
CooldownTimer(std::uint64_t cooldown) : m_Cooldown(0), m_CooldownTime(cooldown) {}
bool update(std::uint64_t delta);
void applyCooldown();
void reset();
void setCooldown(std::uint64_t newCooldown) { m_CooldownTime = newCooldown; }
std::uint64_t getCooldown() const { return m_CooldownTime; }
};
} // namespace utils
} // namespace td