Add TriggerCell for more in-game fun

This commit is contained in:
NaiJi ✨ 2020-03-03 19:11:04 +03:00
parent fbf18501d7
commit 0037de691f
2 changed files with 51 additions and 2 deletions

View File

@ -131,3 +131,31 @@ bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
hero->setPosition(new_x, new_y);
return true;
}
///////////////////////////////////////
TriggerCell::TriggerCell(coordinate cell_x, coordinate cell_y, const std::vector<CellPtr> &cells_to_change, const sf::Color &color) :
Cell(cell_x, cell_y, color)
{
cells.assign(std::move(cells_to_change));
}
TriggerCell::~TriggerCell()
{}
bool TriggerCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{
UNUSED(hero)
Map &map = level->mapArray();
// We replace needed cells with the ones that the trigger provides.
for (const &cell : cells)
map[cell->x()][cell->y()].reset(cell.release());
// Trigger works only once.
cells.clear();
// It's an impassable object, so player can't move to here.
return false;
}

View File

@ -8,8 +8,9 @@
class Hero;
class Level;
using HeroPtr = std::unique_ptr<Hero>;
using LevelPtr = std::unique_ptr<Level>;
using HeroPtr = std::unique_ptr<Hero>;
using LevelPtr = std::unique_ptr<Level>;
using CellPtr = std::unique_ptr<Cell>;
///////////////////////////////////////
@ -131,4 +132,24 @@ public:
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
};
///////////////////////////////////////
/// A cell which replaces and changes other map cells when activated
class TriggerCell : public Cell
{
private:
// Vector of cells to place on map
std::vector<CellPtr> cells;
public:
TriggerCell(coordinate cell_x = 0,
coordinate cell_y = 0,
const std::vector<CellPtr> &cells_to_change = std::vector<CellPtr>(), // Pink
const sf::Color &color = sf::Color(255, 192, 203));
virtual ~TriggerCell() override;
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) override;
};
#endif // CELL_H