diff --git a/include/game/World.h b/include/game/World.h index ca3f10d..a4be7d0 100644 --- a/include/game/World.h +++ b/include/game/World.h @@ -194,6 +194,8 @@ public: private: void moveMobs(std::uint64_t delta); void moveMob(MobPtr mob, std::uint64_t delta); + void moveBackMob(MobPtr mob, const TeamCastle& castle); + bool isMobTouchingCastle(MobPtr mob, const TeamCastle& castle) const; void tickMobs(std::uint64_t delta); void cleanDeadMobs(); }; diff --git a/src/game/World.cpp b/src/game/World.cpp index f0034ec..57a6a01 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -111,7 +111,7 @@ void World::tick(std::uint64_t delta) { void World::spawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir) { MobPtr mob = MobFactory::createMob(id, type, level, sender); - mob->setCenter({x, y}); + mob->setCenter({ x, y }); mob->setDirection(dir); m_Mobs.push_back(mob); } @@ -138,6 +138,20 @@ void World::moveMobs(std::uint64_t delta) { } moveMob(mob, delta); + + TeamColor mobTeam = m_Game->getPlayerById(mob->getSender()).getTeamColor(); + + const TeamCastle* enemyCastle; + + if (mobTeam == TeamColor::Red) { + enemyCastle = &getBlueTeam().getCastle(); + } else if (mobTeam == TeamColor::Blue) { + enemyCastle = &getRedTeam().getCastle(); + } + + if (isMobTouchingCastle(mob, *enemyCastle)) { + moveBackMob(mob, *enemyCastle); + } } } @@ -169,6 +183,31 @@ void World::moveMob(MobPtr mob, std::uint64_t delta) { } } +bool World::isMobTouchingCastle(MobPtr mob, const TeamCastle& enemyCastle) const { + return enemyCastle.collidesWith(*mob); +} + +void World::moveBackMob(MobPtr mob, const TeamCastle& enemyCastle) { + switch (mob->getDirection()) { + case Direction::NegativeX: { + mob->setCenterX(enemyCastle.getBottomRight().getX() + mob->getWidth() / 2.0f); + break; + } + case Direction::PositiveX: { + mob->setCenterX(enemyCastle.getTopLeft().getX() - mob->getWidth() / 2.0f); + break; + } + case Direction::NegativeY: { + mob->setCenterY(enemyCastle.getBottomRight().getY() + mob->getHeight() / 2.0f); + break; + } + case Direction::PositiveY: { + mob->setCenterY(enemyCastle.getTopLeft().getY() - mob->getHeight() / 2.0f); + break; + } + } +} + const Color* World::getTileColor(TilePtr tile) const { switch (tile->getType()) { case TileType::Tower: {