Finish game logic
This commit is contained in:
parent
5ad777e03b
commit
0e647faf20
49
game.cpp
49
game.cpp
|
@ -43,22 +43,26 @@ Direction Game::getDirection(sf::Keyboard::Key &key) const
|
||||||
{
|
{
|
||||||
case sf::Keyboard::A:
|
case sf::Keyboard::A:
|
||||||
case sf::Keyboard::Left:
|
case sf::Keyboard::Left:
|
||||||
return Direction::LEFT;
|
case sf::Keyboard::Num4:
|
||||||
|
return Direction::Left;
|
||||||
|
|
||||||
case sf::Keyboard::W:
|
case sf::Keyboard::W:
|
||||||
case sf::Keyboard::Up:
|
case sf::Keyboard::Up:
|
||||||
return Direction::UP;
|
case sf::Keyboard::Num8:
|
||||||
|
return Direction::Up;
|
||||||
|
|
||||||
case sf::Keyboard::D:
|
case sf::Keyboard::D:
|
||||||
case sf::Keyboard::Right:
|
case sf::Keyboard::Right:
|
||||||
return Direction::RIGHT;
|
case sf::Keyboard::Num6:
|
||||||
|
return Direction::Right;
|
||||||
|
|
||||||
case sf::Keyboard::S:
|
case sf::Keyboard::S:
|
||||||
case sf::Keyboard::Down:
|
case sf::Keyboard::Down:
|
||||||
return Direction::DOWN;
|
case sf::Keyboard::Num2:
|
||||||
|
return Direction::Down;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return Direction::NONE;
|
return Direction::None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,13 +71,42 @@ void Game::onMoving(sf::Keyboard::Key &key)
|
||||||
// Determine where to move
|
// Determine where to move
|
||||||
const Direction direction = getDirection(key);
|
const Direction direction = getDirection(key);
|
||||||
|
|
||||||
if (direction == Direction::NONE)
|
if (direction == Direction::None)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
||||||
int initial_x, initial_y;
|
// Save the initial coordinates
|
||||||
|
coordinate initial_x, initial_y;
|
||||||
hero->position(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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
22
hero.cpp
22
hero.cpp
|
@ -1,6 +1,6 @@
|
||||||
#include "hero.h"
|
#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),
|
hero_charges(initial_charges),
|
||||||
pos_x(position_x),
|
pos_x(position_x),
|
||||||
pos_y(position_y)
|
pos_y(position_y)
|
||||||
|
@ -27,25 +27,31 @@ bool Hero::useCharge()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hero::position(int &x, int &y) const noexcept
|
void Hero::position(coordinate &x, coordinate &y) const noexcept
|
||||||
{
|
{
|
||||||
x = pos_x;
|
x = pos_x;
|
||||||
y = pos_y;
|
y = pos_y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hero::move(Direction &direction)
|
void Hero::move(const Direction &direction)
|
||||||
{
|
{
|
||||||
switch (direction)
|
switch (direction)
|
||||||
{
|
{
|
||||||
case Direction::UP:
|
case Direction::Up:
|
||||||
--pos_y; break;
|
--pos_y; break;
|
||||||
case Direction::DOWN:
|
case Direction::Down:
|
||||||
++pos_y; break;
|
++pos_y; break;
|
||||||
case Direction::LEFT:
|
case Direction::Left:
|
||||||
--pos_x; break;
|
--pos_x; break;
|
||||||
case Direction::RIGHT:
|
case Direction::Right:
|
||||||
++pos_x; break;
|
++pos_x; break;
|
||||||
case Direction::NONE:
|
case Direction::None:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Hero::setPosition(coordinate x, coordinate y)
|
||||||
|
{
|
||||||
|
pos_x = x;
|
||||||
|
pos_y = y;
|
||||||
|
}
|
||||||
|
|
23
hero.h
23
hero.h
|
@ -3,22 +3,24 @@
|
||||||
|
|
||||||
enum class Direction
|
enum class Direction
|
||||||
{
|
{
|
||||||
LEFT,
|
Left,
|
||||||
UP,
|
Up,
|
||||||
RIGHT,
|
Right,
|
||||||
DOWN,
|
Down,
|
||||||
NONE
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using coordinate = unsigned int;
|
||||||
|
|
||||||
/// Represents a controlable by player game character
|
/// Represents a controlable by player game character
|
||||||
class Hero
|
class Hero
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int hero_charges;
|
int hero_charges;
|
||||||
int pos_x, pos_y;
|
coordinate pos_x, pos_y;
|
||||||
|
|
||||||
public:
|
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
|
/// Add more charges for hero to use
|
||||||
void refillCharges(int append_charges);
|
void refillCharges(int append_charges);
|
||||||
|
@ -30,10 +32,13 @@ public:
|
||||||
bool useCharge();
|
bool useCharge();
|
||||||
|
|
||||||
/// Get current Hero position
|
/// 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
|
/// Move hero by one cell to any direction
|
||||||
void move(Direction &direction);
|
void move(const Direction &direction);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HERO_H
|
#endif // HERO_H
|
||||||
|
|
|
@ -22,17 +22,17 @@ Level::Level()
|
||||||
map[2][3] = '$';
|
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<char>(177);
|
map[x][y] = static_cast<char>(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] = '-';
|
map[x][y] = '-';
|
||||||
}
|
}
|
||||||
|
|
11
level.h
11
level.h
|
@ -3,7 +3,8 @@
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
constexpr int side = 32;
|
using coordinate = unsigned int;
|
||||||
|
constexpr coordinate side = 32;
|
||||||
|
|
||||||
using Row = std::array<char, side>;
|
using Row = std::array<char, side>;
|
||||||
using Map = std::array<Row, side>;
|
using Map = std::array<Row, side>;
|
||||||
|
@ -18,16 +19,16 @@ public:
|
||||||
Level();
|
Level();
|
||||||
|
|
||||||
/// Place a bridge cell
|
/// 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
|
/// Get the 2D array of level map
|
||||||
Map& mapArray() const;
|
Map& mapArray() const;
|
||||||
|
|
||||||
/// Request cell type
|
/// Is the following cell has requested type
|
||||||
bool isWater(Row::size_type x, Row::size_type y) const;
|
bool isCellOfType(coordinate x, coordinate y, char type) const;
|
||||||
|
|
||||||
/// Replace a charge cell with a ground cell
|
/// 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
|
#endif // LEVEL_H
|
||||||
|
|
Loading…
Reference in New Issue