RandomTimedSpawnerComponent.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/Math/Transform.h>
  12. #include <AzCore/Slice/SliceAsset.h>
  13. #include <LmbrCentral/Scripting/RandomTimedSpawnerComponentBus.h>
  14. #include <AzCore/Math/Random.h>
  15. #include <random>
  16. namespace AZ
  17. {
  18. class ReflectContext;
  19. }
  20. namespace LmbrCentral
  21. {
  22. /**
  23. * Configuration for the RandomTimedSpawnerComponent
  24. */
  25. class RandomTimedSpawnerConfiguration
  26. {
  27. public:
  28. AZ_TYPE_INFO(RandomTimedSpawnerConfiguration, "4133644F-FADA-4C82-A2A2-B587B20E81FA");
  29. static void Reflect(AZ::ReflectContext* context);
  30. bool m_enabled = true;
  31. AZ::RandomDistributionType m_randomDistribution = AZ::RandomDistributionType::UniformReal;
  32. double m_spawnDelay = 5.0;
  33. double m_spawnDelayVariation = 0.0;
  34. };
  35. /**
  36. * A component to spawn slices at regular intervals
  37. * at random points inside of a volume.
  38. */
  39. class RandomTimedSpawnerComponent
  40. : public AZ::Component
  41. , public AZ::TickBus::Handler
  42. , public RandomTimedSpawnerComponentRequestBus::Handler
  43. {
  44. public:
  45. AZ_COMPONENT(RandomTimedSpawnerComponent, RandomTimedSpawnerComponentTypeId);
  46. static void Reflect(AZ::ReflectContext* context);
  47. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  48. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  49. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  50. RandomTimedSpawnerComponent() {}
  51. explicit RandomTimedSpawnerComponent(RandomTimedSpawnerConfiguration *params)
  52. {
  53. m_config = *params;
  54. }
  55. ~RandomTimedSpawnerComponent() {};
  56. // AZ::Component
  57. void Activate() override;
  58. void Deactivate() override;
  59. // TickBus
  60. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  61. // RandomTimedSpawnerRequestBus
  62. void Enable() override;
  63. void Disable() override;
  64. void Toggle() override;
  65. bool IsEnabled() override { return m_config.m_enabled; }
  66. void SetRandomDistribution(AZ::RandomDistributionType randomDistribution) override { m_config.m_randomDistribution = randomDistribution; }
  67. AZ::RandomDistributionType GetRandomDistribution() override { return m_config.m_randomDistribution; }
  68. void SetSpawnDelay(double spawnDelay) override { m_config.m_spawnDelay = spawnDelay; }
  69. double GetSpawnDelay() override { return m_config.m_spawnDelay; }
  70. void SetSpawnDelayVariation(double spawnDelayVariation) override { m_config.m_spawnDelayVariation = spawnDelayVariation; }
  71. double GetSpawnDelayVariation() override { return m_config.m_spawnDelayVariation; }
  72. private:
  73. //Reflected members
  74. RandomTimedSpawnerConfiguration m_config;
  75. //Unreflected members
  76. double m_currentTime;
  77. double m_nextSpawnTime;
  78. std::default_random_engine m_randomEngine;
  79. std::uniform_real_distribution<double> m_randomDistribution;
  80. void CalculateNextSpawnTime();
  81. AZ::Vector3 CalculateNextSpawnPosition();
  82. };
  83. } //namespace LmbrCentral