/* * KeyboardController.h * * Created on: Mar 1, 2020 * Author: ayoungblood */ #ifndef SRC_ECS_KEYBOARDCONTROLLER_H_ #define SRC_ECS_KEYBOARDCONTROLLER_H_ #include "../game/Game.hpp" #include "ECS.h" #include "Components.h" #include "../assetmgr/AssetManager.h" class KeyboardController : public Component { public: TransformComponent *transform; SpriteComponent *sprite; Game *game; void init() override { transform = &entity->getComponent(); sprite = &entity->getComponent(); } void update() override { if (Game::event.type == SDL_KEYDOWN) { switch (Game::event.key.keysym.sym) { case SDLK_UP: // transform->velocity.y = -1; // sprite->Play("WalkNorth"); // if (transform->velocity.x < 0) // { // sprite->Play("WalkNW"); // } // if (transform->velocity.x > 0) // { // sprite->Play("WalkNW"); // sprite->spriteFlip = SDL_FLIP_HORIZONTAL; // } break; case SDLK_DOWN: // transform->velocity.y = 1; // sprite->Play("WalkSouth"); // if (transform->velocity.x < 0) // { // sprite->Play("WalkSW"); // } // if (transform->velocity.x > 0) // { // sprite->Play("WalkSW"); // sprite->spriteFlip = SDL_FLIP_HORIZONTAL; // } break; case SDLK_LEFT: transform->velocity.x = -1; sprite->Play("Walk"); sprite->spriteFlip = SDL_FLIP_NONE; break; case SDLK_RIGHT: transform->velocity.x = 1; sprite->Play("Walk"); sprite->spriteFlip = SDL_FLIP_HORIZONTAL; break; case SDLK_k: // game->printDebug(""); break; case SDLK_j: if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0) { Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0); } Game::gravityOnPlayer = true; sprite->Play("Jump"); transform->velocity.y = -1; break; default: break; } } if (Game::event.type == SDL_KEYUP) { switch (Game::event.key.keysym.sym) { case SDLK_UP: transform->velocity.y = 0; // sprite->Play("idle"); // sprite->spriteFlip = SDL_FLIP_NONE; break; case SDLK_DOWN: transform->velocity.y = 0; // sprite->Play("idle"); // sprite->spriteFlip = SDL_FLIP_NONE; break; case SDLK_LEFT: transform->velocity.x = 0; sprite->Play("Idle"); break; case SDLK_RIGHT: transform->velocity.x = 0; sprite->Play("Idle"); break; case SDLK_k: if (Game::debugCollisionBoxes) { Game::debugCollisionBoxes = false; } else {Game::debugCollisionBoxes = true; } break; case SDLK_j: transform->velocity.y = 0; sprite->Play("Fall"); Game::gravityOnPlayer = true; break; case SDLK_ESCAPE: // exit the game when Escape pressed Game::isRunning = false; break; default: break; } } } }; #endif /* SRC_ECS_KEYBOARDCONTROLLER_H_ */