UiGameOverComponent.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #pragma once
  8. #include <AzCore/Component/Component.h>
  9. #include <Source/Components/NetworkMatchComponent.h>
  10. #include <UiGameOverBus.h>
  11. namespace MultiplayerSample
  12. {
  13. class UiGameOverComponent
  14. : public AZ::Component
  15. , public UiGameOverBus::Handler
  16. {
  17. public:
  18. AZ_COMPONENT(UiGameOverComponent, "{37a2de13-a8fa-4ee1-8652-e17253137f62}");
  19. static void Reflect(AZ::ReflectContext* context);
  20. void Activate() override;
  21. void Deactivate() override;
  22. //! UiGameOverBus overrides
  23. //! @{
  24. void SetGameOverScreenEnabled(bool enabled) override;
  25. void DisplayResults(MatchResultsSummary results) override;
  26. //! }@
  27. private:
  28. // Every second the GameOver menu is open, display the time remaining until the new match begins
  29. // There aren't any network events around match restart time;
  30. // Real time is controlled on the server within the match state machine, but not shared across network.
  31. // See GameStatePreparingMatch.cpp and GameStateMatchEnded.cpp.
  32. void DisplaySecondsRemainingUI();
  33. AZ::ScheduledEvent m_onSecondsRemainingChanged = AZ::ScheduledEvent( [this]()
  34. {
  35. DisplaySecondsRemainingUI();
  36. }, AZ::Name("GameOverUI Seconds Remaining"));
  37. // Listen for the NetworkMatch Round Number to Change
  38. // Round 1 is the 1st round in a match; turn off this game-over screen.
  39. AZ::Event<uint16_t>::Handler m_onRoundNumberChangedHandler{ [this](uint16_t roundNumber)
  40. {
  41. if (roundNumber == 1)
  42. {
  43. SetGameOverScreenEnabled(false);
  44. }
  45. } };
  46. // Wait for NetworkMatchComponent to activate so we can begin listening for NetworkMatch events
  47. // For example: when the round resets to 1 we know the new match has started.
  48. AZ::ScheduledEvent m_waitForActiveNetworkMatchComponent = AZ::ScheduledEvent([this]
  49. {
  50. if (const auto networkMatchComponent = AZ::Interface<INetworkMatch>::Get())
  51. {
  52. networkMatchComponent->AddRoundNumberEventHandler(m_onRoundNumberChangedHandler);
  53. m_waitForActiveNetworkMatchComponent.RemoveFromQueue();
  54. }
  55. }, AZ::Name("GameOverUI Wait For Active NetworkMatchComponent"));
  56. AZ::EntityId m_gameOverRootElement;
  57. AZ::EntityId m_rankNumbersUIContainer;
  58. AZStd::vector<AZ::EntityId> m_topRankPlayersUIElements;
  59. AZ::EntityId m_timeRemainingUntilNewMatchUIContainer;
  60. };
  61. }