Merge branch 'develop' of https://github.com/WizardNaiJi/sfml-test into develop

This commit is contained in:
oss 2020-03-15 18:22:48 +03:00
commit e8f06c3929
4 changed files with 79 additions and 36 deletions

View File

@ -8,6 +8,16 @@
#include "entity.h"
namespace palette
{
const sf::Color Brown = sf::Color(165, 42, 42);
const sf::Color White = sf::Color(255, 255, 255);
const sf::Color Blue = sf::Color(0, 255, 255);
const sf::Color Gray = sf::Color(125, 125, 125);
}
///////////////////////////////////////
class Hero;
class Level;
class Cell;
@ -27,7 +37,7 @@ protected:
public:
Cell(coordinate cell_x = 0,
coordinate cell_y = 0,
const sf::Color &color = sf::Color::White);
const sf::Color &color = palette::White);
virtual ~Cell() override;
@ -45,7 +55,7 @@ class PassableCell : public Cell
public:
PassableCell(coordinate cell_x = 0,
coordinate cell_y = 0, // Brown
const sf::Color &color = sf::Color(165, 42, 42));
const sf::Color &color = palette::Brown);
virtual ~PassableCell() override;
@ -60,7 +70,7 @@ class WaterCell : public Cell
public:
WaterCell(coordinate cell_x = 0,
coordinate cell_y = 0,
const sf::Color &color = sf::Color::Blue);
const sf::Color &color = palette::Blue);
virtual ~WaterCell() override;
@ -75,7 +85,7 @@ class WallCell : public Cell
public:
WallCell(coordinate cell_x = 0,
coordinate cell_y = 0, // Gray
const sf::Color &color = sf::Color(125, 125, 125));
const sf::Color &color = );
virtual ~WallCell() override;

View File

@ -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()));
// Draw map from 2D array
for (const Row &row : map)
{
for (const std::unique_ptr<Cell> &cell : row)
{
rectangle_brush.setPosition(painter_y, painter_x);
rectangle_brush.setFillColor(cell->color());
main_window.draw(rectangle_brush);
// Move painter to next cell of row
painter_x += cell_length;
}
// Move painter to next row of the map
painter_x = 0;
painter_y += cell_length;
}
// Where is hero
coordinate hero_x, hero_y;
hero->position(hero_x, hero_y);
// Draw map from 2D array
for (coordinate x = 0; x < side; ++x)
{
shift = side * cell_deviation;
for (coordinate y = 0; y < side; ++y)
{
convex_brush.setPosition(shift + painter_x, painter_y);
convex_brush.setFillColor(map[x][y]->color());
main_window.draw(convex_brush);
if (hero_x == x && hero_y == 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);
convex_brush.setFillColor(sf::Color::White);
main_window.draw(convex_brush);
}
// Move painter to next cell of row
painter_y += cell_height;
shift -= cell_deviation;
}
// Move painter to next row of the map
painter_y = 0;
painter_x += cell_width;
}
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);

View File

@ -10,10 +10,20 @@ void Level::placeBridge(coordinate x, coordinate y)
void Level::removeCharge(coordinate x, coordinate y)
{
map[x][y] = std::make_unique<PassableCell>(x, y /* Brown Color */);
map[x][y] = std::make_unique<PassableCell>(x, y, color_ground);
}
Map& Level::mapArray()
{
return map;
}
sf::Color Level::defaultGroundColor()
{
return color_ground;
}
void Level::setDefaultGroundColor(const sf::Color &color)
{
color_ground = color;
}

View File

@ -14,6 +14,7 @@ class Level
{
private:
Map map;
sf::Color color_ground;
public:
Level();
@ -26,6 +27,12 @@ public:
/// Replace a charge cell with a ground cell
void removeCharge(coordinate x, coordinate y);
/// Get default color for passable cells
sf::Color defaultGroundColor();
/// Set default color for passable cells
void setDefaultGroundColor(const sf::Color &color);
};
#endif // LEVEL_H