diff --git a/include/misc/Time.h b/include/misc/Time.h index 55bbefa..24b4ef3 100644 --- a/include/misc/Time.h +++ b/include/misc/Time.h @@ -40,14 +40,17 @@ public: 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; } diff --git a/src/game/Towers.cpp b/src/game/Towers.cpp index 63214bd..e62b545 100644 --- a/src/game/Towers.cpp +++ b/src/game/Towers.cpp @@ -168,34 +168,52 @@ TowerPtr createTower(TowerType type, TowerID id, std::int32_t x, std::int32_t y, 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; } } + 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); // slowness for 1s every second mob->damage(damage); + wasTowerActive = true; } } + 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()); // slowness for 1s every second + wasTowerActive = true; + } + } + if (!wasTowerActive) + m_Timer.wait(); + } } void PoisonTower::tick(std::uint64_t delta, World* world) { diff --git a/src/misc/Time.cpp b/src/misc/Time.cpp index bdda573..d1ca9c5 100644 --- a/src/misc/Time.cpp +++ b/src/misc/Time.cpp @@ -30,6 +30,11 @@ 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; @@ -38,8 +43,13 @@ bool Timer::update(std::uint64_t delta) { return false; } +void Timer::wait() { + m_Waiting = true; +} + void Timer::reset() { - m_InternalTime = 0; + m_InternalTime = 0; // let the timer active once at the beginning + m_Waiting = true; } std::uint64_t getTime() {