Add leveling
This commit is contained in:
parent
9024e5fc51
commit
bfbb81568a
91
src/game.cpp
91
src/game.cpp
|
@ -16,6 +16,9 @@ Game::Game()
|
||||||
|
|
||||||
main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
|
main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
|
||||||
main_window.setActive();
|
main_window.setActive();
|
||||||
|
|
||||||
|
current_level = 1;
|
||||||
|
loadLevel(current_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Game::run()
|
int Game::run()
|
||||||
|
@ -98,26 +101,35 @@ void Game::onMoving(sf::Keyboard::Key &key)
|
||||||
|
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
||||||
// If the following cell is water
|
switch (level->cellOfType(attempt_x, attempt_y))
|
||||||
if (level->isCellOfType(attempt_x, attempt_y, CellType::Water))
|
|
||||||
{
|
{
|
||||||
|
case 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())
|
||||||
level->placeBridge(attempt_x, attempt_y);
|
level->placeBridge(attempt_x, attempt_y);
|
||||||
// If hero doesn't have enough charges, we move Hero back
|
// If hero doesn't have enough charges, we move Hero back
|
||||||
else
|
else
|
||||||
hero->setPosition(initial_x, initial_y);
|
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 picks up the charge; remove it from the map
|
||||||
hero->refillCharges(1);
|
hero->refillCharges(1);
|
||||||
level->removeCharge(attempt_x, attempt_y);
|
level->removeCharge(attempt_x, attempt_y);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CellType::Exit:
|
||||||
|
|
||||||
|
// Hero exists the level!
|
||||||
|
loadLevel(++current_level);
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::renderMap()
|
void Game::renderMap()
|
||||||
|
@ -160,6 +172,9 @@ void Game::renderMap()
|
||||||
case CellType::Bridge:
|
case CellType::Bridge:
|
||||||
rectangle_brush.setFillColor(sf::Color::Black);
|
rectangle_brush.setFillColor(sf::Color::Black);
|
||||||
break;
|
break;
|
||||||
|
case CellType::Exit:
|
||||||
|
rectangle_brush.setFillColor(sf::Color::Red);
|
||||||
|
break;
|
||||||
case CellType::Water:
|
case CellType::Water:
|
||||||
default:
|
default:
|
||||||
rectangle_brush.setFillColor(sf::Color::Blue);
|
rectangle_brush.setFillColor(sf::Color::Blue);
|
||||||
|
@ -186,3 +201,65 @@ void Game::renderMap()
|
||||||
main_window.draw(rectangle_brush);
|
main_window.draw(rectangle_brush);
|
||||||
main_window.draw(text);
|
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<Hero> hero;
|
||||||
std::unique_ptr<Level> level;
|
std::unique_ptr<Level> level;
|
||||||
|
|
||||||
|
int current_level;
|
||||||
|
|
||||||
// SFML entities
|
// SFML entities
|
||||||
sf::RenderWindow main_window;
|
sf::RenderWindow main_window;
|
||||||
|
|
||||||
|
@ -30,6 +32,9 @@ private:
|
||||||
/// Render game state
|
/// Render game state
|
||||||
void renderMap();
|
void renderMap();
|
||||||
|
|
||||||
|
/// Prepare map and hero for a game level
|
||||||
|
void loadLevel(int level_index = 1);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Game();
|
explicit Game();
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,11 @@ bool Hero::useCharge()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Hero::setCharges(int charges) noexcept
|
||||||
|
{
|
||||||
|
hero_charges = charges;
|
||||||
|
}
|
||||||
|
|
||||||
void Hero::position(coordinate &x, coordinate &y) const noexcept
|
void Hero::position(coordinate &x, coordinate &y) const noexcept
|
||||||
{
|
{
|
||||||
x = pos_x;
|
x = pos_x;
|
||||||
|
|
|
@ -31,6 +31,9 @@ public:
|
||||||
/// Spend one charge on action
|
/// Spend one charge on action
|
||||||
bool useCharge();
|
bool useCharge();
|
||||||
|
|
||||||
|
/// Set amount of hero charges explicitly
|
||||||
|
void setCharges(int charges) noexcept;
|
||||||
|
|
||||||
/// Get current Hero position
|
/// Get current Hero position
|
||||||
void position(coordinate &x, coordinate &y) const noexcept;
|
void position(coordinate &x, coordinate &y) const noexcept;
|
||||||
|
|
||||||
|
|
|
@ -1,42 +1,16 @@
|
||||||
#include "level.h"
|
#include "level.h"
|
||||||
|
|
||||||
Level::Level()
|
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)
|
void Level::placeBridge(coordinate x, coordinate y)
|
||||||
{
|
{
|
||||||
map[x][y] = CellType::Bridge;
|
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)
|
void Level::removeCharge(coordinate x, coordinate y)
|
||||||
|
@ -48,3 +22,8 @@ Map& Level::mapArray()
|
||||||
{
|
{
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Level::setMap(const Map &new_map)
|
||||||
|
{
|
||||||
|
map = std::move(new_map);
|
||||||
|
}
|
||||||
|
|
10
src/level.h
10
src/level.h
|
@ -9,7 +9,8 @@ enum class CellType
|
||||||
Ground = '-',
|
Ground = '-',
|
||||||
Charge = '$',
|
Charge = '$',
|
||||||
Bridge = char(177),
|
Bridge = char(177),
|
||||||
Hero = '@'
|
Hero = '@',
|
||||||
|
Exit = '#'
|
||||||
};
|
};
|
||||||
|
|
||||||
using coordinate = unsigned int;
|
using coordinate = unsigned int;
|
||||||
|
@ -33,11 +34,14 @@ public:
|
||||||
/// Get the 2D array of level map
|
/// Get the 2D array of level map
|
||||||
Map& mapArray();
|
Map& mapArray();
|
||||||
|
|
||||||
/// Is the following cell has requested type
|
/// Returns type of requested map cell
|
||||||
bool isCellOfType(coordinate x, coordinate y, CellType type) const;
|
CellType cellOfType(coordinate x, coordinate y) 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);
|
||||||
|
|
||||||
|
/// Set new map for the level
|
||||||
|
void setMap(const Map& new_map);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LEVEL_H
|
#endif // LEVEL_H
|
||||||
|
|
Loading…
Reference in New Issue