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

Compare commits

..

4 Commits

Author SHA1 Message Date
Sara Varga
5dfe42195f Merge branch 'ref/sara' into separation-refactor 2024-05-27 18:31:25 +02:00
Sara Varga
aa83280b6a #32 KeyboardController: movement working
projectiles need adjustments
2024-05-26 22:54:50 +02:00
Sara Varga
2e7a1b45cf refactor: removed Components.h file
consisted of includes of all components -> components included when not necessary
TODO: had to add 3 component includes in Entity.h because of template function; move if possible
2024-05-01 13:47:42 +02:00
Sara Varga
5dc61e6230 refactor: moved input polling from KeyboardController to input class 2024-05-01 13:07:48 +02:00
11 changed files with 250 additions and 148 deletions

View File

@ -1,13 +0,0 @@
#pragma once
#include "ECS.h"
#include "Component.h"
#include "Manager.h"
#include "Entity.h"
#include "TransformComponent.h"
#include "SpriteComponent.h"
#include "KeyboardController.h"
#include "ColliderComponent.h"
#include "TileComponent.h"
#include "ProjectileComponent.h"
#include "HealthComponent.h"

View File

@ -9,6 +9,13 @@
#include "ECS.h" #include "ECS.h"
#include "Constants.h" #include "Constants.h"
// TODO: remove here if possible
// temporary fix: addComponent function template doesnt know TransformComponent -> error undefined type
// #include "KeyboardController.h"
#include "InputComponent.h"
#include "TransformComponent.h"
#include "SpriteComponent.h"
class Manager; class Manager;
class Component; class Component;

107
include/InputComponent.h Normal file
View File

@ -0,0 +1,107 @@
#pragma once
#include <SDL.h>
#include <map>
#include "Component.h"
enum class Key
{
UP,
DOWN,
LEFT,
RIGHT,
SPACE,
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
};
class InputComponent : public Component
{
public:
InputComponent();
~InputComponent();
void init() override;
void update() override;
// 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();
};

View File

@ -1,44 +0,0 @@
#pragma once
#include <SDL.h>
#include "Component.h"
#include "Vector2D.h"
#include "Constants.h"
#include "SoundManager.h"
class TransformComponent;
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() = 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();
};

View File

@ -2,7 +2,7 @@
#include "TextureManager.h" #include "TextureManager.h"
#include "SoundManager.h" #include "SoundManager.h"
#include "Components.h" #include "ProjectileComponent.h"
#include "Game.h" #include "Game.h"
#include "TransformComponent.h" #include "TransformComponent.h"

View File

@ -3,8 +3,10 @@
#include <SDL_error.h> #include <SDL_error.h>
#include "CollisionHandler.h" #include "CollisionHandler.h"
#include "Components.h"
#include "AssetManager.h" #include "AssetManager.h"
#include "SoundManager.h"
// #include "KeyboardController.h"
#include "TileComponent.h"
#include "Direction.h" #include "Direction.h"
#include "Entity.h" #include "Entity.h"
#include "HealthComponent.h" #include "HealthComponent.h"
@ -133,7 +135,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
return; return;
} }
engine::init(); // engine::init(); // temporarily moved down to access groups at engine init call
// character selection // character selection
const char* player1Sprite; const char* player1Sprite;
@ -161,7 +163,8 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
player1.setTeam(Entity::TeamLabel::BLUE); player1.setTeam(Entity::TeamLabel::BLUE);
player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added // player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added
player1.addComponent<InputComponent>();
player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag) player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
player1.addComponent<HealthComponent>(5, Direction::LEFT); player1.addComponent<HealthComponent>(5, Direction::LEFT);
player1.addComponent<StatEffectsComponent>(); player1.addComponent<StatEffectsComponent>();
@ -171,11 +174,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
player2.setTeam(Entity::TeamLabel::RED); player2.setTeam(Entity::TeamLabel::RED);
player2.addComponent<TransformComponent>(600, 500, 2); player2.addComponent<TransformComponent>(600, 500, 2);
player2.addComponent<SpriteComponent>(player2Sprite, true); player2.addComponent<SpriteComponent>(player2Sprite, true);
player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); // player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0));
player2.addComponent<InputComponent>();
player2.addComponent<ColliderComponent>("enemy", 0.8f); player2.addComponent<ColliderComponent>("enemy", 0.8f);
player2.addComponent<HealthComponent>(5, Direction::RIGHT); player2.addComponent<HealthComponent>(5, Direction::RIGHT);
player2.addComponent<StatEffectsComponent>(); player2.addComponent<StatEffectsComponent>();
player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
engine::init();
} }
void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite)

View File

@ -1,6 +1,5 @@
#include "HealthComponent.h" #include "HealthComponent.h"
#include "Components.h"
#include "Direction.h" #include "Direction.h"
#include "Entity.h" #include "Entity.h"
#include "Game.h" #include "Game.h"

121
src/InputComponent.cpp Normal file
View File

@ -0,0 +1,121 @@
#include "InputComponent.h"
InputComponent::InputComponent()
{
m_keyStates = SDL_GetKeyboardState(NULL);
InitKeyMappings();
}
InputComponent::~InputComponent() = default;
void InputComponent::init()
{
// m_keyStates = SDL_GetKeyboardState(NULL);
// InitKeyMappings();
}
void InputComponent::update()
{
SDL_PumpEvents();
}
bool InputComponent::isKeyDown(Key key)
{
return m_keyStates[mapKeyToSDL(key)];
}
SDL_Scancode InputComponent::mapKeyToSDL(Key key)
{
auto it = m_keyMappings.find(key);
if (it == m_keyMappings.end())
{
return SDL_SCANCODE_UNKNOWN;
}
return it->second;
}
void InputComponent::InitKeyMappings()
{
m_keyMappings =
{
{Key::UP, SDL_SCANCODE_UP},
{Key::DOWN, SDL_SCANCODE_DOWN},
{Key::LEFT, SDL_SCANCODE_LEFT},
{Key::RIGHT, SDL_SCANCODE_RIGHT},
{Key::SPACE, 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 +0,0 @@
#include "KeyboardController.h"
#include "Game.h"
#include "Components.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;
}
void KeyboardController::init()
{
sprite = &entity->getComponent<SpriteComponent>();
transform = &entity->getComponent<TransformComponent>();
}
void KeyboardController::update()
{
transform->direction.x = 0;
transform->direction.y = 0;
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);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS);
}
if (keystates[this->fire]) {
Uint32 currentTicks = SDL_GetTicks();
if (currentTicks - lastFireTime >= fireCooldown) {
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,
1, 180, 2, "assets/egg.png", this->entity->getTeam());
}
lastFireTime = currentTicks;
}
}
}
void KeyboardController::modifyAtkSpeed(int8_t modifier)
{
this->fireCooldown -= modifier * 400;
}

View File

@ -1,7 +1,8 @@
#include "ProjectileComponent.h" #include "ProjectileComponent.h"
#include "CollisionHandler.h" #include "CollisionHandler.h"
#include "Components.h" #include "SoundManager.h"
#include "TransformComponent.h"
#include "Entity.h" #include "Entity.h"
#include "Game.h" #include "Game.h"
#include "HealthComponent.h" #include "HealthComponent.h"

View File

@ -1,7 +1,7 @@
#include "StatEffectsComponent.h" #include "StatEffectsComponent.h"
#include "Entity.h" #include "Entity.h"
#include "TransformComponent.h" #include "TransformComponent.h"
#include "KeyboardController.h" // #include "KeyboardController.h"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -35,7 +35,7 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier
this->entity->getComponent<TransformComponent>().modifySpeed(modifier); this->entity->getComponent<TransformComponent>().modifySpeed(modifier);
break; break;
case Stats::ATTACK_SPEED: case Stats::ATTACK_SPEED:
this->entity->getComponent<KeyboardController>().modifyAtkSpeed(modifier); // this->entity->getComponent<KeyboardController>().modifyAtkSpeed(modifier);
break; break;
default: break; default: break;
} }