diff --git a/Userland/Games/FlappyBug/Game.cpp b/Userland/Games/FlappyBug/Game.cpp index 10acc5eff3..64f8a210b0 100644 --- a/Userland/Games/FlappyBug/Game.cpp +++ b/Userland/Games/FlappyBug/Game.cpp @@ -27,9 +27,9 @@ void Game::reset() void Game::game_over() { - if (m_highscore.value_or(0) < m_difficulty) { - m_highscore = m_difficulty; - } + if (on_game_end) + m_highscore = on_game_end(static_cast(m_difficulty)); + reset(); } diff --git a/Userland/Games/FlappyBug/Game.h b/Userland/Games/FlappyBug/Game.h index ff4c72a1e9..150e4dbb45 100644 --- a/Userland/Games/FlappyBug/Game.h +++ b/Userland/Games/FlappyBug/Game.h @@ -25,6 +25,8 @@ public: static const int game_width = 560; static const int game_height = 480; + Function on_game_end; + private: Game(); diff --git a/Userland/Games/FlappyBug/main.cpp b/Userland/Games/FlappyBug/main.cpp index 1112889366..d1135b5c3f 100644 --- a/Userland/Games/FlappyBug/main.cpp +++ b/Userland/Games/FlappyBug/main.cpp @@ -5,10 +5,12 @@ */ #include "Game.h" +#include #include #include #include #include +#include #include #include @@ -20,6 +22,7 @@ int main(int argc, char** argv) } auto app = GUI::Application::construct(argc, argv); + auto config = Core::ConfigFile::get_for_app("FlappyBug"); if (pledge("stdio rpath wpath cpath recvfd sendfd", nullptr) < 0) { perror("pledge"); @@ -31,11 +34,18 @@ int main(int argc, char** argv) return 1; } + if (unveil(config->filename().characters(), "crw") < 0) { + perror("unveil"); + return 1; + } + if (unveil(nullptr, nullptr) < 0) { perror("unveil"); return 1; } + u32 high_score = static_cast(config->read_num_entry("Game", "HighScore", 0)); + auto window = GUI::Window::construct(); window->resize(FlappyBug::Game::game_width, FlappyBug::Game::game_height); auto app_icon = GUI::Icon::default_icon("app-flappybug"); @@ -43,8 +53,20 @@ int main(int argc, char** argv) window->set_title("Flappy Bug"); window->set_double_buffering_enabled(false); window->set_resizable(false); - window->set_main_widget(); - window->show(); + auto& widget = window->set_main_widget(); + + widget.on_game_end = [&](u32 score) { + if (score <= high_score) + return high_score; + + config->write_num_entry("Game", "HighScore", static_cast(score)); + high_score = score; + + if (!config->sync()) + GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + + return high_score; + }; auto menubar = GUI::Menubar::construct(); @@ -57,6 +79,7 @@ int main(int argc, char** argv) help_menu.add_action(GUI::CommonActions::make_about_action("Flappy Bug", app_icon, window)); window->set_menubar(move(menubar)); + window->show(); return app->exec(); }