EnergyCannonComponent.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <Components/PerfTest/NetworkPrefabSpawnerComponent.h>
  15. namespace MultiplayerSample
  16. {
  17. void EnergyCannonComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  20. if (serializeContext)
  21. {
  22. serializeContext->Class<EnergyCannonComponent, EnergyCannonComponentBase>()
  23. ->Version(1);
  24. }
  25. EnergyCannonComponentBase::Reflect(context);
  26. }
  27. void EnergyCannonComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  28. {
  29. m_effect = GetFiringEffect();
  30. m_effect.Initialize();
  31. }
  32. void EnergyCannonComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  33. {
  34. }
  35. #if AZ_TRAIT_CLIENT
  36. void EnergyCannonComponent::HandleRPC_TriggerBuildup([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  37. {
  38. m_effect.TriggerEffect(GetEntity()->GetTransform()->GetWorldTM());
  39. }
  40. void EnergyCannonComponent::KillBuildupEffect() const
  41. {
  42. m_effect.StopEffect();
  43. }
  44. #endif
  45. EnergyCannonComponentController::EnergyCannonComponentController(EnergyCannonComponent& parent)
  46. : EnergyCannonComponentControllerBase(parent)
  47. {
  48. }
  49. void EnergyCannonComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  50. {
  51. #if AZ_TRAIT_SERVER
  52. if (GetRateOfFireMs() > AZ::TimeMs{ 0 })
  53. {
  54. m_firingEvent.Enqueue(GetRateOfFireMs(), true);
  55. }
  56. #endif
  57. }
  58. void EnergyCannonComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  59. {
  60. #if AZ_TRAIT_SERVER
  61. m_firingEvent.RemoveFromQueue();
  62. #endif
  63. }
  64. #if AZ_TRAIT_SERVER
  65. void EnergyCannonComponentController::OnTriggerBuildup()
  66. {
  67. // 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
  68. RPC_TriggerBuildup();
  69. }
  70. void EnergyCannonComponentController::OnFireEnergyBall()
  71. {
  72. const AZ::Transform& cannonTm = GetEntity()->GetTransform()->GetWorldTM();
  73. const AZ::Vector3 effectOffset = GetFiringEffect().GetEffectOffset();
  74. const AZ::Vector3 ballPosition = cannonTm.TransformPoint(effectOffset);
  75. const AZ::Vector3 forward = cannonTm.TransformVector(GetFireVector());
  76. const Multiplayer::PrefabEntityId prefabEntityId(AZ::Name(GetProjectileSpawnable().m_spawnableAsset.GetHint().c_str()));
  77. const AZ::Transform transform = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateIdentity(), ballPosition);
  78. Multiplayer::INetworkEntityManager::EntityList entityList =
  79. Multiplayer::GetNetworkEntityManager()->CreateEntitiesImmediate(prefabEntityId, Multiplayer::NetEntityRole::Authority, transform);
  80. Multiplayer::NetworkEntityHandle spawnedEntity;
  81. if (!entityList.empty())
  82. {
  83. spawnedEntity = entityList[0];
  84. }
  85. else
  86. {
  87. AZLOG_WARN("Attempt to spawn prefab %s failed. Check that prefab is network enabled.", prefabEntityId.m_prefabName.GetCStr());
  88. }
  89. if (EnergyBallComponent* ballComponent = spawnedEntity.FindComponent<EnergyBallComponent>())
  90. {
  91. ballComponent->RPC_LaunchBall(ballPosition, forward, GetNetEntityId());
  92. m_triggerBuildupEvent.Enqueue(GetRateOfFireMs() - GetBuildUpTimeMs(), false);
  93. }
  94. }
  95. #endif
  96. }