diff --git a/src/game/Users.hpp b/src/game/Users.hpp index e1c17943..a38083f3 100644 --- a/src/game/Users.hpp +++ b/src/game/Users.hpp @@ -15,7 +15,7 @@ namespace game { /// Resource allowing to get users actions using the keybinds. - class Users : public ecs::Resource { + class Users final : public ecs::Resource { public: /// Event send by @ref getNextAction() when an action changed. struct ActionEvent { diff --git a/src/game/components/Controlable.hpp b/src/game/components/Controlable.hpp index 90de27e9..b866b359 100644 --- a/src/game/components/Controlable.hpp +++ b/src/game/components/Controlable.hpp @@ -8,17 +8,22 @@ #ifndef GAME_COMPONENTS_CONTROLABLE_HPP_ #define GAME_COMPONENTS_CONTROLABLE_HPP_ +#include #include "ecs/Component.hpp" -#include "game/User.hpp" +#include "game/Users.hpp" namespace game { /// Controlable entity, may be a widget or a player struct Controlable : public ecs::Component { + using Callback = std::function; + /// Id of the user controlling the widget User::UserId userId; + Callback callback; Controlable(User::UserId id) : userId(id) {} + Controlable(User::UserId id, Callback pcallback) : userId(id), callback(pcallback) {} }; } // namespace game diff --git a/src/game/systems/CMakeLists.txt b/src/game/systems/CMakeLists.txt index 2a109b5d..124e061c 100644 --- a/src/game/systems/CMakeLists.txt +++ b/src/game/systems/CMakeLists.txt @@ -5,6 +5,8 @@ set (SRC ${SRC} ${SRCROOT}/DrawText.hpp ${SRCROOT}/DrawText.cpp + ${SRCROOT}/InputManager.hpp + ${SRCROOT}/InputManager.cpp PARENT_SCOPE) set(INCDIRS diff --git a/src/game/systems/InputManager.cpp b/src/game/systems/InputManager.cpp new file mode 100644 index 00000000..cfbd8e8f --- /dev/null +++ b/src/game/systems/InputManager.cpp @@ -0,0 +1,41 @@ +/* +** EPITECH PROJECT, 2022 +** Bomberman +** File description: +** DrawText +*/ + +#include "InputManager.hpp" +#include "components/Controlable.hpp" +#include "ecs/Storage.hpp" +#include "ecs/World.hpp" +#include "ecs/join.hpp" +#include "ecs/system/SystemData.hpp" +#include "game/Users.hpp" + +#include "logger/Logger.hpp" + +namespace game +{ + void InputManager::run(ecs::SystemData data) + { + Users &users = data.getResource(); + Users::ActionEvent event = users.getNextAction(); + + while (event.action != GameAction::NONE) { + Logger::logger.log(Logger::Severity::Debug, [&](std::ostream &writer) { + writer << "Action " << static_cast(event.action) << " with value " << event.value + << " from user " << static_cast(event.user); + }); + handleEvent(event, data); + event = users.getNextAction(); + } + } + + void InputManager::handleEvent(const Users::ActionEvent &event, ecs::SystemData data) + { + for (auto [controlable] : ecs::join(data.getStorage())) + if (controlable.userId == event.user && controlable.callback && controlable.callback(event)) + return; + } +} // namespace game diff --git a/src/game/systems/InputManager.hpp b/src/game/systems/InputManager.hpp new file mode 100644 index 00000000..894ac1f8 --- /dev/null +++ b/src/game/systems/InputManager.hpp @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2022 +** Bomberman +** File description: +** InputManager +*/ + +#ifndef GAME_SYSTEMS_INPUTMANAGER_HPP_ +#define GAME_SYSTEMS_INPUTMANAGER_HPP_ + +#include "ecs/System.hpp" +#include "game/Users.hpp" + +namespace game +{ + struct InputManager : public ecs::System { + void run(ecs::SystemData data) override final; + + private: + void handleEvent(const Users::ActionEvent &event, ecs::SystemData data); + }; +} // namespace game + +#endif /* !GAME_SYSTEMS_INPUTMANAGER_HPP_ */