UiPlayerArmorComponent.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <UiPlayerArmorBus.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <LyShine/Bus/UiImageBus.h>
  11. #include <LyShine/Bus/UiTextBus.h>
  12. #include <Source/Components/UI/UiPlayerArmorComponent.h>
  13. namespace MultiplayerSample
  14. {
  15. class BehaviorUiPlayerArmorNotificationBusHandler
  16. : public UiPlayerArmorNotificationBus::Handler
  17. , public AZ::BehaviorEBusHandler
  18. {
  19. public:
  20. AZ_EBUS_BEHAVIOR_BINDER(BehaviorUiPlayerArmorNotificationBusHandler, "{6EB46F80-3D88-4DB9-8446-348A2CB4320D}", AZ::SystemAllocator,
  21. OnPlayerArmorChanged);
  22. void OnPlayerArmorChanged(float armorPointsForLocalPlayer, float startingArmor) override
  23. {
  24. Call(FN_OnPlayerArmorChanged, armorPointsForLocalPlayer, startingArmor);
  25. }
  26. };
  27. void UiPlayerArmorComponent::Activate()
  28. {
  29. UiPlayerArmorNotificationBus::Handler::BusConnect();
  30. PlayerIdentityNotificationBus::Handler::BusConnect();
  31. // Attempt to grab the player name now, but it's likely the UI is loaded before a player name is chosen
  32. const char* playerName = nullptr;
  33. PlayerIdentityRequestBus::BroadcastResult(playerName, &PlayerIdentityRequestBus::Events::GetPlayerIdentityName);
  34. if (playerName != nullptr)
  35. {
  36. UiTextBus::Event(m_playerName, &UiTextBus::Events::SetText, playerName);
  37. }
  38. }
  39. void UiPlayerArmorComponent::Deactivate()
  40. {
  41. PlayerIdentityNotificationBus::Handler::BusDisconnect();
  42. UiPlayerArmorNotificationBus::Handler::BusDisconnect();
  43. }
  44. void UiPlayerArmorComponent::OnAutonomousPlayerNameChanged(const char* playerName)
  45. {
  46. UiTextBus::Event(m_playerName, &UiTextBus::Events::SetText, playerName);
  47. }
  48. void UiPlayerArmorComponent::OnPlayerArmorChanged(float armorPointsForLocalPlayer, float startingArmor)
  49. {
  50. const AZStd::string armorTextValue = AZStd::string::format("%.0f", armorPointsForLocalPlayer) + AZStd::string("%");
  51. UiTextBus::Event(m_armorText, &UiTextBus::Events::SetText, armorTextValue.c_str());
  52. if (startingArmor > 1.f)
  53. {
  54. const float fillAmount = AZStd::clamp(armorPointsForLocalPlayer / startingArmor, 0.f, 1.f);
  55. UiImageBus::Event(m_armorVisualEntity, &UiImageBus::Events::SetFillAmount, fillAmount);
  56. }
  57. else
  58. {
  59. UiImageBus::Event(m_armorVisualEntity, &UiImageBus::Events::SetFillAmount, 1.f);
  60. }
  61. }
  62. void UiPlayerArmorComponent::Reflect(AZ::ReflectContext* context)
  63. {
  64. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  65. {
  66. serializeContext->Class<UiPlayerArmorComponent, AZ::Component>()
  67. ->Version(1)
  68. ->Field("Root Element", &UiPlayerArmorComponent::m_rootElement)
  69. ->Field("Player Armor Visual", &UiPlayerArmorComponent::m_armorVisualEntity)
  70. ->Field("Player Armor Text", &UiPlayerArmorComponent::m_armorText)
  71. ->Field("Player Name Text", &UiPlayerArmorComponent::m_playerName)
  72. ;
  73. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  74. {
  75. editContext->Class<UiPlayerArmorComponent>("UiPlayerArmorComponent",
  76. "Shows how much armor the local player has.")
  77. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  78. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("UI"))
  79. ->DataElement(AZ::Edit::UIHandlers::Default, &UiPlayerArmorComponent::m_rootElement,
  80. "Root Element",
  81. "Top level element that contains all other elements for player's armor value")
  82. ->DataElement(nullptr, &UiPlayerArmorComponent::m_armorVisualEntity, "Player Armor Visual", "")
  83. ->DataElement(nullptr, &UiPlayerArmorComponent::m_armorText, "Player Armor Text", "")
  84. ->DataElement(nullptr, &UiPlayerArmorComponent::m_playerName, "Player Name Text", "")
  85. ;
  86. }
  87. }
  88. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  89. if (behaviorContext)
  90. {
  91. behaviorContext->EBus<UiPlayerArmorNotificationBus>("UiPlayerArmorNotificationBus")
  92. ->Handler<BehaviorUiPlayerArmorNotificationBusHandler>();
  93. }
  94. }
  95. } // namespace MultiplayerSample