2020-02-19 12:50:09 -05:00
|
|
|
#include "game.h"
|
|
|
|
|
|
|
|
Game::Game()
|
|
|
|
{
|
2020-02-20 13:34:41 -05:00
|
|
|
// Place the player with 10 initial charges onto x: 1, y: 1
|
|
|
|
hero = std::make_unique<Hero>(1, 1, 10);
|
|
|
|
|
|
|
|
// Generate level
|
|
|
|
level = std::make_unique<Level>();
|
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
main_window.create(sf::VideoMode(window_side, window_side), "SFML-Test Application", sf::Style::Default);
|
|
|
|
main_window.setActive();
|
2020-02-19 12:50:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int Game::run()
|
|
|
|
{
|
|
|
|
clock = std::make_unique<sf::Clock>();
|
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
// To where player moved each step of the game loop
|
|
|
|
Direction direction;
|
|
|
|
|
2020-02-19 12:50:09 -05:00
|
|
|
// On the game loop
|
|
|
|
while (main_window.isOpen())
|
|
|
|
{
|
2020-02-21 10:24:03 -05:00
|
|
|
// By default player doesn't move anywhere
|
|
|
|
direction = Direction::None;
|
|
|
|
|
2020-02-19 12:50:09 -05:00
|
|
|
sf::Event event;
|
|
|
|
while (main_window.pollEvent(event))
|
|
|
|
{
|
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
main_window.close();
|
|
|
|
|
2020-02-20 13:34:41 -05:00
|
|
|
// Handling keyboard activity
|
|
|
|
if (event.type == sf::Event::KeyPressed)
|
|
|
|
{
|
|
|
|
// Move
|
2020-02-21 10:24:03 -05:00
|
|
|
direction = onMoving(event.key.code);
|
2020-02-20 13:34:41 -05:00
|
|
|
}
|
2020-02-19 12:50:09 -05:00
|
|
|
}
|
2020-02-21 10:24:03 -05:00
|
|
|
|
|
|
|
// Draw level
|
|
|
|
renderMap(direction);
|
|
|
|
|
|
|
|
main_window.display();
|
2020-02-19 12:50:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2020-02-20 13:34:41 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
////////////////////////////////////////////////////
|
|
|
|
|
2020-02-20 13:34:41 -05:00
|
|
|
Direction Game::getDirection(sf::Keyboard::Key &key) const
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case sf::Keyboard::A:
|
|
|
|
case sf::Keyboard::Left:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num4:
|
|
|
|
return Direction::Left;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
case sf::Keyboard::W:
|
|
|
|
case sf::Keyboard::Up:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num8:
|
|
|
|
return Direction::Up;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
case sf::Keyboard::D:
|
|
|
|
case sf::Keyboard::Right:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num6:
|
|
|
|
return Direction::Right;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
case sf::Keyboard::S:
|
|
|
|
case sf::Keyboard::Down:
|
2020-02-21 09:13:12 -05:00
|
|
|
case sf::Keyboard::Num2:
|
|
|
|
return Direction::Down;
|
2020-02-20 13:34:41 -05:00
|
|
|
|
|
|
|
default:
|
2020-02-21 09:13:12 -05:00
|
|
|
return Direction::None;
|
2020-02-20 13:34:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
Direction Game::onMoving(sf::Keyboard::Key &key)
|
2020-02-20 13:34:41 -05:00
|
|
|
{
|
|
|
|
// Determine where to move
|
|
|
|
const Direction direction = getDirection(key);
|
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
if (direction != Direction::None)
|
|
|
|
{
|
|
|
|
// Save the initial coordinates
|
|
|
|
coordinate initial_x, initial_y;
|
|
|
|
hero->position(initial_x, initial_y);
|
2020-02-20 13:34:41 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
// Try to move hero
|
|
|
|
hero->move(direction);
|
2020-02-20 13:34:41 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
// Save the new coordinates after moving
|
|
|
|
coordinate attempt_x, attempt_y;
|
|
|
|
hero->position(attempt_x, attempt_y);
|
2020-02-20 13:34:41 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
//////////////////////////
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
// If the following cell is water
|
|
|
|
if (level->isCellOfType(attempt_x, attempt_y, 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);
|
|
|
|
}
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
//////////////////////////
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
// 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);
|
|
|
|
}
|
2020-02-21 09:13:12 -05:00
|
|
|
}
|
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::renderMap(const Direction direction)
|
|
|
|
{
|
|
|
|
if (direction == Direction::None)
|
|
|
|
return; // Player didn't move this step of loop, so map couldn't change
|
|
|
|
|
|
|
|
const Map &map = level->mapArray();
|
|
|
|
|
|
|
|
coordinate painter_x = 0, painter_y = 0;
|
2020-02-21 09:13:12 -05:00
|
|
|
|
2020-02-21 10:24:03 -05:00
|
|
|
for (const Row &row : map)
|
2020-02-21 09:13:12 -05:00
|
|
|
{
|
2020-02-21 10:24:03 -05:00
|
|
|
for (const CellType &cell : row)
|
|
|
|
{
|
|
|
|
switch (cell)
|
|
|
|
{ // TO DO!!
|
|
|
|
case CellType::Ground:
|
|
|
|
; break;
|
|
|
|
case CellType::Charge:
|
|
|
|
; break;
|
|
|
|
case CellType::Bridge:
|
|
|
|
; break;
|
|
|
|
case CellType::Water:
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move painter to next cell of row
|
|
|
|
painter_x += cell_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move painter to next row of the map
|
|
|
|
painter_x = 0;
|
|
|
|
painter_y += cell_length;
|
2020-02-21 09:13:12 -05:00
|
|
|
}
|
2020-02-20 13:34:41 -05:00
|
|
|
}
|