From bf47252b8a13ec8d86d710a7d000719eea3e5e59 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Wed, 17 May 2023 14:41:30 +0200 Subject: [PATCH] Fixed inverted controller output --- src/game/input/direction.cpp | 4 ++-- src/game/input/input_mapper.cpp | 28 +++++++++++++--------------- src/game/player/player.cpp | 2 +- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/game/input/direction.cpp b/src/game/input/direction.cpp index 4e0fdc8..6242c7f 100644 --- a/src/game/input/direction.cpp +++ b/src/game/input/direction.cpp @@ -155,12 +155,12 @@ void Direction::set(sf::Vector2f direction) void Direction::setX(float value) { - sf::Vector2f newDirection = {value, directionVector.y}; + sf::Vector2f newDirection = sf::Vector2f(value, directionVector.y); set(newDirection); } void Direction::setY(float value) { - sf::Vector2f newDirection = {directionVector.x, value}; + sf::Vector2f newDirection = sf::Vector2f(directionVector.x, value); set(newDirection); } diff --git a/src/game/input/input_mapper.cpp b/src/game/input/input_mapper.cpp index eb70390..1cc74de 100644 --- a/src/game/input/input_mapper.cpp +++ b/src/game/input/input_mapper.cpp @@ -83,15 +83,12 @@ void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event) value = 0.f; } - if (axis == sf::Joystick::Axis::X || - axis == sf::Joystick::Axis::R || - axis == sf::Joystick::Axis::PovX) + if (axis == sf::Joystick::Axis::X) { gamepadIdentity->direction.setX(value); - } else if (axis == sf::Joystick::Axis::Y || - axis == sf::Joystick::Axis::U || - axis == sf::Joystick::Axis::PovY) + } else if (axis == sf::Joystick::Axis::Y) { + value *= -1; // Is inverted gamepadIdentity->direction.setY(value); } } @@ -103,29 +100,29 @@ std::shared_ptr InputMapper::getInputIdentity(InputDeviceType dev case InputDeviceType::KEYBOARD: return keyboardIdentity; case InputDeviceType::GAMEPAD: - if (InputMapper::gamepadIdentities.contains(gamepadId)) + if (!gamepadIdentities.contains(gamepadId)) { // Create if it does not exist yet - InputMapper::addGamepadIdentity(gamepadId); + addGamepadIdentity(gamepadId); } - return InputMapper::gamepadIdentities[gamepadId]; + return gamepadIdentities[gamepadId]; default: - return InputMapper::allIdentity; + return allIdentity; } } void InputMapper::addGamepadIdentity(unsigned int gamepadId) { // Exists already? - if (InputMapper::gamepadIdentities.contains(gamepadId)) + if (gamepadIdentities.contains(gamepadId)) { - InputMapper::gamepadIdentities[gamepadId]->isActive = true; + gamepadIdentities[gamepadId]->isActive = true; return; } // Does not exist yet, create new auto newIdentity = std::make_shared(InputDeviceType::GAMEPAD, gamepadId); - InputMapper::gamepadIdentities[gamepadId] = newIdentity; + gamepadIdentities[gamepadId] = newIdentity; } void InputMapper::deactivateGamepadIdentity(unsigned int gamepadId) @@ -137,7 +134,8 @@ void InputMapper::deactivateGamepadIdentity(unsigned int gamepadId) std::shared_ptr InputMapper::getInstance() { - if (singletonInstance == nullptr) { + if (singletonInstance == nullptr) + { singletonInstance = std::make_shared(); } @@ -151,7 +149,7 @@ std::vector> InputMapper::getAllInputIdentities() allIdentities.push_back(allIdentity); allIdentities.push_back(keyboardIdentity); - for (auto const& [key, val] : gamepadIdentities) + for (auto const &[key, val]: gamepadIdentities) { allIdentities.push_back(val); } diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 822f196..5350d21 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -29,7 +29,7 @@ void Player::update(Game *game) } // auto moveDirection = InputMapper::getInstance()->getInputIdentity(InputDeviceType::KEYBOARD)->direction.asVector(); - auto moveDirection = gamepadIdentity->direction.asScreenVector(); + auto moveDirection = gamepadIdentity->direction.asVector(); auto moveDelta = moveDirection * 30.0f * FRAME_TIME.asSeconds(); coordinates.move(moveDelta); circle->coordinates.set(coordinates);