RandomTimedSpawnerComponent.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "RandomTimedSpawnerComponent.h"
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  12. #include <LmbrCentral/Scripting/SpawnerComponentBus.h>
  13. #include <AzCore/Component/TransformBus.h>
  14. #include <AzCore/Time/ITime.h>
  15. namespace LmbrCentral
  16. {
  17. void RandomTimedSpawnerConfiguration::Reflect(AZ::ReflectContext * context)
  18. {
  19. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serializeContext->Class<RandomTimedSpawnerConfiguration>()
  22. ->Version(1)
  23. ->Field("Enabled", &RandomTimedSpawnerConfiguration::m_enabled)
  24. ->Field("RandomDistribution", &RandomTimedSpawnerConfiguration::m_randomDistribution)
  25. ->Field("SpawnDelay", &RandomTimedSpawnerConfiguration::m_spawnDelay)
  26. ->Field("SpawnDelayVariation", &RandomTimedSpawnerConfiguration::m_spawnDelayVariation)
  27. ;
  28. }
  29. }
  30. void RandomTimedSpawnerComponent::Reflect(AZ::ReflectContext * context)
  31. {
  32. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  33. {
  34. serializeContext->Class<RandomTimedSpawnerComponent, AZ::Component>()
  35. ->Version(1)
  36. ->Field("m_config", &RandomTimedSpawnerComponent::m_config)
  37. ;
  38. }
  39. RandomTimedSpawnerConfiguration::Reflect(context);
  40. }
  41. void RandomTimedSpawnerComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  42. {
  43. provided.push_back(AZ_CRC_CE("RandomTimedSpawnerService"));
  44. }
  45. void RandomTimedSpawnerComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  46. {
  47. //Only compatible with Box and Cylinder shapes
  48. incompatible.push_back(AZ_CRC_CE("CapsuleShapeService"));
  49. incompatible.push_back(AZ_CRC_CE("SphereShapeService"));
  50. incompatible.push_back(AZ_CRC_CE("CompoundShapeService"));
  51. }
  52. void RandomTimedSpawnerComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  53. {
  54. required.push_back(AZ_CRC_CE("TransformService"));
  55. required.push_back(AZ_CRC_CE("ShapeService"));
  56. required.push_back(AZ_CRC_CE("SpawnerService"));
  57. }
  58. void RandomTimedSpawnerComponent::Activate()
  59. {
  60. const AZ::TimeUs elapsedTimeUs = AZ::GetElapsedTimeUs();
  61. m_currentTime = AZ::TimeUsToSecondsDouble(elapsedTimeUs);
  62. RandomTimedSpawnerComponentRequestBus::Handler::BusConnect(GetEntityId());
  63. CalculateNextSpawnTime();
  64. if (m_config.m_enabled)
  65. {
  66. AZ::TickBus::Handler::BusConnect();
  67. }
  68. }
  69. void RandomTimedSpawnerComponent::Deactivate()
  70. {
  71. if (m_config.m_enabled)
  72. {
  73. AZ::TickBus::Handler::BusDisconnect();
  74. }
  75. RandomTimedSpawnerComponentRequestBus::Handler::BusDisconnect();
  76. }
  77. void RandomTimedSpawnerComponent::OnTick([[maybe_unused]] float deltaTime, AZ::ScriptTimePoint time)
  78. {
  79. m_currentTime = time.GetSeconds();
  80. if (m_currentTime >= m_nextSpawnTime)
  81. {
  82. AZ::Transform spawnTransform = AZ::Transform::CreateIdentity();
  83. spawnTransform.SetTranslation(CalculateNextSpawnPosition());
  84. LmbrCentral::SpawnerComponentRequestBus::Event(GetEntityId(), &LmbrCentral::SpawnerComponentRequestBus::Events::SpawnAbsolute, spawnTransform);
  85. CalculateNextSpawnTime();
  86. }
  87. }
  88. void RandomTimedSpawnerComponent::Enable()
  89. {
  90. m_config.m_enabled = true;
  91. AZ::TickBus::Handler::BusConnect();
  92. }
  93. void RandomTimedSpawnerComponent::Disable()
  94. {
  95. m_config.m_enabled = false;
  96. AZ::TickBus::Handler::BusDisconnect();
  97. }
  98. void RandomTimedSpawnerComponent::Toggle()
  99. {
  100. m_config.m_enabled = !m_config.m_enabled;
  101. if (m_config.m_enabled)
  102. {
  103. AZ::TickBus::Handler::BusConnect();
  104. }
  105. else
  106. {
  107. AZ::TickBus::Handler::BusDisconnect();
  108. }
  109. }
  110. void RandomTimedSpawnerComponent::CalculateNextSpawnTime()
  111. {
  112. m_randomDistribution = std::uniform_real_distribution<double>(-m_config.m_spawnDelayVariation, m_config.m_spawnDelayVariation);
  113. double variation = m_randomDistribution(m_randomEngine);
  114. double spawnDelay = m_config.m_spawnDelay + variation;
  115. m_nextSpawnTime = m_currentTime + spawnDelay;
  116. }
  117. AZ::Vector3 RandomTimedSpawnerComponent::CalculateNextSpawnPosition()
  118. {
  119. AZ::Vector3 spawnPos = AZ::Vector3::CreateZero();
  120. //This spawnPos is untransformed
  121. LmbrCentral::ShapeComponentRequestsBus::EventResult(spawnPos, GetEntityId(), &LmbrCentral::ShapeComponentRequestsBus::Events::GenerateRandomPointInside, m_config.m_randomDistribution);
  122. return spawnPos;
  123. }
  124. } //namespace LmbrCentral