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.
180 lines
4.0 KiB
C++
180 lines
4.0 KiB
C++
#include "cell.h"
|
|
|
|
#include "hero.h"
|
|
#include "level.h"
|
|
|
|
#define UNUSED(expr) (void)(expr)
|
|
|
|
Cell::Cell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
|
|
Entity(cell_row, cell_col),
|
|
cell_color(color),
|
|
height_shift(0) // ground by default
|
|
{}
|
|
|
|
Cell::~Cell()
|
|
{}
|
|
|
|
sf::Color Cell::color() const noexcept
|
|
{
|
|
return cell_color;
|
|
}
|
|
|
|
void Cell::setHeight(coordinate shift_by_y)
|
|
{
|
|
height_shift = shift_by_y;
|
|
}
|
|
|
|
coordinate Cell::heightShift() const
|
|
{
|
|
return height_shift;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
PassableCell::PassableCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
|
|
Cell(cell_row, cell_col, color)
|
|
{}
|
|
|
|
PassableCell::~PassableCell()
|
|
{}
|
|
|
|
bool PassableCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
|
|
{
|
|
UNUSED(hero), UNUSED(level);
|
|
|
|
// Hero just moves on.
|
|
return true;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
WaterCell::WaterCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
|
|
Cell(cell_row, cell_col, color)
|
|
{}
|
|
|
|
WaterCell::~WaterCell()
|
|
{}
|
|
|
|
bool WaterCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
|
|
{
|
|
// Try to use one charge to place a bridge
|
|
if (hero->useCharge())
|
|
level->placeBridge(entity_row, entity_col);
|
|
|
|
return false;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
WallCell::WallCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
|
|
Cell(cell_row, cell_col, color),
|
|
height_shift(10) // walls are a bit higher than ground and water
|
|
{}
|
|
|
|
WallCell::~WallCell()
|
|
{}
|
|
|
|
bool WallCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
|
|
{
|
|
UNUSED(hero), UNUSED(level);
|
|
|
|
// Hero never passes this cell.
|
|
return false;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
ChargeCell::ChargeCell(coordinate cell_row, coordinate cell_col, int has_charges, const sf::Color &color) :
|
|
Cell(cell_row, cell_col, color),
|
|
cell_charges(has_charges),
|
|
height_shift(5) // charges are a bit higher than ground and water, but lower than walls
|
|
{}
|
|
|
|
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(entity_row, entity_col);
|
|
|
|
return true;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
ExitCell::ExitCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
|
|
Cell(cell_row, cell_col, color)
|
|
{}
|
|
|
|
ExitCell::~ExitCell()
|
|
{}
|
|
|
|
bool ExitCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
|
|
{
|
|
UNUSED(level);
|
|
|
|
// Level is over.
|
|
hero->reachExit();
|
|
return true;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
TeleportCell::TeleportCell(coordinate cell_row, coordinate cell_col, coordinate new_cell_row, coordinate new_cell_col, const sf::Color &color) :
|
|
Cell(cell_row, cell_col, color),
|
|
new_row(new_cell_row),
|
|
new_col(new_cell_col)
|
|
{}
|
|
|
|
TeleportCell::~TeleportCell()
|
|
{}
|
|
|
|
bool TeleportCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
|
|
{
|
|
UNUSED(level);
|
|
|
|
// Hero jumps into teleport!
|
|
hero->setPosition(new_row, new_col);
|
|
return true;
|
|
}
|
|
|
|
void TeleportCell::setDestination(coordinate new_cell_row, coordinate new_cell_col)
|
|
{
|
|
new_row = new_cell_row;
|
|
new_col = new_cell_col;
|
|
}
|
|
|
|
///////////////////////////////////////
|
|
|
|
TriggerCell::TriggerCell(/*std::vector<CellPtr> &&cells_to_change,*/ coordinate cell_row, coordinate cell_col, const sf::Color &color) :
|
|
Cell(cell_row, cell_col, color),
|
|
height_shift(5) // triggers are a bit higher than ground and water, but lower than walls
|
|
{
|
|
//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 &row = cell->row();
|
|
const coordinate &col = cell->col();
|
|
|
|
map[row][col].release();
|
|
map[row][col] = std::move(cell);
|
|
}
|
|
|
|
// It's an impassable object, so player can't move to here.
|
|
return false;
|
|
}
|