BackgroundMusicComponent.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <Source/Components/BackgroundMusicComponent.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <LmbrCentral/Audio/AudioProxyComponentBus.h>
  12. #include <AzCore/Console/ILogger.h>
  13. namespace MultiplayerSample
  14. {
  15. void BackgroundMusicComponent::Reflect(AZ::ReflectContext* context)
  16. {
  17. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  18. if (serializeContext)
  19. {
  20. serializeContext->Class<BackgroundMusicComponent, AZ::Component>()
  21. ->Field("Playlist", &BackgroundMusicComponent::m_playlist)
  22. ->Version(1);
  23. AZ::EditContext* editContext = serializeContext->GetEditContext();
  24. if (editContext)
  25. {
  26. editContext->Class<BackgroundMusicComponent>("BackgroundMusicComponent", "Plays a sequence of background music tracks in sequence")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::Category, "MultiplayerSample")
  29. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  30. ->DataElement(AZ::Edit::UIHandlers::Default, &BackgroundMusicComponent::m_playlist, "Playlist", "The set of background music audio triggers to play");
  31. }
  32. }
  33. }
  34. void BackgroundMusicComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  35. {
  36. services.push_back(AZ_CRC_CE("BackgroundMusicComponent"));
  37. }
  38. void BackgroundMusicComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  39. {
  40. required.push_back(AZ_CRC("AudioProxyService", 0x7da4c79c));
  41. }
  42. void BackgroundMusicComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  43. {
  44. services.push_back(AZ_CRC_CE("BackgroundMusicComponent"));
  45. }
  46. void BackgroundMusicComponent::Activate()
  47. {
  48. #if AZ_TRAIT_CLIENT
  49. m_audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
  50. if (m_audioSystem != nullptr)
  51. {
  52. m_trackIndex = size_t(-1);
  53. ReportTriggerFinished(INVALID_AUDIO_CONTROL_ID);
  54. }
  55. #endif
  56. Audio::AudioTriggerNotificationBus::Handler::BusConnect(Audio::TriggerNotificationIdType{ GetEntityId() });
  57. }
  58. void BackgroundMusicComponent::Deactivate()
  59. {
  60. Audio::AudioTriggerNotificationBus::Handler::BusDisconnect(Audio::TriggerNotificationIdType{ GetEntityId() });
  61. }
  62. void BackgroundMusicComponent::ReportTriggerFinished([[maybe_unused]] Audio::TAudioControlID triggerId)
  63. {
  64. if (m_playlist.empty())
  65. {
  66. return;
  67. }
  68. #if AZ_TRAIT_CLIENT
  69. m_trackIndex = ++m_trackIndex % m_playlist.size();
  70. if (m_audioSystem != nullptr)
  71. {
  72. const AZStd::string& trackName = m_playlist[m_trackIndex];
  73. m_currentTrackTriggerId = m_audioSystem->GetAudioTriggerID(trackName.c_str());
  74. if (m_currentTrackTriggerId != INVALID_AUDIO_CONTROL_ID)
  75. {
  76. LmbrCentral::AudioProxyComponentRequestBus::Event(
  77. GetEntityId(),
  78. &LmbrCentral::AudioProxyComponentRequests::ExecuteTrigger,
  79. m_currentTrackTriggerId);
  80. }
  81. }
  82. #endif
  83. }
  84. }