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

122
game.cpp
View File

@ -1,5 +1,10 @@
#include "game.h" #include "game.h"
#include <SFML/Graphics/RectangleShape.hpp>
constexpr int cell_length = 20;
constexpr int window_side = cell_length * side;
Game::Game() Game::Game()
{ {
// Place the player with 10 initial charges onto x: 1, y: 1 // 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>(); clock = std::make_unique<sf::Clock>();
// To where player moved each step of the game loop
Direction direction;
// On the game loop // On the game loop
while (main_window.isOpen()) while (main_window.isOpen())
{ {
// By default player doesn't move anywhere
direction = Direction::None;
sf::Event event; sf::Event event;
while (main_window.pollEvent(event)) while (main_window.pollEvent(event))
{ {
@ -35,12 +34,12 @@ int Game::run()
if (event.type == sf::Event::KeyPressed) if (event.type == sf::Event::KeyPressed)
{ {
// Move // Move
direction = onMoving(event.key.code); onMoving(event.key.code);
} }
} }
// Draw level // Draw level
renderMap(direction); renderMap();
main_window.display(); main_window.display();
} }
@ -79,77 +78,85 @@ 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 // Determine where to move
const Direction direction = getDirection(key); 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);
// Try to move hero
hero->move(direction);
// Save the new coordinates after moving
coordinate attempt_x, attempt_y;
hero->position(attempt_x, attempt_y);
//////////////////////////
// If the following cell is water
if (level->isCellOfType(attempt_x, attempt_y, CellType::Water))
{ {
// Save the initial coordinates // Try to use one charge to place a bridge
coordinate initial_x, initial_y; if (hero->useCharge())
hero->position(initial_x, initial_y); level->placeBridge(attempt_x, attempt_y);
// If hero doesn't have enough charges, we move Hero back
// Try to move hero else
hero->move(direction); hero->setPosition(initial_x, initial_y);
// Save the new coordinates after moving
coordinate attempt_x, attempt_y;
hero->position(attempt_x, attempt_y);
//////////////////////////
// If the following cell is water
if (level->isCellOfType(attempt_x, attempt_y, CellType::Water))
{
// Try to use one charge to place a bridge
if (hero->useCharge())
level->placeBridge(attempt_x, attempt_y);
// If hero doesn't have enough charges, we move Hero back
else
hero->setPosition(initial_x, initial_y);
}
//////////////////////////
// If the following cell is a charge
if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge))
{
// Hero picks up the charge; remove it from the map
hero->refillCharges(1);
level->removeCharge(attempt_x, attempt_y);
}
} }
return direction; //////////////////////////
// If the following cell is a charge
if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge))
{
// Hero picks up the charge; remove it from the map
hero->refillCharges(1);
level->removeCharge(attempt_x, attempt_y);
}
} }
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(); const Map &map = level->mapArray();
coordinate painter_x = 0, painter_y = 0; 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 Row &row : map)
{ {
for (const CellType &cell : row) for (const CellType &cell : row)
{ {
switch (cell) switch (cell)
{ // TO DO!! {
case CellType::Ground: case CellType::Ground:
; break; rectangle_brush.setFillColor(sf::Color(165, 42, 42)); // Brown
break;
case CellType::Charge: case CellType::Charge:
; break; rectangle_brush.setFillColor(sf::Color::Green);
break;
case CellType::Bridge: case CellType::Bridge:
; break; rectangle_brush.setFillColor(sf::Color::Black);
break;
case CellType::Water: case CellType::Water:
default: default:
; rectangle_brush.setFillColor(sf::Color::Blue);
} }
main_window.draw(rectangle_brush);
// Move painter to next cell of row // Move painter to next cell of row
painter_x += cell_length; painter_x += cell_length;
} }
@ -158,4 +165,13 @@ void Game::renderMap(const Direction direction)
painter_x = 0; painter_x = 0;
painter_y += cell_length; 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 "hero.h"
#include "level.h" #include "level.h"
constexpr int cell_length = 20;
constexpr int window_side = cell_length * side;
/// The main class where all the process happens /// The main class where all the process happens
class Game class Game
{ {
@ -29,10 +26,10 @@ private:
Direction getDirection(sf::Keyboard::Key &key) const; Direction getDirection(sf::Keyboard::Key &key) const;
/// Move player by pressed key /// Move player by pressed key
Direction onMoving(sf::Keyboard::Key &key); void onMoving(sf::Keyboard::Key &key);
/// Render game state /// Render game state
void renderMap(const Direction direction); void renderMap();
public: public:
explicit Game(); explicit Game();