diff --git a/lib/Ray/sources/Model/ModelAnimations.hpp b/lib/Ray/sources/Model/ModelAnimations.hpp index 2b99e1d7..5b7508eb 100644 --- a/lib/Ray/sources/Model/ModelAnimations.hpp +++ b/lib/Ray/sources/Model/ModelAnimations.hpp @@ -19,7 +19,7 @@ namespace RAY { public: //! @brief A Model animation constructor //! @param filePath Path to the file containing animations - ModelAnimations(const std::string &filePath); + explicit ModelAnimations(const std::string &filePath); //! @brief default copy ctor ModelAnimations(const ModelAnimations &) = default; @@ -59,7 +59,7 @@ namespace RAY { int _animationCount; //! @brief The file where the animations were loaded (used to create a copy of this class) - const std::string _filePath; + std::string _filePath; static Cache<::ModelAnimation> _animationsCache; }; diff --git a/sources/Component/Animation/AnimationsComponent.cpp b/sources/Component/Animation/AnimationsComponent.cpp index 4461cc8e..2922683e 100644 --- a/sources/Component/Animation/AnimationsComponent.cpp +++ b/sources/Component/Animation/AnimationsComponent.cpp @@ -8,9 +8,9 @@ namespace BBM { - AnimationsComponent::AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play) + AnimationsComponent::AnimationsComponent(WAL::Entity &entity, const RAY::ModelAnimations &modelAnimation, int animIndex, bool play) : WAL::Component(entity), - _modelAnimation(std::move(modelAnimation)), + _modelAnimation(modelAnimation), _currentAnimIndex(animIndex), _animDisabled(play) { diff --git a/sources/Component/Animation/AnimationsComponent.hpp b/sources/Component/Animation/AnimationsComponent.hpp index 463f241f..7d037094 100644 --- a/sources/Component/Animation/AnimationsComponent.hpp +++ b/sources/Component/Animation/AnimationsComponent.hpp @@ -52,7 +52,7 @@ namespace BBM bool isAnimDisabled() const; //! @brief ctor entity and the path of the animation file - explicit AnimationsComponent(WAL::Entity &entity, RAY::ModelAnimations modelAnimation, int animIndex, bool play = true); + explicit AnimationsComponent(WAL::Entity &entity, const RAY::ModelAnimations& modelAnimation, int animIndex, bool play = true); //! @brief copy ctor AnimationsComponent(const AnimationsComponent &) = default; //! @brief dtor diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index d32f0994..480193fd 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -260,12 +260,10 @@ namespace BBM }) .addComponent([](WAL::Entity &entity, WAL::Wal &wal) { - if (gameState.currentScene != GameState::LobbyScene) + if (Runner::gameState.currentScene != GameState::LobbyScene + || !LobbySystem::playersAreReady(*wal.getScene())) return; - if (!LobbySystem::playersAreReady(*wal.getScene())) - return; - gameState._loadedScenes[GameState::SceneID::GameScene] = loadGameScene(); - gameState.nextScene = BBM::GameState::SceneID::GameScene; + LobbySystem::switchToGame(wal); }) .addComponent>(); @@ -637,21 +635,30 @@ namespace BBM std::shared_ptr Runner::loadGameScene() { auto scene = std::make_shared(); + scene->addEntity("camera") + .addComponent(8, 20, 7) + .addComponent(Vector3f(8, 0, 8)); + MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); + + return scene; + } + + WAL::Entity &Runner::createPlayer(WAL::Scene &scene) + { std::map soundPath ={ - {SoundComponent::JUMP, "assets/sounds/jump.wav"}, - {SoundComponent::MOVE, "assets/sounds/move.ogg"}, - {SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"}, - //{SoundComponent::DEATH, "assets/sounds/death.ogg"} + {SoundComponent::JUMP, "assets/sounds/jump.wav"}, + {SoundComponent::MOVE, "assets/sounds/move.ogg"}, + {SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"}, + //{SoundComponent::DEATH, "assets/sounds/death.ogg"} }; - scene->addEntity("player") + + return scene.addEntity("player") .addComponent() .addComponent("assets/player/player.iqm", true, std::make_pair(MAP_DIFFUSE, "assets/player/textures/blue.png")) .addComponent() .addComponent() - .addComponent() .addComponent("assets/shaders/glsl330/predator.fs") .addComponent>() - //.addComponent(0) .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() @@ -662,12 +669,6 @@ namespace BBM auto &animation = entity.getComponent(); animation.setAnimIndex(5); }); - scene->addEntity("camera") - .addComponent(8, 20, 7) - .addComponent(Vector3f(8, 0, 8)); - MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); - - return scene; } std::shared_ptr Runner::loadCreditScene() diff --git a/sources/Runner/Runner.hpp b/sources/Runner/Runner.hpp index 934be51c..b160cfc6 100644 --- a/sources/Runner/Runner.hpp +++ b/sources/Runner/Runner.hpp @@ -51,6 +51,11 @@ namespace BBM //! @brief load all data related to lobby screen static std::shared_ptr loadLobbyScene(); + //! @brief Create a player (without any controllable) and add it to the scene. + //! @param scene The scene where to player should reside. + //! @return A reference to the created player. + static WAL::Entity &createPlayer(WAL::Scene &scene); + //! @brief loads all scenes in the game state static void loadScenes(); }; diff --git a/sources/System/Lobby/LobbySystem.cpp b/sources/System/Lobby/LobbySystem.cpp index 94c03564..a6e921b3 100644 --- a/sources/System/Lobby/LobbySystem.cpp +++ b/sources/System/Lobby/LobbySystem.cpp @@ -10,6 +10,10 @@ #include "System/MenuControllable/MenuControllableSystem.hpp" #include "Component/Tag/TagComponent.hpp" #include +#include +#include +#include +#include namespace BBM { @@ -78,4 +82,48 @@ namespace BBM return lobbyPlayer.ready || lobbyPlayer.layout == ControllableComponent::NONE; }); } + + void LobbySystem::_addController(WAL::Entity &player, ControllableComponent::Layout layout) + { + switch (layout) { + case ControllableComponent::KEYBOARD_0: + case ControllableComponent::KEYBOARD_1: + player.addComponent(layout); + break; + case ControllableComponent::GAMEPAD_0: + player.addComponent(0); + break; + case ControllableComponent::GAMEPAD_1: + player.addComponent(1); + break; + case ControllableComponent::GAMEPAD_2: + player.addComponent(2); + break; + case ControllableComponent::GAMEPAD_3: + player.addComponent(3); + break; + default: + throw std::runtime_error("Invalid controller for a player."); + } + } + + void LobbySystem::switchToGame(WAL::Wal &wal) + { + auto scene = Runner::loadGameScene(); + int mapWidth = 16; + int mapHeight = 16; + int playerCount = 0; + + for (auto &[_, lobby] : wal.getScene()->view()) { + if (lobby.layout == ControllableComponent::NONE) + continue; + auto &player = Runner::createPlayer(*scene); + _addController(player, lobby.layout); + player.getComponent().position = Vector3f(mapWidth * playerCount % 2, 0, + static_cast(mapHeight * playerCount / 2)); + playerCount++; + } + Runner::gameState._loadedScenes[GameState::SceneID::GameScene] = scene; + Runner::gameState.nextScene = BBM::GameState::SceneID::GameScene; + } } \ No newline at end of file diff --git a/sources/System/Lobby/LobbySystem.hpp b/sources/System/Lobby/LobbySystem.hpp index 34f1e6b3..9b419bd8 100644 --- a/sources/System/Lobby/LobbySystem.hpp +++ b/sources/System/Lobby/LobbySystem.hpp @@ -6,12 +6,17 @@ #include "System/System.hpp" #include "Component/Lobby/LobbyComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "Entity/Entity.hpp" namespace BBM { //! @brief A system to handle Health entities. class LobbySystem : public WAL::System { + private: + //! @brief Add a controller for the player. + static void _addController(WAL::Entity &player, ControllableComponent::Layout layout); public: //! @inherit void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; @@ -23,6 +28,10 @@ namespace BBM //! @param scene The lobby scene containing lobby players. static bool playersAreReady(WAL::Scene &scene); + //! @brief Inform the engine that the next scene should be the game scene and load it. + //! @param wal The engine. + static void switchToGame(WAL::Wal &wal); + //! @brief A default constructor explicit LobbySystem(WAL::Wal &wal); //! @brief A Lobby system is copy constructable