Implement level rendering

This commit is contained in:
NaiJi ✨ 2020-02-21 20:34:28 +03:00
parent cf0fa0f7dd
commit 2c31af397c
2 changed files with 71 additions and 58 deletions

View File

@ -1,5 +1,10 @@
#include "game.h"
#include <SFML/Graphics/RectangleShape.hpp>
constexpr int cell_length = 20;
constexpr int window_side = cell_length * side;
Game::Game()
{
// Place the player with 10 initial charges onto x: 1, y: 1
@ -16,15 +21,9 @@ int Game::run()
{
clock = std::make_unique<sf::Clock>();
// To where player moved each step of the game loop
Direction direction;
// On the game loop
while (main_window.isOpen())
{
// By default player doesn't move anywhere
direction = Direction::None;
sf::Event event;
while (main_window.pollEvent(event))
{
@ -35,12 +34,12 @@ int Game::run()
if (event.type == sf::Event::KeyPressed)
{
// Move
direction = onMoving(event.key.code);
onMoving(event.key.code);
}
}
// Draw level
renderMap(direction);
renderMap();
main_window.display();
}
@ -79,13 +78,14 @@ Direction Game::getDirection(sf::Keyboard::Key &key) const
}
}
Direction Game::onMoving(sf::Keyboard::Key &key)
void Game::onMoving(sf::Keyboard::Key &key)
{
// Determine where to move
const Direction direction = getDirection(key);
if (direction != Direction::None)
{
if (direction == Direction::None)
return;
// Save the initial coordinates
coordinate initial_x, initial_y;
hero->position(initial_x, initial_y);
@ -119,37 +119,44 @@ Direction Game::onMoving(sf::Keyboard::Key &key)
hero->refillCharges(1);
level->removeCharge(attempt_x, attempt_y);
}
}
return direction;
}
void Game::renderMap(const Direction direction)
void Game::renderMap()
{
if (direction == Direction::None)
return; // Player didn't move this step of loop, so map couldn't change
const Map &map = level->mapArray();
coordinate painter_x = 0, painter_y = 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);
// Draw map from 2D array
for (const Row &row : map)
{
for (const CellType &cell : row)
{
switch (cell)
{ // TO DO!!
{
case CellType::Ground:
; break;
rectangle_brush.setFillColor(sf::Color(165, 42, 42)); // Brown
break;
case CellType::Charge:
; break;
rectangle_brush.setFillColor(sf::Color::Green);
break;
case CellType::Bridge:
; break;
rectangle_brush.setFillColor(sf::Color::Black);
break;
case CellType::Water:
default:
;
rectangle_brush.setFillColor(sf::Color::Blue);
}
main_window.draw(rectangle_brush);
// Move painter to next cell of row
painter_x += cell_length;
}
@ -158,4 +165,13 @@ void Game::renderMap(const Direction direction)
painter_x = 0;
painter_y += cell_length;
}
// 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(hero_x * cell_length, hero_y * cell_length);
main_window.draw(rectangle_brush);
}

7
game.h
View File

@ -10,9 +10,6 @@
#include "hero.h"
#include "level.h"
constexpr int cell_length = 20;
constexpr int window_side = cell_length * side;
/// The main class where all the process happens
class Game
{
@ -29,10 +26,10 @@ private:
Direction getDirection(sf::Keyboard::Key &key) const;
/// Move player by pressed key
Direction onMoving(sf::Keyboard::Key &key);
void onMoving(sf::Keyboard::Key &key);
/// Render game state
void renderMap(const Direction direction);
void renderMap();
public:
explicit Game();