UiMatchPlayerCoinCountsComponent.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Serialization/EditContext.h>
  9. #include <AzCore/std/sort.h>
  10. #include <Source/Components/Multiplayer/MatchPlayerCoinsComponent.h>
  11. #include <Components/Multiplayer/PlayerIdentityComponent.h>
  12. #include <LyShine/Bus/UiElementBus.h>
  13. #include <LyShine/Bus/UiTextBus.h>
  14. #include <Multiplayer/Components/NetBindComponent.h>
  15. #include <Source/Components/UI/UiMatchPlayerCoinCountsComponent.h>
  16. namespace MultiplayerSample
  17. {
  18. #if AZ_TRAIT_CLIENT
  19. const StartingPointInput::InputEventNotificationId ShowPlayerCoinCountsEventId("show_player_coin_counts");
  20. #endif
  21. void UiMatchPlayerCoinCountsComponent::Activate()
  22. {
  23. m_waitForActiveNetworkMatchComponent.Enqueue(AZ::TimeMs{ 1000 }, true);
  24. StartingPointInput::InputEventNotificationBus::MultiHandler::BusConnect(ShowPlayerCoinCountsEventId);
  25. }
  26. void UiMatchPlayerCoinCountsComponent::Deactivate()
  27. {
  28. StartingPointInput::InputEventNotificationBus::MultiHandler::BusDisconnect();
  29. m_waitForActiveNetworkMatchComponent.RemoveFromQueue();
  30. m_roundTimerHandler.Disconnect();
  31. }
  32. void UiMatchPlayerCoinCountsComponent::UpdatePlayerScoreUI()
  33. {
  34. // Display player scores sorted by coin count (highest score on top)
  35. AZStd::vector<PlayerCoinState> coins = AZ::Interface<MatchPlayerCoinsComponent>::Get()->GetPlayerCoinCounts();
  36. AZStd::sort(coins.begin(), coins.end(), [](const PlayerCoinState& a, const PlayerCoinState& b) {return a.m_coins > b.m_coins; });
  37. AZStd::size_t elementIndex = 0;
  38. for (const PlayerCoinState& state : coins)
  39. {
  40. if (elementIndex >= m_playerRowElement.size())
  41. {
  42. AZ_Error("UiMatchPlayerCoinCounts", false, "Failed to update score screen. Please update UICanvas so there are enough player rows.")
  43. break;
  44. }
  45. AZStd::vector<AZ::EntityId> children;
  46. UiElementBus::EventResult(children, m_playerRowElement[elementIndex], &UiElementBus::Events::GetChildEntityIds);
  47. if (children.size() < 3)
  48. {
  49. AZ_Error("UiMatchPlayerCoinCounts", false, "Failed to update score screen. Please update UICanvas so the player row has at least 3 child elements for setting the player name, coin count, and player highlight.")
  50. break;
  51. }
  52. if (state.m_playerId == Multiplayer::InvalidNetEntityId)
  53. {
  54. continue;
  55. }
  56. const PlayerNameString name = GetPlayerName(state.m_playerId);
  57. UiTextBus::Event(children[0], &UiTextBus::Events::SetText, name.c_str());
  58. UiTextBus::Event(children[1], &UiTextBus::Events::SetText, AZStd::string::format("%d", state.m_coins));
  59. // Highlight the row belonging to this client's autonomous player
  60. bool isAutonomousPlayer = false;
  61. const Multiplayer::ConstNetworkEntityHandle playerHandle = Multiplayer::GetNetworkEntityManager()->GetEntity(state.m_playerId);
  62. if (playerHandle.Exists() && playerHandle.GetNetBindComponent() != nullptr)
  63. {
  64. isAutonomousPlayer = playerHandle.GetNetBindComponent()->IsNetEntityRoleAutonomous();
  65. }
  66. UiElementBus::Event(children[2], &UiElementBus::Events::SetIsEnabled, isAutonomousPlayer);
  67. elementIndex++;
  68. }
  69. // Clear out the unused fields.
  70. for (; elementIndex < m_playerRowElement.size(); ++elementIndex)
  71. {
  72. AZStd::vector<AZ::EntityId> children;
  73. UiElementBus::EventResult(children, m_playerRowElement[elementIndex], &UiElementBus::Events::GetChildEntityIds);
  74. if (children.size() >= 3)
  75. {
  76. UiTextBus::Event(children[0], &UiTextBus::Events::SetText, ""); // name
  77. UiTextBus::Event(children[1], &UiTextBus::Events::SetText, ""); // score
  78. UiElementBus::Event(children[2], &UiElementBus::Events::SetIsEnabled, false); // autonomous player highlight
  79. }
  80. }
  81. }
  82. void UiMatchPlayerCoinCountsComponent::EnableUI(bool enable)
  83. {
  84. UiElementBus::Event(m_rootElementId, &UiElementBus::Events::SetIsEnabled, enable);
  85. if (enable)
  86. {
  87. if (!m_onPlayerScoreChanged.IsConnected())
  88. {
  89. AZ::Interface<MatchPlayerCoinsComponent>::Get()->CoinsPerPlayerAddEvent(m_onPlayerScoreChanged);
  90. }
  91. UpdatePlayerScoreUI();
  92. }
  93. else
  94. {
  95. m_onPlayerScoreChanged.Disconnect();
  96. }
  97. }
  98. void UiMatchPlayerCoinCountsComponent::OnPressed([[maybe_unused]] float value)
  99. {
  100. const StartingPointInput::InputEventNotificationId* inputId = StartingPointInput::InputEventNotificationBus::GetCurrentBusId();
  101. if (inputId == nullptr)
  102. {
  103. return;
  104. }
  105. if (*inputId == ShowPlayerCoinCountsEventId)
  106. {
  107. EnableUI(true);
  108. }
  109. }
  110. void UiMatchPlayerCoinCountsComponent::OnReleased([[maybe_unused]] float value)
  111. {
  112. const StartingPointInput::InputEventNotificationId* inputId = StartingPointInput::InputEventNotificationBus::GetCurrentBusId();
  113. if (inputId == nullptr)
  114. {
  115. return;
  116. }
  117. if (*inputId == ShowPlayerCoinCountsEventId)
  118. {
  119. EnableUI(false);
  120. }
  121. }
  122. PlayerNameString UiMatchPlayerCoinCountsComponent::GetPlayerName(Multiplayer::NetEntityId playerEntity)
  123. {
  124. const auto playerHandle = Multiplayer::GetNetworkEntityManager()->GetEntity(playerEntity);
  125. if (playerHandle.Exists())
  126. {
  127. if (const PlayerIdentityComponent* identity = playerHandle.GetEntity()->FindComponent<PlayerIdentityComponent>())
  128. {
  129. PlayerNameString playerName = identity->GetPlayerName();
  130. if (playerName.empty())
  131. {
  132. return "<player_identity_empty>";
  133. }
  134. return playerName;
  135. }
  136. }
  137. return "<player_handle_does_not_exist>";
  138. }
  139. void UiMatchPlayerCoinCountsComponent::Reflect(AZ::ReflectContext* context)
  140. {
  141. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  142. {
  143. serializeContext->Class<UiMatchPlayerCoinCountsComponent, AZ::Component>()
  144. ->Version(1)
  145. ->Field("Root Element", &UiMatchPlayerCoinCountsComponent::m_rootElementId)
  146. ->Field("Stat Rows for Players", &UiMatchPlayerCoinCountsComponent::m_playerRowElement)
  147. ;
  148. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  149. {
  150. editContext->Class<UiMatchPlayerCoinCountsComponent>("UiMatchPlayerCoinCountsComponent",
  151. "Shows list of coins collected by each player in the match.")
  152. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  153. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("CanvasUI"))
  154. ->DataElement(AZ::Edit::UIHandlers::Default, &UiMatchPlayerCoinCountsComponent::m_rootElementId,
  155. "Root Element",
  156. "Top level element that contains all other elements for showing coins collected by players")
  157. ->DataElement(AZ::Edit::UIHandlers::Default, &UiMatchPlayerCoinCountsComponent::m_playerRowElement,
  158. "Stat Rows for Players",
  159. "List of rows that contains 2 text elements, one for player id and one for coin count")
  160. ;
  161. }
  162. }
  163. }
  164. } // namespace MultiplayerSample