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; }