From 716f0dbe9e1fd85fd9ae77476e6afb5d1ce3e4e9 Mon Sep 17 00:00:00 2001 From: kn65op Date: Thu, 1 Feb 2018 08:41:25 +0100 Subject: [PATCH] Add checking if is bomb at position --- src/game/LimitedBombLauncher.cpp | 11 +++++------ src/game/LimitedBombLauncher.hpp | 2 +- src/game/LimitedBombLauncher.ut.cpp | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/game/LimitedBombLauncher.cpp b/src/game/LimitedBombLauncher.cpp index 714f047..f16e178 100644 --- a/src/game/LimitedBombLauncher.cpp +++ b/src/game/LimitedBombLauncher.cpp @@ -4,14 +4,13 @@ LimitedBombLauncher::LimitedBombLauncher(std::shared_ptr gw, const int maximum_bombs) - : game_world{std::move(gw)}, - max_bombs{maximum_bombs} + : game_world{std::move(gw)}, max_bombs{maximum_bombs} { } -bool LimitedBombLauncher::try_spawn_bomb(const math::Position2) +bool LimitedBombLauncher::try_spawn_bomb(const math::Position2 pos) { - const auto bombCanBeSpawned = can_spawn_bomb(); + const auto bombCanBeSpawned = can_spawn_bomb(pos); if (bombCanBeSpawned) { launch_bomb(); @@ -19,9 +18,9 @@ bool LimitedBombLauncher::try_spawn_bomb(const math::Position2) return bombCanBeSpawned; } -bool LimitedBombLauncher::can_spawn_bomb() const +bool LimitedBombLauncher::can_spawn_bomb(const math::Position2 pos) const { - return max_bombs > bombs; + return max_bombs > bombs && !game_world->is_bomb_at_pos(BombPosition{pos}); } void LimitedBombLauncher::launch_bomb() diff --git a/src/game/LimitedBombLauncher.hpp b/src/game/LimitedBombLauncher.hpp index 53eae8a..6470a29 100644 --- a/src/game/LimitedBombLauncher.hpp +++ b/src/game/LimitedBombLauncher.hpp @@ -19,7 +19,7 @@ class LimitedBombLauncher : public BombLauncher const int max_bombs; int bombs{0}; - bool can_spawn_bomb() const; + bool can_spawn_bomb(math::Position2) const; void launch_bomb(); bool can_retrieve_bomb() const; void retrieve_bomb(); diff --git a/src/game/LimitedBombLauncher.ut.cpp b/src/game/LimitedBombLauncher.ut.cpp index fdc8a19..5b536d3 100644 --- a/src/game/LimitedBombLauncher.ut.cpp +++ b/src/game/LimitedBombLauncher.ut.cpp @@ -9,9 +9,17 @@ using namespace ::fakeit; struct LimitedBombLauncherTest : public ::testing::Test { + LimitedBombLauncherTest() + { + When(Method(game_world, is_bomb_at_pos)).AlwaysReturn(no_bom_at_pos); + } + const math::Position2 default_position{0.2f, 0.2f}; + const BombPosition default_bomb_position{default_position}; const bool bomb_has_been_spawned = true; const bool bomb_cannot_be_spawned = false; + const bool bomb_at_pos = true; + const bool no_bom_at_pos = false; const int max_bombs = 2; Mock game_world; @@ -30,6 +38,16 @@ TEST_F(LimitedBombLauncherWithoutBombsLaunched, ShouldLaunchBomb) ::testing::Eq(bomb_has_been_spawned)); } +TEST_F(LimitedBombLauncherWithoutBombsLaunched, + WhenThereIsBombAtPostition_ShouldNotSpawnBomb) +{ + When(Method(game_world, is_bomb_at_pos).Using(default_bomb_position)) + .Return(bomb_at_pos); + + ASSERT_THAT(launcher.try_spawn_bomb(default_position), + ::testing::Eq(bomb_cannot_be_spawned)); +} + struct LimitedBombLauncherWithAllBombsLunched : public LimitedBombLauncherTest { LimitedBombLauncherWithAllBombsLunched()