cirno-puzzle/src/game.cpp

113 lines
2.6 KiB
C++
Raw Normal View History

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
2020-02-21 16:55:13 -05:00
hero = std::make_unique<Hero>(1, 1, 2);
2020-02-20 13:34:41 -05:00
// Generate level
level = std::make_unique<Level>();
2020-03-19 17:59:21 -04:00
audio = std::make_unique<Audio>("background_music.ogg", std::array<std::string, N_SOUNDS> { "footstep_sound.wav" });
2020-03-19 11:31:21 -04:00
// Prepare level renderer
renderer = std::make_unique<Renderer>();
2020-03-19 17:59:21 -04:00
main_window.create(sf::VideoMode(renderer->windowSize() * 3, renderer->windowSize() * 3), "SFML-Test Application", sf::Style::Default);
main_window.setActive();
2020-03-19 11:31:21 -04:00
main_window.setFramerateLimit(60);
current_level = 1;
2020-03-19 17:59:21 -04:00
audio->setBackgroundVolume(15.f);
2020-02-19 12:50:09 -05:00
}
int Game::run()
{
2020-03-19 17:59:21 -04:00
audio->playBackground();
2020-02-19 12:50:09 -05:00
// On the game loop
while (main_window.isOpen())
{
sf::Event event;
while (main_window.pollEvent(event))
{
2020-03-19 17:59:21 -04:00
switch (event.type)
{
case sf::Event::Closed:
2020-02-19 12:50:09 -05:00
main_window.close();
2020-03-19 17:59:21 -04:00
break;
2020-02-19 12:50:09 -05:00
2020-03-19 17:59:21 -04:00
case sf::Event::KeyPressed:
audio->playSound(FOOTSTEP_SOUND);
2020-02-21 12:34:28 -05:00
onMoving(event.key.code);
2020-03-19 17:59:21 -04:00
break;
2020-03-19 11:31:21 -04:00
2020-03-19 17:59:21 -04:00
default:
break;
2020-02-20 13:34:41 -05:00
}
2020-02-19 12:50:09 -05:00
}
2020-03-19 17:59:21 -04:00
renderer->render(level, hero, main_window);
main_window.display();
2020-02-19 12:50:09 -05:00
}
2020-03-19 17:59:21 -04:00
audio->stopBackground();
2020-02-19 12:50:09 -05:00
return EXIT_SUCCESS;
}
2020-02-20 13:34:41 -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 12:34:28 -05:00
void 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 12:34:28 -05:00
if (direction == Direction::None)
return;
2020-02-20 13:34:41 -05:00
2020-02-21 12:34:28 -05:00
// Save the initial coordinates
2020-03-15 17:57:39 -04:00
coordinate initial_row, initial_col;
hero->position(initial_row, initial_col);
2020-02-20 13:34:41 -05:00
2020-02-21 12:34:28 -05:00
// Try to move hero
hero->move(direction);
2020-02-21 09:13:12 -05:00
2020-02-21 12:34:28 -05:00
// Save the new coordinates after moving
2020-03-15 17:57:39 -04:00
coordinate attempt_row, attempt_col;
hero->position(attempt_row, attempt_col);
2020-02-21 09:13:12 -05:00
2020-02-21 12:34:28 -05:00
//////////////////////////
2020-02-21 09:13:12 -05:00
if (!level->getCellAt(attempt_row, attempt_col)->onMovingTo(hero, level))
2020-03-15 17:57:39 -04:00
hero->setPosition(initial_row, initial_col);
}