holesome/src/game/input/input_mapper.cpp

74 lines
1.8 KiB
C++
Raw Normal View History

#include "input_mapper.h"
2023-05-08 18:20:55 +02:00
void InputMapper::setGame(Game *game)
{
2023-05-08 18:20:55 +02:00
InputMapper::game = game;
}
2023-05-08 18:20:55 +02:00
void InputMapper::processEvents()
{
sf::Event event{};
while (game->window->pollEvent(event))
{
switch (event.type)
{
case sf::Event::KeyPressed:
handleKeyPress(event.key);
break;
case sf::Event::KeyReleased:
2023-05-09 20:50:50 +02:00
handleKeyRelease(event.key);
break;
case sf::Event::Closed:
game->exit();
break;
case sf::Event::Resized:
break;
default:
break;
}
}
}
void InputMapper::handleKeyPress(sf::Event::KeyEvent event)
{
// Close game on Escape or Q in DEV Mode
if (DEVELOPER_MODE && (event.code == sf::Keyboard::Escape || event.code == sf::Keyboard::Q))
{
game->exit();
return;
}
// Handle directionVector
auto direction = Direction::getKeyDirection(event.code);
if (direction != HardDirection::NONE)
{
2023-05-09 20:50:50 +02:00
inputDirectionBuffer.push_back(direction);
}
}
Direction InputMapper::getInputDirection()
2023-05-09 20:50:50 +02:00
{
HardDirection direction = HardDirection::NONE;
2023-05-09 20:50:50 +02:00
for (HardDirection directionPart: inputDirectionBuffer)
2023-05-09 20:50:50 +02:00
{
direction = static_cast<HardDirection>(direction | directionPart);
2023-05-09 20:50:50 +02:00
}
return Direction(direction);
2023-05-09 20:50:50 +02:00
}
void InputMapper::handleKeyRelease(sf::Event::KeyEvent event)
{
// Handle directionVector
auto direction = Direction::getKeyDirection(event.code);
if (direction != HardDirection::NONE)
2023-05-09 20:50:50 +02:00
{
// Remove directionVector from buffer
2023-05-09 20:50:50 +02:00
inputDirectionBuffer.erase(std::remove(inputDirectionBuffer.begin(), inputDirectionBuffer.end(), direction),
inputDirectionBuffer.end());
}
}