BrickGame: Stop bricks from falling when paused

Allows for pausing with either the `P` or `Escape` keys. In this commit
you can still rotate pieces when paused - which makes for an interesting
"stop-time" cheat mechanic, but probably isn't yet what we want.
This commit is contained in:
Karol Baraniecki 2023-03-10 23:54:34 +01:00 committed by Brian Gianforcaro
parent dfb45705e6
commit 06a9e4280b
Notes: sideshowbarker 2024-07-18 22:57:59 +09:00

View file

@ -246,6 +246,7 @@ class Bricks final {
public:
enum class GameState {
Active,
Paused,
GameOver
};
@ -316,6 +317,20 @@ public:
return RenderRequest::RequestUpdate;
}
void toggle_pause()
{
switch (m_state) {
case GameState::Active:
m_state = GameState::Paused;
break;
case GameState::Paused:
m_state = GameState::Active;
break;
case GameState::GameOver:
break;
}
}
[[nodiscard]] RenderRequest update()
{
auto const current_level { m_level };
@ -433,12 +448,17 @@ void BrickGame::reset()
void BrickGame::timer_event(Core::TimerEvent&)
{
if (m_brick_game->state() == Bricks::GameState::GameOver) {
switch (m_brick_game->state()) {
case Bricks::GameState::GameOver:
game_over();
return;
break;
case Bricks::GameState::Active:
if (m_brick_game->update() == Bricks::RenderRequest::RequestUpdate)
update();
break;
case Bricks::GameState::Paused:
break;
}
if (m_brick_game->update() == Bricks::RenderRequest::RequestUpdate)
update();
}
void BrickGame::keydown_event(GUI::KeyEvent& event)
@ -470,6 +490,10 @@ void BrickGame::keydown_event(GUI::KeyEvent& event)
case KeyCode::Key_Space:
render_request = m_brick_game->move_down_fast();
break;
case KeyCode::Key_Escape:
case KeyCode::Key_P:
m_brick_game->toggle_pause();
break;
default:
event.ignore();
break;