73 lines
1.8 KiB
C++
73 lines
1.8 KiB
C++
#include "input_mapper.h"
|
|
|
|
|
|
void InputMapper::setGame(Game *game)
|
|
{
|
|
InputMapper::game = game;
|
|
}
|
|
|
|
|
|
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:
|
|
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 direction
|
|
auto direction = Direction::getDirection(event.code);
|
|
if (direction != InputDirection::NONE)
|
|
{
|
|
inputDirectionBuffer.push_back(direction);
|
|
}
|
|
}
|
|
|
|
InputDirection InputMapper::getInputDirection()
|
|
{
|
|
InputDirection direction = InputDirection::NONE;
|
|
|
|
for (InputDirection directionPart: inputDirectionBuffer)
|
|
{
|
|
direction = static_cast<InputDirection>(direction | directionPart);
|
|
}
|
|
|
|
return direction;
|
|
}
|
|
|
|
void InputMapper::handleKeyRelease(sf::Event::KeyEvent event)
|
|
{
|
|
// Handle direction
|
|
auto direction = Direction::getDirection(event.code);
|
|
if (direction != InputDirection::NONE)
|
|
{
|
|
// Remove direction from buffer
|
|
inputDirectionBuffer.erase(std::remove(inputDirectionBuffer.begin(), inputDirectionBuffer.end(), direction),
|
|
inputDirectionBuffer.end());
|
|
}
|
|
}
|