A pause menu
Pausing is the game over again, without the ending: a scene pushed on top of the
game, which freezes it, and taken off again to carry on. The game scene does not get a
paused flag, and nothing in it checks one.
The pause scene#
Two new files in src/scenes/:
#pragma once
// Pushed on top of the game. The game underneath freezes, stays on screen and
// stops hearing the input: that is what any scene pushed on top does, so
// nothing here says it.
#include <rmp/scene.h>
class PauseScene : public rmp::Scene {
public:
void _update(float delta) override;
void _draw() override;
};#include "pause.h"
#include "main_menu.h"
#include <rmp/input.h>
#include <rmp/ui.h>
void PauseScene::_update(float /*delta*/) {
// The same key that paused resumes.
if (rmp::input::just_pressed("pause")) rmp::Scene::pop();
}
void PauseScene::_draw() {
// A veil over the game, which has already drawn this frame.
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Color{ 0, 0, 0, 150 });
rmp::ui::begin();
rmp::ui::text("Paused", { .size = rmp::ui::Size::LARGE });
if (rmp::ui::button("Resume")) rmp::Scene::pop();
if (rmp::ui::button("Menu")) rmp::Scene::change<MainMenuScene>();
rmp::ui::end();
}rmp::Scene::pop() takes the pause scene off
the top, and the game underneath carries on from the frame it stopped at, with every bat where it
was. Menu is change<MainMenuScene>(), which ends both: the pause scene and the
game under it.
The same key that paused also resumes, which is why the pause scene reads the
pause action in its own _update(). The press that pushed it does not pop
it again straight away: the push happens at the end of the frame, and by the next one the key is
held, not pressed.
What a scene lets through#
Nothing in PauseScene says "freeze the game", and that is the point. Three fields
of every scene decide what reaches the scene underneath it, and their defaults are a pause
menu:
| Field | Default | When it is true |
|---|---|---|
updates_below | false | The scene below keeps running. |
draws_below | true | The scene below stays on screen. |
input_below | false | The scene below still hears the input. |
So with the pause scene on top, the game is drawn and nothing in it runs: no bat moves, no
_update() counts down to the next one, and the game's own pause check
is not even reached. Its interface is drawn too, and nothing in it can be pressed. The game over
scene of the last page works the same way, with the same defaults.
Pausing#
The game scene now pushes the pause scene where it used to go back to the menu. Only its code changes; its header is as it was:
#include "game.h"
#include "../objects/layers.h"
#include "../objects/player.h"
#include "game_over.h"
#include "pause.h"
#include <rmp/behavior.h>
#include <rmp/input.h>
#include <rmp/random.h>
#include <rmp/ui.h>
#include <string>
namespace {
// The arena is the design resolution: [window] width and height in the .toml.
constexpr Rectangle ARENA{ 0, 0, RMP_WINDOW_WIDTH, RMP_WINDOW_HEIGHT };
constexpr float SECONDS_PER_BAT = 4;
} // namespace
void GameScene::_ready() {
background = Color{ 52, 92, 60, 255 }; // grass
// P, Escape, or the gamepad's Start button.
rmp::input::action("pause", KEY_P, KEY_ESCAPE, GAMEPAD_BUTTON_MIDDLE_RIGHT);
auto &player = spawn<Player>({
.position = { ARENA.width / 2, ARENA.height / 2 },
.edges = rmp::Edge::CLAMP,
.bounds = ARENA,
});
player.add<rmp::behavior::Health>({
.hp = 3,
.destroy_on_death = false, // it stays on screen, under the game over
.on_death = [this](rmp::Object &) { caught(); },
.hurt_by = layer::BAT,
});
_player = player.handle();
spawn_coin();
}
void GameScene::_update(float delta) {
if (rmp::input::just_pressed("pause")) rmp::Scene::push<PauseScene>();
_next_bat -= delta;
if (_next_bat <= 0) {
spawn_bat();
_next_bat = SECONDS_PER_BAT;
}
}
void GameScene::_draw() {
const int lives = _player->get<rmp::behavior::Health>()->hp;
rmp::ui::begin({ .placement = rmp::ui::Align::TOP_LEFT });
rmp::ui::row({ .gap = 24 }, [&] {
rmp::ui::text("Coins " + std::to_string(_coins));
rmp::ui::text("Lives " + std::to_string(lives));
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_IOS)
// A phone has no key to pause with.
if (rmp::ui::button("Pause", { .style = rmp::ui::Variant::GHOST })) {
rmp::Scene::push<PauseScene>();
}
#endif
});
rmp::ui::end();
}
void GameScene::spawn_coin() {
// Anywhere in the arena, clear of its edges and of the score at the top.
const Vector2 at{ rmp::random::range(40.0f, ARENA.width - 40),
rmp::random::range(80.0f, ARENA.height - 40) };
auto &coin = spawn({ .position = at, .shape = rmp::circle(10) });
coin.shape.color = GOLD;
coin.collision_layer = layer::COIN;
coin.collision_mask = layer::PLAYER;
coin.on_collision([this](rmp::Object &self, rmp::Object &) {
self.destroy();
++_coins;
spawn_coin();
});
}
void GameScene::spawn_bat() {
// From just outside the arena, above it or below it.
const Vector2 at{ rmp::random::range(0.0f, ARENA.width),
rmp::random::chance(0.5f) ? -40.0f : ARENA.height + 40 };
auto &bat = spawn({ .position = at, .shape = rmp::circle(16) });
bat.shape.color = Color{ 90, 60, 120, 255 };
bat.collision_layer = layer::BAT;
bat.collision_mask = layer::PLAYER;
// Each one a little faster or slower, so that they do not fly as one.
bat.add<rmp::behavior::Follow>({
.target = _player,
.speed = rmp::random::range(70.0f, 120.0f),
});
}
void GameScene::caught() { rmp::Scene::push<GameOverScene>(_coins); }The keyboard and the gamepad have their key, but a phone has neither, so on Android and iOS the
score gets a Pause button; the #if keeps it off a desktop, which has P and Escape.
Where it is, it does not get in the game's way. The rabbit reads the arrows through
rmp::input, so the interface knows this scene is being played and its button is a
HUD's: it takes no focus by itself, so Enter and Space never press it in the middle of the game, and
a finger or the mouse does. The interface
chapter has the rest.
The button is also where the pointer check of the player pays off. A finger on Pause is a finger
on the screen, and consumed_pointer() is what keeps the rabbit from running to the
corner as the game pauses: the pointer is the interface's while it is on a button.
Run it#
rmp runP, Escape or the gamepad's Start button pauses, and the same key resumes. Resume does too, and Menu leaves the game.