NetworkTestComponent.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <AzCore/Component/TransformBus.h>
  8. #include <AzCore/Serialization/EditContext.h>
  9. #include <Components/PerfTest/NetworkTestComponent.h>
  10. #include <Multiplayer/IMultiplayer.h>
  11. #include <Multiplayer/Components/NetBindComponent.h>
  12. namespace MultiplayerSample
  13. {
  14. void NetworkTestComponent::Reflect(AZ::ReflectContext* context)
  15. {
  16. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  17. if (serializeContext)
  18. {
  19. serializeContext->Class<NetworkTestComponent, AZ::Component>()
  20. ->Field("Enable Movement", &NetworkTestComponent::m_enableMotion)
  21. ->Field("Oscillator Amplitude", &NetworkTestComponent::m_oscillatorAmplitude)
  22. ->Field("Oscillator Speed", &NetworkTestComponent::m_oscillatorSpeedFactor)
  23. ->Version(1);
  24. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  25. {
  26. using namespace AZ::Edit;
  27. editContext->Class<NetworkTestComponent>("Network Test Helper",
  28. "Various helpful test tools and behaviors to test multiplayer logic and performance.")
  29. ->ClassElement(ClassElements::EditorData, "")
  30. ->Attribute(AZ::Edit::Attributes::Category, "MultiplayerSample")
  31. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  32. ->DataElement(nullptr, &NetworkTestComponent::m_enableMotion, "Enabled", "enabled oscillation along Z axis")
  33. ->DataElement(nullptr, &NetworkTestComponent::m_oscillatorAmplitude, "Oscillator Amplitude", "amplitude along Z axis")
  34. ->DataElement(nullptr, &NetworkTestComponent::m_oscillatorSpeedFactor, "Oscillator Speed", "speed factor along Z axis")
  35. ;
  36. }
  37. }
  38. }
  39. void NetworkTestComponent::Activate()
  40. {
  41. if (const Multiplayer::NetBindComponent* netBindComponent = GetEntity()->FindComponent<Multiplayer::NetBindComponent>())
  42. {
  43. if (netBindComponent->IsNetEntityRoleAuthority())
  44. {
  45. AZ::TickBus::Handler::BusConnect();
  46. m_startTranslation = GetEntity()->GetTransform()->GetWorldTranslation();
  47. }
  48. }
  49. }
  50. void NetworkTestComponent::Deactivate()
  51. {
  52. AZ::TickBus::Handler::BusDisconnect();
  53. }
  54. void NetworkTestComponent::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  55. {
  56. m_accumulatedTime += deltaTime;
  57. AZ::Vector3 copy = m_startTranslation;
  58. copy.SetZ(copy.GetZ() + AZStd::sin(m_accumulatedTime * m_oscillatorSpeedFactor) * m_oscillatorAmplitude);
  59. GetEntity()->GetTransform()->SetWorldTranslation(copy);
  60. }
  61. }