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

View File

@@ -208,51 +208,42 @@ std::string getTowerName(TowerType type) {
void ArcherTower::tick(std::uint64_t delta, World* world) {
if (m_Timer.update(delta)) {
bool wasTowerActive = false;
std::uint8_t arrowsShot = 0;
bool explosiveArrows = getLevel().getPath() == TowerPath::Bottom;
std::uint8_t arrows = explosiveArrows ? 2 : getLevel().getLevel();
for (MobPtr mob : world->getMobList()) {
if (isMobInRange(mob)) {
world->OnArrowShot(mob, this);
wasTowerActive = true;
arrowsShot++;
if (arrowsShot >= arrows)
break;
m_Timer.applyCooldown();
}
}
if (!wasTowerActive)
m_Timer.wait();
}
}
void IceTower::tick(std::uint64_t delta, World* world) {
if (m_Timer.update(delta)) {
float damage = getStats()->getDamage();
bool wasTowerActive = false;
for (MobPtr mob : world->getMobList()) {
if (isMobInRange(mob)) {
mob->addEffect(EffectType::Slowness, 1, this); // slowness for 1s every second
mob->damage(damage, this);
wasTowerActive = true;
m_Timer.applyCooldown();
}
}
if (!wasTowerActive)
m_Timer.wait();
}
}
void MageTower::tick(std::uint64_t delta, World* world) {
if (m_Timer.update(delta)) {
bool wasTowerActive = false;
for (MobPtr mob : world->getMobList()) {
if (isMobInRange(mob)) {
mob->addEffect(EffectType::Fire, getLevel().getLevel() * 3, this);
wasTowerActive = true;
m_Timer.applyCooldown();
}
}
if (!wasTowerActive)
m_Timer.wait();
}
}

View File

@@ -30,11 +30,6 @@ void AutoTimer::reset() {
}
bool Timer::update(std::uint64_t delta) {
if (m_Waiting) {
m_InternalTime = 0;
m_Waiting = false;
return true;
}
m_InternalTime += delta;
if (m_InternalTime >= m_Interval) {
m_InternalTime %= m_Interval;
@@ -43,13 +38,23 @@ bool Timer::update(std::uint64_t delta) {
return false;
}
void Timer::wait() {
m_Waiting = true;
}
void Timer::reset() {
m_InternalTime = 0; // let the timer active once at the beginning
m_Waiting = true;
}
bool CooldownTimer::update(std::uint64_t delta) {
if (m_Cooldown > 0) {
m_Cooldown = std::max(0L, static_cast<std::int64_t>(m_Cooldown - delta));
}
return m_Cooldown == 0;
}
void CooldownTimer::reset() {
m_Cooldown = 0; // let the timer active once at the beginning
}
void CooldownTimer::applyCooldown(){
m_Cooldown = m_CooldownTime;
}
std::uint64_t getTime() {