From b7fca7d07afdf112331c679a48c513c66aad5759 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Tue, 16 May 2023 21:56:55 +0200 Subject: [PATCH] Sync commit --- CMakeLists.txt | 2 +- src/game/input/direction.cpp | 35 +++++++++++++++++++++++++++++++++ src/game/input/direction.h | 10 ++++++++++ src/game/input/input_identity.h | 28 ++++++++++++++++++++++++++ src/game/input/input_mapper.cpp | 30 ++++++++++++++++++++++++++++ src/game/input/input_mapper.h | 9 ++++++++- 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/game/input/input_identity.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d3ec716..2ccd8e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,7 @@ set(SOURCES src/game/input/direction.cpp src/game/player/player.cpp src/game/player/player.hpp - src/game/input/game_inputs.hpp src/game/world/world_view.cpp src/game/world/world_view.h src/utilities/smart_list.cpp src/utilities/smart_list.h src/utilities/vector_utils.hpp src/game/world/ITrackable.h) + src/game/input/game_inputs.hpp src/game/world/world_view.cpp src/game/world/world_view.h src/utilities/smart_list.cpp src/utilities/smart_list.h src/utilities/vector_utils.hpp src/game/world/ITrackable.h src/game/input/input_identity.h) set(PHYSICS_00_SOURCES src/prototypes/physics_00.cpp) diff --git a/src/game/input/direction.cpp b/src/game/input/direction.cpp index c132476..e80d676 100644 --- a/src/game/input/direction.cpp +++ b/src/game/input/direction.cpp @@ -117,3 +117,38 @@ sf::Vector2f Direction::asScreenVector() const screenVector.y *= -1; return screenVector; } + +Direction::Direction() +{ + directionVector = sf::Vector2f(0.0f, 0.0f); +} + +void Direction::add(HardDirection direction) +{ + summedDirections = static_cast(summedDirections | direction); + directionVector = getVector(summedDirections); +} + +void Direction::remove(HardDirection direction) +{ + summedDirections = static_cast(summedDirections & ~direction); + directionVector = getVector(summedDirections); +} + +void Direction::set(HardDirection direction) +{ + summedDirections = direction; + directionVector = getVector(summedDirections); +} + +void Direction::clear() +{ + summedDirections = HardDirection::NONE; + directionVector = sf::Vector2f(0.0f, 0.0f); +} + +void Direction::set(sf::Vector2f direction) +{ + summedDirections = HardDirection::NONE; + this->directionVector = direction; +} diff --git a/src/game/input/direction.h b/src/game/input/direction.h index 6bc8028..dd6e78f 100644 --- a/src/game/input/direction.h +++ b/src/game/input/direction.h @@ -16,6 +16,8 @@ enum HardDirection struct Direction { public: + Direction(); + explicit Direction(sf::Vector2f directionVector); explicit Direction(HardDirection direction); @@ -33,8 +35,16 @@ public: HardDirection asHardDirection() const; +public: + void add(HardDirection direction); + void remove(HardDirection direction); + void set(HardDirection direction); + void set(sf::Vector2f direction); + void clear(); + private: sf::Vector2f directionVector; + HardDirection summedDirections = HardDirection::NONE; }; #endif //HOLESOME_DIRECTION_H diff --git a/src/game/input/input_identity.h b/src/game/input/input_identity.h new file mode 100644 index 0000000..aa0f3f8 --- /dev/null +++ b/src/game/input/input_identity.h @@ -0,0 +1,28 @@ +#ifndef HOLESOME_INPUT_IDENTITY_H +#define HOLESOME_INPUT_IDENTITY_H + +#include "direction.h" + +enum InputDeviceType +{ + ALL, + KEYBOARD, + GAMEPAD +}; + + +struct InputIdentity +{ + Direction direction = Direction(); + InputDeviceType deviceType = InputDeviceType::ALL; + unsigned int gamepadId = 0; + unsigned int inputOrder = 0; + bool isActive = true; + + explicit InputIdentity(InputDeviceType type) { + deviceType = type; + }; +}; + + +#endif //HOLESOME_INPUT_IDENTITY_H diff --git a/src/game/input/input_mapper.cpp b/src/game/input/input_mapper.cpp index af70a98..6a148af 100644 --- a/src/game/input/input_mapper.cpp +++ b/src/game/input/input_mapper.cpp @@ -4,6 +4,11 @@ void InputMapper::setGame(Game *game) { InputMapper::game = game; + + // Initialize identities + allIdentity = std::make_shared(InputDeviceType::ALL); + keyboardIdentity = std::make_shared(InputDeviceType::KEYBOARD); + gamepadIdentities = std::map>(); } @@ -25,6 +30,9 @@ void InputMapper::processEvents() break; case sf::Event::Resized: break; + case sf::Event::JoystickMoved: + handleJoystickMovement(event.joystickMove); + break; default: break; } @@ -71,3 +79,25 @@ void InputMapper::handleKeyRelease(sf::Event::KeyEvent event) inputDirectionBuffer.end()); } } + +void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event) +{ + event.joystickId; +} + +std::shared_ptr InputMapper::getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId) +{ + switch (deviceType) + { + case InputDeviceType::KEYBOARD: + return keyboardIdentity; + case InputDeviceType::GAMEPAD: + if (gamepadIdentities.find(gamepadId) == gamepadIdentities.end()) + { + throw std::invalid_argument("Gamepad with id " + std::to_string(gamepadId) + " not found."); + } + return gamepadIdentities[gamepadId]; + default: + return allIdentity; + } +} diff --git a/src/game/input/input_mapper.h b/src/game/input/input_mapper.h index 849de26..c0c392f 100644 --- a/src/game/input/input_mapper.h +++ b/src/game/input/input_mapper.h @@ -5,6 +5,7 @@ #include #include "../../config.h" #include "direction.h" +#include "input_identity.h" class Game; @@ -18,16 +19,22 @@ public: static void processEvents(); - static Direction getInputDirection(); + static std::shared_ptr getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId = 0); private: static inline Game *game = nullptr; + static std::shared_ptr allIdentity; + static std::shared_ptr keyboardIdentity; + static std::map> gamepadIdentities; + static inline std::vector inputDirectionBuffer = std::vector(); static void handleKeyPress(sf::Event::KeyEvent event); static void handleKeyRelease(sf::Event::KeyEvent event); + + static void handleJoystickMovement(sf::Event::JoystickMoveEvent event); };