2023-05-07 12:27:11 +02:00
|
|
|
#include <map>
|
|
|
|
#include "direction.h"
|
2023-05-10 11:29:50 +02:00
|
|
|
#include "../../utilities/vector_utils.hpp"
|
2023-05-10 14:51:09 +02:00
|
|
|
#include "../../config.h"
|
2023-05-07 12:27:11 +02:00
|
|
|
|
2023-05-10 14:51:09 +02:00
|
|
|
HardDirection Direction::getKeyDirection(sf::Keyboard::Key key)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
2023-05-10 14:51:09 +02:00
|
|
|
auto map = std::map<sf::Keyboard::Key, HardDirection>();
|
|
|
|
map[sf::Keyboard::W] = HardDirection::UP;
|
|
|
|
map[sf::Keyboard::S] = HardDirection::DOWN;
|
|
|
|
map[sf::Keyboard::A] = HardDirection::LEFT;
|
|
|
|
map[sf::Keyboard::D] = HardDirection::RIGHT;
|
|
|
|
map[sf::Keyboard::Up] = HardDirection::UP;
|
|
|
|
map[sf::Keyboard::Down] = HardDirection::DOWN;
|
|
|
|
map[sf::Keyboard::Left] = HardDirection::LEFT;
|
|
|
|
map[sf::Keyboard::Right] = HardDirection::RIGHT;
|
2023-05-07 12:27:11 +02:00
|
|
|
|
|
|
|
if (map.find(key) == map.end())
|
2023-05-10 14:51:09 +02:00
|
|
|
return HardDirection::NONE;
|
2023-05-07 12:27:11 +02:00
|
|
|
|
|
|
|
return map[key];
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:23:25 +02:00
|
|
|
sf::Vector2f Direction::getScreenVector(HardDirection direction)
|
|
|
|
{
|
|
|
|
auto vector = getVector(direction);
|
|
|
|
vector.y *= -1;
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:09 +02:00
|
|
|
sf::Vector2f Direction::getVector(HardDirection direction)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
|
|
|
auto vector = sf::Vector2f(0.0f, 0.0f);
|
|
|
|
|
2023-05-10 14:51:09 +02:00
|
|
|
if (direction == HardDirection::NONE)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine all relevant directions into one vector
|
2023-05-10 14:51:09 +02:00
|
|
|
if (direction & HardDirection::UP)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
2023-05-14 22:23:25 +02:00
|
|
|
vector.y += 1;
|
2023-05-07 12:27:11 +02:00
|
|
|
}
|
2023-05-10 14:51:09 +02:00
|
|
|
if (direction & HardDirection::DOWN)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
2023-05-14 22:23:25 +02:00
|
|
|
vector.y -= 1;
|
2023-05-07 12:27:11 +02:00
|
|
|
}
|
2023-05-10 14:51:09 +02:00
|
|
|
if (direction & HardDirection::LEFT)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
|
|
|
vector.x -= 1;
|
|
|
|
}
|
2023-05-10 14:51:09 +02:00
|
|
|
if (direction & HardDirection::RIGHT)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
|
|
|
vector.x += 1;
|
|
|
|
}
|
|
|
|
|
2023-05-10 11:29:50 +02:00
|
|
|
return normalize(vector);
|
2023-05-07 12:27:11 +02:00
|
|
|
}
|
2023-05-10 14:51:09 +02:00
|
|
|
|
2023-05-14 22:23:25 +02:00
|
|
|
HardDirection Direction::getHardDirectionFromVector(sf::Vector2f vector)
|
2023-05-10 14:51:09 +02:00
|
|
|
{
|
|
|
|
HardDirection direction = HardDirection::NONE;
|
|
|
|
vector = normalize(vector);
|
|
|
|
auto absVector = abs(vector);
|
|
|
|
|
|
|
|
// X axis
|
|
|
|
if (absVector.x >= DIRECTION_HARD_ACTIVATION_THRESHOLD)
|
|
|
|
{
|
|
|
|
if (vector.x > 0)
|
|
|
|
{
|
|
|
|
direction = static_cast<HardDirection>(direction | HardDirection::RIGHT);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
direction = static_cast<HardDirection>(direction | HardDirection::LEFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Y axis
|
|
|
|
if (absVector.y >= DIRECTION_HARD_ACTIVATION_THRESHOLD)
|
|
|
|
{
|
|
|
|
if (vector.y > 0)
|
|
|
|
{
|
|
|
|
direction = static_cast<HardDirection>(direction | HardDirection::DOWN);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
direction = static_cast<HardDirection>(direction | HardDirection::UP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Vector2f Direction::asVector() const
|
|
|
|
{
|
|
|
|
return directionVector;
|
|
|
|
}
|
|
|
|
|
|
|
|
HardDirection Direction::asHardDirection() const
|
|
|
|
{
|
2023-05-14 22:23:25 +02:00
|
|
|
return getHardDirectionFromVector(directionVector);
|
2023-05-10 14:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Direction::Direction(sf::Vector2f directionVector)
|
|
|
|
{
|
|
|
|
this->directionVector = directionVector;
|
|
|
|
}
|
|
|
|
|
|
|
|
Direction::Direction(HardDirection direction)
|
|
|
|
{
|
|
|
|
this->directionVector = getVector(direction);
|
|
|
|
}
|
2023-05-14 22:23:25 +02:00
|
|
|
|
|
|
|
sf::Vector2f Direction::asScreenVector() const
|
|
|
|
{
|
|
|
|
auto screenVector = directionVector;
|
|
|
|
screenVector.y *= -1;
|
|
|
|
return screenVector;
|
|
|
|
}
|
2023-05-16 21:56:55 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-05-17 14:13:39 +02:00
|
|
|
|
|
|
|
void Direction::setX(float value)
|
|
|
|
{
|
2023-05-17 14:41:30 +02:00
|
|
|
sf::Vector2f newDirection = sf::Vector2f(value, directionVector.y);
|
2023-05-17 14:13:39 +02:00
|
|
|
set(newDirection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Direction::setY(float value)
|
|
|
|
{
|
2023-05-17 14:41:30 +02:00
|
|
|
sf::Vector2f newDirection = sf::Vector2f(directionVector.x, value);
|
2023-05-17 14:13:39 +02:00
|
|
|
set(newDirection);
|
|
|
|
}
|