From cbbde0bc0c6b33735c1ec12e7fd0dcf43c50886f Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 31 May 2021 12:48:59 +0200 Subject: [PATCH] gamepad ecs --- CMakeLists.txt | 4 ++ .../Component/Gamepad/GamepadComponent.cpp | 34 ++++++++++ .../Component/Gamepad/GamepadComponent.hpp | 62 +++++++++++++++++++ sources/System/Gamepad/GamepadSystem.cpp | 45 ++++++++++++++ sources/System/Gamepad/GamepadSystem.hpp | 29 +++++++++ .../GridCentered/GridCenteredSystem.hpp | 2 +- 6 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 sources/Component/Gamepad/GamepadComponent.cpp create mode 100644 sources/Component/Gamepad/GamepadComponent.hpp create mode 100644 sources/System/Gamepad/GamepadSystem.cpp create mode 100644 sources/System/Gamepad/GamepadSystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c948f529..f4efb796 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,8 @@ set(SOURCES sources/Component/Movable/MovableComponent.hpp sources/Component/Controllable/ControllableComponent.hpp sources/Component/Controllable/ControllableComponent.cpp + sources/Component/Gamepad/GamepadComponent.cpp + sources/Component/Gamepad/GamepadComponent.hpp sources/Component/Keyboard/KeyboardComponent.cpp sources/Component/Keyboard/KeyboardComponent.hpp sources/Component/BombHolder/BombHolderComponent.cpp @@ -29,6 +31,8 @@ set(SOURCES sources/System/Movable/MovableSystem.cpp sources/System/Controllable/ControllableSystem.cpp sources/System/Controllable/ControllableSystem.hpp + sources/System/Gamepad/GamepadSystem.cpp + sources/System/Gamepad/GamepadSystem.hpp sources/System/Health/HealthSystem.cpp sources/System/Health/HealthSystem.hpp sources/System/Keyboard/KeyboardSystem.cpp diff --git a/sources/Component/Gamepad/GamepadComponent.cpp b/sources/Component/Gamepad/GamepadComponent.cpp new file mode 100644 index 00000000..0b889b7a --- /dev/null +++ b/sources/Component/Gamepad/GamepadComponent.cpp @@ -0,0 +1,34 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include "GamepadComponent.hpp" + +namespace BBM +{ + GamepadComponent::GamepadComponent(WAL::Entity &entity) + : WAL::Component(entity), _ID(0) + {} + + GamepadComponent::GamepadComponent(WAL::Entity &entity, int ID) + : WAL::Component(entity), _ID(ID) + {} + + WAL::Component *GamepadComponent::clone(WAL::Entity &entity) const + { + return new GamepadComponent(entity, _ID); + } + + GamepadComponent &GamepadComponent::setID(int ID) + { + this->_ID = ID; + return *this; + } + + int GamepadComponent::getID() const + { + return this->_ID; + } + +} // namespace BMM diff --git a/sources/Component/Gamepad/GamepadComponent.hpp b/sources/Component/Gamepad/GamepadComponent.hpp new file mode 100644 index 00000000..be00a49b --- /dev/null +++ b/sources/Component/Gamepad/GamepadComponent.hpp @@ -0,0 +1,62 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include "Controllers/Gamepad.hpp" +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" + +using Button = RAY::Controller::GamePad::Button; +using Gamepad = RAY::Controller::GamePad; + +namespace BBM +{ + class GamepadComponent : public WAL::Component + { + private: + //! @brief Identifier of the gamepad, used to fetch events + int _ID; + public: + //! @brief jump key + Button keyJump = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; + //! @brief bomb key + Button keyBomb = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; + //! @brief pause key + Button keyPause = GAMEPAD_BUTTON_MIDDLE; + //! @brief move right key + Button keyRight = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; + //! @brief move left key + Button keyLeft = GAMEPAD_BUTTON_LEFT_FACE_LEFT; + //! @brief move up key + Button keyUp = GAMEPAD_BUTTON_LEFT_FACE_UP; + //! @brief move down key + Button keyDown = GAMEPAD_BUTTON_LEFT_FACE_DOWN; + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief Create a new gampad component using default keys. + explicit GamepadComponent(WAL::Entity &entity); + + //! @brief Create a new PositionComponent at a certain position + GamepadComponent(WAL::Entity &entity, int id); + + //! @brief A Gamepad component is copy constructable. + GamepadComponent(const GamepadComponent &) = default; + + //! @brief default destructor + ~GamepadComponent() override = default; + + //! @brief A Gamepad component can't be assigned + GamepadComponent &operator=(const GamepadComponent &) = delete; + + //! @brief Set the ID of the Gamepad the events must be fetch from; + GamepadComponent &setID(int ID); + + //! @brief Get the ID of the Gamepad the events must be fetch from; + int getID() const; + }; +} \ No newline at end of file diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp new file mode 100644 index 00000000..2a12a11b --- /dev/null +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -0,0 +1,45 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#include "GamepadSystem.hpp" +#include "Component/Gamepad/GamepadComponent.hpp" +#include "Component/Controllable/ControllableComponent.hpp" +#include "Entity/Entity.hpp" +#include "Controllers/Gamepad.hpp" + +using Button = RAY::Controller::GamePad::Button; +using Gamepad = RAY::Controller::GamePad; + +namespace BBM +{ + GamepadSystem::GamepadSystem() + : WAL::System({ + typeid(GamepadComponent), + typeid(ControllableComponent) + }) + {} + + void GamepadSystem::onFixedUpdate(WAL::Entity &entity) + { + const auto &gamepadComponent = entity.getComponent(); + auto &controllable = entity.getComponent(); + Gamepad gamepad(gamepadComponent.getID()); + + const std::map keyPressedMap = { + {gamepadComponent.keyJump, controllable.jump}, + {gamepadComponent.keyBomb, controllable.bomb}, + {gamepadComponent.keyPause, controllable.pause} + }; + + for (auto key : keyPressedMap) + key.second = gamepad.isPressed(key.first); + controllable.moveX = 0; + controllable.moveZ = 0; + controllable.moveX += gamepad.isPressed(gamepadComponent.keyRight); + controllable.moveX -= gamepad.isPressed(gamepadComponent.keyLeft); + controllable.moveX += gamepad.isPressed(gamepadComponent.keyUp); + controllable.moveX -= gamepad.isPressed(gamepadComponent.keyDown); + } +} \ No newline at end of file diff --git a/sources/System/Gamepad/GamepadSystem.hpp b/sources/System/Gamepad/GamepadSystem.hpp new file mode 100644 index 00000000..775248c1 --- /dev/null +++ b/sources/System/Gamepad/GamepadSystem.hpp @@ -0,0 +1,29 @@ +// +// Created by Tom Augier on 2021-05-20. +// Edited by Benjamin Henry on 2021-05-20. +// + +#pragma once + +#include "System/System.hpp" +#include + +namespace BBM +{ + //! @brief A system to handle Gamepad entities. + class GamepadSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + GamepadSystem(); + //! @brief A Gamepad system is copy constructable + GamepadSystem(const GamepadSystem &) = default; + //! @brief A default destructor + ~GamepadSystem() override = default; + //! @brief A Gamepad system is assignable. + GamepadSystem &operator=(const GamepadSystem &) = default; + }; +} diff --git a/sources/System/GridCentered/GridCenteredSystem.hpp b/sources/System/GridCentered/GridCenteredSystem.hpp index 206e9eca..e84e65fb 100644 --- a/sources/System/GridCentered/GridCenteredSystem.hpp +++ b/sources/System/GridCentered/GridCenteredSystem.hpp @@ -12,7 +12,7 @@ namespace BBM class GridCenteredSystem : public WAL::System { public: - void onFixedUpdate(Entity &entity) override; + void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor GridCenteredSystem();