KeyFeatures added and sync commit
This commit is contained in:
parent
bf47252b8a
commit
c3bc72fb4c
14 changed files with 155 additions and 69 deletions
|
@ -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
|
||||
|
|
38
src/config.h
38
src/config.h
|
@ -2,6 +2,9 @@
|
|||
#define HOLESOME_CONFIG_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <map>
|
||||
#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<InputDeviceGroup, std::set<sf::Keyboard::Key>> 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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
16
src/game/input/input_device_group.h
Normal file
16
src/game/input/input_device_group.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef HOLESOME_INPUT_DEVICE_GROUP_H
|
||||
#define HOLESOME_INPUT_DEVICE_GROUP_H
|
||||
|
||||
#include "direction.h"
|
||||
#include <set>
|
||||
|
||||
enum InputDeviceGroup
|
||||
{
|
||||
UNKNOWN,
|
||||
KEYBOARD_WASD,
|
||||
KEYBOARD_ARROWS,
|
||||
KEYBOARD_IJKL,
|
||||
GAMEPAD
|
||||
};
|
||||
|
||||
#endif //HOLESOME_INPUT_DEVICE_GROUP_H
|
|
@ -1,28 +1,48 @@
|
|||
#ifndef HOLESOME_INPUT_IDENTITY_H
|
||||
#define HOLESOME_INPUT_IDENTITY_H
|
||||
|
||||
#include <set>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
#include "input_mapper.h"
|
||||
#include "input_device_group.h"
|
||||
|
||||
|
||||
InputMapper::InputMapper()
|
||||
{
|
||||
// Initialize identities
|
||||
allIdentity = std::make_shared<InputIdentity>(InputDeviceType::ALL);
|
||||
keyboardIdentity = std::make_shared<InputIdentity>(InputDeviceType::KEYBOARD);
|
||||
allIdentity = std::make_shared<InputIdentity>(InputDeviceGroup::ALL);
|
||||
keyboardIdentity = std::make_shared<InputIdentity>(InputDeviceGroup::KEYBOARD);
|
||||
gamepadIdentities = std::map<unsigned int, std::shared_ptr<InputIdentity>>();
|
||||
}
|
||||
|
||||
|
@ -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<InputIdentity> InputMapper::getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId)
|
||||
std::shared_ptr<InputIdentity> 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<InputIdentity>(InputDeviceType::GAMEPAD, gamepadId);
|
||||
auto newIdentity = std::make_shared<InputIdentity>(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;
|
||||
|
||||
}
|
||||
|
|
|
@ -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<InputIdentity> getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId = 0);
|
||||
std::shared_ptr<InputIdentity> getInputIdentity(InputDeviceGroup deviceType, unsigned int gamepadId = 0);
|
||||
std::vector<std::shared_ptr<InputIdentity>> getAllInputIdentities();
|
||||
|
||||
private:
|
||||
static inline std::shared_ptr<InputMapper> singletonInstance = nullptr;
|
||||
|
||||
std::shared_ptr<InputIdentity> allIdentity;
|
||||
std::shared_ptr<InputIdentity> keyboardIdentity;
|
||||
std::map<unsigned int, std::shared_ptr<InputIdentity>> gamepadIdentities;
|
||||
|
||||
|
|
35
src/game/input/key_features.hpp
Normal file
35
src/game/input/key_features.hpp
Normal file
|
@ -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
|
|
@ -1,5 +1,7 @@
|
|||
#include "player.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
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<InputIdentity> 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<InputIdentity> assignedInput, const sf::Color color, WorldCoordinates initCoordinates)
|
||||
{
|
||||
coordinates.set(initCoordinates);
|
||||
|
||||
input = std::move(assignedInput);
|
||||
|
||||
circle = new CircleObject(10, color);
|
||||
circle->coordinates.set(coordinates);
|
||||
}
|
||||
|
|
|
@ -8,9 +8,7 @@
|
|||
class Player : public GameObject, public ITrackable
|
||||
{
|
||||
public:
|
||||
Player();
|
||||
|
||||
Player(const sf::Color color, WorldCoordinates initCoordinates);
|
||||
Player(std::shared_ptr<InputIdentity> assignedInput, sf::Color color, WorldCoordinates initCoordinates);
|
||||
|
||||
~Player();
|
||||
|
||||
|
@ -24,6 +22,7 @@ public:
|
|||
|
||||
private:
|
||||
CircleObject *circle;
|
||||
std::shared_ptr<InputIdentity> input;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue