EnergyCannonComponent.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_TriggerBuildup([[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::OnTriggerBuildup()
  61. {
  62. // This RPC starts the buildup effect on the client, we want it to start before the actual ball launch event occurs to make everyhing line up nicely
  63. RPC_TriggerBuildup();
  64. }
  65. void EnergyCannonComponentController::OnFireEnergyBall()
  66. {
  67. // Re-using the same ball entity.
  68. AZ::Entity* ball = nullptr;
  69. AZ::ComponentApplicationBus::BroadcastResult(ball, &AZ::ComponentApplicationBus::Events::FindEntity, GetEnergyBallEntity());
  70. if (ball)
  71. {
  72. if (EnergyBallComponent* ballComponent = ball->FindComponent<EnergyBallComponent>())
  73. {
  74. const AZ::Transform& cannonTm = GetEntity()->GetTransform()->GetWorldTM();
  75. const AZ::Vector3 forward = cannonTm.TransformVector(AZ::Vector3::CreateAxisY(-1.f));
  76. const AZ::Vector3 effectOffset = GetFiringEffect().GetEffectOffset();
  77. ballComponent->RPC_LaunchBall(cannonTm.GetTranslation() + cannonTm.TransformVector(effectOffset), forward, GetNetEntityId());
  78. // Enqueue our ball kill event
  79. m_killEvent.Enqueue(GetBallLifetimeMs(), false);
  80. m_triggerBuildupEvent.Enqueue(GetRateOfFireMs() - GetBuildUpTimeMs(), false);
  81. }
  82. }
  83. }
  84. void EnergyCannonComponentController::OnKillEnergyBall()
  85. {
  86. // Re-using the same ball entity.
  87. AZ::Entity* ball = nullptr;
  88. AZ::ComponentApplicationBus::BroadcastResult(ball, &AZ::ComponentApplicationBus::Events::FindEntity, GetEnergyBallEntity());
  89. if (ball)
  90. {
  91. if (EnergyBallComponent* ballComponent = ball->FindComponent<EnergyBallComponent>())
  92. {
  93. ballComponent->RPC_KillBall();
  94. }
  95. }
  96. }
  97. #endif
  98. }