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

master
oss 4 years ago
commit e8f06c3929

@ -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;

@ -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);

@ -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;
}

@ -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

Loading…
Cancel
Save