AudioMultiPositionComponent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/EntityBus.h>
  11. #include <IAudioInterfacesCommonData.h>
  12. #include <LmbrCentral/Audio/AudioMultiPositionComponentBus.h>
  13. namespace UnitTest
  14. {
  15. class AudioMultiPositionComponentTests;
  16. }
  17. namespace LmbrCentral
  18. {
  19. /*!
  20. * Audio Multi-Position Component
  21. * Used to simulate "area" sounds and consume less resources.
  22. * Example: A river sound can be created by placing a bunch of entities along the river
  23. * and adding them to this component. The positions of those entities will be sent to
  24. * audio system and treated as one sound.
  25. * Example: A hallway lined with torches. The torches are individual sources, but they
  26. * can all use the same resources via this component.
  27. *
  28. * Note: This component doesn't yet support full orientation of the entities, only position.
  29. * Note: This component doesn't yet support tracking movement of the entities.
  30. */
  31. class AudioMultiPositionComponent
  32. : public AZ::Component
  33. , public AudioMultiPositionComponentRequestBus::Handler
  34. , private AZ::EntityBus::MultiHandler
  35. {
  36. friend class UnitTest::AudioMultiPositionComponentTests;
  37. public:
  38. //! AZ::Component interface
  39. AZ_COMPONENT(AudioMultiPositionComponent, "{CF3B3C77-746C-4EB0-83C6-FE4AAA4203B0}");
  40. void Activate() override;
  41. void Deactivate() override;
  42. AudioMultiPositionComponent() = default;
  43. AudioMultiPositionComponent(const AZStd::vector<AZ::EntityId>& entities, Audio::MultiPositionBehaviorType type);
  44. //! AudioMultiPositionComponentRequestBus interface
  45. void AddEntity(const AZ::EntityId& entityId) override;
  46. void RemoveEntity(const AZ::EntityId& entityId) override;
  47. void SetBehaviorType(Audio::MultiPositionBehaviorType type) override;
  48. //! AZ::EntityBus interface
  49. void OnEntityActivated(const AZ::EntityId& entityId) override;
  50. void OnEntityDeactivated(const AZ::EntityId& entityId) override;
  51. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  52. {
  53. dependent.push_back(AZ_CRC_CE("AudioTriggerService"));
  54. }
  55. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  56. {
  57. provided.push_back(AZ_CRC_CE("AudioMultiPositionService"));
  58. }
  59. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  60. {
  61. required.push_back(AZ_CRC_CE("AudioTriggerService"));
  62. }
  63. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  64. {
  65. incompatible.push_back(AZ_CRC_CE("AudioMultiPositionService"));
  66. }
  67. static void Reflect(AZ::ReflectContext* context);
  68. private:
  69. AZStd::size_t GetNumEntityRefs() const
  70. {
  71. return m_entityRefs.size();
  72. }
  73. AZStd::size_t GetNumEntityPositions() const
  74. {
  75. return m_entityPositions.size();
  76. }
  77. protected:
  78. //! Send the positions to the audio system.
  79. void SendMultiplePositions();
  80. private:
  81. //! Serialized Data
  82. AZStd::vector<AZ::EntityId> m_entityRefs;
  83. Audio::MultiPositionBehaviorType m_behaviorType;
  84. //! Transient Data
  85. using EntityPosPair = AZStd::pair<AZ::EntityId, AZ::Vector3>;
  86. AZStd::vector<EntityPosPair> m_entityPositions;
  87. };
  88. } // namespace LmbrCentral