BackgroundMusicComponent.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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("Shuffle", &BackgroundMusicComponent::m_shuffle)
  22. ->Field("Playlist", &BackgroundMusicComponent::m_playlist)
  23. ->Version(1);
  24. AZ::EditContext* editContext = serializeContext->GetEditContext();
  25. if (editContext)
  26. {
  27. editContext->Class<BackgroundMusicComponent>("BackgroundMusicComponent", "Plays a sequence of background music tracks in sequence")
  28. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  29. ->Attribute(AZ::Edit::Attributes::Category, "MultiplayerSample")
  30. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  31. ->DataElement(AZ::Edit::UIHandlers::Default, &BackgroundMusicComponent::m_shuffle, "Shuffle", "If true, the playlist will be shuffled and play in a randomized ordering")
  32. ->DataElement(AZ::Edit::UIHandlers::Default, &BackgroundMusicComponent::m_playlist, "Playlist", "The set of background music audio triggers to play");
  33. }
  34. }
  35. }
  36. void BackgroundMusicComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  37. {
  38. services.push_back(AZ_CRC_CE("BackgroundMusicComponent"));
  39. }
  40. void BackgroundMusicComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  41. {
  42. required.push_back(AZ_CRC("AudioProxyService", 0x7da4c79c));
  43. }
  44. void BackgroundMusicComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  45. {
  46. services.push_back(AZ_CRC_CE("BackgroundMusicComponent"));
  47. }
  48. void BackgroundMusicComponent::Activate()
  49. {
  50. #if AZ_TRAIT_CLIENT
  51. m_audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
  52. if (m_shuffle)
  53. {
  54. // Basic Knuth shuffle
  55. for (size_t index = m_playlist.size() - 1; index >= 1; --index)
  56. {
  57. size_t shuffleElement = rand() % (index + 1);
  58. AZStd::swap(m_playlist[index], m_playlist[shuffleElement]);
  59. }
  60. }
  61. if (m_audioSystem != nullptr)
  62. {
  63. m_trackIndex = size_t(-1);
  64. ReportTriggerFinished(INVALID_AUDIO_CONTROL_ID);
  65. }
  66. #endif
  67. Audio::AudioTriggerNotificationBus::Handler::BusConnect(Audio::TriggerNotificationIdType{ GetEntityId() });
  68. }
  69. void BackgroundMusicComponent::Deactivate()
  70. {
  71. Audio::AudioTriggerNotificationBus::Handler::BusDisconnect(Audio::TriggerNotificationIdType{ GetEntityId() });
  72. }
  73. void BackgroundMusicComponent::ReportTriggerFinished([[maybe_unused]] Audio::TAudioControlID triggerId)
  74. {
  75. if (m_playlist.empty())
  76. {
  77. return;
  78. }
  79. #if AZ_TRAIT_CLIENT
  80. m_trackIndex = ++m_trackIndex % m_playlist.size();
  81. if (m_audioSystem != nullptr)
  82. {
  83. const AZStd::string& trackName = m_playlist[m_trackIndex];
  84. m_currentTrackTriggerId = m_audioSystem->GetAudioTriggerID(trackName.c_str());
  85. if (m_currentTrackTriggerId != INVALID_AUDIO_CONTROL_ID)
  86. {
  87. LmbrCentral::AudioProxyComponentRequestBus::Event(
  88. GetEntityId(),
  89. &LmbrCentral::AudioProxyComponentRequests::ExecuteTrigger,
  90. m_currentTrackTriggerId);
  91. }
  92. }
  93. #endif
  94. }
  95. }