Sync commit

This commit is contained in:
Maximilian Giller 2023-05-16 21:56:55 +02:00
parent f9c592b6e0
commit b7fca7d07a
6 changed files with 112 additions and 2 deletions

View file

@ -44,7 +44,7 @@ set(SOURCES
src/game/input/direction.cpp src/game/input/direction.cpp
src/game/player/player.cpp src/game/player/player.cpp
src/game/player/player.hpp 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 set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp) src/prototypes/physics_00.cpp)

View file

@ -117,3 +117,38 @@ sf::Vector2f Direction::asScreenVector() const
screenVector.y *= -1; screenVector.y *= -1;
return screenVector; return screenVector;
} }
Direction::Direction()
{
directionVector = sf::Vector2f(0.0f, 0.0f);
}
void Direction::add(HardDirection direction)
{
summedDirections = static_cast<HardDirection>(summedDirections | direction);
directionVector = getVector(summedDirections);
}
void Direction::remove(HardDirection direction)
{
summedDirections = static_cast<HardDirection>(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;
}

View file

@ -16,6 +16,8 @@ enum HardDirection
struct Direction struct Direction
{ {
public: public:
Direction();
explicit Direction(sf::Vector2f directionVector); explicit Direction(sf::Vector2f directionVector);
explicit Direction(HardDirection direction); explicit Direction(HardDirection direction);
@ -33,8 +35,16 @@ public:
HardDirection asHardDirection() const; HardDirection asHardDirection() const;
public:
void add(HardDirection direction);
void remove(HardDirection direction);
void set(HardDirection direction);
void set(sf::Vector2f direction);
void clear();
private: private:
sf::Vector2f directionVector; sf::Vector2f directionVector;
HardDirection summedDirections = HardDirection::NONE;
}; };
#endif //HOLESOME_DIRECTION_H #endif //HOLESOME_DIRECTION_H

View file

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

View file

@ -4,6 +4,11 @@
void InputMapper::setGame(Game *game) void InputMapper::setGame(Game *game)
{ {
InputMapper::game = game; InputMapper::game = game;
// Initialize identities
allIdentity = std::make_shared<InputIdentity>(InputDeviceType::ALL);
keyboardIdentity = std::make_shared<InputIdentity>(InputDeviceType::KEYBOARD);
gamepadIdentities = std::map<unsigned int, std::shared_ptr<InputIdentity>>();
} }
@ -25,6 +30,9 @@ void InputMapper::processEvents()
break; break;
case sf::Event::Resized: case sf::Event::Resized:
break; break;
case sf::Event::JoystickMoved:
handleJoystickMovement(event.joystickMove);
break;
default: default:
break; break;
} }
@ -71,3 +79,25 @@ void InputMapper::handleKeyRelease(sf::Event::KeyEvent event)
inputDirectionBuffer.end()); inputDirectionBuffer.end());
} }
} }
void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event)
{
event.joystickId;
}
std::shared_ptr<InputIdentity> 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;
}
}

View file

@ -5,6 +5,7 @@
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
#include "../../config.h" #include "../../config.h"
#include "direction.h" #include "direction.h"
#include "input_identity.h"
class Game; class Game;
@ -18,16 +19,22 @@ public:
static void processEvents(); static void processEvents();
static Direction getInputDirection(); static std::shared_ptr<InputIdentity> getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId = 0);
private: private:
static inline Game *game = nullptr; static inline Game *game = nullptr;
static std::shared_ptr<InputIdentity> allIdentity;
static std::shared_ptr<InputIdentity> keyboardIdentity;
static std::map<unsigned int, std::shared_ptr<InputIdentity>> gamepadIdentities;
static inline std::vector<HardDirection> inputDirectionBuffer = std::vector<HardDirection>(); static inline std::vector<HardDirection> inputDirectionBuffer = std::vector<HardDirection>();
static void handleKeyPress(sf::Event::KeyEvent event); static void handleKeyPress(sf::Event::KeyEvent event);
static void handleKeyRelease(sf::Event::KeyEvent event); static void handleKeyRelease(sf::Event::KeyEvent event);
static void handleJoystickMovement(sf::Event::JoystickMoveEvent event);
}; };