Add height for cells

master
NaiJi ✨ 4 years ago
parent 95143f5d89
commit 9c4914b16b

@ -7,7 +7,8 @@
Cell::Cell(coordinate cell_row, coordinate cell_col, const sf::Color &color) : Cell::Cell(coordinate cell_row, coordinate cell_col, const sf::Color &color) :
Entity(cell_row, cell_col), Entity(cell_row, cell_col),
cell_color(color) cell_color(color),
height_shift(0) // ground by default
{} {}
Cell::~Cell() Cell::~Cell()
@ -18,6 +19,16 @@ sf::Color Cell::color() const noexcept
return cell_color; 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) : 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) : 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() 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) : ChargeCell::ChargeCell(coordinate cell_row, coordinate cell_col, int has_charges, const sf::Color &color) :
Cell(cell_row, cell_col, 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() 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) : 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); //cells = std::move(cells_to_change);
} }

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

@ -113,7 +113,7 @@ void Game::renderMap()
const Map &map = level->mapArray(); const Map &map = level->mapArray();
float painter_x = 0, painter_y = 0; float painter_x = 0, painter_y = 0;
float shift = 0; float horizontal_shift = 0, vertical_shift = 0;
// Brush for cell sprites // Brush for cell sprites
sf::ConvexShape convex_brush; sf::ConvexShape convex_brush;
@ -143,28 +143,57 @@ void Game::renderMap()
// Draw map from 2D array // Draw map from 2D array
for (coordinate x = 0; x < level->width(); ++x) 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) 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()); convex_brush.setFillColor(map[y][x]->color());
main_window.draw(convex_brush); main_window.draw(convex_brush);
if (hero_row == y && hero_col == x) if (hero_row == y && hero_col == x)
{ {
// Place the hero sprite // Draw the hero sprite
convex_brush.setFillColor(palette::White); convex_brush.setFillColor(palette::White);
main_window.draw(convex_brush); main_window.draw(convex_brush);
} }
// Move painter to next cell of row // Move painter to next cell of current column
painter_y += cell_height; 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_y = 0;
painter_x += cell_width; painter_x += cell_width;
} }

Loading…
Cancel
Save