CameraActor.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "AudioSystem.h"
  13. #include "Game.h"
  14. #include "AudioComponent.h"
  15. #include "MeshComponent.h"
  16. CameraActor::CameraActor(Game* game)
  17. :Actor(game)
  18. {
  19. mMoveComp = new MoveComponent(this);
  20. mAudioComp = new AudioComponent(this);
  21. MeshComponent* mc = new MeshComponent(this);
  22. mc->SetMesh(game->GetRenderer()->GetMesh("Assets/Sphere.gpmesh"));
  23. mLastFootstep = 0.0f;
  24. mFootstep = mAudioComp->PlayEvent("event:/Footstep");
  25. mFootstep.SetPaused(true);
  26. }
  27. void CameraActor::UpdateActor(float deltaTime)
  28. {
  29. Actor::UpdateActor(deltaTime);
  30. // Play the footstep if we're moving and haven't recently
  31. mLastFootstep -= deltaTime;
  32. if (!Math::NearZero(mMoveComp->GetForwardSpeed()) && mLastFootstep <= 0.0f)
  33. {
  34. mFootstep.SetPaused(false);
  35. mFootstep.Restart();
  36. mLastFootstep = 0.5f;
  37. }
  38. // Compute new camera from this actor
  39. mCameraPos = GetPosition() - GetForward() * 200.0f + Vector3::UnitZ * 100.0f;
  40. Vector3 target = GetPosition() + GetForward() * 100.0f;
  41. Vector3 up = Vector3::UnitZ;
  42. Matrix4 view = Matrix4::CreateLookAt(mCameraPos, target, up);
  43. GetGame()->GetRenderer()->SetViewMatrix(view);
  44. GetGame()->GetAudioSystem()->SetListener(view);
  45. }
  46. void CameraActor::ActorInput(const uint8_t* keys)
  47. {
  48. float forwardSpeed = 0.0f;
  49. float angularSpeed = 0.0f;
  50. // wasd movement
  51. if (keys[SDL_SCANCODE_W])
  52. {
  53. forwardSpeed += 300.0f;
  54. }
  55. if (keys[SDL_SCANCODE_S])
  56. {
  57. forwardSpeed -= 300.0f;
  58. }
  59. if (keys[SDL_SCANCODE_A])
  60. {
  61. angularSpeed -= Math::TwoPi;
  62. }
  63. if (keys[SDL_SCANCODE_D])
  64. {
  65. angularSpeed += Math::TwoPi;
  66. }
  67. mMoveComp->SetForwardSpeed(forwardSpeed);
  68. mMoveComp->SetAngularSpeed(angularSpeed);
  69. }
  70. void CameraActor::SetFootstepSurface(float value)
  71. {
  72. // Pause here because the way I setup the parameter in FMOD
  73. // changing it will play a footstep
  74. mFootstep.SetPaused(true);
  75. mFootstep.SetParameter("Surface", value);
  76. }