InputComponent.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "InputComponent.h"
  9. #include "Actor.h"
  10. #include "InputSystem.h"
  11. InputComponent::InputComponent(class Actor* owner)
  12. :MoveComponent(owner)
  13. ,mForwardKey(0)
  14. ,mBackKey(0)
  15. ,mClockwiseKey(0)
  16. ,mCounterClockwiseKey(0)
  17. {
  18. }
  19. void InputComponent::ProcessInput(const InputState& state)
  20. {
  21. // Calculate forward speed for MoveComponent
  22. float forwardSpeed = 0.0f;
  23. if (state.Keyboard.GetKeyValue(SDL_Scancode(mForwardKey)))
  24. {
  25. forwardSpeed += mMaxForwardSpeed;
  26. }
  27. if (state.Keyboard.GetKeyValue(SDL_Scancode(mBackKey)))
  28. {
  29. forwardSpeed -= mMaxForwardSpeed;
  30. }
  31. SetForwardSpeed(forwardSpeed);
  32. // Calculate angular speed for MoveComponent
  33. float angularSpeed = 0.0f;
  34. if (state.Keyboard.GetKeyValue(SDL_Scancode(mClockwiseKey)))
  35. {
  36. angularSpeed += mMaxAngularSpeed;
  37. }
  38. if (state.Keyboard.GetKeyValue(SDL_Scancode(mCounterClockwiseKey)))
  39. {
  40. angularSpeed -= mMaxAngularSpeed;
  41. }
  42. SetAngularSpeed(angularSpeed);
  43. }