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" #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 Hero;
class Level; class Level;
class Cell; class Cell;
@ -27,7 +37,7 @@ protected:
public: public:
Cell(coordinate cell_x = 0, Cell(coordinate cell_x = 0,
coordinate cell_y = 0, coordinate cell_y = 0,
const sf::Color &color = sf::Color::White); const sf::Color &color = palette::White);
virtual ~Cell() override; virtual ~Cell() override;
@ -45,7 +55,7 @@ class PassableCell : public Cell
public: public:
PassableCell(coordinate cell_x = 0, PassableCell(coordinate cell_x = 0,
coordinate cell_y = 0, // Brown coordinate cell_y = 0, // Brown
const sf::Color &color = sf::Color(165, 42, 42)); const sf::Color &color = palette::Brown);
virtual ~PassableCell() override; virtual ~PassableCell() override;
@ -60,7 +70,7 @@ class WaterCell : public Cell
public: public:
WaterCell(coordinate cell_x = 0, WaterCell(coordinate cell_x = 0,
coordinate cell_y = 0, coordinate cell_y = 0,
const sf::Color &color = sf::Color::Blue); const sf::Color &color = palette::Blue);
virtual ~WaterCell() override; virtual ~WaterCell() override;
@ -75,7 +85,7 @@ class WallCell : public Cell
public: public:
WallCell(coordinate cell_x = 0, WallCell(coordinate cell_x = 0,
coordinate cell_y = 0, // Gray coordinate cell_y = 0, // Gray
const sf::Color &color = sf::Color(125, 125, 125)); const sf::Color &color = );
virtual ~WallCell() override; virtual ~WallCell() override;

View File

@ -1,10 +1,14 @@
#include "game.h" #include "game.h"
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/ConvexShape.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
constexpr int cell_length = 30; constexpr int cell_width = 60;
constexpr int window_side = cell_length * side; constexpr int cell_height = 35;
constexpr int cell_deviation = 25;
constexpr int window_side = cell_width * side;
Game::Game() Game::Game()
{ {
@ -114,13 +118,18 @@ 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;
// Brush for cell sprites // Brush for cell sprites
sf::RectangleShape rectangle_brush; sf::ConvexShape convex_brush;
rectangle_brush.setSize({cell_length, cell_length}); convex_brush.setPointCount(4);
rectangle_brush.setFillColor(sf::Color::Blue); convex_brush.setPoint(0, sf::Vector2f(cell_deviation, 0.f));
rectangle_brush.setOutlineThickness(0); convex_brush.setPoint(1, sf::Vector2f(cell_deviation + cell_width, 0.f));
rectangle_brush.setPosition(painter_x, painter_y); 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 // Counter for available charges
sf::Text text; sf::Text text;
@ -132,33 +141,39 @@ void Game::renderMap()
text.setPosition(50, 350); text.setPosition(50, 350);
text.setString("Available bridge cells: " + std::to_string(hero->charges())); 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 // Where is hero
coordinate hero_x, hero_y; coordinate hero_x, hero_y;
hero->position(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 // Place the hero sprite
rectangle_brush.setFillColor(sf::Color::White); convex_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(convex_brush);
main_window.draw(rectangle_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); main_window.draw(text);
} }
@ -172,7 +187,6 @@ void Game::loadLevel(int level_index)
map[x][y] = std::make_unique<WaterCell>(x, y); map[x][y] = std::make_unique<WaterCell>(x, y);
std::vector<CellPtr> trigger_cells; std::vector<CellPtr> trigger_cells;
trigger_cells.emplace_back(std::make_unique<PassableCell>(0, 0)); 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>(1, 0));
trigger_cells.emplace_back(std::make_unique<PassableCell>(0, 1)); 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->setPosition(1, 1);
hero->setCharges(2); hero->setCharges(2);
level->setDefaultGroundColor(sf::Color(165, 42, 42));
map[0][0] = std::make_unique<WallCell>(0, 0); map[0][0] = std::make_unique<WallCell>(0, 0);
map[0][1] = std::make_unique<WallCell>(0, 1); map[0][1] = std::make_unique<WallCell>(0, 1);
map[1][0] = std::make_unique<WallCell>(1, 0); 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) 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() Map& Level::mapArray()
{ {
return map; 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: private:
Map map; Map map;
sf::Color color_ground;
public: public:
Level(); Level();
@ -26,6 +27,12 @@ public:
/// Replace a charge cell with a ground cell /// Replace a charge cell with a ground cell
void removeCharge(coordinate x, coordinate y); 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 #endif // LEVEL_H