Replace raw chars with CellType enum variables

This commit is contained in:
NaiJi ✨ 2020-02-21 17:20:40 +03:00
parent 0e647faf20
commit 5e5ea14693
3 changed files with 26 additions and 19 deletions

View File

@ -90,7 +90,7 @@ void Game::onMoving(sf::Keyboard::Key &key)
////////////////////////// //////////////////////////
// If the following cell is water // If the following cell is water
if (level->isCellOfType(attempt_x, attempt_y, '.')) if (level->isCellOfType(attempt_x, attempt_y, CellType::Water))
{ {
// Try to use one charge to place a bridge // Try to use one charge to place a bridge
if (hero->useCharge()) if (hero->useCharge())
@ -103,7 +103,7 @@ void Game::onMoving(sf::Keyboard::Key &key)
////////////////////////// //////////////////////////
// If the following cell is a charge // If the following cell is a charge
if (level->isCellOfType(attempt_x, attempt_y, '$')) if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge))
{ {
// Hero picks up the charge; remove it from the map // Hero picks up the charge; remove it from the map
hero->refillCharges(1); hero->refillCharges(1);

View File

@ -5,34 +5,33 @@ Level::Level()
// Fill the level with water // Fill the level with water
for (Row &row : map) for (Row &row : map)
{ {
for (char &cell : row) for (CellType &cell : row)
cell = '.'; cell = CellType::Water;
} }
// Temporary mock // Temporary mock
// Ground map[1][2] = CellType::Ground;
map[1][2] = '-'; map[1][3] = CellType::Ground;
map[1][3] = '-'; map[1][4] = CellType::Ground;
map[1][4] = '-'; map[2][2] = CellType::Ground;
map[2][2] = '-'; map[3][2] = CellType::Ground;
map[3][2] = '-'; map[3][3] = CellType::Ground;
map[3][3] = '-';
// Charge map[2][3] = CellType::Charge;
map[2][3] = '$';
} }
void Level::placeBridge(coordinate x, coordinate y) void Level::placeBridge(coordinate x, coordinate y)
{ {
map[x][y] = static_cast<char>(177); map[x][y] = CellType::Bridge;
} }
bool Level::isCellOfType(coordinate x, coordinate y, char type) const bool Level::isCellOfType(coordinate x, coordinate y, CellType type) const
{ {
return (map[x][y] == type); return (map[x][y] == type);
} }
void Level::removeCharge(coordinate x, coordinate y) void Level::removeCharge(coordinate x, coordinate y)
{ {
map[x][y] = '-'; map[x][y] = CellType::Ground;
} }

14
level.h
View File

@ -3,13 +3,21 @@
#include <array> #include <array>
enum CellType
{
Water = '.',
Ground = '-',
Charge = '$',
Bridge = char(177)
};
using coordinate = unsigned int; using coordinate = unsigned int;
constexpr coordinate side = 32; constexpr coordinate side = 32;
using Row = std::array<char, side>; using Row = std::array<CellType, side>;
using Map = std::array<Row, side>; using Map = std::array<Row, side>;
/// Abstraction over 2D array to quickly get access to unchangable level cells /// Abstraction over 2D array to quickly get access to level cells
class Level class Level
{ {
private: private:
@ -25,7 +33,7 @@ public:
Map& mapArray() const; Map& mapArray() const;
/// Is the following cell has requested type /// Is the following cell has requested type
bool isCellOfType(coordinate x, coordinate y, char type) const; bool isCellOfType(coordinate x, coordinate y, CellType type) const;
/// Replace a charge cell with a ground cell /// Replace a charge cell with a ground cell
void removeCharge(coordinate x, coordinate y); void removeCharge(coordinate x, coordinate y);