feat: changed timer mecanic and mage tower

This commit is contained in:
2021-10-11 19:09:56 +02:00
parent a6ccdcc7af
commit a62549e93e
3 changed files with 34 additions and 3 deletions

View File

@@ -40,14 +40,17 @@ public:
class Timer { class Timer {
private: private:
std::uint64_t m_Interval; std::uint64_t m_Interval;
std::uint64_t m_InternalTime = 0; std::uint64_t m_InternalTime = 0;
bool m_Waiting = true;
public: public:
Timer() : m_Interval(0) {} Timer() : m_Interval(0) {}
Timer(std::uint64_t interval) : m_Interval(interval) {} Timer(std::uint64_t interval) : m_Interval(interval) {}
bool update(std::uint64_t delta); bool update(std::uint64_t delta);
void wait(); // let update return true on the next tick (if it was not used)
void reset(); void reset();
void setInterval(std::uint64_t newInterval) { m_Interval = newInterval; } void setInterval(std::uint64_t newInterval) { m_Interval = newInterval; }

View File

@@ -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) { void ArcherTower::tick(std::uint64_t delta, World* world) {
if (m_Timer.update(delta)) { if (m_Timer.update(delta)) {
bool wasTowerActive = false;
std::uint8_t arrowsShot = 0; std::uint8_t arrowsShot = 0;
bool explosiveArrows = getLevel().getPath() == TowerPath::Bottom; bool explosiveArrows = getLevel().getPath() == TowerPath::Bottom;
std::uint8_t arrows = explosiveArrows ? 2 : getLevel().getLevel(); std::uint8_t arrows = explosiveArrows ? 2 : getLevel().getLevel();
for (MobPtr mob : world->getMobList()) { for (MobPtr mob : world->getMobList()) {
if (isMobInRange(mob)) { if (isMobInRange(mob)) {
world->OnArrowShot(mob, this); world->OnArrowShot(mob, this);
wasTowerActive = true;
arrowsShot++; arrowsShot++;
if (arrowsShot >= arrows) if (arrowsShot >= arrows)
break; break;
} }
} }
if (!wasTowerActive)
m_Timer.wait();
} }
} }
void IceTower::tick(std::uint64_t delta, World* world) { void IceTower::tick(std::uint64_t delta, World* world) {
if (m_Timer.update(delta)) { if (m_Timer.update(delta)) {
float damage = getStats()->getDamage(); float damage = getStats()->getDamage();
bool wasTowerActive = false;
for (MobPtr mob : world->getMobList()) { for (MobPtr mob : world->getMobList()) {
if (isMobInRange(mob)) { if (isMobInRange(mob)) {
mob->addEffect(EffectType::Slowness, 1); // slowness for 1s every second mob->addEffect(EffectType::Slowness, 1); // slowness for 1s every second
mob->damage(damage); mob->damage(damage);
wasTowerActive = true;
} }
} }
if (!wasTowerActive)
m_Timer.wait();
} }
} }
void MageTower::tick(std::uint64_t delta, World* world) { 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) { void PoisonTower::tick(std::uint64_t delta, World* world) {

View File

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