HUDComponent.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/Event.h>
  11. #include <Source/Components/NetworkMatchComponent.h>
  12. #include "MultiplayerSampleTypes.h"
  13. namespace MultiplayerSample
  14. {
  15. class HUDComponent
  16. : public AZ::Component
  17. {
  18. public:
  19. AZ_COMPONENT(MultiplayerSample::HUDComponent, "{8061E5D2-A1F7-4B40-9AAC-8FF14BD094FC}");
  20. /*
  21. * Reflects component data into the reflection contexts, including the serialization, edit, and behavior contexts.
  22. */
  23. static void Reflect(AZ::ReflectContext* context);
  24. protected:
  25. void Activate() override;
  26. void Deactivate() override;
  27. private:
  28. // Wait for NetworkMatchComponent to activate so we can begin listening for NetworkMatch events
  29. AZ::ScheduledEvent m_waitForActiveNetworkMatchComponent = AZ::ScheduledEvent([this]
  30. {
  31. if (const auto networkMatchComponent = AZ::Interface<INetworkMatch>::Get())
  32. {
  33. m_waitForActiveNetworkMatchComponent.RemoveFromQueue();
  34. SetRoundNumberText(aznumeric_cast<uint16_t>(networkMatchComponent->GetCurrentRoundNumber()));
  35. m_roundNumberHandler = AZ::EventHandler<uint16_t>([this](uint16_t value) { SetRoundNumberText(value); });
  36. networkMatchComponent->AddRoundNumberEventHandler(m_roundNumberHandler);
  37. m_roundTimerHandler = AZ::EventHandler<RoundTimeSec>([this](RoundTimeSec value) { SetRoundTimerText(value); });
  38. networkMatchComponent->AddRoundTimeRemainingEventHandler(m_roundTimerHandler);
  39. // Listen for an event if the host changes the match start time
  40. // This can happen if an admin changes the start time during a tournament to wait for more players.
  41. UpdateFirstMatchTimerUi();
  42. m_firstMatchStartHostTimeHandler = AZ::EventHandler<AZ::TimeMs>([this]([[maybe_unused]]AZ::TimeMs value) {UpdateFirstMatchTimerUi(); });
  43. networkMatchComponent->AddFirstMatchStartHostTime(m_firstMatchStartHostTimeHandler);
  44. }
  45. }, AZ::Name("HUDComponent Wait For Active NetworkMatchComponent"));
  46. void SetRoundNumberText(uint16_t round);
  47. void SetRoundTimerText(RoundTimeSec time);
  48. void UpdateFirstMatchTimerUi();
  49. AZ::EventHandler<uint16_t> m_roundNumberHandler;
  50. AZ::EventHandler<RoundTimeSec> m_roundTimerHandler;
  51. AZ::EventHandler<AZ::TimeMs> m_firstMatchStartHostTimeHandler;
  52. AZ::ScheduledEvent m_updateFirstMatchTimer = AZ::ScheduledEvent([this]
  53. {
  54. UpdateFirstMatchTimerUi();
  55. }, AZ::Name("HUDComponent Update First Match Timer"));
  56. AZ::EntityId m_roundNumberUi;
  57. AZ::EntityId m_roundTimerUi;
  58. AZStd::string m_roundNumberText;
  59. AZ::EntityId m_roundSecondsRemainingUiParent;
  60. AZ::EntityId m_firstMatchStartingUiParent;
  61. AZ::EntityId m_firstMatchStartingTimerUi;
  62. };
  63. } // namespace MultiplayerSample