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
level = std::make_unique<Level>();
sf::Window window(sf::VideoMode(640, 480), "SFML-Test Application", sf::Style::Default);
window.setActive();
main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
main_window.setActive();
}
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))
{
@ -29,14 +35,21 @@ int Game::run()
if (event.type == sf::Event::KeyPressed)
{
// Move
onMoving(event.key.code);
direction = onMoving(event.key.code);
}
}
// Draw level
renderMap(direction);
main_window.display();
}
return EXIT_SUCCESS;
}
////////////////////////////////////////////////////
Direction Game::getDirection(sf::Keyboard::Key &key) const
{
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
const Direction direction = getDirection(key);
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))
if (direction != Direction::None)
{
// 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);
// 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
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))
void Game::renderMap(const Direction direction)
{
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
hero->refillCharges(1);
level->removeCharge(attempt_x, attempt_y);
for (const CellType &cell : row)
{
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 <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Window.hpp>
#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
{
@ -19,13 +23,16 @@ private:
// SFML entities
std::unique_ptr<sf::Clock> clock;
sf::Window main_window;
sf::RenderWindow main_window;
/// Convert pressed key into a game direction
Direction getDirection(sf::Keyboard::Key &key) const;
/// 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:
explicit Game();