From 5dc61e62309d557aa1ff5f7aa6e825e3b8b7252b Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Wed, 1 May 2024 13:07:48 +0200 Subject: [PATCH 1/3] refactor: moved input polling from KeyboardController to input class --- include/Input.h | 101 ++++++++++++++++++++++++++++++ include/KeyboardController.h | 44 ++++++-------- src/Input.cpp | 115 +++++++++++++++++++++++++++++++++++ src/KeyboardController.cpp | 100 +++++++++++++++--------------- 4 files changed, 288 insertions(+), 72 deletions(-) create mode 100644 include/Input.h create mode 100644 src/Input.cpp diff --git a/include/Input.h b/include/Input.h new file mode 100644 index 0000000..3fc6f80 --- /dev/null +++ b/include/Input.h @@ -0,0 +1,101 @@ +#include +#include + +class Input +{ +public: + Input(); + ~Input(); + + void pollEvents(); + bool isKeyDown(Key key); + +private: + const Uint8* m_keyStates; + SDL_Scancode mapKeyToSDL(Key key); + std::map 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 +}; diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 9079b73..1d1fbd3 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -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 }; diff --git a/src/Input.cpp b/src/Input.cpp new file mode 100644 index 0000000..737c976 --- /dev/null +++ b/src/Input.cpp @@ -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} + }; +} \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index df1d193..48d2b82 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -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(); - transform = &entity->getComponent(); + m_sprite = &entity->getComponent(); + m_transform = &entity->getComponent(); } 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(); + m_player = &entity->getComponent(); //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; } \ No newline at end of file From 2e7a1b45cfc6fcddf90f45b8fec1105a382e9c6b Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Wed, 1 May 2024 13:47:42 +0200 Subject: [PATCH 2/3] 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 --- include/Components.h | 13 ------------- include/Entity.h | 6 ++++++ include/Input.h | 32 ++++++++++++++++---------------- src/AssetManager.cpp | 3 ++- src/Game.cpp | 4 +++- src/HealthComponent.cpp | 1 - src/ProjectileComponent.cpp | 3 ++- 7 files changed, 29 insertions(+), 33 deletions(-) delete mode 100644 include/Components.h diff --git a/include/Components.h b/include/Components.h deleted file mode 100644 index b6f506f..0000000 --- a/include/Components.h +++ /dev/null @@ -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" diff --git a/include/Entity.h b/include/Entity.h index 82ed6a5..7e1fa88 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -9,6 +9,12 @@ #include "ECS.h" #include "Constants.h" +// TODO: remove here if possible +// temporary fix: addComponent function template doesnt know TransformComponent -> error undefined type +#include "TransformComponent.h" +#include "KeyboardController.h" +#include "SpriteComponent.h" + class Manager; class Component; diff --git a/include/Input.h b/include/Input.h index 3fc6f80..b0cdb1d 100644 --- a/include/Input.h +++ b/include/Input.h @@ -1,22 +1,6 @@ #include #include -class Input -{ -public: - Input(); - ~Input(); - - void pollEvents(); - bool isKeyDown(Key key); - -private: - const Uint8* m_keyStates; - SDL_Scancode mapKeyToSDL(Key key); - std::map m_keyMappings; - void InitKeyMappings(); -}; - enum class Key { UP, @@ -99,3 +83,19 @@ enum class Key BACKSLASH, GRAVE }; + +class Input +{ +public: + Input(); + ~Input(); + + void pollEvents(); + bool isKeyDown(Key key); + +private: + const Uint8* m_keyStates; + SDL_Scancode mapKeyToSDL(Key key); + std::map m_keyMappings; + void InitKeyMappings(); +}; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index f759b19..27ac965 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -2,8 +2,9 @@ #include "TextureManager.h" #include "SoundManager.h" -#include "Components.h" +#include "ProjectileComponent.h" #include "Game.h" +#include "TextureDict.h" #include "TransformComponent.h" diff --git a/src/Game.cpp b/src/Game.cpp index e7ac1d4..5da420f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -3,8 +3,10 @@ #include #include "CollisionHandler.h" -#include "Components.h" #include "AssetManager.h" +#include "SoundManager.h" +#include "KeyboardController.h" +#include "TileComponent.h" #include "Direction.h" #include "Entity.h" #include "HealthComponent.h" diff --git a/src/HealthComponent.cpp b/src/HealthComponent.cpp index 7bbaa60..25dd3ff 100644 --- a/src/HealthComponent.cpp +++ b/src/HealthComponent.cpp @@ -1,6 +1,5 @@ #include "HealthComponent.h" -#include "Components.h" #include "Direction.h" #include "Entity.h" #include "Game.h" diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index 957fc66..deb588d 100644 --- a/src/ProjectileComponent.cpp +++ b/src/ProjectileComponent.cpp @@ -1,7 +1,8 @@ #include "ProjectileComponent.h" #include "CollisionHandler.h" -#include "Components.h" +#include "SoundManager.h" +#include "TransformComponent.h" #include "Entity.h" #include "Game.h" #include "HealthComponent.h" From aa83280b6a5c5d24bd327d2c4ce65ef4798e663b Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Sun, 26 May 2024 22:54:50 +0200 Subject: [PATCH 3/3] #32 KeyboardController: movement working projectiles need adjustments --- include/Entity.h | 3 +- include/{Input.h => InputComponent.h} | 16 +++-- include/KeyboardController.h | 40 ------------- src/Game.cpp | 12 ++-- src/{Input.cpp => InputComponent.cpp} | 24 +++++--- src/KeyboardController.cpp | 86 --------------------------- src/StatEffectsComponent.cpp | 4 +- 7 files changed, 38 insertions(+), 147 deletions(-) rename include/{Input.h => InputComponent.h} (83%) delete mode 100644 include/KeyboardController.h rename src/{Input.cpp => InputComponent.cpp} (89%) delete mode 100644 src/KeyboardController.cpp diff --git a/include/Entity.h b/include/Entity.h index 7e1fa88..8f56b2f 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -11,8 +11,9 @@ // 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 "KeyboardController.h" #include "SpriteComponent.h" class Manager; diff --git a/include/Input.h b/include/InputComponent.h similarity index 83% rename from include/Input.h rename to include/InputComponent.h index b0cdb1d..f9c235a 100644 --- a/include/Input.h +++ b/include/InputComponent.h @@ -1,13 +1,16 @@ +#pragma once #include #include +#include "Component.h" + enum class Key { UP, DOWN, LEFT, RIGHT, - FIRE, + SPACE, ENTER, ESCAPE, TAB, @@ -84,13 +87,16 @@ enum class Key GRAVE }; -class Input +class InputComponent : public Component { public: - Input(); - ~Input(); + InputComponent(); + ~InputComponent(); - void pollEvents(); + void init() override; + void update() override; + + // void pollEvents(); bool isKeyDown(Key key); private: diff --git a/include/KeyboardController.h b/include/KeyboardController.h deleted file mode 100644 index 1d1fbd3..0000000 --- a/include/KeyboardController.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include - -#include "Component.h" -#include "Vector2D.h" -#include "Constants.h" // TODO: change so gamespecific constants are in own file -#include "SoundManager.h" -#include "Input.h" - -class TransformComponent; -class SpriteComponent; - -class KeyboardController : public Component -{ -public: - 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: - 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 -}; diff --git a/src/Game.cpp b/src/Game.cpp index 5da420f..d4d1df5 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -5,7 +5,7 @@ #include "CollisionHandler.h" #include "AssetManager.h" #include "SoundManager.h" -#include "KeyboardController.h" +// #include "KeyboardController.h" #include "TileComponent.h" #include "Direction.h" #include "Entity.h" @@ -135,7 +135,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo return; } - engine::init(); + // engine::init(); // temporarily moved down to access groups at engine init call // character selection const char* player1Sprite; @@ -167,7 +167,8 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo player1.setTeam(Entity::TeamLabel::BLUE); player1.addComponent(80,80,2); //posx, posy, scale player1.addComponent(player1Sprite, true); //adds sprite (32x32px), path needed - player1.addComponent(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(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(); player1.addComponent("player", 0.8f); //adds tag (for further use, reference tag) player1.addComponent(5, Direction::LEFT); player1.addComponent(); @@ -177,11 +178,14 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo player2.setTeam(Entity::TeamLabel::RED); player2.addComponent(600, 500, 2); player2.addComponent(player2Sprite, true); - player2.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); + // player2.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0)); + player2.addComponent(); player2.addComponent("enemy", 0.8f); player2.addComponent(5, Direction::RIGHT); player2.addComponent(); player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); + + engine::init(); } void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite) diff --git a/src/Input.cpp b/src/InputComponent.cpp similarity index 89% rename from src/Input.cpp rename to src/InputComponent.cpp index 737c976..538ad93 100644 --- a/src/Input.cpp +++ b/src/InputComponent.cpp @@ -1,24 +1,30 @@ -#include "Input.h" +#include "InputComponent.h" -Input::Input() +InputComponent::InputComponent() { m_keyStates = SDL_GetKeyboardState(NULL); InitKeyMappings(); } -Input::~Input() = default; +InputComponent::~InputComponent() = default; -void Input::pollEvents() +void InputComponent::init() +{ + // m_keyStates = SDL_GetKeyboardState(NULL); + // InitKeyMappings(); +} + +void InputComponent::update() { SDL_PumpEvents(); } -bool Input::isKeyDown(Key key) +bool InputComponent::isKeyDown(Key key) { return m_keyStates[mapKeyToSDL(key)]; } -SDL_Scancode Input::mapKeyToSDL(Key key) +SDL_Scancode InputComponent::mapKeyToSDL(Key key) { auto it = m_keyMappings.find(key); if (it == m_keyMappings.end()) @@ -28,7 +34,7 @@ SDL_Scancode Input::mapKeyToSDL(Key key) return it->second; } -void Input::InitKeyMappings() +void InputComponent::InitKeyMappings() { m_keyMappings = { @@ -36,7 +42,7 @@ void Input::InitKeyMappings() {Key::DOWN, SDL_SCANCODE_DOWN}, {Key::LEFT, SDL_SCANCODE_LEFT}, {Key::RIGHT, SDL_SCANCODE_RIGHT}, - {Key::FIRE, SDL_SCANCODE_SPACE}, + {Key::SPACE, SDL_SCANCODE_SPACE}, {Key::ENTER, SDL_SCANCODE_RETURN}, {Key::ESCAPE, SDL_SCANCODE_ESCAPE}, {Key::TAB, SDL_SCANCODE_TAB}, @@ -112,4 +118,4 @@ void Input::InitKeyMappings() {Key::BACKSLASH, SDL_SCANCODE_BACKSLASH}, {Key::GRAVE, SDL_SCANCODE_GRAVE} }; -} \ No newline at end of file +} diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp deleted file mode 100644 index 48d2b82..0000000 --- a/src/KeyboardController.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "KeyboardController.h" - -#include "Game.h" -#include "TransformComponent.h" -#include "AssetManager.h" -#include "SpriteComponent.h" - -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() -{ - m_sprite = &entity->getComponent(); - m_transform = &entity->getComponent(); -} - -void KeyboardController::update() -{ - m_transform->direction.x = 0; - m_transform->direction.y = 0; - m_sprite->playAnimation(IDLE); - - 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 (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 - m_lastFireTime >= m_fireCooldown) - { - - m_player = &entity->getComponent(); - - //checks player source via the firing velocity - //TODO: adding actual projectile textures - 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()); - } - - 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->m_fireCooldown -= modifier * 400; -} \ No newline at end of file diff --git a/src/StatEffectsComponent.cpp b/src/StatEffectsComponent.cpp index 6c06263..e113462 100644 --- a/src/StatEffectsComponent.cpp +++ b/src/StatEffectsComponent.cpp @@ -1,7 +1,7 @@ #include "StatEffectsComponent.h" #include "Entity.h" #include "TransformComponent.h" -#include "KeyboardController.h" +// #include "KeyboardController.h" #include #include @@ -35,7 +35,7 @@ void StatEffectsComponent::modifyStatValue(Stats stat, int modifier) //modifier this->entity->getComponent().modifySpeed(modifier); break; case Stats::ATTACK_SPEED: - this->entity->getComponent().modifyAtkSpeed(modifier); + // this->entity->getComponent().modifyAtkSpeed(modifier); break; default: break; }