PlayerIdentityComponent.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. AZ::TickBus::Handler::BusDisconnect();
  59. #endif
  60. }
  61. #if AZ_TRAIT_CLIENT
  62. void PlayerIdentityComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  63. {
  64. // Don't render others players' on-screen debug text if the player is behind the camera
  65. const AZ::Vector3 renderWorldSpace = GetEntity()->GetTransform()->GetWorldTranslation();
  66. if (!IsNetEntityRoleAutonomous())
  67. {
  68. AZ::Vector3 cameraForward = m_viewport->GetCameraTransform().GetBasisY();
  69. AZ::Vector3 cameraToPlayer = renderWorldSpace - m_viewport->GetCameraTransform().GetTranslation();
  70. if (cameraForward.Dot(cameraToPlayer) < 0.0f)
  71. {
  72. return;
  73. }
  74. }
  75. const AzFramework::WindowSize windowSize = m_viewport->GetViewportSize();
  76. AzFramework::ScreenPoint renderScreenpoint = AzFramework::WorldToScreen(
  77. renderWorldSpace, m_viewport->GetCameraViewMatrixAsMatrix3x4(), m_viewport->GetCameraProjectionMatrix(), AzFramework::ScreenSize(windowSize.m_width, windowSize.m_height));
  78. m_drawParams.m_hAlign = AzFramework::TextHorizontalAlignment::Center;
  79. m_drawParams.m_position = AZ::Vector3(aznumeric_cast<float>(renderScreenpoint.m_x), aznumeric_cast<float>(renderScreenpoint.m_y), 0.f);
  80. m_fontDrawInterface->DrawScreenAlignedText2d(m_drawParams, GetPlayerName().c_str());
  81. }
  82. #endif
  83. PlayerIdentityComponentController::PlayerIdentityComponentController(PlayerIdentityComponent& parent)
  84. : PlayerIdentityComponentControllerBase(parent)
  85. {
  86. }
  87. void PlayerIdentityComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  88. {
  89. if (IsNetEntityRoleAuthority())
  90. {
  91. PlayerIdentityNotificationBus::Broadcast(&PlayerIdentityNotificationBus::Events::OnPlayerActivated, GetNetEntityId());
  92. }
  93. if (IsNetEntityRoleAutonomous())
  94. {
  95. PlayerNameAddEvent(m_onAutomonousPlayerNameChanged);
  96. PlayerIdentityRequestBus::Handler::BusConnect();
  97. }
  98. }
  99. void PlayerIdentityComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  100. {
  101. if (IsNetEntityRoleAuthority())
  102. {
  103. PlayerIdentityNotificationBus::Broadcast(&PlayerIdentityNotificationBus::Events::OnPlayerDeactivated, GetNetEntityId());
  104. }
  105. PlayerIdentityRequestBus::Handler::BusDisconnect();
  106. m_onAutomonousPlayerNameChanged.Disconnect();
  107. }
  108. #if AZ_TRAIT_SERVER
  109. void PlayerIdentityComponentController::HandleRPC_AssignPlayerName([[maybe_unused]] AzNetworking::IConnection* invokingConnection,
  110. const PlayerNameString& newPlayerName)
  111. {
  112. SetPlayerName(newPlayerName);
  113. }
  114. void PlayerIdentityComponentController::HandleRPC_ResetPlayerState([[maybe_unused]] AzNetworking::IConnection* invokingConnection, const PlayerResetOptions& resetOptions)
  115. {
  116. if (resetOptions.m_resetArmor)
  117. {
  118. GetNetworkHealthComponentController()->SetHealth(GetNetworkHealthComponentController()->GetMaxHealth());
  119. }
  120. auto currentCoins = GetPlayerCoinCollectorComponentController()->GetCoinsCollected();
  121. float coinsToDeduct = currentCoins * (resetOptions.m_coinPenalty * 0.01f);
  122. GetPlayerCoinCollectorComponentController()->SetCoinsCollected(currentCoins - aznumeric_cast<uint16_t>(coinsToDeduct));
  123. PlayerIdentityComponentControllerBase::HandleRPC_ResetPlayerState(invokingConnection, resetOptions);
  124. }
  125. #endif
  126. const char* PlayerIdentityComponentController::GetPlayerIdentityName()
  127. {
  128. return GetParent().GetPlayerName().c_str();
  129. }
  130. }