PlayerIdentityComponent.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <PlayerIdentityBus.h>
  8. #include <Source/Components/NetworkHealthComponent.h>
  9. #include <Source/Components/Multiplayer/PlayerCoinCollectorComponent.h>
  10. #include <Source/Components/Multiplayer/PlayerIdentityComponent.h>
  11. #if AZ_TRAIT_CLIENT
  12. #include <Atom/RPI.Public/ViewportContextBus.h>
  13. #include <AzFramework/Viewport/ViewportScreen.h>
  14. #include <AzCore/Component/TransformBus.h>
  15. #endif
  16. namespace MultiplayerSample
  17. {
  18. void PlayerIdentityComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  21. if (serializeContext)
  22. {
  23. serializeContext->Class<PlayerIdentityComponent, PlayerIdentityComponentBase>()
  24. ->Version(1);
  25. }
  26. PlayerIdentityComponentBase::Reflect(context);
  27. }
  28. void PlayerIdentityComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  29. {
  30. #if AZ_TRAIT_CLIENT
  31. m_viewport = AZ::RPI::ViewportContextRequests::Get()->GetDefaultViewportContext();
  32. if (!m_viewport)
  33. {
  34. AZ_Assert(false, "NetworkDebugPlayerIdComponent failed to find the a rendering viewport. Debug rendering will be disabled.");
  35. return;
  36. }
  37. const auto fontQueryInterface = AZ::Interface<AzFramework::FontQueryInterface>::Get();
  38. if (!fontQueryInterface)
  39. {
  40. AZ_Assert(false, "NetworkDebugPlayerIdComponent failed to find the FontQueryInterface. Debug rendering will be disabled.");
  41. return;
  42. }
  43. m_fontDrawInterface = fontQueryInterface->GetDefaultFontDrawInterface();
  44. if (!m_fontDrawInterface)
  45. {
  46. AZ_Assert(false, "NetworkDebugPlayerIdComponent failed to find the FontDrawInterface. Debug rendering will be disabled.");
  47. return;
  48. }
  49. m_drawParams.m_drawViewportId = m_viewport->GetId();
  50. m_drawParams.m_scale = AZ::Vector2(FontScale);
  51. m_drawParams.m_color = AZ::Colors::Wheat;
  52. AZ::TickBus::Handler::BusConnect();
  53. #endif
  54. }
  55. void PlayerIdentityComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  56. {
  57. #if AZ_TRAIT_CLIENT
  58. #endif
  59. }
  60. #if AZ_TRAIT_CLIENT
  61. void PlayerIdentityComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  62. {
  63. // Don't render others players' on-screen debug text if the player is behind the camera
  64. const AZ::Vector3 renderWorldSpace = GetEntity()->GetTransform()->GetWorldTranslation();
  65. if (!IsNetEntityRoleAutonomous())
  66. {
  67. AZ::Vector3 cameraForward = m_viewport->GetCameraTransform().GetBasisY();
  68. AZ::Vector3 cameraToPlayer = renderWorldSpace - m_viewport->GetCameraTransform().GetTranslation();
  69. if (cameraForward.Dot(cameraToPlayer) < 0.0f)
  70. {
  71. return;
  72. }
  73. }
  74. const AzFramework::WindowSize windowSize = m_viewport->GetViewportSize();
  75. AzFramework::ScreenPoint renderScreenpoint = AzFramework::WorldToScreen(
  76. renderWorldSpace, m_viewport->GetCameraViewMatrixAsMatrix3x4(), m_viewport->GetCameraProjectionMatrix(), AzFramework::ScreenSize(windowSize.m_width, windowSize.m_height));
  77. m_drawParams.m_hAlign = AzFramework::TextHorizontalAlignment::Center;
  78. m_drawParams.m_position = AZ::Vector3(aznumeric_cast<float>(renderScreenpoint.m_x), aznumeric_cast<float>(renderScreenpoint.m_y), 0.f);
  79. m_fontDrawInterface->DrawScreenAlignedText2d(m_drawParams, GetPlayerName().c_str());
  80. }
  81. #endif
  82. PlayerIdentityComponentController::PlayerIdentityComponentController(PlayerIdentityComponent& parent)
  83. : PlayerIdentityComponentControllerBase(parent)
  84. {
  85. }
  86. void PlayerIdentityComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  87. {
  88. if (IsNetEntityRoleAuthority())
  89. {
  90. PlayerIdentityNotificationBus::Broadcast(&PlayerIdentityNotificationBus::Events::OnPlayerActivated, GetNetEntityId());
  91. }
  92. if (IsNetEntityRoleAutonomous())
  93. {
  94. PlayerNameAddEvent(m_onAutomonousPlayerNameChanged);
  95. PlayerIdentityRequestBus::Handler::BusConnect();
  96. }
  97. }
  98. void PlayerIdentityComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  99. {
  100. if (IsNetEntityRoleAuthority())
  101. {
  102. PlayerIdentityNotificationBus::Broadcast(&PlayerIdentityNotificationBus::Events::OnPlayerDeactivated, GetNetEntityId());
  103. }
  104. PlayerIdentityRequestBus::Handler::BusDisconnect();
  105. m_onAutomonousPlayerNameChanged.Disconnect();
  106. }
  107. #if AZ_TRAIT_SERVER
  108. void PlayerIdentityComponentController::HandleRPC_AssignPlayerName([[maybe_unused]] AzNetworking::IConnection* invokingConnection,
  109. const PlayerNameString& newPlayerName)
  110. {
  111. SetPlayerName(newPlayerName);
  112. }
  113. void PlayerIdentityComponentController::HandleRPC_ResetPlayerState([[maybe_unused]] AzNetworking::IConnection* invokingConnection, const PlayerResetOptions& resetOptions)
  114. {
  115. if (resetOptions.m_resetArmor)
  116. {
  117. GetNetworkHealthComponentController()->SetHealth(GetNetworkHealthComponentController()->GetMaxHealth());
  118. }
  119. auto currentCoins = GetPlayerCoinCollectorComponentController()->GetCoinsCollected();
  120. float coinsToDeduct = currentCoins * (resetOptions.m_coinPenalty * 0.01f);
  121. GetPlayerCoinCollectorComponentController()->SetCoinsCollected(currentCoins - aznumeric_cast<uint16_t>(coinsToDeduct));
  122. PlayerIdentityComponentControllerBase::HandleRPC_ResetPlayerState(invokingConnection, resetOptions);
  123. }
  124. #endif
  125. const char* PlayerIdentityComponentController::GetPlayerIdentityName()
  126. {
  127. return GetParent().GetPlayerName().c_str();
  128. }
  129. }