From 0e647faf2043c4b65fde5787b0bec208472e7c73 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Fri, 21 Feb 2020 17:13:12 +0300 Subject: [PATCH] Finish game logic --- game.cpp | 49 +++++++++++++++++++++++++++++++++++++++++-------- hero.cpp | 22 ++++++++++++++-------- hero.h | 23 ++++++++++++++--------- level.cpp | 8 ++++---- level.h | 11 ++++++----- 5 files changed, 79 insertions(+), 34 deletions(-) diff --git a/game.cpp b/game.cpp index 51adcc5..ec1d9dc 100644 --- a/game.cpp +++ b/game.cpp @@ -43,22 +43,26 @@ Direction Game::getDirection(sf::Keyboard::Key &key) const { case sf::Keyboard::A: case sf::Keyboard::Left: - return Direction::LEFT; + case sf::Keyboard::Num4: + return Direction::Left; case sf::Keyboard::W: case sf::Keyboard::Up: - return Direction::UP; + case sf::Keyboard::Num8: + return Direction::Up; case sf::Keyboard::D: case sf::Keyboard::Right: - return Direction::RIGHT; + case sf::Keyboard::Num6: + return Direction::Right; case sf::Keyboard::S: case sf::Keyboard::Down: - return Direction::DOWN; + case sf::Keyboard::Num2: + return Direction::Down; default: - return Direction::NONE; + return Direction::None; } } @@ -67,13 +71,42 @@ void Game::onMoving(sf::Keyboard::Key &key) // Determine where to move const Direction direction = getDirection(key); - if (direction == Direction::NONE) + if (direction == Direction::None) return; ////////////////////////// - int initial_x, initial_y; + // Save the initial coordinates + coordinate initial_x, initial_y; hero->position(initial_x, initial_y); - // TO DO THE REST + // 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, '.')) + { + // 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, '$')) + { + // Hero picks up the charge; remove it from the map + hero->refillCharges(1); + level->removeCharge(attempt_x, attempt_y); + } } diff --git a/hero.cpp b/hero.cpp index 1f0deb9..e6303a9 100644 --- a/hero.cpp +++ b/hero.cpp @@ -1,6 +1,6 @@ #include "hero.h" -Hero::Hero(int position_x, int position_y, int initial_charges) : +Hero::Hero(coordinate position_x, coordinate position_y, int initial_charges) : hero_charges(initial_charges), pos_x(position_x), pos_y(position_y) @@ -27,25 +27,31 @@ bool Hero::useCharge() return false; } -void Hero::position(int &x, int &y) const noexcept +void Hero::position(coordinate &x, coordinate &y) const noexcept { x = pos_x; y = pos_y; } -void Hero::move(Direction &direction) +void Hero::move(const Direction &direction) { switch (direction) { - case Direction::UP: + case Direction::Up: --pos_y; break; - case Direction::DOWN: + case Direction::Down: ++pos_y; break; - case Direction::LEFT: + case Direction::Left: --pos_x; break; - case Direction::RIGHT: + case Direction::Right: ++pos_x; break; - case Direction::NONE: + case Direction::None: break; } } + +void Hero::setPosition(coordinate x, coordinate y) +{ + pos_x = x; + pos_y = y; +} diff --git a/hero.h b/hero.h index 7e73c50..482d277 100644 --- a/hero.h +++ b/hero.h @@ -3,22 +3,24 @@ enum class Direction { - LEFT, - UP, - RIGHT, - DOWN, - NONE + Left, + Up, + Right, + Down, + None }; +using coordinate = unsigned int; + /// Represents a controlable by player game character class Hero { private: int hero_charges; - int pos_x, pos_y; + coordinate pos_x, pos_y; public: - explicit Hero(int position_x = 0, int position_y = 0, int initial_charges = 0); + explicit Hero(coordinate position_x = 0, coordinate position_y = 0, int initial_charges = 0); /// Add more charges for hero to use void refillCharges(int append_charges); @@ -30,10 +32,13 @@ public: bool useCharge(); /// Get current Hero position - void position(int &x, int &y) const noexcept; + void position(coordinate &x, coordinate &y) const noexcept; + + /// Set Hero position explicitly + void setPosition(coordinate x, coordinate y); /// Move hero by one cell to any direction - void move(Direction &direction); + void move(const Direction &direction); }; #endif // HERO_H diff --git a/level.cpp b/level.cpp index 8b105e0..86de50c 100644 --- a/level.cpp +++ b/level.cpp @@ -22,17 +22,17 @@ Level::Level() map[2][3] = '$'; } -void Level::placeBridge(Row::size_type x, Row::size_type y) +void Level::placeBridge(coordinate x, coordinate y) { map[x][y] = static_cast(177); } -bool Level::isWater(Row::size_type x, Row::size_type y) const +bool Level::isCellOfType(coordinate x, coordinate y, char type) const { - return (map[x][y] == '.'); + return (map[x][y] == type); } -void Level::removeCharge(Row::size_type x, Row::size_type y) +void Level::removeCharge(coordinate x, coordinate y) { map[x][y] = '-'; } diff --git a/level.h b/level.h index 2aa7ccd..cf3f83f 100644 --- a/level.h +++ b/level.h @@ -3,7 +3,8 @@ #include -constexpr int side = 32; +using coordinate = unsigned int; +constexpr coordinate side = 32; using Row = std::array; using Map = std::array; @@ -18,16 +19,16 @@ public: Level(); /// Place a bridge cell - void placeBridge(Row::size_type x, Row::size_type y); + void placeBridge(coordinate x, coordinate y); /// Get the 2D array of level map Map& mapArray() const; - /// Request cell type - bool isWater(Row::size_type x, Row::size_type y) const; + /// Is the following cell has requested type + bool isCellOfType(coordinate x, coordinate y, char type) const; /// Replace a charge cell with a ground cell - void removeCharge(Row::size_type x, Row::size_type y); + void removeCharge(coordinate x, coordinate y); }; #endif // LEVEL_H