diff --git a/Client/include/Client/GameSession.hpp b/Client/include/Client/GameSession.hpp index f033940..525efe8 100644 --- a/Client/include/Client/GameSession.hpp +++ b/Client/include/Client/GameSession.hpp @@ -38,6 +38,9 @@ namespace CaveGame::Client { class GameSession : public Scene { public: + Event<> OnSessionExit; + Event<> RequestToggleSettings; + GameSession() = default; explicit GameSession(bool createNewWorld); void Update(float elapsed) override; diff --git a/Client/include/Client/MainMenu.hpp b/Client/include/Client/MainMenu.hpp index c32d131..9f6433a 100644 --- a/Client/include/Client/MainMenu.hpp +++ b/Client/include/Client/MainMenu.hpp @@ -30,7 +30,9 @@ namespace CaveGame::Client public: Event RequestWorld; Event<> RequestQuit; - Event<> RequestShowCredits; + Event<> RequestToggleCredits; + Event<> RequestToggleSettings; + MainMenu(); void Update(float elapsed) override; void Draw() override; diff --git a/Client/include/Client/SceneManager.hpp b/Client/include/Client/SceneManager.hpp index fd45c6f..7a7db57 100644 --- a/Client/include/Client/SceneManager.hpp +++ b/Client/include/Client/SceneManager.hpp @@ -21,7 +21,10 @@ namespace CaveGame::Client void ChangeScene(Scene* new_scene); Scene* CurrentScene(); protected: + void UpdateSceneState(float elapsed); + protected: + Scene* next_scene = nullptr; Scene* current_scene = nullptr; - private: + Scene* prev_scene = nullptr; }; } \ No newline at end of file diff --git a/Client/include/Client/SettingsMenu.hpp b/Client/include/Client/SettingsMenu.hpp index c2bafae..fe57c09 100644 --- a/Client/include/Client/SettingsMenu.hpp +++ b/Client/include/Client/SettingsMenu.hpp @@ -20,35 +20,53 @@ namespace CaveGame::Client { // TODO: Analyze behavior of singleton on an object that requires specific initialization, like this. - class SettingsMenu : public JUI::Window, public Singleton + class SettingsMenu : public JUI::Window { public: SettingsMenu() { - SetTitle("Settings"); - auto* root_layout = new JUI::VerticalListLayout(this); + using namespace JUI::UDimLiterals; + + SetTitle("Settings"); + Size({400_px, 400_px}); + + auto* root_layout = new JUI::VerticalListLayout(this->ViewportInstance()); general_section = new JUI::Collapsible(root_layout); + general_section->Size({100_percent, 100_px}); general_section->Title("General"); auto* gen_layout = new JUI::VerticalListLayout(general_section); sound_section = new JUI::Collapsible(root_layout); + sound_section->Size({100_percent, 100_px}); sound_section->Title("Sound"); auto* snd_layout = new JUI::VerticalListLayout(sound_section); graphics_section = new JUI::Collapsible(root_layout); + graphics_section->Size({100_percent, 100_px}); graphics_section->Title("Graphics"); auto* gfx_layout = new JUI::VerticalListLayout(graphics_section); input_section = new JUI::Collapsible(root_layout); + input_section->Size({100_percent, 100_px}); input_section->Title("Input"); + + + Close(); } + explicit SettingsMenu(Widget* parent) : SettingsMenu() + { + Parent(parent); + } + + + protected: JUI::Collapsible* general_section; JUI::Collapsible* sound_section; diff --git a/Client/src/Client/GameSession.cpp b/Client/src/Client/GameSession.cpp index 72a99a4..7e55a95 100644 --- a/Client/src/Client/GameSession.cpp +++ b/Client/src/Client/GameSession.cpp @@ -29,7 +29,7 @@ void CaveGame::Client::GameSession::Load() { pause_menu = new PauseMenuWidget(hud); pause_menu->OnQuitButtonPressed += [this] (){ SaveAndExit();}; pause_menu->OnResumeButtonPressed += [this] () { pause_menu->Close(); }; - pause_menu->OnSettingsButtonPressed += [this] () { Client::SettingsMenu::Instance().Toggle(); }; + pause_menu->OnSettingsButtonPressed += [this] () { RequestToggleSettings.Invoke(); }; hotbar = TileHotbar(); // TODO: Redundant, use the constructor. @@ -69,6 +69,7 @@ void CaveGame::Client::GameSession::Draw() { void CaveGame::Client::GameSession::SaveAndExit() { world->SaveAndExit(); + OnSessionExit.Invoke(); } diff --git a/Client/src/Client/MainMenu.cpp b/Client/src/Client/MainMenu.cpp index bc31950..f17f042 100644 --- a/Client/src/Client/MainMenu.cpp +++ b/Client/src/Client/MainMenu.cpp @@ -4,6 +4,7 @@ #include "JUI/Widgets/Button.hpp" #include "JUI/Widgets/Image.hpp" #include "Client/AssetService.hpp" +#include CaveGame::Client::MainMenu::MainMenu() : Scene() { @@ -116,19 +117,18 @@ void CaveGame::Client::MainMenu::BuildWidgets() { }; - //auto *sp_btn = create_mainmenu_btn("Singleplayer", button_list); + auto *settings_btn = create_mainmenu_btn("Settings", button_list); - //sp_btn->OnReleaseEvent += [this](Vector2 pos, MouseButton btn, bool b) { - // if (b) - // RequestWorld.Invoke({}); - //}; + settings_btn->OnReleaseEvent += [this] (Vector2 pos, JUI::MouseButton btn, bool b) mutable { + if (b) + RequestToggleSettings.Invoke(); + }; - auto *mp_btn = create_mainmenu_btn("Multiplayer", button_list); auto *credits_btn = create_mainmenu_btn("Credits", button_list); credits_btn->OnReleaseEvent += [this](Vector2 pos, JUI::MouseButton btn, bool b) { if (b) - RequestShowCredits.Invoke(); + RequestToggleCredits.Invoke(); }; // TODO: Replace with IconButton with steam logo diff --git a/Client/src/Client/SceneManager.cpp b/Client/src/Client/SceneManager.cpp index 90f0c3a..fc7c4c5 100644 --- a/Client/src/Client/SceneManager.cpp +++ b/Client/src/Client/SceneManager.cpp @@ -6,11 +6,31 @@ namespace CaveGame::Client { void SceneManager::ChangeScene(Scene* new_scene) { - if (current_scene != nullptr) - current_scene->Unload(); + next_scene = new_scene; - current_scene = new_scene; - current_scene->Load(); + //if (current_scene != nullptr) + // current_scene->Unload(); + + //current_scene = new_scene; + //current_scene->Load(); + } + + void SceneManager::UpdateSceneState(float elapsed) + { + if (next_scene != nullptr) + { + if (current_scene != nullptr) + { + current_scene->Unload(); + prev_scene = current_scene; + } + + current_scene = next_scene; + current_scene->Load(); + + + next_scene = nullptr; + } } Scene* SceneManager::CurrentScene() { return current_scene; } diff --git a/ClientApp/src/ClientApp/CaveGameWindow.cpp b/ClientApp/src/ClientApp/CaveGameWindow.cpp index 3dca3b1..d529413 100644 --- a/ClientApp/src/ClientApp/CaveGameWindow.cpp +++ b/ClientApp/src/ClientApp/CaveGameWindow.cpp @@ -1,6 +1,6 @@ #include #include "ClientApp/CaveGameWindow.hpp" - +#include #include #include #include @@ -46,8 +46,17 @@ namespace CaveGame::ClientApp { { Logs::Info(std::format("Game session requested.", info.NewWorld)); game_ctx = new CaveGame::Client::GameSession(info.NewWorld); + // TODO: Parse Info to construct gameworld files. ChangeScene(game_ctx); + + game_ctx->RequestToggleSettings += [&, this] mutable { + settings_window->Toggle(); + }; + + game_ctx->OnSessionExit += [&, this] mutable { + ChangeScene(menu_ctx); + }; } void CaveGameWindow::CreateContexts() @@ -56,18 +65,15 @@ namespace CaveGame::ClientApp { splash_ctx = new CaveGame::Client::Splash(); Logs::Info("Building main menu."); menu_ctx = new CaveGame::Client::MainMenu(); - game_ctx = new CaveGame::Client::GameSession(); menu_ctx->RequestWorld += [this](Client::SingleplayerSessionInfo info) { OpenWorld(info); }; - menu_ctx->RequestQuit += [this]() { - Die(); - }; - menu_ctx->RequestShowCredits += [this]() - { - credits_window->Visible(true); - }; + menu_ctx->RequestQuit += [this] { Die(); }; + menu_ctx->RequestToggleCredits += [this]{ credits_window->Toggle(); }; + menu_ctx->RequestToggleSettings += [this] { settings_window->Toggle(); }; + + game_ctx = new CaveGame::Client::GameSession(); } void CaveGameWindow::CreateMenuWindows() @@ -154,10 +160,7 @@ namespace CaveGame::ClientApp { void CaveGameWindow::create_settings_window() { - settings_window = new JUI::Window(wm); - settings_window->SetTitleFont(JGL::Fonts::Jupiteroid); - settings_window->SetTitle("Settings"); - settings_window->Visible(false); + settings_window = new Client::SettingsMenu(wm); } @@ -236,9 +239,10 @@ namespace CaveGame::ClientApp { void CaveGameWindow::Update(float elapsed) { - jstick::ReadEventLoop(); + SceneManager::UpdateSceneState(elapsed); + // Update floating windows. wm->Update(elapsed);