#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->velocity.x = 0; transform->velocity.y = 0; sprite->playAnimation(IDLE); if (keystates[this->up]) { transform->velocity.y = -1; sprite->playAnimation(WALK); SoundManager::playSound(STEPS); } if (keystates[this->left]) { transform->velocity.x = -1; sprite->playAnimation(WALK); sprite->setDirection(LEFT); SoundManager::playSound(STEPS); } if (keystates[this->down]) { transform->velocity.y = 1; sprite->playAnimation(WALK); SoundManager::playSound(STEPS); } if (keystates[this->right]) { transform->velocity.x = 1; sprite->playAnimation(WALK); sprite->setDirection(RIGHT); SoundManager::playSound(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(RIGHT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, false, 1, 180, 1, "assets/egg.png"); } else { sprite->setDirection(LEFT); Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity, true, 1, 180, 1, "assets/egg.png"); } lastFireTime = currentTicks; } } }