Finish game logic

master
NaiJi ✨ 4 years ago
parent 5ad777e03b
commit 0e647faf20

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

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

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

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

@ -3,7 +3,8 @@
#include <array>
constexpr int side = 32;
using coordinate = unsigned int;
constexpr coordinate side = 32;
using Row = std::array<char, side>;
using Map = std::array<Row, side>;
@ -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

Loading…
Cancel
Save