Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue104/add block inside map #128

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion external/range-v3
Submodule range-v3 updated 636 files
10 changes: 8 additions & 2 deletions src/game/BombermanGameWorld.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
#include <boost/core/null_deleter.hpp>
#include <experimental/map>

#include <boost/core/null_deleter.hpp>

#include "Bomberman.hpp"
#include "BombermanGameWorld.hpp"
#include "BoundaryWallsPositionsGenerator.hpp"
#include "DefaultBombFactory.hpp"
#include "FieldSize.hpp"
#include "HumanPlayerSfml.hpp"
#include "InsideWallsPositionsGenerator.hpp"
#include "LimitedBombLauncher.hpp"
#include "WallPositionsGeneratorComposite.hpp"

BombermanGameWorld::BombermanGameWorld(
std::unique_ptr<physics::PhysicsEngine> a,
std::unique_ptr<graphics::renderer_pool> b, const math::Size2u& map_size)
: gen(std::make_unique<BoundaryWallsPositionsGenerator>()),
: gen(std::make_unique<wall_positions_generator_composite>(
wall_positions_generator_composite::Generators{
std::make_shared<inside_walls_positions_generator>(),
std::make_shared<BoundaryWallsPositionsGenerator>()})),
simpleMap{*a, *gen, *b, map_size}, ppool{std::move(a)}, rpool{
std::move(b)}

Expand Down
2 changes: 1 addition & 1 deletion src/game/BoundaryWallsPositionsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "FieldSize.hpp"

BoundaryWallsPositionsGenerator::Walls
BoundaryWallsPositionsGenerator::generate_boundary_walls(
BoundaryWallsPositionsGenerator::generate_walls(
BoundarySize boundary_size) const
{
if (zero_size(boundary_size))
Expand Down
2 changes: 1 addition & 1 deletion src/game/BoundaryWallsPositionsGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class BoundaryWallsPositionsGenerator : public WallPositionsGenerator
{
public:
Walls generate_boundary_walls(BoundarySize) const;
Walls generate_walls(BoundarySize) const;

private:
static bool zero_size(BoundarySize);
Expand Down
29 changes: 7 additions & 22 deletions src/game/BoundaryWallsPositionsGenerator.ut.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "range/v3/all.hpp"

#include "BoundaryWallsPositionsGenerator.hpp"
#include "FieldSize.hpp"
#include "WallsPositionGeneratorCommon.ut.hpp"
#include "gtest/gtest.h"

using namespace ::testing;

class BoundaryWallsPositionsGeneratorTest : public Test
{
public:
BoundaryWallsPositionsGenerator generator;

WallPositionsGenerator::Walls
create_expected_walls(const WallPositionsGenerator::Walls& raw_walls)
{
const auto scale_with_field_size = [](const auto& wall) {
return WallPositionsGenerator::Wall{
{wall.first.first * field_size.width,
wall.first.second * field_size.height},
{wall.second.first * field_size.width,
wall.second.second * field_size.height}};
};
return raw_walls | ranges::view::transform(scale_with_field_size);
};
};

TEST_F(BoundaryWallsPositionsGeneratorTest, For0SizeShouldReturnNowWalls)
{
ASSERT_THAT(generator.generate_boundary_walls({0, 0}), IsEmpty());
ASSERT_THAT(generator.generate_walls({0, 0}), IsEmpty());
}

TEST_F(BoundaryWallsPositionsGeneratorTest, For1SizeShouldReturnOneWall)
{
const WallPositionsGenerator::Walls expected_walls{
{{0, 0}, {field_size.width, field_size.height}}};

ASSERT_THAT(generator.generate_boundary_walls({1, 1}),
ASSERT_THAT(generator.generate_walls({1, 1}),
UnorderedElementsAreArray(expected_walls));
}

Expand All @@ -49,7 +34,7 @@ TEST_F(BoundaryWallsPositionsGeneratorTest, For5SizeShouldReturnWalls)
{{4, 0}, {1, 5}},
{{0, 4}, {5, 1}}});

ASSERT_THAT(generator.generate_boundary_walls({5, 5}),
ASSERT_THAT(generator.generate_walls({5, 5}),
UnorderedElementsAreArray(expected_walls));
}

Expand All @@ -61,7 +46,7 @@ TEST_F(BoundaryWallsPositionsGeneratorTest, For2SizeShouldReturnWalls)
{{1, 0}, {1, 2}},
{{0, 1}, {2, 1}}})};

ASSERT_THAT(generator.generate_boundary_walls({2, 2}),
ASSERT_THAT(generator.generate_walls({2, 2}),
UnorderedElementsAreArray(expected_walls));
}

Expand All @@ -73,6 +58,6 @@ TEST_F(BoundaryWallsPositionsGeneratorTest, ForDifferentEdgesShouldReturnWalls)
{{5, 0}, {1, 8}},
{{0, 7}, {6, 1}}})};

ASSERT_THAT(generator.generate_boundary_walls({6, 8}),
ASSERT_THAT(generator.generate_walls({6, 8}),
UnorderedElementsAreArray(expected_walls));
}
10 changes: 7 additions & 3 deletions src/game/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
find_package(SFML ${SFML_MINIMAL_VERSION} COMPONENTS window REQUIRED)

add_library(game
Bomberman.cpp
BombermanGameWorld.cpp
BoundaryWallsPositionsGenerator.cpp
DefaultBombFactory.cpp
ExplosionRange.cpp
HumanPlayerSfml.cpp
InsideWallsPositionsGenerator.cpp
LimitedBombLauncher.cpp
SimpleMap.cpp
TimeBomb.cpp
DefaultBombFactory.cpp
Bomberman.cpp
WallPositionsGeneratorComposite.cpp
)

target_include_directories(game SYSTEM
Expand All @@ -21,13 +23,15 @@ target_link_libraries(game
)

add_executable(game-ut
Bomberman.ut.cpp
BoundaryWallsPositionsGenerator.ut.cpp
Entity.ut.cpp
InsideWallsPositionsGenerator.ut.cpp
LimitedBombLauncher.ut.cpp
Map.ut.cpp
SimpleMap.ut.cpp
TimeBomb.ut.cpp
Bomberman.ut.cpp
WallPositionsGeneratorComposite.ut.cpp
)


Expand Down
31 changes: 31 additions & 0 deletions src/game/InsideWallsPositionsGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>

#include "FieldSize.hpp"
#include "InsideWallsPositionsGenerator.hpp"
#include "range/v3/algorithm.hpp"
#include "range/v3/range/conversion.hpp"
#include "range/v3/view.hpp"

inside_walls_positions_generator::Walls
inside_walls_positions_generator::generate_walls(
BoundarySize boundary_size) const
{
constexpr auto border = 2u;
constexpr auto first_wall = border;
constexpr auto last_wall = border;
const auto last_wall_x =
boundary_size.width >= 5 ? boundary_size.width - last_wall : border;
const auto last_wall_y =
boundary_size.height >= 5 ? boundary_size.height - last_wall : border;

using namespace ranges;
auto xs = views::ints(first_wall, last_wall_x) | views::stride(2);
auto ys = views::ints(first_wall, last_wall_y) | views::stride(2);
return views::transform(xs,
[&](auto x) {
return views::transform(ys, [x](auto y) {
return Wall{{x, y}, {1, 1}};
});
}) |
views::join | to<std::vector>();
}
11 changes: 11 additions & 0 deletions src/game/InsideWallsPositionsGenerator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "WallPositionsGenerator.hpp"

class inside_walls_positions_generator : public WallPositionsGenerator
{
public:
Walls generate_walls(BoundarySize) const;

private:
};
48 changes: 48 additions & 0 deletions src/game/InsideWallsPositionsGenerator.ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "gmock/gmock.h"

#include "InsideWallsPositionsGenerator.hpp"
#include "WallsPositionGeneratorCommon.ut.hpp"
#include "gtest/gtest.h"
#include "range/v3/all.hpp"

using namespace ::testing;

class inside_walls_positions_generator_test : public Test
{
public:
inside_walls_positions_generator generator;
};

TEST_F(inside_walls_positions_generator_test,
givenSizeBelow5_ShouldNotGenerateAnyWalls)
{
ASSERT_THAT(generator.generate_walls({0, 1}), IsEmpty());
ASSERT_THAT(generator.generate_walls({2, 3}), IsEmpty());
ASSERT_THAT(generator.generate_walls({4, 4}), IsEmpty());
}

TEST_F(inside_walls_positions_generator_test, given5_shouldReturnOneWall)
{
const auto expected_walls = create_expected_one_square_walls({{2, 2}});
ASSERT_THAT(generator.generate_walls({5, 5}), Eq(expected_walls));
}

TEST_F(inside_walls_positions_generator_test, given6_shuoldReturnOneWall)
{
const auto expected_walls = create_expected_one_square_walls({{2, 2}});
ASSERT_THAT(generator.generate_walls({6, 6}), Eq(expected_walls));
}

TEST_F(inside_walls_positions_generator_test, given7And8_shuoldReturnFourWalls)
{
const auto expected_walls =
create_expected_one_square_walls({{2, 2}, {2, 4}, {4, 2}, {4, 4}});
ASSERT_THAT(generator.generate_walls({7, 8}), Eq(expected_walls));
}

TEST_F(inside_walls_positions_generator_test, given7And6_shuoldReturnTwoWalls)
{
const auto expected_walls =
create_expected_one_square_walls({{2, 2}, {4, 2}});
ASSERT_THAT(generator.generate_walls({7, 6}), Eq(expected_walls));
}
2 changes: 1 addition & 1 deletion src/game/SimpleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SimpleMap::SimpleMap(physics::PhysicsEngine& pEngine,
math::Position2f{0.0f, 0.0f}, "resources/map.png"));

for (const auto& generated_walls :
generated_walls_generator.generate_boundary_walls(map_size))
generated_walls_generator.generate_walls(map_size))
{
physics_ids.push_back(physics_engine.register_colider(
{static_cast<float>(generated_walls.second.first),
Expand Down
10 changes: 5 additions & 5 deletions src/game/SimpleMap.ut.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include <vector>

#include "FieldSize.hpp"
#include "SimpleMap.hpp"
#include "WallPositionsGenerator.mock.hpp"
#include "glm/glm.hpp"
#include "gmock/gmock.h"
#include "graphics/renderer_pool.mock.hpp"
#include "physics/PhysicsEngine.mock.hpp"

#include "FieldSize.hpp"
#include "SimpleMap.hpp"
#include "glm/glm.hpp"

using namespace ::testing;

class SimpleMapConstructorExpectations : public Test
Expand All @@ -28,8 +29,7 @@ class SimpleMapConstructorExpectations : public Test
.WillRepeatedly(Invoke([&](const auto id) {
deregistered_physics_ids.push_back(id);
}));
EXPECT_CALL(wall_positions_generator,
generate_boundary_walls(desired_map_size))
EXPECT_CALL(wall_positions_generator, generate_walls(desired_map_size))
.WillOnce(Return(generated_walls));
{
InSequence seq;
Expand Down
3 changes: 2 additions & 1 deletion src/game/WallPositionsGenerator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <exception>
#include <utility>
#include <vector>

Expand All @@ -16,5 +17,5 @@ class WallPositionsGenerator
using Walls = std::vector<Wall>;

virtual ~WallPositionsGenerator() = default;
virtual Walls generate_boundary_walls(BoundarySize) const = 0;
virtual Walls generate_walls(BoundarySize) const = 0;
};
2 changes: 1 addition & 1 deletion src/game/WallPositionsGenerator.mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
class MockWallPositionsGenerator : public WallPositionsGenerator
{
public:
MOCK_CONST_METHOD1(generate_boundary_walls, Walls(BoundarySize));
MOCK_CONST_METHOD1(generate_walls, Walls(BoundarySize));
};
21 changes: 21 additions & 0 deletions src/game/WallPositionsGeneratorComposite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "WallPositionsGeneratorComposite.hpp"
#include "range/v3/action.hpp"
#include "range/v3/algorithm.hpp"
#include "range/v3/range/conversion.hpp"
#include "range/v3/view.hpp"

wall_positions_generator_composite::wall_positions_generator_composite(
Generators g)
: generators{std::move(g)}
{
}

auto wall_positions_generator_composite::generate_walls(BoundarySize size) const
-> Walls
{
using namespace ranges;
return actions::join(generators |
views::transform([&](const auto& generator) {
return generator->generate_walls(size);
}));
}
18 changes: 18 additions & 0 deletions src/game/WallPositionsGeneratorComposite.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <memory>
#include <vector>

#include "WallPositionsGenerator.hpp"

class wall_positions_generator_composite : public WallPositionsGenerator
{
public:
using Generators =
std::vector<std::shared_ptr<const WallPositionsGenerator>>;
wall_positions_generator_composite(Generators);
Walls generate_walls(BoundarySize) const override;

private:
const Generators generators;
};
Loading