51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
|
#include <map>
|
||
|
#include "direction.h"
|
||
|
|
||
|
InputDirection Direction::getDirection(sf::Keyboard::Key key)
|
||
|
{
|
||
|
auto map = std::map<sf::Keyboard::Key, InputDirection>();
|
||
|
map[sf::Keyboard::W] = InputDirection::UP;
|
||
|
map[sf::Keyboard::S] = InputDirection::DOWN;
|
||
|
map[sf::Keyboard::A] = InputDirection::LEFT;
|
||
|
map[sf::Keyboard::D] = InputDirection::RIGHT;
|
||
|
map[sf::Keyboard::Up] = InputDirection::UP;
|
||
|
map[sf::Keyboard::Down] = InputDirection::DOWN;
|
||
|
map[sf::Keyboard::Left] = InputDirection::LEFT;
|
||
|
map[sf::Keyboard::Right] = InputDirection::RIGHT;
|
||
|
|
||
|
if (map.find(key) == map.end())
|
||
|
return InputDirection::NONE;
|
||
|
|
||
|
return map[key];
|
||
|
}
|
||
|
|
||
|
sf::Vector2f Direction::getVector(InputDirection direction)
|
||
|
{
|
||
|
auto vector = sf::Vector2f(0.0f, 0.0f);
|
||
|
|
||
|
if (direction == InputDirection::NONE)
|
||
|
{
|
||
|
return vector;
|
||
|
}
|
||
|
|
||
|
// Combine all relevant directions into one vector
|
||
|
if (direction & InputDirection::UP)
|
||
|
{
|
||
|
vector.y -= 1;
|
||
|
}
|
||
|
if (direction & InputDirection::DOWN)
|
||
|
{
|
||
|
vector.y += 1;
|
||
|
}
|
||
|
if (direction & InputDirection::LEFT)
|
||
|
{
|
||
|
vector.x -= 1;
|
||
|
}
|
||
|
if (direction & InputDirection::RIGHT)
|
||
|
{
|
||
|
vector.x += 1;
|
||
|
}
|
||
|
|
||
|
return vector;
|
||
|
}
|