EnergyCannonComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #if AZ_TRAIT_CLIENT
  34. m_effect = {};
  35. #endif
  36. }
  37. #if AZ_TRAIT_CLIENT
  38. void EnergyCannonComponent::HandleRPC_TriggerBuildup([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  39. {
  40. m_effect.TriggerEffect(GetEntity()->GetTransform()->GetWorldTM());
  41. }
  42. void EnergyCannonComponent::HandleRPC_StopBuildup([[maybe_unused]] AzNetworking::IConnection* invokingConnection)
  43. {
  44. m_effect.StopEffect();
  45. }
  46. #endif
  47. EnergyCannonComponentController::EnergyCannonComponentController(EnergyCannonComponent& parent)
  48. : EnergyCannonComponentControllerBase(parent)
  49. {
  50. }
  51. void EnergyCannonComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  52. {
  53. #if AZ_TRAIT_SERVER
  54. if (GetRateOfFireMs() > AZ::TimeMs{ 0 })
  55. {
  56. m_firingEvent.Enqueue(GetRateOfFireMs(), true);
  57. }
  58. #endif
  59. }
  60. void EnergyCannonComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  61. {
  62. #if AZ_TRAIT_SERVER
  63. m_triggerBuildupEvent.RemoveFromQueue();
  64. m_firingEvent.RemoveFromQueue();
  65. #endif
  66. }
  67. #if AZ_TRAIT_SERVER
  68. void EnergyCannonComponentController::OnTriggerBuildup()
  69. {
  70. // 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
  71. RPC_TriggerBuildup();
  72. }
  73. void EnergyCannonComponentController::OnFireEnergyBall()
  74. {
  75. RPC_StopBuildup();
  76. const AZ::Transform& cannonTm = GetEntity()->GetTransform()->GetWorldTM();
  77. const AZ::Vector3 effectOffset = GetFiringEffect().GetEffectOffset();
  78. const AZ::Vector3 ballPosition = cannonTm.TransformPoint(effectOffset);
  79. const AZ::Vector3 forward = cannonTm.TransformVector(GetFireVector());
  80. const Multiplayer::PrefabEntityId prefabEntityId(AZ::Name(GetProjectileSpawnable().m_spawnableAsset.GetHint().c_str()));
  81. const AZ::Transform transform = AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateIdentity(), ballPosition);
  82. Multiplayer::INetworkEntityManager::EntityList entityList =
  83. Multiplayer::GetNetworkEntityManager()->CreateEntitiesImmediate(prefabEntityId, Multiplayer::NetEntityRole::Authority, transform);
  84. Multiplayer::NetworkEntityHandle spawnedEntity;
  85. if (entityList.size() == 1)
  86. {
  87. spawnedEntity = entityList[0];
  88. }
  89. else
  90. {
  91. AZLOG_WARN("Attempt to spawn prefab %s failed. Check that prefab is network enabled and only contains a single entity. "
  92. "If multiple entities are in the prefab, only the first one will get deleted. Spawn count: %zu",
  93. prefabEntityId.m_prefabName.GetCStr(), entityList.size());
  94. }
  95. if (EnergyBallComponent* ballComponent = spawnedEntity.FindComponent<EnergyBallComponent>())
  96. {
  97. ballComponent->RPC_LaunchBall(ballPosition, forward, GetNetEntityId());
  98. m_triggerBuildupEvent.Enqueue(GetRateOfFireMs() - GetBuildUpTimeMs(), false);
  99. }
  100. }
  101. #endif
  102. }