NetworkSimplePlayerCameraComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <AzCore/Component/ComponentApplicationBus.h>
  8. #include <Source/Components/NetworkAiComponent.h>
  9. #include <Source/Components/NetworkSimplePlayerCameraComponent.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzFramework/Components/CameraBus.h>
  12. #include <Multiplayer/IMultiplayer.h>
  13. namespace MultiplayerSample
  14. {
  15. AZ_CVAR(AZ::Vector3, cl_cameraOffset, AZ::Vector3(0.0f, -5.0f, 3.0f), nullptr, AZ::ConsoleFunctorFlags::Null, "Offset to use for the player camera");
  16. NetworkSimplePlayerCameraComponentController::NetworkSimplePlayerCameraComponentController(NetworkSimplePlayerCameraComponent& parent)
  17. : NetworkSimplePlayerCameraComponentControllerBase(parent)
  18. {
  19. ;
  20. }
  21. void NetworkSimplePlayerCameraComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  22. {
  23. // Synchronize aim angles with initial transform
  24. AZ::Vector3& aimAngles = ModifyAimAngles();
  25. aimAngles.SetZ(GetEntity()->GetTransform()->GetLocalRotation().GetZ());
  26. SetSyncAimImmediate(true);
  27. if (IsAutonomous())
  28. {
  29. m_aiEnabled = FindComponent<NetworkAiComponent>()->GetEnabled();
  30. if (!m_aiEnabled)
  31. {
  32. AZ::EntityId activeCameraId;
  33. Camera::CameraSystemRequestBus::BroadcastResult(activeCameraId, &Camera::CameraSystemRequestBus::Events::GetActiveCamera);
  34. m_activeCameraEntity = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->FindEntity(activeCameraId);
  35. }
  36. }
  37. AZ::TickBus::Handler::BusConnect();
  38. }
  39. void NetworkSimplePlayerCameraComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  40. {
  41. AZ::TickBus::Handler::BusDisconnect();
  42. }
  43. float NetworkSimplePlayerCameraComponentController::GetCameraYaw() const
  44. {
  45. return GetAimAngles().GetZ();
  46. }
  47. float NetworkSimplePlayerCameraComponentController::GetCameraPitch() const
  48. {
  49. return GetAimAngles().GetX();
  50. }
  51. float NetworkSimplePlayerCameraComponentController::GetCameraRoll() const
  52. {
  53. return GetAimAngles().GetY();
  54. }
  55. float NetworkSimplePlayerCameraComponentController::GetCameraYawPrevious() const
  56. {
  57. return GetAimAnglesPrevious().GetZ();
  58. }
  59. float NetworkSimplePlayerCameraComponentController::GetCameraPitchPrevious() const
  60. {
  61. return GetAimAnglesPrevious().GetX();
  62. }
  63. float NetworkSimplePlayerCameraComponentController::GetCameraRollPrevious() const
  64. {
  65. return GetAimAnglesPrevious().GetY();
  66. }
  67. void NetworkSimplePlayerCameraComponentController::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  68. {
  69. if (m_activeCameraEntity != nullptr && m_activeCameraEntity->GetState() == AZ::Entity::State::Active)
  70. {
  71. const AZ::Quaternion targetRotation = AZ::Quaternion::CreateRotationZ(GetCameraYaw()) * AZ::Quaternion::CreateRotationX(GetCameraPitch());
  72. const float blendFactor = Multiplayer::GetMultiplayer()->GetCurrentBlendFactor();
  73. AZ::Quaternion aimRotation = targetRotation;
  74. if (!GetSyncAimImmediate() && !AZ::IsClose(blendFactor, 1.0f))
  75. {
  76. const AZ::Quaternion prevRotation = AZ::Quaternion::CreateRotationZ(GetCameraYawPrevious()) * AZ::Quaternion::CreateRotationX(GetCameraPitchPrevious());
  77. if (!prevRotation.IsClose(targetRotation))
  78. {
  79. aimRotation = prevRotation.Slerp(targetRotation, blendFactor).GetNormalized();
  80. }
  81. }
  82. const AZ::Vector3 targetTranslation = GetEntity()->GetTransform()->GetWorldTM().GetTranslation();
  83. const AZ::Vector3 cameraOffset = aimRotation.TransformVector(cl_cameraOffset);
  84. const AZ::Transform cameraTransform = AZ::Transform::CreateFromQuaternionAndTranslation(aimRotation, targetTranslation + cameraOffset);
  85. m_activeCameraEntity->GetTransform()->SetWorldTM(cameraTransform);
  86. }
  87. if (GetSyncAimImmediate())
  88. {
  89. SetSyncAimImmediate(false);
  90. }
  91. }
  92. int NetworkSimplePlayerCameraComponentController::GetTickOrder()
  93. {
  94. return AZ::TICK_PRE_RENDER;
  95. }
  96. }