diff --git a/game.cpp b/game.cpp index 792ac98..51adcc5 100644 --- a/game.cpp +++ b/game.cpp @@ -2,6 +2,12 @@ Game::Game() { + // Place the player with 10 initial charges onto x: 1, y: 1 + hero = std::make_unique(1, 1, 10); + + // Generate level + level = std::make_unique(); + sf::Window window(sf::VideoMode(640, 480), "SFML-Test Application", sf::Style::Default); window.setActive(); } @@ -19,10 +25,55 @@ int Game::run() if (event.type == sf::Event::Closed) main_window.close(); - if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)) - main_window.close(); + // Handling keyboard activity + if (event.type == sf::Event::KeyPressed) + { + // Move + onMoving(event.key.code); + } } } return EXIT_SUCCESS; } + +Direction Game::getDirection(sf::Keyboard::Key &key) const +{ + switch (key) + { + case sf::Keyboard::A: + case sf::Keyboard::Left: + return Direction::LEFT; + + case sf::Keyboard::W: + case sf::Keyboard::Up: + return Direction::UP; + + case sf::Keyboard::D: + case sf::Keyboard::Right: + return Direction::RIGHT; + + case sf::Keyboard::S: + case sf::Keyboard::Down: + return Direction::DOWN; + + default: + return Direction::NONE; + } +} + +void Game::onMoving(sf::Keyboard::Key &key) +{ + // Determine where to move + const Direction direction = getDirection(key); + + if (direction == Direction::NONE) + return; + + ////////////////////////// + + int initial_x, initial_y; + hero->position(initial_x, initial_y); + + // TO DO THE REST +} diff --git a/game.h b/game.h index 39ea3bf..6b74af7 100644 --- a/game.h +++ b/game.h @@ -6,16 +6,31 @@ #include #include +#include "hero.h" +#include "level.h" + +/// The main class where all the process happens class Game { private: + // Game entities + std::unique_ptr hero; + std::unique_ptr level; + + // SFML entities std::unique_ptr clock; sf::Window 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); + public: explicit Game(); - // Start the game loop + /// Start the game loop int run(); }; diff --git a/hero.cpp b/hero.cpp index e713c06..1f0deb9 100644 --- a/hero.cpp +++ b/hero.cpp @@ -1,7 +1,9 @@ #include "hero.h" -Hero::Hero(int initial_charges) : - hero_charges(initial_charges) +Hero::Hero(int position_x, int position_y, int initial_charges) : + hero_charges(initial_charges), + pos_x(position_x), + pos_y(position_y) {} void Hero::refillCharges(int append_charges) @@ -16,10 +18,34 @@ int Hero::charges() const noexcept bool Hero::useCharge() { - if (hero_charges > 0) { + if (hero_charges > 0) + { --hero_charges; return true; } return false; } + +void Hero::position(int &x, int &y) const noexcept +{ + x = pos_x; + y = pos_y; +} + +void Hero::move(Direction &direction) +{ + switch (direction) + { + case Direction::UP: + --pos_y; break; + case Direction::DOWN: + ++pos_y; break; + case Direction::LEFT: + --pos_x; break; + case Direction::RIGHT: + ++pos_x; break; + case Direction::NONE: + break; + } +} diff --git a/hero.h b/hero.h index dc462e5..7e73c50 100644 --- a/hero.h +++ b/hero.h @@ -1,22 +1,39 @@ #ifndef HERO_H #define HERO_H +enum class Direction +{ + LEFT, + UP, + RIGHT, + DOWN, + NONE +}; + +/// Represents a controlable by player game character class Hero { private: int hero_charges; + int pos_x, pos_y; public: - explicit Hero(int initial_charges = 0); + explicit Hero(int position_x = 0, int position_y = 0, int initial_charges = 0); + + /// Add more charges for hero to use + void refillCharges(int append_charges); + + /// Get amount of charges + int charges() const noexcept; - // Add more charges for hero to use - inline void refillCharges(int append_charges); + /// Spend one charge on action + bool useCharge(); - // Get amount of charges - inline int charges() const noexcept; + /// Get current Hero position + void position(int &x, int &y) const noexcept; - // Spend one charge on action - inline bool useCharge(); + /// Move hero by one cell to any direction + void move(Direction &direction); }; #endif // HERO_H diff --git a/level.cpp b/level.cpp index c3545be..8b105e0 100644 --- a/level.cpp +++ b/level.cpp @@ -2,5 +2,37 @@ Level::Level() { + // Fill the level with water + for (Row &row : map) + { + for (char &cell : row) + cell = '.'; + } + // Temporary mock + + // Ground + map[1][2] = '-'; + map[1][3] = '-'; + map[1][4] = '-'; + map[2][2] = '-'; + map[3][2] = '-'; + map[3][3] = '-'; + // Charge + map[2][3] = '$'; +} + +void Level::placeBridge(Row::size_type x, Row::size_type y) +{ + map[x][y] = static_cast(177); +} + +bool Level::isWater(Row::size_type x, Row::size_type y) const +{ + return (map[x][y] == '.'); +} + +void Level::removeCharge(Row::size_type x, Row::size_type y) +{ + map[x][y] = '-'; } diff --git a/level.h b/level.h index e94475d..2aa7ccd 100644 --- a/level.h +++ b/level.h @@ -5,8 +5,10 @@ constexpr int side = 32; -using Map = std::array, side>; +using Row = std::array; +using Map = std::array; +/// Abstraction over 2D array to quickly get access to unchangable level cells class Level { private: @@ -14,6 +16,18 @@ private: public: Level(); + + /// Place a bridge cell + void placeBridge(Row::size_type x, Row::size_type 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; + + /// Replace a charge cell with a ground cell + void removeCharge(Row::size_type x, Row::size_type y); }; #endif // LEVEL_H