From c3bc72fb4ccf8a5ba58da737d62cc0297518cc2e Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Wed, 17 May 2023 23:22:45 +0200 Subject: [PATCH] KeyFeatures added and sync commit --- CMakeLists.txt | 2 +- src/config.h | 38 +++++++++++++++++++++++++++++ src/game/input/direction.cpp | 10 ++++++++ src/game/input/direction.h | 3 +++ src/game/input/game_inputs.hpp | 25 ------------------- src/game/input/input_device_group.h | 16 ++++++++++++ src/game/input/input_identity.h | 38 ++++++++++++++++++++++------- src/game/input/input_mapper.cpp | 21 ++++++++-------- src/game/input/input_mapper.h | 4 +-- src/game/input/key_features.hpp | 35 ++++++++++++++++++++++++++ src/game/player/player.cpp | 24 +++++------------- src/game/player/player.hpp | 5 ++-- src/game/world/world_view.h | 2 +- src/main.cpp | 1 + 14 files changed, 155 insertions(+), 69 deletions(-) delete mode 100644 src/game/input/game_inputs.hpp create mode 100644 src/game/input/input_device_group.h create mode 100644 src/game/input/key_features.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 260029e..d1d2254 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/input/key_features.hpp src/game/world/world_view.cpp src/game/world/world_view.h src/utilities/smart_list.cpp diff --git a/src/config.h b/src/config.h index b7c6da1..a99a336 100644 --- a/src/config.h +++ b/src/config.h @@ -2,6 +2,9 @@ #define HOLESOME_CONFIG_H #include +#include +#include "game/input/" +#include "game/input/input_device_group.h" #define DEVELOPER_MODE true @@ -22,6 +25,41 @@ // Inputs #define JOYSTICK_DEADZONE 0.1f +// Key groups +const std::map> KEY_GROUPS = { + {InputDeviceGroup::KEYBOARD_WASD, + { + sf::Keyboard::W, + sf::Keyboard::A, + sf::Keyboard::S, + sf::Keyboard::D, + sf::Keyboard::Q, + sf::Keyboard::E + } + }, + {InputDeviceGroup::KEYBOARD_ARROWS, + { + sf::Keyboard::Up, + sf::Keyboard::Left, + sf::Keyboard::Down, + sf::Keyboard::Right, + sf::Keyboard::RShift, + sf::Keyboard::PageDown, + sf::Keyboard::PageUp, + sf::Keyboard::RControl, + } + }, + {InputDeviceGroup::KEYBOARD_IJKL, + { + sf::Keyboard::I, + sf::Keyboard::J, + sf::Keyboard::K, + sf::Keyboard::L, + sf::Keyboard::U, + sf::Keyboard::O + } + } +}; // Directions #define DIRECTION_HARD_ACTIVATION_THRESHOLD 0.1f diff --git a/src/game/input/direction.cpp b/src/game/input/direction.cpp index 6242c7f..40b642c 100644 --- a/src/game/input/direction.cpp +++ b/src/game/input/direction.cpp @@ -164,3 +164,13 @@ void Direction::setY(float value) sf::Vector2f newDirection = sf::Vector2f(directionVector.x, value); set(newDirection); } + +sf::Vector2f Direction::asIsometricVector() const +{ + // Rotate vector by 45 degrees clockwise + auto isometricVector = sf::Vector2f(0.0f, 0.0f); +// Todo + isometricVector.x = directionVector.y + directionVector.x; + isometricVector.y = directionVector.y + directionVector.x; + return isometricVector / 2.f; +} diff --git a/src/game/input/direction.h b/src/game/input/direction.h index f241253..5126908 100644 --- a/src/game/input/direction.h +++ b/src/game/input/direction.h @@ -31,6 +31,9 @@ public: public: sf::Vector2f asVector() const; + /// \brief Returns a vector that is rotated to accommodate isometric projection + /// \return + sf::Vector2f asIsometricVector() const; sf::Vector2f asScreenVector() const; HardDirection asHardDirection() const; diff --git a/src/game/input/game_inputs.hpp b/src/game/input/game_inputs.hpp deleted file mode 100644 index 313d7d3..0000000 --- a/src/game/input/game_inputs.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef HOLESOME_GAME_INPUTS_HPP -#define HOLESOME_GAME_INPUTS_HPP - - -enum MenuInputs -{ - MENU_NONE, - MENU_UP, - MENU_DOWN, - MENU_LEFT, - MENU_RIGHT, - MENU_SELECT, - MENU_BACK, - MENU_OPEN -}; - -enum GameInputs -{ - GAME_NONE, - GAME_HIDE, - GAME_MOVE, - GAME_RUN -}; - -#endif //HOLESOME_GAME_INPUTS_HPP diff --git a/src/game/input/input_device_group.h b/src/game/input/input_device_group.h new file mode 100644 index 0000000..db84220 --- /dev/null +++ b/src/game/input/input_device_group.h @@ -0,0 +1,16 @@ +#ifndef HOLESOME_INPUT_DEVICE_GROUP_H +#define HOLESOME_INPUT_DEVICE_GROUP_H + +#include "direction.h" +#include + +enum InputDeviceGroup +{ + UNKNOWN, + KEYBOARD_WASD, + KEYBOARD_ARROWS, + KEYBOARD_IJKL, + GAMEPAD +}; + +#endif //HOLESOME_INPUT_DEVICE_GROUP_H diff --git a/src/game/input/input_identity.h b/src/game/input/input_identity.h index 2914833..777c666 100644 --- a/src/game/input/input_identity.h +++ b/src/game/input/input_identity.h @@ -1,28 +1,48 @@ #ifndef HOLESOME_INPUT_IDENTITY_H #define HOLESOME_INPUT_IDENTITY_H +#include #include "direction.h" - -enum InputDeviceType -{ - ALL, - KEYBOARD, - GAMEPAD -}; +#include "input_device_group.h" struct InputIdentity { Direction direction = Direction(); - InputDeviceType deviceType = InputDeviceType::ALL; + InputDeviceGroup deviceType = InputDeviceGroup::UNKNOWN; unsigned int gamepadId = 0; unsigned int inputOrder = 0; bool isActive = true; - explicit InputIdentity(InputDeviceType type, unsigned int gamepad = 0) { + explicit InputIdentity(InputDeviceGroup type, unsigned int gamepad = 0) + { deviceType = type; gamepadId = gamepad; }; + + static InputDeviceGroup getDeviceTypeFromEvent(sf::Event event) + { + switch (event.type) + { + case sf::Event::KeyPressed: + case sf::Event::KeyReleased: + if (wasd_keys.contains(event.key.code)) + { + return InputDeviceGroup::KEYBOARD_WASD; + } else + { + return InputDeviceGroup::KEYBOARD_ARROWS; + } + case sf::Event::JoystickButtonPressed: + case sf::Event::JoystickButtonReleased: + case sf::Event::JoystickConnected: + case sf::Event::JoystickDisconnected: + case sf::Event::JoystickMoved: + return InputDeviceGroup::GAMEPAD; + default: + return InputDeviceGroup::UNKNOWN; + } + } }; diff --git a/src/game/input/input_mapper.cpp b/src/game/input/input_mapper.cpp index 1cc74de..4280e98 100644 --- a/src/game/input/input_mapper.cpp +++ b/src/game/input/input_mapper.cpp @@ -1,11 +1,12 @@ #include "input_mapper.h" +#include "input_device_group.h" InputMapper::InputMapper() { // Initialize identities - allIdentity = std::make_shared(InputDeviceType::ALL); - keyboardIdentity = std::make_shared(InputDeviceType::KEYBOARD); + allIdentity = std::make_shared(InputDeviceGroup::ALL); + keyboardIdentity = std::make_shared(InputDeviceGroup::KEYBOARD); gamepadIdentities = std::map>(); } @@ -56,7 +57,7 @@ void InputMapper::handleKeyPress(sf::Event::KeyEvent event) auto direction = Direction::getKeyDirection(event.code); if (direction != HardDirection::NONE) { - getInputIdentity(InputDeviceType::KEYBOARD)->direction.add(direction); + getInputIdentity(InputDeviceGroup::KEYBOARD)->direction.add(direction); } } @@ -66,13 +67,13 @@ void InputMapper::handleKeyRelease(sf::Event::KeyEvent event) auto direction = Direction::getKeyDirection(event.code); if (direction != HardDirection::NONE) { - getInputIdentity(InputDeviceType::KEYBOARD)->direction.remove(direction); + getInputIdentity(InputDeviceGroup::KEYBOARD)->direction.remove(direction); } } void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event) { - auto gamepadIdentity = getInputIdentity(InputDeviceType::GAMEPAD, event.joystickId); + auto gamepadIdentity = getInputIdentity(InputDeviceGroup::GAMEPAD, event.joystickId); auto value = event.position / 100.f; auto axis = event.axis; @@ -93,13 +94,13 @@ void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event) } } -std::shared_ptr InputMapper::getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId) +std::shared_ptr InputMapper::getInputIdentity(InputDeviceGroup deviceType, unsigned int gamepadId) { switch (deviceType) { - case InputDeviceType::KEYBOARD: + case InputDeviceGroup::KEYBOARD: return keyboardIdentity; - case InputDeviceType::GAMEPAD: + case InputDeviceGroup::GAMEPAD: if (!gamepadIdentities.contains(gamepadId)) { // Create if it does not exist yet @@ -121,13 +122,13 @@ void InputMapper::addGamepadIdentity(unsigned int gamepadId) } // Does not exist yet, create new - auto newIdentity = std::make_shared(InputDeviceType::GAMEPAD, gamepadId); + auto newIdentity = std::make_shared(InputDeviceGroup::GAMEPAD, gamepadId); gamepadIdentities[gamepadId] = newIdentity; } void InputMapper::deactivateGamepadIdentity(unsigned int gamepadId) { - auto gamepadIdentity = getInputIdentity(InputDeviceType::GAMEPAD, gamepadId); + auto gamepadIdentity = getInputIdentity(InputDeviceGroup::GAMEPAD, gamepadId); gamepadIdentity->isActive = false; } diff --git a/src/game/input/input_mapper.h b/src/game/input/input_mapper.h index 6c9855c..a73fc22 100644 --- a/src/game/input/input_mapper.h +++ b/src/game/input/input_mapper.h @@ -6,6 +6,7 @@ #include "../../config.h" #include "direction.h" #include "input_identity.h" +#include "input_device_group.h" /** * Maps the inputs to actions. @@ -19,13 +20,12 @@ public: void processEvents(); - std::shared_ptr getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId = 0); + std::shared_ptr getInputIdentity(InputDeviceGroup deviceType, unsigned int gamepadId = 0); std::vector> getAllInputIdentities(); private: static inline std::shared_ptr singletonInstance = nullptr; - std::shared_ptr allIdentity; std::shared_ptr keyboardIdentity; std::map> gamepadIdentities; diff --git a/src/game/input/key_features.hpp b/src/game/input/key_features.hpp new file mode 100644 index 0000000..3fbe344 --- /dev/null +++ b/src/game/input/key_features.hpp @@ -0,0 +1,35 @@ +#ifndef HOLESOME_KEY_FEATURES_HPP +#define HOLESOME_KEY_FEATURES_HPP + + +#include "direction.h" +#include "input_identity.h" +#include "../../config.h" +#include "input_device_group.h" + +struct KeyFeatures +{ + sf::Keyboard::Key key; + HardDirection hardDirection = HardDirection::NONE; + InputDeviceGroup deviceGroup = InputDeviceGroup::UNKNOWN; + + explicit KeyFeatures(sf::Keyboard::Key key); +}; + +KeyFeatures::KeyFeatures(sf::Keyboard::Key key) +{ + this->key = key; + hardDirection = Direction::getKeyDirection(key); + + // Determine device group + for (const auto& groupKeys: KEY_GROUPS) + { + if (groupKeys.second.contains(key)) + { + deviceGroup = groupKeys.first; + break; + } + } +} + +#endif //HOLESOME_KEY_FEATURES_HPP diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 5350d21..6fc9e58 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -1,5 +1,7 @@ #include "player.hpp" +#include + sf::Vector2f Player::getTrackablePosition() const { return coordinates.isometric().toScreen(); @@ -13,23 +15,7 @@ sf::Vector2f Player::getTrackableSize() const void Player::update(Game *game) { - // Get gamepad input - std::shared_ptr gamepadIdentity = nullptr; - for (auto inputIdentity: InputMapper::getInstance()->getAllInputIdentities()) - { - if (inputIdentity->deviceType == InputDeviceType::GAMEPAD) - { - gamepadIdentity = inputIdentity; - break; - } - } - if (gamepadIdentity == nullptr) - { - return; - } - -// auto moveDirection = InputMapper::getInstance()->getInputIdentity(InputDeviceType::KEYBOARD)->direction.asVector(); - auto moveDirection = gamepadIdentity->direction.asVector(); + auto moveDirection = input->direction.asVector(); auto moveDelta = moveDirection * 30.0f * FRAME_TIME.asSeconds(); coordinates.move(moveDelta); circle->coordinates.set(coordinates); @@ -45,10 +31,12 @@ Player::~Player() delete circle; } -Player::Player(const sf::Color color, WorldCoordinates initCoordinates) +Player::Player(std::shared_ptr assignedInput, const sf::Color color, WorldCoordinates initCoordinates) { coordinates.set(initCoordinates); + input = std::move(assignedInput); + circle = new CircleObject(10, color); circle->coordinates.set(coordinates); } diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index 30a5124..329699d 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -8,9 +8,7 @@ class Player : public GameObject, public ITrackable { public: - Player(); - - Player(const sf::Color color, WorldCoordinates initCoordinates); + Player(std::shared_ptr assignedInput, sf::Color color, WorldCoordinates initCoordinates); ~Player(); @@ -24,6 +22,7 @@ public: private: CircleObject *circle; + std::shared_ptr input; }; diff --git a/src/game/world/world_view.h b/src/game/world/world_view.h index 1f7f252..3c31886 100644 --- a/src/game/world/world_view.h +++ b/src/game/world/world_view.h @@ -42,7 +42,7 @@ private: sf::Vector2f getClosestPositionInArea(sf::Vector2f areaSize) const; - /// Calculates the direction the target should be pulled back in. + /// Calculates the hardDirection the target should be pulled back in. /// \return Normalized vector, pointing from center to target. sf::Vector2f getRubber() const; diff --git a/src/main.cpp b/src/main.cpp index 3c7406c..e454861 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ int main(int argc, char *argv[]) game->addGameObject(new GridDebugLayer(0, 50, 0, 50)); game->addGameObject(player); + game->addGameObject(new Player(sf::Color::Blue, {0, 0})); game->addGameObject(new WorldView()); game->run();