diff --git a/build-debug/sfml-test b/build-debug/sfml-test index 1e3af72..608749c 100755 Binary files a/build-debug/sfml-test and b/build-debug/sfml-test differ diff --git a/build-debug/test_map b/build-debug/test_map new file mode 100644 index 0000000..6a50c37 --- /dev/null +++ b/build-debug/test_map @@ -0,0 +1,8 @@ +size +5 5 +map +0 0 0 0 0 +0 0 1 0 0 +0 0 1 1 0 +1 1 2 2 2 +0 0 0 0 0 diff --git a/src/cell.cpp b/src/cell.cpp index 3797fd3..95de48c 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -19,7 +19,7 @@ sf::Color Cell::color() const noexcept return cell_color; } -void Cell::setHeight(coordinate shift_by_y) +void Cell::setHeightShift(coordinate shift_by_y) { height_shift = shift_by_y; } @@ -46,6 +46,11 @@ bool PassableCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } +CellPtr PassableCell::getDefaultInstance() const +{ + return std::make_unique(); +} + /////////////////////////////////////// WaterCell::WaterCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : @@ -64,12 +69,19 @@ bool WaterCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return false; } +CellPtr WaterCell::getDefaultInstance() const +{ + return std::make_unique(); +} + /////////////////////////////////////// WallCell::WallCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : - Cell(cell_row, cell_col, color), - height_shift(10) // walls are a bit higher than ground and water -{} + Cell(cell_row, cell_col, color) +{ + // walls are a bit higher than ground and water + height_shift = 10; +} WallCell::~WallCell() {} @@ -82,13 +94,19 @@ bool WallCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return false; } +CellPtr WallCell::getDefaultInstance() const +{ + return std::make_unique(); +} + /////////////////////////////////////// ChargeCell::ChargeCell(coordinate cell_row, coordinate cell_col, int has_charges, const sf::Color &color) : Cell(cell_row, cell_col, color), - cell_charges(has_charges), - height_shift(5) // charges are a bit higher than ground and water, but lower than walls -{} + cell_charges(has_charges) +{ + height_shift = 5; // charges are a bit higher than ground and water, but lower than walls +} ChargeCell::~ChargeCell() {} @@ -102,6 +120,11 @@ bool ChargeCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } +CellPtr ChargeCell::getDefaultInstance() const +{ + return std::make_unique(); +} + /////////////////////////////////////// ExitCell::ExitCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : @@ -120,6 +143,11 @@ bool ExitCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } +CellPtr ExitCell::getDefaultInstance() const +{ + return std::make_unique(); +} + /////////////////////////////////////// TeleportCell::TeleportCell(coordinate cell_row, coordinate cell_col, coordinate new_cell_row, coordinate new_cell_col, const sf::Color &color) : @@ -146,13 +174,21 @@ void TeleportCell::setDestination(coordinate new_cell_row, coordinate new_cell_c new_col = new_cell_col; } +CellPtr TeleportCell::getDefaultInstance() const +{ + return std::make_unique(); +} + + /////////////////////////////////////// TriggerCell::TriggerCell(/*std::vector &&cells_to_change,*/ coordinate cell_row, coordinate cell_col, const sf::Color &color) : - Cell(cell_row, cell_col, color), - height_shift(5) // triggers are a bit higher than ground and water, but lower than walls + Cell(cell_row, cell_col, color) { //cells = std::move(cells_to_change); + + // triggers are a bit higher than ground and water, but lower than walls + height_shift = 5; } TriggerCell::~TriggerCell() @@ -177,3 +213,8 @@ bool TriggerCell::onMovingTo(HeroPtr &hero, LevelPtr &level) // It's an impassable object, so player can't move to here. return false; } + +CellPtr TriggerCell::getDefaultInstance() const +{ + return std::make_unique(); +} diff --git a/src/cell.h b/src/cell.h index e15f232..3228fa7 100644 --- a/src/cell.h +++ b/src/cell.h @@ -18,7 +18,7 @@ const sf::Color Green = sf::Color( 0, 255, 0); const sf::Color Red = sf::Color(250, 0, 0); const sf::Color Purple = sf::Color(128, 0, 128); const sf::Color Pink = sf::Color(255, 192, 203); - +const sf::Color Black = sf::Color( 0, 0, 0); } enum CELL_TYPES { @@ -41,7 +41,7 @@ class Cell; using HeroPtr = std::unique_ptr; using LevelPtr = std::unique_ptr; -using CellPtr = std::unique_ptr +using CellPtr = std::unique_ptr; /////////////////////////////////////// @@ -68,6 +68,8 @@ public: /// Determine if Hero can move onto this cell or not virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) = 0; + + virtual CellPtr getDefaultInstance() const = 0; }; /////////////////////////////////////// @@ -83,6 +85,8 @@ public: virtual ~PassableCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; + + virtual CellPtr getDefaultInstance() const override; }; /////////////////////////////////////// @@ -98,6 +102,8 @@ public: virtual ~WaterCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; + + virtual CellPtr getDefaultInstance() const override; }; /////////////////////////////////////// @@ -113,6 +119,8 @@ public: virtual ~WallCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; + + virtual CellPtr getDefaultInstance() const override; }; /////////////////////////////////////// @@ -132,6 +140,8 @@ public: virtual ~ChargeCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; + + virtual CellPtr getDefaultInstance() const override; }; /////////////////////////////////////// @@ -147,6 +157,8 @@ public: virtual ~ExitCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; + + virtual CellPtr getDefaultInstance() const override; }; /////////////////////////////////////// @@ -169,6 +181,8 @@ public: void setDestination(coordinate new_cell_row, coordinate new_cell_col); virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; + + virtual CellPtr getDefaultInstance() const override; }; /////////////////////////////////////// @@ -189,6 +203,8 @@ public: virtual ~TriggerCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; + + virtual CellPtr getDefaultInstance() const override; }; #endif // CELL_H diff --git a/src/game.cpp b/src/game.cpp index 862f38e..ace2be9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -150,7 +150,7 @@ void Game::renderMap() vertical_shift = static_cast(map[y][x]->heightShift()); // If cell has any height value, we should draw walls for it - if (vertical_shift > 0) + /*if (vertical_shift > 0) { // Brush for vertical walls sf::ConvexShape convex_wall_brush; @@ -163,13 +163,13 @@ void Game::renderMap() convex_wall_brush.setPoint(5, sf::Vector2f(cell_width, cell_height)); convex_wall_brush.setOutlineThickness(0); - sf::Color wall_color = convex_wall_brush.getFillColor() - sf::Color(50, 50, 50); + sf::Color wall_color = map[y][x]->color(); convex_wall_brush.setFillColor(wall_color); convex_wall_brush.setPosition(painter_x, painter_y); main_window.draw(convex_wall_brush); - } + }*/ // Draw the top surface of the cell itself diff --git a/src/level.cpp b/src/level.cpp index f073ff3..df23285 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -22,16 +22,16 @@ void Level::readMap(std::ifstream &file) for (coordinate k = 0; k < map[j].size(); ++k) { file >> i; - map[j][k].reset(default_cells[i]); + map[j][k] = default_cells[i]->getDefaultInstance(); map[j][k]->setPosition(j, k); } } } template // [D]erived - [B]ase -unique_ptr static_unique_pointer_cast (unique_ptr&& old) +std::unique_ptr static_unique_pointer_cast (std::unique_ptr&& old) { - return unique_ptr{static_cast(old.release())}; + return std::unique_ptr{static_cast(old.release())}; } Level::Level(const std::string &map_file)