NetworkRandomImpulseComponent.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 <RigidBodyComponent.h>
  8. #include <Components/PerfTest/NetworkRandomImpulseComponent.h>
  9. namespace MultiplayerSample
  10. {
  11. NetworkRandomImpulseComponentController::NetworkRandomImpulseComponentController(NetworkRandomImpulseComponent& parent)
  12. : NetworkRandomImpulseComponentControllerBase(parent)
  13. , m_tickEvent{ [this] { TickEvent(); }, AZ::Name{ "NetworkRandomImpulseComponent" } }
  14. {
  15. }
  16. void NetworkRandomImpulseComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  17. {
  18. if (GetParent().GetEnableHopping())
  19. {
  20. m_tickEvent.Enqueue(AZ::TimeMs{ 0 }, true);
  21. m_accumulatedTime = 0.f;
  22. }
  23. }
  24. void NetworkRandomImpulseComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  25. {
  26. }
  27. void NetworkRandomImpulseComponentController::TickEvent()
  28. {
  29. const float deltaTime = static_cast<float>(m_tickEvent.TimeInQueueMs()) / 1000.f;
  30. m_accumulatedTime += deltaTime;
  31. if (m_accumulatedTime > GetParent().GetHopPeriod())
  32. {
  33. m_accumulatedTime = 0.f;
  34. if (PhysX::RigidBodyComponent* body = GetEntity()->FindComponent<PhysX::RigidBodyComponent>())
  35. {
  36. const AZ::Quaternion rotation = GetEntity()->GetTransform()->GetWorldRotationQuaternion();
  37. body->ApplyLinearImpulse(rotation.TransformVector(AZ::Vector3::CreateAxisZ(GetParent().GetHopForce())));
  38. }
  39. }
  40. }
  41. }