EnergyCannonComponent.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <MultiplayerSampleTypes.h>
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Settings/SettingsRegistry.h>
  12. #include <Source/Components/Multiplayer/EnergyBallComponent.h>
  13. #include <Source/Components/Multiplayer/EnergyCannonComponent.h>
  14. namespace MultiplayerSample
  15. {
  16. void EnergyCannonComponent::Reflect(AZ::ReflectContext* context)
  17. {
  18. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  19. if (serializeContext)
  20. {
  21. serializeContext->Class<EnergyCannonComponent, EnergyCannonComponentBase>()
  22. ->Version(1);
  23. }
  24. EnergyCannonComponentBase::Reflect(context);
  25. }
  26. void EnergyCannonComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  27. {
  28. m_effect = GetFiringEffect();
  29. m_effect.Initialize();
  30. }
  31. void EnergyCannonComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  32. {
  33. }
  34. #if AZ_TRAIT_CLIENT
  35. void EnergyCannonComponent::HandleRPC_BallLaunched([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  36. {
  37. m_effect.TriggerEffect(GetEntity()->GetTransform()->GetWorldTM());
  38. }
  39. #endif
  40. EnergyCannonComponentController::EnergyCannonComponentController(EnergyCannonComponent& parent)
  41. : EnergyCannonComponentControllerBase(parent)
  42. {
  43. }
  44. void EnergyCannonComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  45. {
  46. #if AZ_TRAIT_SERVER
  47. if (GetRateOfFireMs() > AZ::TimeMs{ 0 })
  48. {
  49. m_firingEvent.Enqueue(GetRateOfFireMs(), true);
  50. }
  51. #endif
  52. }
  53. void EnergyCannonComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  54. {
  55. #if AZ_TRAIT_SERVER
  56. m_firingEvent.RemoveFromQueue();
  57. #endif
  58. }
  59. #if AZ_TRAIT_SERVER
  60. void EnergyCannonComponentController::OnFireEnergyBall()
  61. {
  62. // Re-using the same ball entity.
  63. AZ::Entity* ball = nullptr;
  64. AZ::ComponentApplicationBus::BroadcastResult(ball, &AZ::ComponentApplicationBus::Events::FindEntity, GetEnergyBallEntity());
  65. if (ball)
  66. {
  67. if (EnergyBallComponent* ballComponent = ball->FindComponent<EnergyBallComponent>())
  68. {
  69. const AZ::Transform& cannonTm = GetEntity()->GetTransform()->GetWorldTM();
  70. const AZ::Vector3 forward = cannonTm.TransformVector(AZ::Vector3::CreateAxisY(-1.f));
  71. const AZ::Vector3 effectOffset = GetFiringEffect().GetEffectOffset();
  72. ballComponent->RPC_LaunchBall(cannonTm.GetTranslation() + effectOffset, forward, GetNetEntityId());
  73. RPC_BallLaunched();
  74. // Enqueue our ball kill event
  75. m_killEvent.Enqueue(GetBallLifetimeMs(), false);
  76. }
  77. }
  78. }
  79. void EnergyCannonComponentController::OnKillEnergyBall()
  80. {
  81. // Re-using the same ball entity.
  82. AZ::Entity* ball = nullptr;
  83. AZ::ComponentApplicationBus::BroadcastResult(ball, &AZ::ComponentApplicationBus::Events::FindEntity, GetEnergyBallEntity());
  84. if (ball)
  85. {
  86. if (EnergyBallComponent* ballComponent = ball->FindComponent<EnergyBallComponent>())
  87. {
  88. ballComponent->RPC_KillBall();
  89. }
  90. }
  91. }
  92. #endif
  93. }