UiMatchPlayerCoinCountsComponent.h 3.3 KB

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