Add leveling

master
NaiJi ✨ 4 years ago
parent 9024e5fc51
commit bfbb81568a

@ -16,6 +16,9 @@ Game::Game()
main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
main_window.setActive();
current_level = 1;
loadLevel(current_level);
}
int Game::run()
@ -98,26 +101,35 @@ void Game::onMoving(sf::Keyboard::Key &key)
//////////////////////////
// If the following cell is water
if (level->isCellOfType(attempt_x, attempt_y, CellType::Water))
switch (level->cellOfType(attempt_x, attempt_y))
{
case CellType::Water:
// 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);
}
//////////////////////////
break;
case CellType::Charge:
// If the following cell is a charge
if (level->isCellOfType(attempt_x, attempt_y, CellType::Charge))
{
// Hero picks up the charge; remove it from the map
hero->refillCharges(1);
level->removeCharge(attempt_x, attempt_y);
break;
case CellType::Exit:
// Hero exists the level!
loadLevel(++current_level);
break;
}
}
void Game::renderMap()
@ -160,6 +172,9 @@ void Game::renderMap()
case CellType::Bridge:
rectangle_brush.setFillColor(sf::Color::Black);
break;
case CellType::Exit:
rectangle_brush.setFillColor(sf::Color::Red);
break;
case CellType::Water:
default:
rectangle_brush.setFillColor(sf::Color::Blue);
@ -186,3 +201,65 @@ void Game::renderMap()
main_window.draw(rectangle_brush);
main_window.draw(text);
}
void Game::loadLevel(int level_index)
{
Map map;
// Fill the level with water
for (Row &row : map)
{
for (CellType &cell : row)
cell = CellType::Water;
}
switch (level_index)
{
case 1:
// Hardcoding is temporary!
hero->setPosition(1, 1);
hero->setCharges(2);
map[1][1] = CellType::Ground;
map[1][2] = CellType::Ground;
map[1][3] = CellType::Ground;
map[1][4] = CellType::Ground;
map[2][2] = CellType::Ground;
map[3][2] = CellType::Ground;
map[3][3] = CellType::Ground;
map[3][3] = CellType::Ground;
map[6][3] = CellType::Ground;
map[6][4] = CellType::Ground;
map[6][5] = CellType::Ground;
map[6][6] = CellType::Ground;
map[7][6] = CellType::Ground;
map[9][6] = CellType::Ground;
map[8][7] = CellType::Exit;
map[2][3] = CellType::Charge;
level->setMap(map);
break;
case 2:
// Hardcoding is temporary!
hero->setPosition(5, 5);
hero->setCharges(10);
map[5][6] = CellType::Ground;
map[5][5] = CellType::Ground;
map[5][4] = CellType::Ground;
map[4][5] = CellType::Ground;
map[4][6] = CellType::Ground;
map[4][4] = CellType::Ground;
map[6][6] = CellType::Ground;
map[6][5] = CellType::Ground;
map[6][4] = CellType::Ground;
map[6][7] = CellType::Ground;
map[6][8] = CellType::Ground;
map[5][8] = CellType::Ground;
map[8][8] = CellType::Ground;
map[8][9] = CellType::Ground;
map[8][10] = CellType::Exit;
map[4][7] = CellType::Charge;
level->setMap(map);
break;
default:
main_window.close();
}
}

@ -18,6 +18,8 @@ private:
std::unique_ptr<Hero> hero;
std::unique_ptr<Level> level;
int current_level;
// SFML entities
sf::RenderWindow main_window;
@ -30,6 +32,9 @@ private:
/// Render game state
void renderMap();
/// Prepare map and hero for a game level
void loadLevel(int level_index = 1);
public:
explicit Game();

@ -27,6 +27,11 @@ bool Hero::useCharge()
return false;
}
void Hero::setCharges(int charges) noexcept
{
hero_charges = charges;
}
void Hero::position(coordinate &x, coordinate &y) const noexcept
{
x = pos_x;

@ -31,6 +31,9 @@ public:
/// Spend one charge on action
bool useCharge();
/// Set amount of hero charges explicitly
void setCharges(int charges) noexcept;
/// Get current Hero position
void position(coordinate &x, coordinate &y) const noexcept;

@ -1,42 +1,16 @@
#include "level.h"
Level::Level()
{
// Fill the level with water
for (Row &row : map)
{
for (CellType &cell : row)
cell = CellType::Water;
}
// Temporary mock
map[1][1] = CellType::Ground;
map[1][2] = CellType::Ground;
map[1][3] = CellType::Ground;
map[1][4] = CellType::Ground;
map[2][2] = CellType::Ground;
map[3][2] = CellType::Ground;
map[3][3] = CellType::Ground;
map[3][3] = CellType::Ground;
map[6][3] = CellType::Ground;
map[6][4] = CellType::Ground;
map[6][5] = CellType::Ground;
map[6][6] = CellType::Ground;
map[7][6] = CellType::Ground;
map[9][6] = CellType::Ground;
map[2][3] = CellType::Charge;
}
{}
void Level::placeBridge(coordinate x, coordinate y)
{
map[x][y] = CellType::Bridge;
}
bool Level::isCellOfType(coordinate x, coordinate y, CellType type) const
CellType Level::cellOfType(coordinate x, coordinate y) const
{
return (map[x][y] == type);
return map[x][y];
}
void Level::removeCharge(coordinate x, coordinate y)
@ -48,3 +22,8 @@ Map& Level::mapArray()
{
return map;
}
void Level::setMap(const Map &new_map)
{
map = std::move(new_map);
}

@ -9,7 +9,8 @@ enum class CellType
Ground = '-',
Charge = '$',
Bridge = char(177),
Hero = '@'
Hero = '@',
Exit = '#'
};
using coordinate = unsigned int;
@ -33,11 +34,14 @@ public:
/// Get the 2D array of level map
Map& mapArray();
/// Is the following cell has requested type
bool isCellOfType(coordinate x, coordinate y, CellType type) const;
/// Returns type of requested map cell
CellType cellOfType(coordinate x, coordinate y) const;
/// Replace a charge cell with a ground cell
void removeCharge(coordinate x, coordinate y);
/// Set new map for the level
void setMap(const Map& new_map);
};
#endif // LEVEL_H

Loading…
Cancel
Save