You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
3.4 KiB
C++

#include "cell.h"
#include "hero.h"
#include "level.h"
#define UNUSED(expr) (void)(expr);
Cell::Cell(coordinate cell_x, coordinate cell_y, const sf::Color &color) :
Entity(cell_x, cell_y),
cell_color(color)
{}
Cell::~Cell()
{}
sf::Color Cell::color() const noexcept
{
return cell_color;
}
///////////////////////////////////////
PassableCell::PassableCell(coordinate cell_x, coordinate cell_y, const sf::Color &color) :
Cell(cell_x, cell_y, color)
{}
PassableCell::~PassableCell()
{}
bool PassableCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{
UNUSED(hero)
UNUSED(level)
// Hero just moves on.
return true;
}
///////////////////////////////////////
WaterCell::WaterCell(coordinate cell_x, coordinate cell_y, const sf::Color &color) :
Cell(cell_x, cell_y, color)
{}
WaterCell::~WaterCell()
{}
bool WaterCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{
// Try to use one charge to place a bridge
if (hero->useCharge()) {
level->placeBridge(pos_x, pos_y);
return true;
}
// If hero doesn't have enough charges, we move Hero back
return false;
}
///////////////////////////////////////
WallCell::WallCell(coordinate cell_x, coordinate cell_y, const sf::Color &color) :
Cell(cell_x, cell_y, color)
{}
WallCell::~WallCell()
{}
bool WallCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{
UNUSED(hero)
UNUSED(level)
// Hero never passes this cell.
return false;
}
///////////////////////////////////////
ChargeCell::ChargeCell(coordinate cell_x, coordinate cell_y, int has_charges, const sf::Color &color) :
Cell(cell_x, cell_y, color),
cell_charges(has_charges)
{}
ChargeCell::~ChargeCell()
{}
bool ChargeCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{
// Hero picks up the charge; remove it from the map
hero->refillCharges(cell_charges);
level->removeCharge(pos_x, pos_y);
return true;
}
///////////////////////////////////////
ExitCell::ExitCell(coordinate cell_x, coordinate cell_y, const sf::Color &color) :
Cell(cell_x, cell_y, color)
{}
ExitCell::~ExitCell()
{}
bool ExitCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{
UNUSED(level)
// Level is over.
hero->reachExit();
return true;
}
///////////////////////////////////////
TeleportCell::TeleportCell(coordinate cell_x, coordinate cell_y, coordinate new_cell_x, coordinate new_cell_y, const sf::Color &color) :
Cell(cell_x, cell_y, color),
new_x(new_cell_x),
new_y(new_cell_y)
{}
TeleportCell::~TeleportCell()
{}
bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
{
UNUSED(level)
// Hero jumps into teleport!
hero->setPosition(new_x, new_y);
return true;
}
///////////////////////////////////////
TriggerCell::TriggerCell(std::vector<CellPtr> &&cells_to_change, coordinate cell_x, coordinate cell_y, const sf::Color &color) :
Cell(cell_x, cell_y, color)
{
cells = 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 (CellPtr &cell : cells)
{
const coordinate &y = cell->y();
const coordinate &x = cell->x();
map[x][y].release();
map[x][y] = std::move(cell);
}
// It's an impassable object, so player can't move to here.
return false;
}