yupplemayham/YuppleMayham/src/gameplay/input.cpp

42 lines
1.2 KiB
C++

#include "gameplay/input.h"
#include "utility/command.h"
#include "gameplay/gameactor.h"
#include <SDL_timer.h>
void InputHandler::handleInput()
{
if (!actor) return;
Uint32 currentTime = SDL_GetTicks();
// check for bound keys that were pressed,
// next check if the hasn't been executed within the amount specified in delay
// if not execute the command and set lastExecution to currentTime
for (auto& [key, command] : keyCommands)
{
if (keys[key] == true)
{
if (currentTime - command.lastExecution >= command.delay)
{
command.cmd->execute(*actor);
command.lastExecution = currentTime;
}
}
}
// Same with the mouse, for this context we'll be checking for motion events and for click events
for (auto& [button, command] : mouseCommands)
{
if (mouseButtons[button] == true)
{
if (currentTime - command.lastExecution >= command.delay)
{
command.cmd->execute(*actor, mouse_state);
command.lastExecution = currentTime;
}
}
}
if (mouseMotionCommand)
mouseMotionCommand->execute(*actor, mouse_state);
if (mouseScrollCommand)
mouseScrollCommand->execute(*actor, mouse_state);
mouse_state.scroll = 0.0f; // clear mouse scroll since we have handled the event.
}