AudioTriggerComponent.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <Audio/AudioTriggerComponent.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Debug/Trace.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <ISystem.h>
  14. #include <LmbrCentral/Audio/AudioProxyComponentBus.h>
  15. namespace LmbrCentral
  16. {
  17. //=========================================================================
  18. // Behavior Context AudioTriggerComponentNotificationBus forwarder
  19. class BehaviorAudioTriggerNotificationBusHandler
  20. : public Audio::AudioTriggerNotificationBus::Handler
  21. , public AZ::BehaviorEBusHandler
  22. {
  23. public:
  24. AZ_EBUS_BEHAVIOR_BINDER(
  25. BehaviorAudioTriggerNotificationBusHandler, "{ACCB0C42-3752-496B-9B1F-19276925EBB0}", AZ::SystemAllocator,
  26. ReportTriggerStarted, ReportTriggerFinished);
  27. void ReportTriggerStarted(Audio::TAudioControlID triggerId) override
  28. {
  29. Call(FN_ReportTriggerStarted, triggerId);
  30. }
  31. void ReportTriggerFinished(Audio::TAudioControlID triggerId) override
  32. {
  33. Call(FN_ReportTriggerFinished, triggerId);
  34. }
  35. };
  36. //=========================================================================
  37. void AudioTriggerComponent::Reflect(AZ::ReflectContext* context)
  38. {
  39. auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  40. if (serializeContext)
  41. {
  42. serializeContext->Class<AudioTriggerComponent, AZ::Component>()
  43. ->Version(2)
  44. ->Field("Play Trigger", &AudioTriggerComponent::m_defaultPlayTriggerName)
  45. ->Field("Stop Trigger", &AudioTriggerComponent::m_defaultStopTriggerName)
  46. ->Field("Obstruction Type", &AudioTriggerComponent::m_obstructionType)
  47. ->Field("Plays Immediately", &AudioTriggerComponent::m_playsImmediately)
  48. ;
  49. serializeContext->Class<Audio::TriggerNotificationIdType>()
  50. ->Version(1)
  51. ->Field("Owner", &Audio::TriggerNotificationIdType::m_owner)
  52. ;
  53. }
  54. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  55. {
  56. behaviorContext->Enum<static_cast<AZ::u32>(Audio::ObstructionType::Ignore)>("AudioObstructionType_Ignore")
  57. ->Enum<static_cast<AZ::u32>(Audio::ObstructionType::SingleRay)>("AudioObstructionType_SingleRay")
  58. ->Enum<static_cast<AZ::u32>(Audio::ObstructionType::MultiRay)>("AudioObstructionType_MultiRay")
  59. ;
  60. behaviorContext->EBus<AudioTriggerComponentRequestBus>("AudioTriggerComponentRequestBus")
  61. ->Event("Play", &AudioTriggerComponentRequestBus::Events::Play)
  62. ->Event("Stop", &AudioTriggerComponentRequestBus::Events::Stop)
  63. ->Event("ExecuteTrigger", &AudioTriggerComponentRequestBus::Events::ExecuteTrigger)
  64. ->Event("KillTrigger", &AudioTriggerComponentRequestBus::Events::KillTrigger)
  65. ->Event("KillAllTriggers", &AudioTriggerComponentRequestBus::Events::KillAllTriggers)
  66. ->Event("SetMovesWithEntity", &AudioTriggerComponentRequestBus::Events::SetMovesWithEntity)
  67. ->Event("SetObstructionType", &AudioTriggerComponentRequestBus::Events::SetObstructionType)
  68. ;
  69. behaviorContext->Class<Audio::TriggerNotificationIdType>("AudioTriggerNotificationIdType")
  70. ->Constructor<AZ::EntityId>()
  71. ->Attribute(AZ::Script::Attributes::DefaultConstructorOverrideIndex, 0)
  72. ;
  73. behaviorContext->EBus<Audio::AudioTriggerNotificationBus>("AudioTriggerNotificationBus")
  74. ->Handler<BehaviorAudioTriggerNotificationBusHandler>()
  75. ;
  76. }
  77. }
  78. //=========================================================================
  79. void AudioTriggerComponent::Activate()
  80. {
  81. OnPlayTriggerChanged();
  82. OnStopTriggerChanged();
  83. OnObstructionTypeChanged();
  84. AudioTriggerComponentRequestBus::Handler::BusConnect(GetEntityId());
  85. if (m_playsImmediately)
  86. {
  87. // if requested, play the set trigger at Activation time...
  88. Play();
  89. }
  90. }
  91. //=========================================================================
  92. void AudioTriggerComponent::Deactivate()
  93. {
  94. AudioTriggerComponentRequestBus::Handler::BusDisconnect(GetEntityId());
  95. KillAllTriggers();
  96. }
  97. //=========================================================================
  98. AudioTriggerComponent::AudioTriggerComponent(const AZStd::string& playTriggerName, const AZStd::string& stopTriggerName, Audio::ObstructionType obstructionType, bool playsImmediately)
  99. : m_defaultPlayTriggerName(playTriggerName)
  100. , m_defaultStopTriggerName(stopTriggerName)
  101. , m_obstructionType(obstructionType)
  102. , m_playsImmediately(playsImmediately)
  103. {
  104. }
  105. //=========================================================================
  106. void AudioTriggerComponent::Play()
  107. {
  108. if (m_defaultPlayTriggerID != INVALID_AUDIO_CONTROL_ID)
  109. {
  110. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::ExecuteTrigger, m_defaultPlayTriggerID);
  111. }
  112. }
  113. //=========================================================================
  114. void AudioTriggerComponent::Stop()
  115. {
  116. if (m_defaultStopTriggerID == INVALID_AUDIO_CONTROL_ID)
  117. {
  118. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::KillTrigger, m_defaultPlayTriggerID);
  119. }
  120. else
  121. {
  122. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::ExecuteTrigger, m_defaultStopTriggerID);
  123. }
  124. }
  125. //=========================================================================
  126. void AudioTriggerComponent::ExecuteTrigger(const char* triggerName)
  127. {
  128. if (triggerName && triggerName[0] != '\0')
  129. {
  130. Audio::TAudioControlID triggerID = INVALID_AUDIO_CONTROL_ID;
  131. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  132. {
  133. triggerID = audioSystem->GetAudioTriggerID(triggerName);
  134. }
  135. if (triggerID != INVALID_AUDIO_CONTROL_ID)
  136. {
  137. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::ExecuteTrigger, triggerID);
  138. }
  139. }
  140. }
  141. //=========================================================================
  142. void AudioTriggerComponent::KillTrigger(const char* triggerName)
  143. {
  144. if (triggerName && triggerName[0] != '\0')
  145. {
  146. Audio::TAudioControlID triggerID = INVALID_AUDIO_CONTROL_ID;
  147. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  148. {
  149. triggerID = audioSystem->GetAudioTriggerID(triggerName);
  150. }
  151. if (triggerID != INVALID_AUDIO_CONTROL_ID)
  152. {
  153. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::KillTrigger, triggerID);
  154. }
  155. }
  156. }
  157. //=========================================================================
  158. void AudioTriggerComponent::KillAllTriggers()
  159. {
  160. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::KillAllTriggers);
  161. }
  162. //=========================================================================
  163. void AudioTriggerComponent::SetMovesWithEntity(bool shouldTrackEntity)
  164. {
  165. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetMovesWithEntity, shouldTrackEntity);
  166. }
  167. //=========================================================================
  168. void AudioTriggerComponent::SetObstructionType(Audio::ObstructionType obstructionType)
  169. {
  170. if (m_obstructionType != obstructionType && obstructionType != Audio::ObstructionType::Count)
  171. {
  172. m_obstructionType = obstructionType;
  173. OnObstructionTypeChanged();
  174. }
  175. }
  176. //=========================================================================
  177. void AudioTriggerComponent::OnPlayTriggerChanged()
  178. {
  179. m_defaultPlayTriggerID = INVALID_AUDIO_CONTROL_ID;
  180. if (!m_defaultPlayTriggerName.empty())
  181. {
  182. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  183. {
  184. m_defaultPlayTriggerID = audioSystem->GetAudioTriggerID(m_defaultPlayTriggerName.c_str());
  185. }
  186. }
  187. // "ChangeNotify" sends callbacks on every key press for a text field!
  188. // This results in a lot of failed lookups.
  189. }
  190. //=========================================================================
  191. void AudioTriggerComponent::OnStopTriggerChanged()
  192. {
  193. m_defaultStopTriggerID = INVALID_AUDIO_CONTROL_ID;
  194. if (!m_defaultStopTriggerName.empty())
  195. {
  196. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  197. {
  198. m_defaultStopTriggerID = audioSystem->GetAudioTriggerID(m_defaultStopTriggerName.c_str());
  199. }
  200. }
  201. }
  202. //=========================================================================
  203. void AudioTriggerComponent::OnObstructionTypeChanged()
  204. {
  205. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetObstructionCalcType, m_obstructionType);
  206. }
  207. } // namespace LmbrCentral