From adeddee09d94782576d73dbec3e617b3e9513264 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Mon, 29 Nov 2021 16:36:27 +0100 Subject: [PATCH] refactor: mob move lock (Castle) --- include/game/Mobs.h | 12 ++++++++---- src/game/World.cpp | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/game/Mobs.h b/include/game/Mobs.h index a9dea6b..1b007f0 100644 --- a/include/game/Mobs.h +++ b/include/game/Mobs.h @@ -76,7 +76,7 @@ const MobStats* getMobStats(MobType type, std::uint8_t level); const TowerImmunities& getMobTowerImmunities(MobType type, std::uint8_t level); const EffectImmunities& getMobEffectImmunities(MobType type, std::uint8_t level); -class Mob : public utils::shape::Rectangle{ +class Mob : public utils::shape::Rectangle { protected: float m_Health; private: @@ -86,6 +86,7 @@ private: Direction m_Direction; std::vector m_Effects; const Tower* m_LastDamage; // the last tower that damaged the mob + bool m_HasReachedCastle; // if true, no need to walk anymore utils::Timer m_EffectFireTimer; utils::Timer m_EffectPoisonTimer; @@ -93,7 +94,8 @@ private: public: Mob(MobID id, MobLevel level, PlayerID sender) : m_Sender(sender), m_Level(level), - m_EffectFireTimer(1000), m_EffectPoisonTimer(1000), m_EffectHealTimer(1000) { + m_HasReachedCastle(false), m_EffectFireTimer(1000), m_EffectPoisonTimer(1000), + m_EffectHealTimer(1000) { } @@ -112,9 +114,11 @@ public: bool isDead() const { return m_Health <= 0; } bool isAlive() const { return m_Health > 0; } const Tower* getLastDamageTower() { return m_LastDamage; } + bool hasReachedEnemyCastle() { return m_HasReachedCastle; } void damage(float dmg, const Tower* damager) { m_Health = std::max(0.0f, m_Health - dmg); m_LastDamage = damager; } void heal(float heal) { m_Health = std::min(static_cast(getStats()->getMaxLife()), m_Health + heal); } + void setMobReachedCastle() { m_HasReachedCastle = true; } // used when mob is in front of the castle bool isImmuneTo(TowerType type); @@ -126,8 +130,8 @@ public: Direction getDirection() const { return m_Direction; } void setDirection(Direction dir) { m_Direction = dir; } protected: - void initMob() { - m_Health = static_cast(getStats()->getMaxLife()); + void initMob() { + m_Health = static_cast(getStats()->getMaxLife()); setSize(getStats()->getSize().x, getStats()->getSize().y); } private: diff --git a/src/game/World.cpp b/src/game/World.cpp index 4db2540..e4caabe 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -137,6 +137,8 @@ void World::moveMobs(std::uint64_t delta) { mob->setDirection(walkTile->direction); } + if (mob->hasReachedEnemyCastle()) continue; + moveMob(mob, delta); TeamColor mobTeam = m_Game->getPlayerById(mob->getSender()).getTeamColor(); @@ -151,6 +153,7 @@ void World::moveMobs(std::uint64_t delta) { if (isMobTouchingCastle(mob, *enemyCastle)) { moveBackMob(mob, *enemyCastle); + mob->setMobReachedCastle(); } } }