0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +00:00

refactor: moved input polling from KeyboardController to input class

This commit is contained in:
Sara Varga 2024-05-01 13:07:48 +02:00
parent e3852379e4
commit 5dc61e6230
4 changed files with 288 additions and 72 deletions

101
include/Input.h Normal file
View File

@ -0,0 +1,101 @@
#include <SDL.h>
#include <map>
class Input
{
public:
Input();
~Input();
void pollEvents();
bool isKeyDown(Key key);
private:
const Uint8* m_keyStates;
SDL_Scancode mapKeyToSDL(Key key);
std::map<Key, SDL_Scancode> m_keyMappings;
void InitKeyMappings();
};
enum class Key
{
UP,
DOWN,
LEFT,
RIGHT,
FIRE,
ENTER,
ESCAPE,
TAB,
BACKSPACE,
DELETE,
HOME,
END,
PAGE_UP,
PAGE_DOWN,
INSERT,
CAPS_LOCK,
LEFT_SHIFT,
RIGHT_SHIFT,
LEFT_CTRL,
RIGHT_CTRL,
LEFT_ALT,
RIGHT_ALT,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
NUM_0,
NUM_1,
NUM_2,
NUM_3,
NUM_4,
NUM_5,
NUM_6,
NUM_7,
NUM_8,
NUM_9,
LEFT_BRACKET,
RIGHT_BRACKET,
SEMICOLON,
APOSTROPHE,
COMMA,
PERIOD,
SLASH,
BACKSLASH,
GRAVE
};

View File

@ -3,8 +3,9 @@
#include "Component.h"
#include "Vector2D.h"
#include "Constants.h"
#include "Constants.h" // TODO: change so gamespecific constants are in own file
#include "SoundManager.h"
#include "Input.h"
class TransformComponent;
class SpriteComponent;
@ -12,33 +13,28 @@ class SpriteComponent;
class KeyboardController : public Component
{
public:
TransformComponent* transform;
const uint8_t* keystates = SDL_GetKeyboardState(NULL);
SDL_Scancode up;
SDL_Scancode down;
SDL_Scancode left;
SDL_Scancode right;
SDL_Scancode fire;
SpriteComponent* sprite;
//for attack cooldown in between shots
uint32_t lastFireTime = 0;
uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
KeyboardController() = default;
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity);
KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity);
~KeyboardController() = default;
void init() override;
void update() override;
void modifyAtkSpeed(int8_t modifier);
private:
//for creation of projectiles
TransformComponent* player; //for starting position of projectile
Vector2D fireVelocity; //decide source of projectile and flying direction
// SoundManager* soundEffect = Game::assets->getSound;
//SoundManager* soundEffect = new SoundManager();
Input* m_input;
Key m_up;
Key m_down;
Key m_left;
Key m_right;
Key m_fire;
TransformComponent* m_transform;
SpriteComponent* m_sprite;
TransformComponent* m_player; //for starting position of projectile
Vector2D m_fireVelocity; //decide source of projectile and flying direction
//for attack cooldown in between shots
uint32_t m_lastFireTime = 0;
uint32_t m_fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
};

115
src/Input.cpp Normal file
View File

@ -0,0 +1,115 @@
#include "Input.h"
Input::Input()
{
m_keyStates = SDL_GetKeyboardState(NULL);
InitKeyMappings();
}
Input::~Input() = default;
void Input::pollEvents()
{
SDL_PumpEvents();
}
bool Input::isKeyDown(Key key)
{
return m_keyStates[mapKeyToSDL(key)];
}
SDL_Scancode Input::mapKeyToSDL(Key key)
{
auto it = m_keyMappings.find(key);
if (it == m_keyMappings.end())
{
return SDL_SCANCODE_UNKNOWN;
}
return it->second;
}
void Input::InitKeyMappings()
{
m_keyMappings =
{
{Key::UP, SDL_SCANCODE_UP},
{Key::DOWN, SDL_SCANCODE_DOWN},
{Key::LEFT, SDL_SCANCODE_LEFT},
{Key::RIGHT, SDL_SCANCODE_RIGHT},
{Key::FIRE, SDL_SCANCODE_SPACE},
{Key::ENTER, SDL_SCANCODE_RETURN},
{Key::ESCAPE, SDL_SCANCODE_ESCAPE},
{Key::TAB, SDL_SCANCODE_TAB},
{Key::BACKSPACE, SDL_SCANCODE_BACKSPACE},
{Key::DELETE, SDL_SCANCODE_DELETE},
{Key::HOME, SDL_SCANCODE_HOME},
{Key::END, SDL_SCANCODE_END},
{Key::PAGE_UP, SDL_SCANCODE_PAGEUP},
{Key::PAGE_DOWN, SDL_SCANCODE_PAGEDOWN},
{Key::INSERT, SDL_SCANCODE_INSERT},
{Key::CAPS_LOCK, SDL_SCANCODE_CAPSLOCK},
{Key::LEFT_SHIFT, SDL_SCANCODE_LSHIFT},
{Key::RIGHT_SHIFT, SDL_SCANCODE_RSHIFT},
{Key::LEFT_CTRL, SDL_SCANCODE_LCTRL},
{Key::RIGHT_CTRL, SDL_SCANCODE_RCTRL},
{Key::LEFT_ALT, SDL_SCANCODE_LALT},
{Key::RIGHT_ALT, SDL_SCANCODE_RALT},
{Key::F1, SDL_SCANCODE_F1},
{Key::F2, SDL_SCANCODE_F2},
{Key::F3, SDL_SCANCODE_F3},
{Key::F4, SDL_SCANCODE_F4},
{Key::F5, SDL_SCANCODE_F5},
{Key::F6, SDL_SCANCODE_F6},
{Key::F7, SDL_SCANCODE_F7},
{Key::F8, SDL_SCANCODE_F8},
{Key::F9, SDL_SCANCODE_F9},
{Key::F10, SDL_SCANCODE_F10},
{Key::F11, SDL_SCANCODE_F11},
{Key::F12, SDL_SCANCODE_F12},
{Key::A, SDL_SCANCODE_A},
{Key::B, SDL_SCANCODE_B},
{Key::C, SDL_SCANCODE_C},
{Key::D, SDL_SCANCODE_D},
{Key::E, SDL_SCANCODE_E},
{Key::F, SDL_SCANCODE_F},
{Key::G, SDL_SCANCODE_G},
{Key::H, SDL_SCANCODE_H},
{Key::I, SDL_SCANCODE_I},
{Key::J, SDL_SCANCODE_J},
{Key::K, SDL_SCANCODE_K},
{Key::L, SDL_SCANCODE_L},
{Key::M, SDL_SCANCODE_M},
{Key::N, SDL_SCANCODE_N},
{Key::O, SDL_SCANCODE_O},
{Key::P, SDL_SCANCODE_P},
{Key::Q, SDL_SCANCODE_Q},
{Key::R, SDL_SCANCODE_R},
{Key::S, SDL_SCANCODE_S},
{Key::T, SDL_SCANCODE_T},
{Key::U, SDL_SCANCODE_U},
{Key::V, SDL_SCANCODE_V},
{Key::W, SDL_SCANCODE_W},
{Key::X, SDL_SCANCODE_X},
{Key::Y, SDL_SCANCODE_Y},
{Key::Z, SDL_SCANCODE_Z},
{Key::NUM_0, SDL_SCANCODE_0},
{Key::NUM_1, SDL_SCANCODE_1},
{Key::NUM_2, SDL_SCANCODE_2},
{Key::NUM_3, SDL_SCANCODE_3},
{Key::NUM_4, SDL_SCANCODE_4},
{Key::NUM_5, SDL_SCANCODE_5},
{Key::NUM_6, SDL_SCANCODE_6},
{Key::NUM_7, SDL_SCANCODE_7},
{Key::NUM_8, SDL_SCANCODE_8},
{Key::NUM_9, SDL_SCANCODE_9},
{Key::LEFT_BRACKET, SDL_SCANCODE_LEFTBRACKET},
{Key::RIGHT_BRACKET, SDL_SCANCODE_RIGHTBRACKET},
{Key::SEMICOLON, SDL_SCANCODE_SEMICOLON},
{Key::APOSTROPHE, SDL_SCANCODE_APOSTROPHE},
{Key::COMMA, SDL_SCANCODE_COMMA},
{Key::PERIOD, SDL_SCANCODE_PERIOD},
{Key::SLASH, SDL_SCANCODE_SLASH},
{Key::BACKSLASH, SDL_SCANCODE_BACKSLASH},
{Key::GRAVE, SDL_SCANCODE_GRAVE}
};
}

View File

@ -1,82 +1,86 @@
#include "KeyboardController.h"
#include "Game.h"
#include "Components.h"
#include "TransformComponent.h"
#include "AssetManager.h"
#include "SpriteComponent.h"
KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity)
{
this->up = up;
this->down = down;
this->left = left;
this->right = right;
this->fire = fire;
this->fireVelocity = fireVelocity;
}
KeyboardController::KeyboardController(Input* input, Key up, Key down, Key left, Key right, Key fire, Vector2D fireVelocity)
: m_input(input), m_up(up), m_down(down), m_left(left), m_right(right), m_fire(fire) {}
void KeyboardController::init()
{
sprite = &entity->getComponent<SpriteComponent>();
transform = &entity->getComponent<TransformComponent>();
m_sprite = &entity->getComponent<SpriteComponent>();
m_transform = &entity->getComponent<TransformComponent>();
}
void KeyboardController::update()
{
transform->direction.x = 0;
transform->direction.y = 0;
sprite->playAnimation(IDLE);
m_transform->direction.x = 0;
m_transform->direction.y = 0;
m_sprite->playAnimation(IDLE);
if (keystates[this->up]) {
transform->direction.y = -1;
sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (keystates[this->left]) {
transform->direction.x = -1;
sprite->playAnimation(WALK);
sprite->setDirection(Direction::LEFT);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (keystates[this->down]) {
transform->direction.y = 1;
sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (keystates[this->right]) {
transform->direction.x = 1;
sprite->playAnimation(WALK);
sprite->setDirection(Direction::RIGHT);
if (m_input->isKeyDown(m_left))
{
m_transform->direction.x = -1;
m_sprite->playAnimation(WALK);
m_sprite->setDirection(Direction::LEFT);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (keystates[this->fire]) {
if (m_input->isKeyDown(m_right))
{
m_transform->direction.x = 1;
m_sprite->playAnimation(WALK);
m_sprite->setDirection(Direction::RIGHT);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (m_input->isKeyDown(m_up))
{
m_transform->direction.y = -1;
m_sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (m_input->isKeyDown(m_down))
{
m_transform->direction.y = 1;
m_sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (m_input->isKeyDown(m_fire))
{
Uint32 currentTicks = SDL_GetTicks();
if (currentTicks - lastFireTime >= fireCooldown) {
if (currentTicks - m_lastFireTime >= m_fireCooldown)
{
player = &entity->getComponent<TransformComponent>();
m_player = &entity->getComponent<TransformComponent>();
//checks player source via the firing velocity
//TODO: adding actual projectile textures
if (fireVelocity.x > 0) {
sprite->setDirection(Direction::RIGHT);
this->entity->getManager().getGame()->assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity,
1, 180, 2, "assets/egg.png", this->entity->getTeam());
}
else {
sprite->setDirection(Direction::LEFT);
this->entity->getManager().getGame()->assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity,
if (m_fireVelocity.x > 0)
{
m_sprite->setDirection(Direction::RIGHT);
this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity,
1, 180, 2, "assets/egg.png", this->entity->getTeam());
}
lastFireTime = currentTicks;
else
{
m_sprite->setDirection(Direction::LEFT);
this->entity->getManager().getGame()->assets->createProjectile(Vector2D(m_player->position.x, m_player->position.y), m_fireVelocity,
1, 180, 2, "assets/egg.png", this->entity->getTeam());
}
m_lastFireTime = currentTicks;
}
}
}
void KeyboardController::modifyAtkSpeed(int8_t modifier)
{
this->fireCooldown -= modifier * 400;
this->m_fireCooldown -= modifier * 400;
}