UiMatchPlayerCoinCountsComponent.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/EBus/ScheduledEvent.h>
  11. #include <AzCore/std/containers/vector.h>
  12. #include <Components/NetworkMatchComponent.h>
  13. #include <StartingPointInput/InputEventNotificationBus.h>
  14. namespace MultiplayerSample
  15. {
  16. class UiMatchPlayerCoinCountsComponent
  17. : public AZ::Component
  18. , public StartingPointInput::InputEventNotificationBus::MultiHandler
  19. {
  20. public:
  21. static constexpr float SecondsBeforeNewRoundToHideUI = 3.0f;
  22. AZ_COMPONENT(UiMatchPlayerCoinCountsComponent, "{529b9b3b-bea2-4120-9089-c4451438e4c0}");
  23. static void Reflect(AZ::ReflectContext* context);
  24. void Activate() override;
  25. void Deactivate() override;
  26. //! StartingPointInput::InputEventNotificationBus overrides ...
  27. //! @{
  28. void OnPressed(float value) override;
  29. void OnReleased(float value) override;
  30. //! @}
  31. //! Show or hide the player score menu
  32. //! @param enable true will display the menu; false will hide the menu.
  33. void EnableUI(bool enable);
  34. private:
  35. AZ::EntityId m_rootElementId;
  36. AZStd::vector<AZ::EntityId> m_playerRowElement;
  37. static PlayerNameString GetPlayerName(Multiplayer::NetEntityId playerEntity);
  38. void UpdatePlayerScoreUI();
  39. AZ::Event<int32_t, PlayerCoinState>::Handler m_onPlayerScoreChanged{[this](int32_t, PlayerCoinState)
  40. {
  41. UpdatePlayerScoreUI();
  42. } };
  43. // Wait for NetworkMatchComponent to activate so we can begin listening for NetworkMatch events
  44. // For example: when the round resets to 1 we know the new match has started.
  45. AZ::ScheduledEvent m_waitForActiveNetworkMatchComponent = AZ::ScheduledEvent([this]
  46. {
  47. if (const auto networkMatchComponent = AZ::Interface<INetworkMatch>::Get())
  48. {
  49. networkMatchComponent->AddRoundTimeRemainingEventHandler(m_roundTimerHandler);
  50. m_waitForActiveNetworkMatchComponent.RemoveFromQueue();
  51. }
  52. }, AZ::Name("GameOverUI Wait For Active NetworkMatchComponent"));
  53. // Listen for rest time between rounds coming to an end
  54. // Automatically close the player score menu when the round is about to start
  55. AZ::Event<RoundTimeSec>::Handler m_restTimerHandler{ [this](RoundTimeSec secondsRemaining)
  56. {
  57. if (secondsRemaining <= SecondsBeforeNewRoundToHideUI)
  58. {
  59. EnableUI(false);
  60. m_restTimerHandler.Disconnect();
  61. }
  62. } };
  63. // Listen for the round coming to an end
  64. // Automatically open up the player score menu in between rounds
  65. AZ::Event<RoundTimeSec>::Handler m_roundTimerHandler{ [this](RoundTimeSec secondsRemaining)
  66. {
  67. const auto networkMatch = AZ::Interface<INetworkMatch>::Get();
  68. if (secondsRemaining <= 0 &&
  69. networkMatch->GetCurrentRoundNumber() <= networkMatch->GetTotalRoundCount())
  70. {
  71. EnableUI(true);
  72. networkMatch->AddRoundRestTimeRemainingEventHandler(m_restTimerHandler);
  73. }
  74. } };
  75. };
  76. } // namespace MultiplayerSample