|
|
|
@ -1,10 +1,14 @@
|
|
|
|
|
#include "game.h"
|
|
|
|
|
|
|
|
|
|
#include <SFML/Graphics/RectangleShape.hpp>
|
|
|
|
|
#include <SFML/Graphics/ConvexShape.hpp>
|
|
|
|
|
#include <SFML/Graphics/Text.hpp>
|
|
|
|
|
|
|
|
|
|
constexpr int cell_length = 30;
|
|
|
|
|
constexpr int window_side = cell_length * side;
|
|
|
|
|
constexpr int cell_width = 60;
|
|
|
|
|
constexpr int cell_height = 35;
|
|
|
|
|
constexpr int cell_deviation = 25;
|
|
|
|
|
|
|
|
|
|
constexpr int window_side = cell_width * side;
|
|
|
|
|
|
|
|
|
|
Game::Game()
|
|
|
|
|
{
|
|
|
|
@ -114,13 +118,18 @@ void Game::renderMap()
|
|
|
|
|
const Map &map = level->mapArray();
|
|
|
|
|
|
|
|
|
|
float painter_x = 0, painter_y = 0;
|
|
|
|
|
float shift = 0;
|
|
|
|
|
|
|
|
|
|
// Brush for cell sprites
|
|
|
|
|
sf::RectangleShape rectangle_brush;
|
|
|
|
|
rectangle_brush.setSize({cell_length, cell_length});
|
|
|
|
|
rectangle_brush.setFillColor(sf::Color::Blue);
|
|
|
|
|
rectangle_brush.setOutlineThickness(0);
|
|
|
|
|
rectangle_brush.setPosition(painter_x, painter_y);
|
|
|
|
|
sf::ConvexShape convex_brush;
|
|
|
|
|
convex_brush.setPointCount(4);
|
|
|
|
|
convex_brush.setPoint(0, sf::Vector2f(cell_deviation, 0.f));
|
|
|
|
|
convex_brush.setPoint(1, sf::Vector2f(cell_deviation + cell_width, 0.f));
|
|
|
|
|
convex_brush.setPoint(2, sf::Vector2f(cell_width, cell_height));
|
|
|
|
|
convex_brush.setPoint(3, sf::Vector2f(0.f, cell_height));
|
|
|
|
|
convex_brush.setFillColor(sf::Color::Blue);
|
|
|
|
|
convex_brush.setOutlineThickness(0);
|
|
|
|
|
convex_brush.setPosition(painter_x, painter_y);
|
|
|
|
|
|
|
|
|
|
// Counter for available charges
|
|
|
|
|
sf::Text text;
|
|
|
|
@ -132,33 +141,39 @@ void Game::renderMap()
|
|
|
|
|
text.setPosition(50, 350);
|
|
|
|
|
text.setString("Available bridge cells: " + std::to_string(hero->charges()));
|
|
|
|
|
|
|
|
|
|
// Where is hero
|
|
|
|
|
coordinate hero_x, hero_y;
|
|
|
|
|
hero->position(hero_x, hero_y);
|
|
|
|
|
|
|
|
|
|
// Draw map from 2D array
|
|
|
|
|
for (const Row &row : map)
|
|
|
|
|
for (coordinate x = 0; x < side; ++x)
|
|
|
|
|
{
|
|
|
|
|
for (const std::unique_ptr<Cell> &cell : row)
|
|
|
|
|
shift = side * cell_deviation;
|
|
|
|
|
|
|
|
|
|
for (coordinate y = 0; y < side; ++y)
|
|
|
|
|
{
|
|
|
|
|
rectangle_brush.setPosition(painter_y, painter_x);
|
|
|
|
|
rectangle_brush.setFillColor(cell->color());
|
|
|
|
|
convex_brush.setPosition(shift + painter_x, painter_y);
|
|
|
|
|
convex_brush.setFillColor(map[x][y]->color());
|
|
|
|
|
|
|
|
|
|
main_window.draw(rectangle_brush);
|
|
|
|
|
main_window.draw(convex_brush);
|
|
|
|
|
|
|
|
|
|
if (hero_x == x && hero_y == y)
|
|
|
|
|
{
|
|
|
|
|
// Place the hero sprite
|
|
|
|
|
convex_brush.setFillColor(sf::Color::White);
|
|
|
|
|
main_window.draw(convex_brush);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Move painter to next cell of row
|
|
|
|
|
painter_x += cell_length;
|
|
|
|
|
painter_y += cell_height;
|
|
|
|
|
shift -= cell_deviation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Move painter to next row of the map
|
|
|
|
|
painter_x = 0;
|
|
|
|
|
painter_y += cell_length;
|
|
|
|
|
painter_y = 0;
|
|
|
|
|
painter_x += cell_width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Where is hero
|
|
|
|
|
coordinate hero_x, hero_y;
|
|
|
|
|
hero->position(hero_x, hero_y);
|
|
|
|
|
|
|
|
|
|
// Place the hero sprite
|
|
|
|
|
rectangle_brush.setFillColor(sf::Color::White);
|
|
|
|
|
rectangle_brush.setPosition(static_cast<float>(hero_x) * cell_length, static_cast<float>(hero_y) * cell_length);
|
|
|
|
|
main_window.draw(rectangle_brush);
|
|
|
|
|
main_window.draw(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -172,7 +187,6 @@ void Game::loadLevel(int level_index)
|
|
|
|
|
map[x][y] = std::make_unique<WaterCell>(x, y);
|
|
|
|
|
|
|
|
|
|
std::vector<CellPtr> trigger_cells;
|
|
|
|
|
|
|
|
|
|
trigger_cells.emplace_back(std::make_unique<PassableCell>(0, 0));
|
|
|
|
|
trigger_cells.emplace_back(std::make_unique<PassableCell>(1, 0));
|
|
|
|
|
trigger_cells.emplace_back(std::make_unique<PassableCell>(0, 1));
|
|
|
|
@ -184,6 +198,8 @@ void Game::loadLevel(int level_index)
|
|
|
|
|
hero->setPosition(1, 1);
|
|
|
|
|
hero->setCharges(2);
|
|
|
|
|
|
|
|
|
|
level->setDefaultGroundColor(sf::Color(165, 42, 42));
|
|
|
|
|
|
|
|
|
|
map[0][0] = std::make_unique<WallCell>(0, 0);
|
|
|
|
|
map[0][1] = std::make_unique<WallCell>(0, 1);
|
|
|
|
|
map[1][0] = std::make_unique<WallCell>(1, 0);
|
|
|
|
|