diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 648b7d21..3ad5f012 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -39,9 +39,9 @@ namespace WAL //! @param type The type of component void _componentRemoved(const std::type_index &type); protected: + public: //! @brief A reference to the ECS. Scene &_scene; - public: //! @brief Get the ID of the entity. unsigned getUid() const; //! @brief Get the name fo the entity diff --git a/lib/wal/sources/Scene/Scene.cpp b/lib/wal/sources/Scene/Scene.cpp index 77526662..2168941c 100644 --- a/lib/wal/sources/Scene/Scene.cpp +++ b/lib/wal/sources/Scene/Scene.cpp @@ -45,4 +45,9 @@ namespace WAL view->erase(entity); } } + + int Scene::getID() const + { + return this->_id; + } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Scene/Scene.hpp b/lib/wal/sources/Scene/Scene.hpp index aab47566..b853abec 100644 --- a/lib/wal/sources/Scene/Scene.hpp +++ b/lib/wal/sources/Scene/Scene.hpp @@ -56,6 +56,9 @@ namespace WAL return *view; } + //! @return ID of the scene + int getID() const; + //! @brief A default constructor Scene() = default; //! @brief A scene is copy constructable diff --git a/sources/Models/GameState.hpp b/sources/Models/GameState.hpp index ab075fd2..b9fc3e81 100644 --- a/sources/Models/GameState.hpp +++ b/sources/Models/GameState.hpp @@ -14,6 +14,7 @@ namespace BBM //! @brief A class representing the current game state. This allow one to retain information between update calls. class GameState { + public: //! @brief The list of scenes available. enum SceneID { @@ -27,9 +28,12 @@ namespace BBM //! @brief The currently loaded scene - SceneID currentScene = MainMenuScene; + SceneID currentScene = TitleScreenScene; + + //! @brief The next scene to load (if smae as currentScene, nothing to do) + SceneID nextScene = TitleScreenScene; //! @brief The list of loaded scenes. - std::unordered_map _loadedScenes = {}; + std::unordered_map> _loadedScenes = {}; }; } \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 9e95f04b..995289ab 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -37,7 +37,9 @@ namespace RAY2D = RAY::Drawables::Drawables2D; namespace BBM { - void updateState(WAL::Wal &engine, GameState &state) + GameState Runner::gameState; + + void Runner::updateState(WAL::Wal &engine, GameState &state) { // You can change the scene here or update the game state based on entities values. @@ -45,9 +47,13 @@ namespace BBM // If you don't need the scene anymore, remember to remove it from the loadedScene array. if (RAY::Window::getInstance().shouldClose()) engine.shouldClose = true; + if (gameState.nextScene == gameState.currentScene) + return; + gameState._loadedScenes[gameState.currentScene] = engine.scene; + engine.scene = gameState._loadedScenes[gameState.nextScene]; } - void addSystems(WAL::Wal &wal) + void Runner::addSystems(WAL::Wal &wal) { wal.addSystem() .addSystem() @@ -57,7 +63,7 @@ namespace BBM .addSystem(); } - void enableRaylib(WAL::Wal &wal) + void Runner::enableRaylib(WAL::Wal &wal) { RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(1920, 1080, "Bomberman"); @@ -65,7 +71,7 @@ namespace BBM .addSystem(window); } - std::shared_ptr loadTitleScreenScene() + std::shared_ptr Runner::loadTitleScreenScene() { auto scene = std::make_shared(); scene->addEntity("control") @@ -78,23 +84,22 @@ namespace BBM .addComponent(320, 180, 0) .addComponent("assets/logo_big.png"); scene->addEntity("text_prompt") - .addComponent(1920 / 5, 1080 - 180, 0) - .addComponent("Press any button to continue", 70, RAY::Vector2(), WHITE) - .addComponent([](WAL::Entity &entity) + .addComponent(1920 / 2.5, 1080 - 130, 0) + .addComponent("Press space", 70, RAY::Vector2(), ORANGE) + .addComponent() + .addComponent() + .addComponent([](WAL::Entity &entity) { - entity.getComponent().drawable->setColor(WHITE); - }) - .addComponent([](WAL::Entity &entity) - { - entity.getComponent().drawable->setColor(ORANGE); + gameState.nextScene = BBM::GameState::SceneID::MainMenuScene; }); + //needed material //music //sound return scene; } - std::shared_ptr loadMainMenuScene() + std::shared_ptr Runner::loadMainMenuScene() { auto scene = std::make_shared(); @@ -107,7 +112,7 @@ namespace BBM scene->addEntity("logo") .addComponent(1920 / 3, 180, 0) .addComponent("assets/logo_small.png"); - scene->addEntity("play button") + auto &play = scene->addEntity("play button") .addComponent(1920 / 2.5, 1080 - 540, 0) .addComponent("assets/buttons/button_new_game.png") .addComponent([](WAL::Entity &entity) @@ -123,7 +128,7 @@ namespace BBM texture->use("assets/buttons/button_new_game_hovered.png"); }) .addComponent(OnClickComponent::emptyButtonCallback); - scene->addEntity("settings button") + auto &settings = scene->addEntity("settings button") .addComponent(1920 / 2.5, 1080 - 360, 0) .addComponent("assets/buttons/button_settings.png") .addComponent([](WAL::Entity &entity) @@ -139,7 +144,7 @@ namespace BBM texture->use("assets/buttons/button_settings_hovered.png"); }) .addComponent(OnClickComponent::emptyButtonCallback); - scene->addEntity("exit button") + auto &exit = scene->addEntity("exit button") .addComponent(1920 / 2.5, 1080 - 180, 0) .addComponent("assets/buttons/button_exit.png") .addComponent([](WAL::Entity &entity) @@ -155,13 +160,17 @@ namespace BBM texture->use("assets/buttons/button_exit_hovered.png"); }) .addComponent(OnClickComponent::emptyButtonCallback); + + play.getComponent().setButtonLinks(&exit, &settings); + settings.getComponent().setButtonLinks(&play, &exit); + exit.getComponent().setButtonLinks(&settings, &play); //needed material //music //sound return scene; } - std::shared_ptr loadPauseMenuScene() + std::shared_ptr Runner::loadPauseMenuScene() { auto scene = std::make_shared(); //needed material @@ -178,7 +187,7 @@ namespace BBM return scene; } - std::shared_ptr loadSettingsMenuScene() + std::shared_ptr Runner::loadSettingsMenuScene() { auto scene = std::make_shared(); @@ -243,10 +252,7 @@ namespace BBM .addComponent(1920 / 2.5, 1080 - 360, 0) .addComponent("Sound Volume", 70, RAY::Vector2(), ORANGE) .addComponent() - .addComponent([](WAL::Entity &entity) - { - entity.getComponent().drawable->setColor(BLACK); - }) + .addComponent() .addComponent([](WAL::Entity &entity) { entity.getComponent().drawable->setColor(ORANGE); @@ -336,12 +342,10 @@ namespace BBM soundDown.getComponent().setButtonLinks(&music, &debug, &sound); debug.getComponent().setButtonLinks(&sound, &back); back.getComponent().setButtonLinks(&debug, &music); - - return scene; } - std::shared_ptr loadGameScene() + std::shared_ptr Runner::loadGameScene() { auto scene = std::make_shared(); scene->addEntity("player") @@ -361,15 +365,25 @@ namespace BBM return scene; } - int run() + void Runner::loadScenes() + { + gameState._loadedScenes[GameState::SceneID::MainMenuScene] = loadMainMenuScene(); + gameState._loadedScenes[GameState::SceneID::GameScene] = loadGameScene(); + gameState._loadedScenes[GameState::SceneID::SettingsScene] = loadSettingsMenuScene(); + gameState._loadedScenes[GameState::SceneID::PauseMenuScene] = loadPauseMenuScene(); + gameState._loadedScenes[GameState::SceneID::TitleScreenScene] = loadTitleScreenScene(); + } + + int Runner::run() { WAL::Wal wal; - addSystems(wal); - enableRaylib(wal); - wal.scene = loadSettingsMenuScene(); + Runner::addSystems(wal); + Runner::enableRaylib(wal); + Runner::loadScenes(); + wal.scene = Runner::gameState._loadedScenes[GameState::SceneID::TitleScreenScene]; try { - wal.run(updateState); + wal.run(Runner::updateState, Runner::gameState); return 0; } catch (const std::exception &ex) { std::cerr << ex.what() << std::endl; diff --git a/sources/Runner/Runner.hpp b/sources/Runner/Runner.hpp index 0f622f11..608dba0b 100644 --- a/sources/Runner/Runner.hpp +++ b/sources/Runner/Runner.hpp @@ -3,10 +3,47 @@ // #pragma once +#include "Models/GameState.hpp" +#include "Wal.hpp" namespace BBM { - //! @brief Start the game and run a Bomberman. - //! @return 0 on success, another value on error. - int run(); + class Runner { + public: + + //! @brief store current scenes informations + static GameState gameState; + //! @brief Start the game and run a Bomberman. + //! @return 0 on success, another value on error. + static int run(); + + //! @brief Update current scene (loading related data) according to state + //! @param engine: Wal ECS + //! @param state current game state + static void updateState(WAL::Wal &engine, GameState &state); + + //! @brief Add required systems to wal + static void addSystems(WAL::Wal &wal); + + //! @brief init all raylib-related data & context + static void enableRaylib(WAL::Wal &wal); + + //! @brief load all data related to title screen + static std::shared_ptr loadTitleScreenScene(); + + //! @brief load all data related to main menu screen + static std::shared_ptr loadMainMenuScene(); + + //! @brief load all data related to pause menu screen + static std::shared_ptr loadPauseMenuScene(); + + //! @brief load all data related to settings screen + static std::shared_ptr loadSettingsMenuScene(); + + //! @brief load all data related to game screen + static std::shared_ptr loadGameScene(); + + //! @brief loads all scenes in the game state + static void loadScenes(); + }; } \ No newline at end of file diff --git a/sources/System/MenuControllable/MenuControllableSystem.cpp b/sources/System/MenuControllable/MenuControllableSystem.cpp index e59b0d43..47550893 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.cpp +++ b/sources/System/MenuControllable/MenuControllableSystem.cpp @@ -40,7 +40,9 @@ namespace BBM this->_now = lastTick; move = controllable.move; - select = controllable.bomb; + select = controllable.jump; + if (currentButton && currentButton->_scene.getID() != wal.scene->getID()) + currentButton = nullptr; if (currentButton == nullptr && buttons.size()) currentButton = &(**buttons.begin()); this->updateCurrentButton(); diff --git a/sources/main.cpp b/sources/main.cpp index bd13441a..c4e69c4a 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -23,5 +23,5 @@ int main(int argc, char **argv) usage(argv[0]); return 1; } - return BBM::run(); + return BBM::Runner::run(); }