Implement basic interfaces

master
NaiJi ✨ 4 years ago
parent f8f640ae89
commit 5ad777e03b

@ -2,6 +2,12 @@
Game::Game() Game::Game()
{ {
// Place the player with 10 initial charges onto x: 1, y: 1
hero = std::make_unique<Hero>(1, 1, 10);
// Generate level
level = std::make_unique<Level>();
sf::Window window(sf::VideoMode(640, 480), "SFML-Test Application", sf::Style::Default); sf::Window window(sf::VideoMode(640, 480), "SFML-Test Application", sf::Style::Default);
window.setActive(); window.setActive();
} }
@ -19,10 +25,55 @@ int Game::run()
if (event.type == sf::Event::Closed) if (event.type == sf::Event::Closed)
main_window.close(); main_window.close();
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)) // Handling keyboard activity
main_window.close(); if (event.type == sf::Event::KeyPressed)
{
// Move
onMoving(event.key.code);
}
} }
} }
return EXIT_SUCCESS; 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
}

@ -6,16 +6,31 @@
#include <SFML/System/Time.hpp> #include <SFML/System/Time.hpp>
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include "hero.h"
#include "level.h"
/// The main class where all the process happens
class Game class Game
{ {
private: private:
// Game entities
std::unique_ptr<Hero> hero;
std::unique_ptr<Level> level;
// SFML entities
std::unique_ptr<sf::Clock> clock; std::unique_ptr<sf::Clock> clock;
sf::Window main_window; 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: public:
explicit Game(); explicit Game();
// Start the game loop /// Start the game loop
int run(); int run();
}; };

@ -1,7 +1,9 @@
#include "hero.h" #include "hero.h"
Hero::Hero(int initial_charges) : Hero::Hero(int position_x, int position_y, int initial_charges) :
hero_charges(initial_charges) hero_charges(initial_charges),
pos_x(position_x),
pos_y(position_y)
{} {}
void Hero::refillCharges(int append_charges) void Hero::refillCharges(int append_charges)
@ -16,10 +18,34 @@ int Hero::charges() const noexcept
bool Hero::useCharge() bool Hero::useCharge()
{ {
if (hero_charges > 0) { if (hero_charges > 0)
{
--hero_charges; --hero_charges;
return true; return true;
} }
return false; 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;
}
}

@ -1,22 +1,39 @@
#ifndef HERO_H #ifndef HERO_H
#define HERO_H #define HERO_H
enum class Direction
{
LEFT,
UP,
RIGHT,
DOWN,
NONE
};
/// Represents a controlable by player game character
class Hero class Hero
{ {
private: private:
int hero_charges; int hero_charges;
int pos_x, pos_y;
public: 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 /// Spend one charge on action
inline void refillCharges(int append_charges); bool useCharge();
// Get amount of charges /// Get current Hero position
inline int charges() const noexcept; void position(int &x, int &y) const noexcept;
// Spend one charge on action /// Move hero by one cell to any direction
inline bool useCharge(); void move(Direction &direction);
}; };
#endif // HERO_H #endif // HERO_H

@ -2,5 +2,37 @@
Level::Level() 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<char>(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] = '-';
} }

@ -5,8 +5,10 @@
constexpr int side = 32; constexpr int side = 32;
using Map = std::array<std::array<int, side>, side>; using Row = std::array<char, side>;
using Map = std::array<Row, side>;
/// Abstraction over 2D array to quickly get access to unchangable level cells
class Level class Level
{ {
private: private:
@ -14,6 +16,18 @@ private:
public: public:
Level(); 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 #endif // LEVEL_H

Loading…
Cancel
Save