PlayerArmorComponent.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include <GameplayEffectsNotificationBus.h>
  8. #include <PlayerMatchLifecycleBus.h>
  9. #include <UiPlayerArmorBus.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <Components/NetworkHealthComponent.h>
  12. #include <Source/Components/Multiplayer/PlayerArmorComponent.h>
  13. namespace MultiplayerSample
  14. {
  15. PlayerArmorComponentController::PlayerArmorComponentController(PlayerArmorComponent& parent)
  16. : PlayerArmorComponentControllerBase(parent)
  17. {
  18. }
  19. void PlayerArmorComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  20. {
  21. m_previousArmorAmount = GetNetworkHealthComponentController()->GetParent().GetHealth();
  22. GetNetworkHealthComponentController()->GetParent().HealthAddEvent(m_changedHandler);
  23. if (IsNetEntityRoleAuthority())
  24. {
  25. #if AZ_TRAIT_SERVER
  26. GetNetworkHealthComponentController()->SetHealth(GetStartingArmor());
  27. #endif
  28. }
  29. }
  30. void PlayerArmorComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  31. {
  32. m_changedHandler.Disconnect();
  33. }
  34. void PlayerArmorComponentController::OnAmountChanged(float armor)
  35. {
  36. if (armor > m_previousArmorAmount)
  37. {
  38. GameplayEffectsNotificationBus::Broadcast(&GameplayEffectsNotificationBus::Events::OnPositionalEffect,
  39. SoundEffect::ArmorMend, GetEntity()->GetTransform()->GetWorldTranslation());
  40. }
  41. else if (armor < m_previousArmorAmount)
  42. {
  43. const float halfArmor = GetNetworkHealthComponentController()->GetParent().GetMaxHealth() / 2.f;
  44. if (armor < halfArmor && m_previousArmorAmount > halfArmor)
  45. {
  46. // Armor breaking defined as going below 50% of armor
  47. GameplayEffectsNotificationBus::Broadcast(&GameplayEffectsNotificationBus::Events::OnPositionalEffect,
  48. SoundEffect::ArmorBreaking, GetEntity()->GetTransform()->GetWorldTranslation());
  49. }
  50. GameplayEffectsNotificationBus::Broadcast(&GameplayEffectsNotificationBus::Events::OnPositionalEffect,
  51. SoundEffect::PlayerOuch, GetEntity()->GetTransform()->GetWorldTranslation());
  52. }
  53. m_previousArmorAmount = armor;
  54. UiPlayerArmorNotificationBus::Broadcast(&UiPlayerArmorNotificationBus::Events::OnPlayerArmorChanged, armor, GetStartingArmor());
  55. if (armor <= 0)
  56. {
  57. PlayerMatchLifecycleBus::Broadcast(&PlayerMatchLifecycleNotifications::OnPlayerArmorZero, GetNetEntityId());
  58. }
  59. }
  60. }