Skip to content

Commit

Permalink
Add input manager system
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasLrx committed Jun 9, 2022
1 parent 0d99e97 commit 3184548
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/game/Users.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion src/game/components/Controlable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@
#ifndef GAME_COMPONENTS_CONTROLABLE_HPP_
#define GAME_COMPONENTS_CONTROLABLE_HPP_

#include <functional>
#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<bool(const Users::ActionEvent &)>;

/// 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

Expand Down
2 changes: 2 additions & 0 deletions src/game/systems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set (SRC
${SRC}
${SRCROOT}/DrawText.hpp
${SRCROOT}/DrawText.cpp
${SRCROOT}/InputManager.hpp
${SRCROOT}/InputManager.cpp
PARENT_SCOPE)

set(INCDIRS
Expand Down
41 changes: 41 additions & 0 deletions src/game/systems/InputManager.cpp
Original file line number Diff line number Diff line change
@@ -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>();
Users::ActionEvent event = users.getNextAction();

while (event.action != GameAction::NONE) {
Logger::logger.log(Logger::Severity::Debug, [&](std::ostream &writer) {
writer << "Action " << static_cast<size_t>(event.action) << " with value " << event.value
<< " from user " << static_cast<size_t>(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<Controlable>()))
if (controlable.userId == event.user && controlable.callback && controlable.callback(event))
return;
}
} // namespace game
24 changes: 24 additions & 0 deletions src/game/systems/InputManager.hpp
Original file line number Diff line number Diff line change
@@ -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_ */

0 comments on commit 3184548

Please sign in to comment.