Replace raw ptrs with unique ptrs

master
NaiJi ✨ 4 years ago
parent 6eac3c84f6
commit 05826aeb3c

@ -35,11 +35,6 @@ bool PassableCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
return true; return true;
} }
Cell *PassableCell::getDefaultInstance()
{
return new PassableCell();
}
/////////////////////////////////////// ///////////////////////////////////////
WaterCell::WaterCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : 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; return false;
} }
Cell *WaterCell::getDefaultInstance()
{
return new WaterCell();
}
/////////////////////////////////////// ///////////////////////////////////////
WallCell::WallCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : 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; return false;
} }
Cell *WallCell::getDefaultInstance()
{
return new WallCell();
}
/////////////////////////////////////// ///////////////////////////////////////
ChargeCell::ChargeCell(coordinate cell_row, coordinate cell_col, int has_charges, const sf::Color &color) : 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; return true;
} }
Cell *ChargeCell::getDefaultInstance()
{
return new ChargeCell();
}
/////////////////////////////////////// ///////////////////////////////////////
ExitCell::ExitCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : 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; 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) : 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; return true;
} }
Cell *TeleportCell::getDefaultInstance()
{
return new TeleportCell();
}
/////////////////////////////////////// ///////////////////////////////////////
TriggerCell::TriggerCell(/*std::vector<CellPtr> &&cells_to_change,*/ coordinate cell_row, coordinate cell_col, const sf::Color &color) : TriggerCell::TriggerCell(/*std::vector<CellPtr> &&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(); Map &map = level->mapArray();
// We replace needed cells with the ones that the trigger provides. // 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 &row = cell->row();
const coordinate &col = cell->col(); const coordinate &col = cell->col();
delete map[row][col]; map[row][col].release();
map[row][col] = cell; map[row][col] = std::move(cell);
} }
// It's an impassable object, so player can't move to here. // It's an impassable object, so player can't move to here.
return false; return false;
} }
Cell *TriggerCell::getDefaultInstance()
{
return new TriggerCell();
}

@ -10,10 +10,15 @@
namespace palette namespace palette
{ {
const sf::Color Brown = sf::Color(165, 42, 42); const sf::Color Brown = sf::Color(165, 42, 42);
const sf::Color White = sf::Color(255, 255, 255); const sf::Color White = sf::Color(255, 255, 255);
const sf::Color Blue = sf::Color(0, 255, 255); const sf::Color Blue = sf::Color( 0, 255, 255);
const sf::Color Gray = sf::Color(125, 125, 125); 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 { enum CELL_TYPES {
@ -36,6 +41,7 @@ class Cell;
using HeroPtr = std::unique_ptr<Hero>; using HeroPtr = std::unique_ptr<Hero>;
using LevelPtr = std::unique_ptr<Level>; using LevelPtr = std::unique_ptr<Level>;
using CellPtr = std::unique_ptr<Cell>
/////////////////////////////////////// ///////////////////////////////////////
@ -56,8 +62,6 @@ public:
/// Determine if Hero can move onto this cell or not /// Determine if Hero can move onto this cell or not
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) = 0; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) = 0;
virtual Cell *getDefaultInstance() = 0;
}; };
/////////////////////////////////////// ///////////////////////////////////////
@ -67,14 +71,12 @@ class PassableCell : public Cell
{ {
public: public:
PassableCell(coordinate cell_row = 0, PassableCell(coordinate cell_row = 0,
coordinate cell_col = 0, // Brown coordinate cell_col = 0,
const sf::Color &color = palette::Brown); const sf::Color &color = palette::Brown);
virtual ~PassableCell() override; virtual ~PassableCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
virtual Cell *getDefaultInstance() override;
}; };
/////////////////////////////////////// ///////////////////////////////////////
@ -90,8 +92,6 @@ public:
virtual ~WaterCell() override; virtual ~WaterCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
virtual Cell *getDefaultInstance() override;
}; };
/////////////////////////////////////// ///////////////////////////////////////
@ -101,14 +101,12 @@ class WallCell : public Cell
{ {
public: public:
WallCell(coordinate cell_row = 0, WallCell(coordinate cell_row = 0,
coordinate cell_col = 0, // Gray coordinate cell_col = 0,
const sf::Color &color = palette::Gray); const sf::Color &color = palette::Gray);
virtual ~WallCell() override; virtual ~WallCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
virtual Cell *getDefaultInstance() override;
}; };
/////////////////////////////////////// ///////////////////////////////////////
@ -123,13 +121,11 @@ public:
ChargeCell(coordinate cell_row = 0, ChargeCell(coordinate cell_row = 0,
coordinate cell_col = 0, coordinate cell_col = 0,
int has_charges = 1, int has_charges = 1,
const sf::Color &color = sf::Color::Green); const sf::Color &color = palette::Green);
virtual ~ChargeCell() override; virtual ~ChargeCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
virtual Cell *getDefaultInstance() override;
}; };
/////////////////////////////////////// ///////////////////////////////////////
@ -140,13 +136,11 @@ class ExitCell : public Cell
public: public:
ExitCell(coordinate cell_row = 0, ExitCell(coordinate cell_row = 0,
coordinate cell_col = 0, coordinate cell_col = 0,
const sf::Color &color = sf::Color::Red); const sf::Color &color = palette::Red);
virtual ~ExitCell() override; virtual ~ExitCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
virtual Cell *getDefaultInstance() override;
}; };
/////////////////////////////////////// ///////////////////////////////////////
@ -161,14 +155,12 @@ public:
TeleportCell(coordinate cell_row = 0, TeleportCell(coordinate cell_row = 0,
coordinate cell_col = 0, coordinate cell_col = 0,
coordinate new_cell_row = 0, coordinate new_cell_row = 0,
coordinate new_cell_col = 0, // Purple coordinate new_cell_col = 0,
const sf::Color &color = sf::Color(128, 0, 128)); const sf::Color &color = palette::Purple);
virtual ~TeleportCell() override; virtual ~TeleportCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
virtual Cell *getDefaultInstance() override;
}; };
/////////////////////////////////////// ///////////////////////////////////////
@ -178,19 +170,17 @@ class TriggerCell : public Cell
{ {
private: private:
// Vector of cells to place on map // Vector of cells to place on map
std::vector<Cell *> cells; std::vector<CellPtr> cells;
public: public:
TriggerCell(//std::vector<CellPtr> &&cells_to_change, TriggerCell(//std::vector<CellPtr> &&cells_to_change,
coordinate cell_row = 0, coordinate cell_row = 0,
coordinate cell_col = 0, // Pink coordinate cell_col = 0,
const sf::Color &color = sf::Color(255, 192, 203)); const sf::Color &color = palette::Pink);
virtual ~TriggerCell() override; virtual ~TriggerCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override; virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
virtual Cell *getDefaultInstance() override;
}; };
#endif // CELL_H #endif // CELL_H

@ -18,7 +18,7 @@ Game::Game()
// Generate level // Generate level
level = std::make_unique<Level>(); level = std::make_unique<Level>();
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(); main_window.setActive();
current_level = 1; 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(1, sf::Vector2f(cell_deviation + cell_width, 0.f));
convex_brush.setPoint(2, sf::Vector2f(cell_width, cell_height)); convex_brush.setPoint(2, sf::Vector2f(cell_width, cell_height));
convex_brush.setPoint(3, sf::Vector2f(0.f, 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.setOutlineThickness(0);
convex_brush.setPosition(painter_x, painter_y); convex_brush.setPosition(painter_x, painter_y);
@ -131,7 +131,7 @@ void Game::renderMap()
sf::Font font; sf::Font font;
font.loadFromFile("font/VeraMono.ttf"); font.loadFromFile("font/VeraMono.ttf");
text.setFont(font); text.setFont(font);
text.setFillColor(sf::Color::White); text.setFillColor(palette::White);
text.setCharacterSize(25); text.setCharacterSize(25);
text.setPosition(50, 350); text.setPosition(50, 350);
text.setString("Available bridge cells: " + std::to_string(hero->charges())); text.setString("Available bridge cells: " + std::to_string(hero->charges()));
@ -155,7 +155,7 @@ void Game::renderMap()
if (hero_row == y && hero_col == x) if (hero_row == y && hero_col == x)
{ {
// Place the hero sprite // Place the hero sprite
convex_brush.setFillColor(sf::Color::White); convex_brush.setFillColor(palette::White);
main_window.draw(convex_brush); main_window.draw(convex_brush);
} }

@ -15,8 +15,8 @@ class Game
{ {
private: private:
// Game entities // Game entities
std::unique_ptr<Hero> hero; HeroPtr hero;
std::unique_ptr<Level> level; LevelPtr level;
int current_level; int current_level;

@ -22,12 +22,18 @@ void Level::readMap(std::ifstream &file)
for (coordinate k = 0; k < map[j].size(); ++k) for (coordinate k = 0; k < map[j].size(); ++k)
{ {
file >> i; file >> i;
map[j][k] = default_cells[i]->getDefaultInstance(); map[j][k].reset(default_cells[i]);
map[j][k]->setPosition(j, k); map[j][k]->setPosition(j, k);
} }
} }
} }
template<typename D, typename B> // [D]erived - [B]ase
unique_ptr<D> static_unique_pointer_cast (unique_ptr<B>&& old)
{
return unique_ptr<D>{static_cast<D*>(old.release())};
}
Level::Level(const std::string &map_file) Level::Level(const std::string &map_file)
{ {
prepareCellInstances(); prepareCellInstances();
@ -56,7 +62,9 @@ Level::Level(const std::string &map_file)
coordinate dest_row, dest_col; coordinate dest_row, dest_col;
file >> src_row >> src_col >> dest_row >> dest_col; file >> src_row >> src_col >> dest_row >> dest_col;
// reinterpret_cast<TeleportCell *>(map[src_row][src_col])->setDestination(dest_row, dest_col); auto teleport_cell = static_unique_pointer_cast<TeleportCell>(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) for (Cell *cell : default_cells)
delete cell; delete cell;
for (Row &row : map)
for (Cell *cell : row)
delete cell;
} }
size_t Level::width() const size_t Level::width() const
@ -83,16 +87,12 @@ size_t Level::height() const
void Level::placeBridge(coordinate row, coordinate col) void Level::placeBridge(coordinate row, coordinate col)
{ {
Cell *buf = map[row][col]; map[row][col] = std::make_unique<PassableCell>(row, col, palette::Black);
map[row][col] = new PassableCell(row, col, sf::Color::Black);
delete buf;
} }
void Level::removeCharge(coordinate row, coordinate col) void Level::removeCharge(coordinate row, coordinate col)
{ {
Cell *buf = map[row][col]; map[row][col] = std::make_unique<PassableCell>(row, col, color_ground);
map[row][col] = new PassableCell(row, col, color_ground);
delete buf;
} }
Map& Level::mapArray() Map& Level::mapArray()

@ -6,7 +6,7 @@
const std::string default_file_name = "test_map"; const std::string default_file_name = "test_map";
using Row = std::vector<Cell *>; using Row = std::vector<CellPtr>;
using Map = std::vector<Row>; using Map = std::vector<Row>;
/// Abstraction over 2D array to quickly get access to level cells /// Abstraction over 2D array to quickly get access to level cells

Loading…
Cancel
Save