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..8f56b2f 100644 --- a/include/Entity.h +++ b/include/Entity.h @@ -9,6 +9,13 @@ #include "ECS.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 Component; diff --git a/include/InputComponent.h b/include/InputComponent.h new file mode 100644 index 0000000..f9c235a --- /dev/null +++ b/include/InputComponent.h @@ -0,0 +1,107 @@ +#pragma once +#include +#include + +#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 m_keyMappings; + void InitKeyMappings(); +}; diff --git a/include/KeyboardController.h b/include/KeyboardController.h deleted file mode 100644 index 9079b73..0000000 --- a/include/KeyboardController.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include - -#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(); -}; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 77915ae..8a70be9 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -2,7 +2,7 @@ #include "TextureManager.h" #include "SoundManager.h" -#include "Components.h" +#include "ProjectileComponent.h" #include "Game.h" #include "TransformComponent.h" diff --git a/src/Game.cpp b/src/Game.cpp index 22bf7bf..c38cc9c 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" @@ -133,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; @@ -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.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(); @@ -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.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/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/InputComponent.cpp b/src/InputComponent.cpp new file mode 100644 index 0000000..538ad93 --- /dev/null +++ b/src/InputComponent.cpp @@ -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} + }; +} diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp deleted file mode 100644 index df1d193..0000000 --- a/src/KeyboardController.cpp +++ /dev/null @@ -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(); - transform = &entity->getComponent(); -} - -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(); - - //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; -} \ No newline at end of file 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" 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; }