Replace raw ptrs with unique ptrs
This commit is contained in:
parent
6eac3c84f6
commit
05826aeb3c
41
src/cell.cpp
41
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<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();
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
|
48
src/cell.h
48
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<Hero>;
|
||||
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
|
||||
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<Cell *> cells;
|
||||
std::vector<CellPtr> cells;
|
||||
|
||||
public:
|
||||
TriggerCell(//std::vector<CellPtr> &&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
|
||||
|
|
|
@ -18,7 +18,7 @@ Game::Game()
|
|||
// Generate 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();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@ class Game
|
|||
{
|
||||
private:
|
||||
// Game entities
|
||||
std::unique_ptr<Hero> hero;
|
||||
std::unique_ptr<Level> level;
|
||||
HeroPtr hero;
|
||||
LevelPtr level;
|
||||
|
||||
int current_level;
|
||||
|
||||
|
|
|
@ -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<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)
|
||||
{
|
||||
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<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)
|
||||
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<PassableCell>(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<PassableCell>(row, col, color_ground);
|
||||
}
|
||||
|
||||
Map& Level::mapArray()
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
const std::string default_file_name = "test_map";
|
||||
|
||||
using Row = std::vector<Cell *>;
|
||||
using Row = std::vector<CellPtr>;
|
||||
using Map = std::vector<Row>;
|
||||
|
||||
/// Abstraction over 2D array to quickly get access to level cells
|
||||
|
|
Loading…
Reference in New Issue