Add function for rendering map onto main window

This commit is contained in:
NaiJi ✨ 2020-02-21 18:24:03 +03:00
parent a3ab2e32da
commit cf0fa0f7dd
2 changed files with 94 additions and 38 deletions

121
game.cpp
View File

@ -8,17 +8,23 @@ Game::Game()
// Generate level // Generate level
level = std::make_unique<Level>(); level = std::make_unique<Level>();
sf::Window window(sf::VideoMode(640, 480), "SFML-Test Application", sf::Style::Default); main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
window.setActive(); main_window.setActive();
} }
int Game::run() 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))
{ {
@ -29,14 +35,21 @@ int Game::run()
if (event.type == sf::Event::KeyPressed) if (event.type == sf::Event::KeyPressed)
{ {
// Move // Move
onMoving(event.key.code); direction = onMoving(event.key.code);
} }
} }
// Draw level
renderMap(direction);
main_window.display();
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
////////////////////////////////////////////////////
Direction Game::getDirection(sf::Keyboard::Key &key) const Direction Game::getDirection(sf::Keyboard::Key &key) const
{ {
switch (key) switch (key)
@ -66,47 +79,83 @@ Direction Game::getDirection(sf::Keyboard::Key &key) const
} }
} }
void Game::onMoving(sf::Keyboard::Key &key) Direction 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))
{ {
// Try to use one charge to place a bridge // Save the initial coordinates
if (hero->useCharge()) coordinate initial_x, initial_y;
level->placeBridge(attempt_x, attempt_y); hero->position(initial_x, initial_y);
// If hero doesn't have enough charges, we move Hero back
else // Try to move hero
hero->setPosition(initial_x, initial_y); 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))
{
// 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 void Game::renderMap(const Direction direction)
if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge)) {
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;
for (const Row &row : map)
{ {
// Hero picks up the charge; remove it from the map for (const CellType &cell : row)
hero->refillCharges(1); {
level->removeCharge(attempt_x, attempt_y); switch (cell)
{ // TO DO!!
case CellType::Ground:
; break;
case CellType::Charge:
; break;
case CellType::Bridge:
; break;
case CellType::Water:
default:
;
}
// 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;
} }
} }

11
game.h
View File

@ -3,12 +3,16 @@
#include <memory> #include <memory>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Time.hpp> #include <SFML/System/Time.hpp>
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#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
{ {
@ -19,13 +23,16 @@ private:
// SFML entities // SFML entities
std::unique_ptr<sf::Clock> clock; std::unique_ptr<sf::Clock> clock;
sf::Window main_window; sf::RenderWindow main_window;
/// Convert pressed key into a game direction /// Convert pressed key into a game direction
Direction getDirection(sf::Keyboard::Key &key) const; Direction getDirection(sf::Keyboard::Key &key) const;
/// Move player by pressed key /// Move player by pressed key
void onMoving(sf::Keyboard::Key &key); Direction onMoving(sf::Keyboard::Key &key);
/// Render game state
void renderMap(const Direction direction);
public: public:
explicit Game(); explicit Game();