/* * 2D Game Engine * KeyboardMovement.h: * Based on pikuma.com 2D game engine in C++ and Lua course * Copyright (c) 2021 986-Studio. All rights reserved. * * Created by Manoƫl Trapier on 16/02/2021. */ #ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_KEYBOARDMOVEMENT_H #define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_KEYBOARDMOVEMENT_H #include #include #include #include #include #include #include class KeyboardMovementSystem : public System { public: KeyboardMovementSystem() { this->requireComponent(); this->requireComponent(); this->requireComponent(); } void subscriptToEvents(std::unique_ptr &eventBus) { eventBus->subscribeToEvent(this, &KeyboardMovementSystem::onKeyPressed); } void onKeyPressed(KeyPressedEvent &event) { for(auto entity: this->getSystemEntities()) { auto keyboardControl = entity.getComponent(); auto &sprite = entity.getComponent(); auto &rigitBody = entity.getComponent(); switch(event.keyCode) { case SDLK_UP: rigitBody.velocity = keyboardControl.upVelocity; sprite.sourceRect.y = sprite.height * 0; break; case SDLK_RIGHT: rigitBody.velocity = keyboardControl.rightVelocity; sprite.sourceRect.y = sprite.height * 1; break; case SDLK_DOWN: rigitBody.velocity = keyboardControl.downVelocity; sprite.sourceRect.y = sprite.height * 2; break; case SDLK_LEFT: rigitBody.velocity = keyboardControl.leftVelocity; sprite.sourceRect.y = sprite.height * 3; break; default: /* Ignore all other keys */ break; } } } void Update() { } }; #endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_KEYBOARDMOVEMENT_H */