CameraActor.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "CameraActor.h"
  9. #include "MoveComponent.h"
  10. #include "SDL/SDL_scancode.h"
  11. #include "Renderer.h"
  12. #include "Game.h"
  13. CameraActor::CameraActor(Game* game)
  14. :Actor(game)
  15. {
  16. mMoveComp = new MoveComponent(this);
  17. }
  18. void CameraActor::UpdateActor(float deltaTime)
  19. {
  20. Actor::UpdateActor(deltaTime);
  21. // Compute new camera from this actor
  22. Vector3 cameraPos = GetPosition();
  23. Vector3 target = GetPosition() + GetForward() * 100.0f;
  24. Vector3 up = Vector3::UnitZ;
  25. Matrix4 view = Matrix4::CreateLookAt(cameraPos, target, up);
  26. GetGame()->GetRenderer()->SetViewMatrix(view);
  27. }
  28. void CameraActor::ActorInput(const uint8_t* keys)
  29. {
  30. float forwardSpeed = 0.0f;
  31. float angularSpeed = 0.0f;
  32. // wasd movement
  33. if (keys[SDL_SCANCODE_W])
  34. {
  35. forwardSpeed += 300.0f;
  36. }
  37. if (keys[SDL_SCANCODE_S])
  38. {
  39. forwardSpeed -= 300.0f;
  40. }
  41. if (keys[SDL_SCANCODE_A])
  42. {
  43. angularSpeed -= Math::TwoPi;
  44. }
  45. if (keys[SDL_SCANCODE_D])
  46. {
  47. angularSpeed += Math::TwoPi;
  48. }
  49. mMoveComp->SetForwardSpeed(forwardSpeed);
  50. mMoveComp->SetAngularSpeed(angularSpeed);
  51. }