From cf0fa0f7dd8e236c2ba8ca9a6039fe6c5ae0bd35 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Fri, 21 Feb 2020 18:24:03 +0300 Subject: [PATCH] Add function for rendering map onto main window --- game.cpp | 111 +++++++++++++++++++++++++++++++++++++++---------------- game.h | 11 +++++- 2 files changed, 89 insertions(+), 33 deletions(-) diff --git a/game.cpp b/game.cpp index 34b5c98..7244efb 100644 --- a/game.cpp +++ b/game.cpp @@ -8,17 +8,23 @@ Game::Game() // Generate level level = std::make_unique(); - 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(); + // 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; + if (direction != Direction::None) + { + // Save the initial coordinates + coordinate initial_x, initial_y; + hero->position(initial_x, initial_y); - ////////////////////////// + // Try to move hero + hero->move(direction); - // Save the initial coordinates - coordinate initial_x, initial_y; - hero->position(initial_x, initial_y); + // Save the new coordinates after moving + coordinate attempt_x, attempt_y; + hero->position(attempt_x, attempt_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 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; +} + +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; - // If the following cell is a charge - if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge)) + 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; } } diff --git a/game.h b/game.h index 6b74af7..ec56231 100644 --- a/game.h +++ b/game.h @@ -3,12 +3,16 @@ #include +#include #include #include #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 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();