From bfbb81568ac6aabcbc9a5b7277ce1ad224ab28d2 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Tue, 25 Feb 2020 19:53:57 +0300 Subject: [PATCH] Add leveling --- src/game.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++---- src/game.h | 5 +++ src/hero.cpp | 5 +++ src/hero.h | 3 ++ src/level.cpp | 37 +++++---------------- src/level.h | 10 ++++-- 6 files changed, 112 insertions(+), 39 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index f14c450..3655b3e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -16,6 +16,9 @@ Game::Game() main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default); main_window.setActive(); + + current_level = 1; + loadLevel(current_level); } int Game::run() @@ -98,26 +101,35 @@ void Game::onMoving(sf::Keyboard::Key &key) ////////////////////////// - // If the following cell is water - if (level->isCellOfType(attempt_x, attempt_y, CellType::Water)) + switch (level->cellOfType(attempt_x, attempt_y)) { + case 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); - } - ////////////////////////// + break; + + case CellType::Charge: - // 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); + + break; + + case CellType::Exit: + + // Hero exists the level! + loadLevel(++current_level); + + break; } + } void Game::renderMap() @@ -160,6 +172,9 @@ void Game::renderMap() case CellType::Bridge: rectangle_brush.setFillColor(sf::Color::Black); break; + case CellType::Exit: + rectangle_brush.setFillColor(sf::Color::Red); + break; case CellType::Water: default: rectangle_brush.setFillColor(sf::Color::Blue); @@ -186,3 +201,65 @@ void Game::renderMap() main_window.draw(rectangle_brush); main_window.draw(text); } + +void Game::loadLevel(int level_index) +{ + Map map; + + // Fill the level with water + for (Row &row : map) + { + for (CellType &cell : row) + cell = CellType::Water; + } + + switch (level_index) + { + case 1: + // Hardcoding is temporary! + hero->setPosition(1, 1); + hero->setCharges(2); + map[1][1] = CellType::Ground; + map[1][2] = CellType::Ground; + map[1][3] = CellType::Ground; + map[1][4] = CellType::Ground; + map[2][2] = CellType::Ground; + map[3][2] = CellType::Ground; + map[3][3] = CellType::Ground; + map[3][3] = CellType::Ground; + map[6][3] = CellType::Ground; + map[6][4] = CellType::Ground; + map[6][5] = CellType::Ground; + map[6][6] = CellType::Ground; + map[7][6] = CellType::Ground; + map[9][6] = CellType::Ground; + map[8][7] = CellType::Exit; + map[2][3] = CellType::Charge; + level->setMap(map); + break; + case 2: + // Hardcoding is temporary! + hero->setPosition(5, 5); + hero->setCharges(10); + map[5][6] = CellType::Ground; + map[5][5] = CellType::Ground; + map[5][4] = CellType::Ground; + map[4][5] = CellType::Ground; + map[4][6] = CellType::Ground; + map[4][4] = CellType::Ground; + map[6][6] = CellType::Ground; + map[6][5] = CellType::Ground; + map[6][4] = CellType::Ground; + map[6][7] = CellType::Ground; + map[6][8] = CellType::Ground; + map[5][8] = CellType::Ground; + map[8][8] = CellType::Ground; + map[8][9] = CellType::Ground; + map[8][10] = CellType::Exit; + map[4][7] = CellType::Charge; + level->setMap(map); + break; + default: + main_window.close(); + } +} diff --git a/src/game.h b/src/game.h index a69619b..e6d50eb 100644 --- a/src/game.h +++ b/src/game.h @@ -18,6 +18,8 @@ private: std::unique_ptr hero; std::unique_ptr level; + int current_level; + // SFML entities sf::RenderWindow main_window; @@ -30,6 +32,9 @@ private: /// Render game state void renderMap(); + /// Prepare map and hero for a game level + void loadLevel(int level_index = 1); + public: explicit Game(); diff --git a/src/hero.cpp b/src/hero.cpp index e6303a9..53de84f 100644 --- a/src/hero.cpp +++ b/src/hero.cpp @@ -27,6 +27,11 @@ bool Hero::useCharge() return false; } +void Hero::setCharges(int charges) noexcept +{ + hero_charges = charges; +} + void Hero::position(coordinate &x, coordinate &y) const noexcept { x = pos_x; diff --git a/src/hero.h b/src/hero.h index 482d277..b40c7c7 100644 --- a/src/hero.h +++ b/src/hero.h @@ -31,6 +31,9 @@ public: /// Spend one charge on action bool useCharge(); + /// Set amount of hero charges explicitly + void setCharges(int charges) noexcept; + /// Get current Hero position void position(coordinate &x, coordinate &y) const noexcept; diff --git a/src/level.cpp b/src/level.cpp index 17c293b..26e24a1 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -1,42 +1,16 @@ #include "level.h" Level::Level() -{ - // Fill the level with water - for (Row &row : map) - { - for (CellType &cell : row) - cell = CellType::Water; - } - - // Temporary mock - - map[1][1] = CellType::Ground; - map[1][2] = CellType::Ground; - map[1][3] = CellType::Ground; - map[1][4] = CellType::Ground; - map[2][2] = CellType::Ground; - map[3][2] = CellType::Ground; - map[3][3] = CellType::Ground; - map[3][3] = CellType::Ground; - map[6][3] = CellType::Ground; - map[6][4] = CellType::Ground; - map[6][5] = CellType::Ground; - map[6][6] = CellType::Ground; - map[7][6] = CellType::Ground; - map[9][6] = CellType::Ground; - - map[2][3] = CellType::Charge; -} +{} void Level::placeBridge(coordinate x, coordinate y) { map[x][y] = CellType::Bridge; } -bool Level::isCellOfType(coordinate x, coordinate y, CellType type) const +CellType Level::cellOfType(coordinate x, coordinate y) const { - return (map[x][y] == type); + return map[x][y]; } void Level::removeCharge(coordinate x, coordinate y) @@ -48,3 +22,8 @@ Map& Level::mapArray() { return map; } + +void Level::setMap(const Map &new_map) +{ + map = std::move(new_map); +} diff --git a/src/level.h b/src/level.h index 469725b..6f9cd81 100644 --- a/src/level.h +++ b/src/level.h @@ -9,7 +9,8 @@ enum class CellType Ground = '-', Charge = '$', Bridge = char(177), - Hero = '@' + Hero = '@', + Exit = '#' }; using coordinate = unsigned int; @@ -33,11 +34,14 @@ public: /// Get the 2D array of level map Map& mapArray(); - /// Is the following cell has requested type - bool isCellOfType(coordinate x, coordinate y, CellType type) const; + /// Returns type of requested map cell + CellType cellOfType(coordinate x, coordinate y) const; /// Replace a charge cell with a ground cell void removeCharge(coordinate x, coordinate y); + + /// Set new map for the level + void setMap(const Map& new_map); }; #endif // LEVEL_H