Add height for cells

This commit is contained in:
NaiJi ✨ 2020-03-16 19:54:42 +03:00
parent 95143f5d89
commit 9c4914b16b
3 changed files with 69 additions and 20 deletions

View File

@ -7,7 +7,8 @@
Cell::Cell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
Entity(cell_row, cell_col),
cell_color(color)
cell_color(color),
height_shift(0) // ground by default
{}
Cell::~Cell()
@ -18,6 +19,16 @@ 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) :
@ -56,7 +67,8 @@ bool WaterCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
///////////////////////////////////////
WallCell::WallCell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
Cell(cell_row, cell_col, color)
Cell(cell_row, cell_col, color),
height_shift(10) // walls are a bit higher than ground and water
{}
WallCell::~WallCell()
@ -74,7 +86,8 @@ bool WallCell::onMovingTo(HeroPtr &hero, LevelPtr &level)
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)
cell_charges(has_charges),
height_shift(5) // charges are a bit higher than ground and water, but lower than walls
{}
ChargeCell::~ChargeCell()
@ -136,7 +149,8 @@ void TeleportCell::setDestination(coordinate new_cell_row, coordinate new_cell_c
///////////////////////////////////////
TriggerCell::TriggerCell(/*std::vector<CellPtr> &&cells_to_change,*/ coordinate cell_row, coordinate cell_col, const sf::Color &color) :
Cell(cell_row, cell_col, 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);
}

View File

@ -50,6 +50,7 @@ class Cell : public Entity
{
protected:
sf::Color cell_color;
coordinate height_shift;
public:
Cell(coordinate cell_row = 0,
@ -60,6 +61,11 @@ public:
sf::Color color() const noexcept;
/// "shift_by_y" indicates the height of current cell
/// Height is a shift of y coordinate on the scene, relatively to the ground (which is 0)
void setHeightShift(coordinate shift_by_y);
coordinate heightShift() const;
/// Determine if Hero can move onto this cell or not
virtual bool onMovingTo(HeroPtr &hero, LevelPtr &level) = 0;
};
@ -67,7 +73,7 @@ public:
///////////////////////////////////////
/// Any cell where Hero is free to move
class PassableCell : public Cell
class PassableCell final : public Cell
{
public:
PassableCell(coordinate cell_row = 0,
@ -82,7 +88,7 @@ public:
///////////////////////////////////////
/// A cell which requires Hero to spend a charge for bridge to move on
class WaterCell : public Cell
class WaterCell final : public Cell
{
public:
WaterCell(coordinate cell_row = 0,
@ -97,7 +103,7 @@ public:
///////////////////////////////////////
/// A cell which is impossible to move on
class WallCell : public Cell
class WallCell final : public Cell
{
public:
WallCell(coordinate cell_row = 0,
@ -112,7 +118,7 @@ public:
///////////////////////////////////////
/// A cell which gives hero a charge
class ChargeCell : public Cell
class ChargeCell final : public Cell
{
private:
int cell_charges;
@ -131,7 +137,7 @@ public:
///////////////////////////////////////
/// A cell which moves hero to next level
class ExitCell : public Cell
class ExitCell final : public Cell
{
public:
ExitCell(coordinate cell_row = 0,
@ -146,7 +152,7 @@ public:
///////////////////////////////////////
/// A cell which teleports hero to following coordinates
class TeleportCell : public Cell
class TeleportCell final : public Cell
{
private:
coordinate new_row, new_col;
@ -168,7 +174,7 @@ public:
///////////////////////////////////////
/// A cell which replaces and changes other map cells when activated
class TriggerCell : public Cell
class TriggerCell final : public Cell
{
private:
// Vector of cells to place on map

View File

@ -113,7 +113,7 @@ void Game::renderMap()
const Map &map = level->mapArray();
float painter_x = 0, painter_y = 0;
float shift = 0;
float horizontal_shift = 0, vertical_shift = 0;
// Brush for cell sprites
sf::ConvexShape convex_brush;
@ -143,28 +143,57 @@ void Game::renderMap()
// Draw map from 2D array
for (coordinate x = 0; x < level->width(); ++x)
{
shift = static_cast<float>(level->width()) * cell_deviation;
horizontal_shift = static_cast<float>(level->width()) * cell_deviation;
for (coordinate y = 0; y < level->height(); ++y)
{
convex_brush.setPosition(shift + painter_x, painter_y);
vertical_shift = static_cast<float>(map[y][x]->heightShift());
// If cell has any height value, we should draw walls for it
if (vertical_shift > 0)
{
// Brush for vertical walls
sf::ConvexShape convex_wall_brush;
convex_wall_brush.setPointCount(6);
convex_wall_brush.setPoint(0, sf::Vector2f(cell_deviation + cell_width, 0.f));
convex_wall_brush.setPoint(1, sf::Vector2f(cell_deviation + cell_width, vertical_shift));
convex_wall_brush.setPoint(2, sf::Vector2f(cell_width, cell_height + vertical_shift));
convex_wall_brush.setPoint(3, sf::Vector2f(0.f, cell_height + vertical_shift));
convex_wall_brush.setPoint(4, sf::Vector2f(0.f, cell_height));
convex_wall_brush.setPoint(5, sf::Vector2f(cell_width, cell_height));
convex_wall_brush.setOutlineThickness(0);
sf::Color wall_color = convex_wall_brush.getFillColor() - sf::Color(50, 50, 50);
convex_wall_brush.setFillColor(wall_color);
convex_wall_brush.setPosition(painter_x, painter_y);
main_window.draw(convex_wall_brush);
}
// Draw the top surface of the cell itself
float final_x = painter_x + horizontal_shift;
float final_y = painter_y - vertical_shift;
convex_brush.setPosition(final_x, final_y);
convex_brush.setFillColor(map[y][x]->color());
main_window.draw(convex_brush);
main_window.draw(convex_brush);
if (hero_row == y && hero_col == x)
{
// Place the hero sprite
// Draw the hero sprite
convex_brush.setFillColor(palette::White);
main_window.draw(convex_brush);
}
// Move painter to next cell of row
// Move painter to next cell of current column
painter_y += cell_height;
shift -= cell_deviation;
horizontal_shift -= cell_deviation;
}
// Move painter to next row of the map
// Move painter to next column
painter_y = 0;
painter_x += cell_width;
}