RpcTesterComponent.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
  8. #include <Source/Components/RpcTesterComponent.h>
  9. #if AZ_TRAIT_CLIENT
  10. #include <DebugDraw/DebugDrawBus.h>
  11. #endif
  12. namespace MultiplayerSample
  13. {
  14. void RpcTesterComponent::Reflect(AZ::ReflectContext* context)
  15. {
  16. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  17. if (serializeContext)
  18. {
  19. serializeContext->Class<RpcTesterComponent, RpcTesterComponentBase>()
  20. ->Version(1);
  21. }
  22. RpcTesterComponentBase::Reflect(context);
  23. }
  24. void RpcTesterComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  25. {
  26. AZStd::string testType;
  27. if (GetTestAutoToAuthorityRPC())
  28. {
  29. testType = "Auto->Auth";
  30. }
  31. else if (GetTestServerToAuthorityRPC())
  32. {
  33. testType = "Server->Auth";
  34. }
  35. else if (GetTestAuthorityToAutoRPC())
  36. {
  37. testType = "Auth->Auto";
  38. }
  39. else if (GetTestAuthorityToClientRPC())
  40. {
  41. testType = "Auth->Client";
  42. }
  43. #if AZ_TRAIT_CLIENT
  44. DebugDraw::DebugDrawRequestBus::Broadcast(&DebugDraw::DebugDrawRequestBus::Events::DrawTextOnEntity,
  45. GetEntityId(), testType, AZ::Colors::White, -1.f);
  46. #endif
  47. if (AZ::Render::MaterialComponentRequests* material =
  48. AZ::Render::MaterialComponentRequestBus::FindFirstHandler(GetEntityId()))
  49. {
  50. material->SetMaterialAssetIdOnDefaultSlot(GetTestWaitingMaterial().GetId());
  51. }
  52. }
  53. void RpcTesterComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  54. {
  55. m_delayTestRun.RemoveFromQueue();
  56. }
  57. #if AZ_TRAIT_CLIENT
  58. void RpcTesterComponent::HandleRPC_TestPassed([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  59. {
  60. if (AZ::Render::MaterialComponentRequests* material =
  61. AZ::Render::MaterialComponentRequestBus::FindFirstHandler(GetEntityId()))
  62. {
  63. material->SetMaterialAssetIdOnDefaultSlot(GetTestPassedMaterial().GetId());
  64. }
  65. }
  66. #endif
  67. void RpcTesterComponent::RunTests()
  68. {
  69. if (GetController())
  70. {
  71. auto controller = static_cast<RpcTesterComponentController*>(GetController());
  72. #if AZ_TRAIT_CLIENT
  73. if (GetTestAutoToAuthorityRPC())
  74. {
  75. controller->RPC_AutonomousToAuthority();
  76. return;
  77. }
  78. #endif
  79. #if AZ_TRAIT_SERVER
  80. if (GetTestServerToAuthorityRPC())
  81. {
  82. RPC_ServerToAuthority();
  83. }
  84. else if (GetTestAuthorityToAutoRPC())
  85. {
  86. controller->RPC_AuthorityToAutonomous();
  87. }
  88. else if (GetTestAuthorityToClientRPC())
  89. {
  90. controller->RPC_TestPassed();
  91. }
  92. #endif
  93. }
  94. }
  95. RpcTesterComponentController::RpcTesterComponentController(RpcTesterComponent& parent)
  96. : RpcTesterComponentControllerBase(parent)
  97. {
  98. }
  99. void RpcTesterComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  100. {
  101. GetParent().m_delayTestRun.Enqueue(AZ::TimeMs{ 2000 }, false);
  102. }
  103. void RpcTesterComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  104. {
  105. }
  106. #if AZ_TRAIT_CLIENT
  107. void RpcTesterComponentController::HandleRPC_AuthorityToAutonomous([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  108. {
  109. ;
  110. }
  111. #endif
  112. #if AZ_TRAIT_SERVER
  113. void RpcTesterComponentController::HandleRPC_AutonomousToAuthority([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  114. {
  115. RPC_TestPassed();
  116. }
  117. void RpcTesterComponentController::HandleRPC_ServerToAuthority([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  118. {
  119. RPC_TestPassed();
  120. }
  121. #endif
  122. }