feat: implement freeze effect

This commit is contained in:
2021-10-08 19:04:26 +02:00
parent 48b5372533
commit 4fcf25b5bc
5 changed files with 79 additions and 5 deletions

View File

@@ -1,9 +1,53 @@
#include "game/Mobs.h"
#include <map>
#include <algorithm>
namespace td {
namespace game {
bool Mob::isImmuneTo(TowerType type) {
return std::find(getTowerImmunities().begin(), getTowerImmunities().end(), type) != getTowerImmunities().end();
}
bool Mob::isImmuneTo(EffectType type) {
return std::find(getEffectImmunities().begin(), getEffectImmunities().end(), type) != getEffectImmunities().end();
}
void Mob::addEffect(EffectType effectType, float durationSec) {
if (isImmuneTo(effectType))
return;
if (hasEffect(effectType)) {
auto it = std::find_if(m_Effects.begin(), m_Effects.end(), [&effectType](EffectDuration effect){ return effect.first == effectType;});
EffectDuration& effect = *it;
if(effect.second < durationSec)
effect.second = durationSec; // setting new duration if it's greater then the actual
} else {
m_Effects.push_back({ effectType, durationSec });
}
}
void Mob::tick(std::uint64_t delta) {
updateEffects(delta);
}
void Mob::updateEffects(std::uint64_t delta) {
float deltaSec = (float)delta / 1000.0f;
for (auto it = m_Effects.begin(); it != m_Effects.end(); it++) {
EffectDuration& effect = *it;
effect.second -= deltaSec;
if (effect.second < 0) // effect has gone
m_Effects.erase(it);
}
}
bool Mob::hasEffect(EffectType type) {
return std::find_if(m_Effects.begin(), m_Effects.end(), [&type](EffectDuration effect){ return effect.first == type;}) != m_Effects.end();
}
typedef std::pair<MobType, std::uint8_t> MobKey;
const std::map<MobKey, MobStats> MobConstants = {