diff --git a/src/cell.cpp b/src/cell.cpp index e7ffffd..c50376d 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -35,11 +35,6 @@ bool PassableCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } -Cell *PassableCell::getDefaultInstance() -{ - return new PassableCell(); -} - /////////////////////////////////////// WaterCell::WaterCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : @@ -58,11 +53,6 @@ bool WaterCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return false; } -Cell *WaterCell::getDefaultInstance() -{ - return new WaterCell(); -} - /////////////////////////////////////// WallCell::WallCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : @@ -80,11 +70,6 @@ bool WallCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return false; } -Cell *WallCell::getDefaultInstance() -{ - return new WallCell(); -} - /////////////////////////////////////// ChargeCell::ChargeCell(coordinate cell_row, coordinate cell_col, int has_charges, const sf::Color &color) : @@ -104,11 +89,6 @@ bool ChargeCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } -Cell *ChargeCell::getDefaultInstance() -{ - return new ChargeCell(); -} - /////////////////////////////////////// ExitCell::ExitCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : @@ -127,11 +107,6 @@ bool ExitCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } -Cell *ExitCell::getDefaultInstance() -{ - return new ExitCell(); -} - /////////////////////////////////////// TeleportCell::TeleportCell(coordinate cell_row, coordinate cell_col, coordinate new_cell_row, coordinate new_cell_col, const sf::Color &color) : @@ -152,11 +127,6 @@ bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level) return true; } -Cell *TeleportCell::getDefaultInstance() -{ - return new TeleportCell(); -} - /////////////////////////////////////// TriggerCell::TriggerCell(/*std::vector &&cells_to_change,*/ coordinate cell_row, coordinate cell_col, const sf::Color &color) : @@ -175,20 +145,15 @@ bool TriggerCell::onMovingTo(HeroPtr &hero, LevelPtr &level) Map &map = level->mapArray(); // We replace needed cells with the ones that the trigger provides. - for (Cell *cell : cells) + for (CellPtr &cell : cells) { const coordinate &row = cell->row(); const coordinate &col = cell->col(); - delete map[row][col]; - map[row][col] = cell; + map[row][col].release(); + map[row][col] = std::move(cell); } // It's an impassable object, so player can't move to here. return false; } - -Cell *TriggerCell::getDefaultInstance() -{ - return new TriggerCell(); -} diff --git a/src/cell.h b/src/cell.h index f2b6391..f2c0086 100644 --- a/src/cell.h +++ b/src/cell.h @@ -10,10 +10,15 @@ namespace palette { -const sf::Color Brown = sf::Color(165, 42, 42); -const sf::Color White = sf::Color(255, 255, 255); -const sf::Color Blue = sf::Color(0, 255, 255); -const sf::Color Gray = sf::Color(125, 125, 125); +const sf::Color Brown = sf::Color(165, 42, 42); +const sf::Color White = sf::Color(255, 255, 255); +const sf::Color Blue = sf::Color( 0, 255, 255); +const sf::Color Gray = sf::Color(125, 125, 125); +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); + } enum CELL_TYPES { @@ -36,6 +41,7 @@ class Cell; using HeroPtr = std::unique_ptr; using LevelPtr = std::unique_ptr; +using CellPtr = std::unique_ptr /////////////////////////////////////// @@ -56,8 +62,6 @@ public: /// Determine if Hero can move onto this cell or not virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) = 0; - - virtual Cell *getDefaultInstance() = 0; }; /////////////////////////////////////// @@ -67,14 +71,12 @@ class PassableCell : public Cell { public: PassableCell(coordinate cell_row = 0, - coordinate cell_col = 0, // Brown + coordinate cell_col = 0, const sf::Color &color = palette::Brown); virtual ~PassableCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; - - virtual Cell *getDefaultInstance() override; }; /////////////////////////////////////// @@ -90,8 +92,6 @@ public: virtual ~WaterCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; - - virtual Cell *getDefaultInstance() override; }; /////////////////////////////////////// @@ -101,14 +101,12 @@ class WallCell : public Cell { public: WallCell(coordinate cell_row = 0, - coordinate cell_col = 0, // Gray + coordinate cell_col = 0, const sf::Color &color = palette::Gray); virtual ~WallCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; - - virtual Cell *getDefaultInstance() override; }; /////////////////////////////////////// @@ -123,13 +121,11 @@ public: ChargeCell(coordinate cell_row = 0, coordinate cell_col = 0, int has_charges = 1, - const sf::Color &color = sf::Color::Green); + const sf::Color &color = palette::Green); virtual ~ChargeCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; - - virtual Cell *getDefaultInstance() override; }; /////////////////////////////////////// @@ -140,13 +136,11 @@ class ExitCell : public Cell public: ExitCell(coordinate cell_row = 0, coordinate cell_col = 0, - const sf::Color &color = sf::Color::Red); + const sf::Color &color = palette::Red); virtual ~ExitCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; - - virtual Cell *getDefaultInstance() override; }; /////////////////////////////////////// @@ -161,14 +155,12 @@ public: TeleportCell(coordinate cell_row = 0, coordinate cell_col = 0, coordinate new_cell_row = 0, - coordinate new_cell_col = 0, // Purple - const sf::Color &color = sf::Color(128, 0, 128)); + coordinate new_cell_col = 0, + const sf::Color &color = palette::Purple); virtual ~TeleportCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; - - virtual Cell *getDefaultInstance() override; }; /////////////////////////////////////// @@ -178,19 +170,17 @@ class TriggerCell : public Cell { private: // Vector of cells to place on map - std::vector cells; + std::vector cells; public: TriggerCell(//std::vector &&cells_to_change, coordinate cell_row = 0, - coordinate cell_col = 0, // Pink - const sf::Color &color = sf::Color(255, 192, 203)); + coordinate cell_col = 0, + const sf::Color &color = palette::Pink); virtual ~TriggerCell() override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; - - virtual Cell *getDefaultInstance() override; }; #endif // CELL_H diff --git a/src/game.cpp b/src/game.cpp index cba684a..ea559ff 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -18,7 +18,7 @@ Game::Game() // Generate level level = std::make_unique(); - main_window.create(sf::VideoMode(window_side * 2, window_side * 2), "SFML-Test Application", sf::Style::Default); + main_window.create(sf::VideoMode(window_side * 3, window_side * 3), "SFML-Test Application", sf::Style::Default); main_window.setActive(); current_level = 1; @@ -122,7 +122,7 @@ void Game::renderMap() convex_brush.setPoint(1, sf::Vector2f(cell_deviation + cell_width, 0.f)); convex_brush.setPoint(2, sf::Vector2f(cell_width, cell_height)); convex_brush.setPoint(3, sf::Vector2f(0.f, cell_height)); - convex_brush.setFillColor(sf::Color::Blue); + convex_brush.setFillColor(palette::Blue); convex_brush.setOutlineThickness(0); convex_brush.setPosition(painter_x, painter_y); @@ -131,7 +131,7 @@ void Game::renderMap() sf::Font font; font.loadFromFile("font/VeraMono.ttf"); text.setFont(font); - text.setFillColor(sf::Color::White); + text.setFillColor(palette::White); text.setCharacterSize(25); text.setPosition(50, 350); text.setString("Available bridge cells: " + std::to_string(hero->charges())); @@ -155,7 +155,7 @@ void Game::renderMap() if (hero_row == y && hero_col == x) { // Place the hero sprite - convex_brush.setFillColor(sf::Color::White); + convex_brush.setFillColor(palette::White); main_window.draw(convex_brush); } diff --git a/src/game.h b/src/game.h index 75d5136..778c6fa 100644 --- a/src/game.h +++ b/src/game.h @@ -15,8 +15,8 @@ class Game { private: // Game entities - std::unique_ptr hero; - std::unique_ptr level; + HeroPtr hero; + LevelPtr level; int current_level; diff --git a/src/level.cpp b/src/level.cpp index 70045e3..f073ff3 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -22,12 +22,18 @@ void Level::readMap(std::ifstream &file) for (coordinate k = 0; k < map[j].size(); ++k) { file >> i; - map[j][k] = default_cells[i]->getDefaultInstance(); + map[j][k].reset(default_cells[i]); map[j][k]->setPosition(j, k); } } } +template // [D]erived - [B]ase +unique_ptr static_unique_pointer_cast (unique_ptr&& old) +{ + return unique_ptr{static_cast(old.release())}; +} + Level::Level(const std::string &map_file) { prepareCellInstances(); @@ -56,7 +62,9 @@ Level::Level(const std::string &map_file) coordinate dest_row, dest_col; file >> src_row >> src_col >> dest_row >> dest_col; - // reinterpret_cast(map[src_row][src_col])->setDestination(dest_row, dest_col); + auto teleport_cell = static_unique_pointer_cast(std::move(map[src_row][src_col])); + teleport_cell->setDestination(dest_row, dest_col); + map[src_row][src_col] = std::move(teleport_cell); } } } @@ -65,10 +73,6 @@ Level::~Level() { for (Cell *cell : default_cells) delete cell; - - for (Row &row : map) - for (Cell *cell : row) - delete cell; } size_t Level::width() const @@ -83,16 +87,12 @@ size_t Level::height() const void Level::placeBridge(coordinate row, coordinate col) { - Cell *buf = map[row][col]; - map[row][col] = new PassableCell(row, col, sf::Color::Black); - delete buf; + map[row][col] = std::make_unique(row, col, palette::Black); } void Level::removeCharge(coordinate row, coordinate col) { - Cell *buf = map[row][col]; - map[row][col] = new PassableCell(row, col, color_ground); - delete buf; + map[row][col] = std::make_unique(row, col, color_ground); } Map& Level::mapArray() diff --git a/src/level.h b/src/level.h index 4683bab..fb5a0f1 100644 --- a/src/level.h +++ b/src/level.h @@ -6,7 +6,7 @@ const std::string default_file_name = "test_map"; -using Row = std::vector; +using Row = std::vector; using Map = std::vector; /// Abstraction over 2D array to quickly get access to level cells