Implement basic interfaces

This commit is contained in:
NaiJi ✨ 2020-02-20 21:34:41 +03:00
parent f8f640ae89
commit 5ad777e03b
6 changed files with 169 additions and 14 deletions

View File

@ -2,6 +2,12 @@
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);
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
}

17
game.h
View File

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

View File

@ -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;
}
}

31
hero.h
View File

@ -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
inline void refillCharges(int append_charges);
/// Add more charges for hero to use
void refillCharges(int append_charges);
// Get amount of charges
inline int charges() const noexcept;
/// Get amount of charges
int charges() const noexcept;
// Spend one charge on action
inline bool useCharge();
/// Spend one charge on action
bool useCharge();
/// Get current Hero position
void position(int &x, int &y) const noexcept;
/// Move hero by one cell to any direction
void move(Direction &direction);
};
#endif // HERO_H

View File

@ -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<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] = '-';
}

16
level.h
View File

@ -5,8 +5,10 @@
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
{
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