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

@@ -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() {