NetworkRandomTranslateComponent.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <Components/PerfTest/NetworkRandomTranslateComponent.h>
  8. #include <AzCore/Component/TransformBus.h>
  9. #include <AzCore/std/time.h>
  10. namespace MultiplayerSample
  11. {
  12. NetworkRandomTranslateComponentController::NetworkRandomTranslateComponentController(NetworkRandomTranslateComponent& parent)
  13. : NetworkRandomTranslateComponentControllerBase(parent), m_simpleLcgRandom(AZStd::GetTimeUTCMilliSecond())
  14. {
  15. }
  16. void NetworkRandomTranslateComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  17. {
  18. m_originalPosition = GetParent().GetEntity()->GetTransform()->GetWorldTranslation();
  19. m_destination = CalculateNextDestination();
  20. AZ::TickBus::Handler::BusConnect();
  21. }
  22. void NetworkRandomTranslateComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  23. {
  24. AZ::TickBus::Handler::BusDisconnect();
  25. }
  26. void NetworkRandomTranslateComponentController::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  27. {
  28. m_travelTime += deltaTime;
  29. const AZ::Vector3 currentPosition = GetParent().GetEntity()->GetTransform()->GetWorldTranslation();
  30. const float t = m_travelTime/GetParent().GetMovementDuration();
  31. const AZ::Vector3 newPosition = currentPosition.Lerp(m_destination, t);
  32. GetParent().GetEntity()->GetTransform()->SetWorldTranslation(newPosition);
  33. if (m_travelTime > GetParent().GetMovementDuration())
  34. {
  35. m_travelTime = 0.0f;
  36. m_destination = CalculateNextDestination();
  37. }
  38. }
  39. AZ::Vector3 NetworkRandomTranslateComponentController::CalculateNextDestination()
  40. {
  41. AZ::Vector3 random(0.5f - m_simpleLcgRandom.GetRandomFloat(), 0.5f - m_simpleLcgRandom.GetRandomFloat(), 0.5f - m_simpleLcgRandom.GetRandomFloat());
  42. random = GetParent().GetMaxMoveDistance() * random.GetNormalizedEstimate();
  43. return m_originalPosition+random;
  44. }
  45. }