123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <Source/Components/BackgroundMusicComponent.h>
- #include <AzCore/Serialization/EditContext.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <LmbrCentral/Audio/AudioProxyComponentBus.h>
- #include <AzCore/Console/ILogger.h>
- namespace MultiplayerSample
- {
- void BackgroundMusicComponent::Reflect(AZ::ReflectContext* context)
- {
- AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
- if (serializeContext)
- {
- serializeContext->Class<BackgroundMusicComponent, AZ::Component>()
- ->Field("Shuffle", &BackgroundMusicComponent::m_shuffle)
- ->Field("Playlist", &BackgroundMusicComponent::m_playlist)
- ->Version(1);
- AZ::EditContext* editContext = serializeContext->GetEditContext();
- if (editContext)
- {
- editContext->Class<BackgroundMusicComponent>("BackgroundMusicComponent", "Plays a sequence of background music tracks in sequence")
- ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
- ->Attribute(AZ::Edit::Attributes::Category, "MultiplayerSample")
- ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
- ->DataElement(AZ::Edit::UIHandlers::Default, &BackgroundMusicComponent::m_shuffle, "Shuffle", "If true, the playlist will be shuffled and play in a randomized ordering")
- ->DataElement(AZ::Edit::UIHandlers::Default, &BackgroundMusicComponent::m_playlist, "Playlist", "The set of background music audio triggers to play");
- }
- }
- }
- void BackgroundMusicComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
- {
- services.push_back(AZ_CRC_CE("BackgroundMusicComponent"));
- }
- void BackgroundMusicComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
- {
- required.push_back(AZ_CRC("AudioProxyService", 0x7da4c79c));
- }
- void BackgroundMusicComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
- {
- services.push_back(AZ_CRC_CE("BackgroundMusicComponent"));
- }
- void BackgroundMusicComponent::Activate()
- {
- #if AZ_TRAIT_CLIENT
- m_audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
- if (m_shuffle)
- {
- // Basic Knuth shuffle
- for (size_t index = m_playlist.size() - 1; index >= 1; --index)
- {
- size_t shuffleElement = rand() % (index + 1);
- AZStd::swap(m_playlist[index], m_playlist[shuffleElement]);
- }
- }
- if (m_audioSystem != nullptr)
- {
- m_trackIndex = size_t(-1);
- ReportTriggerFinished(INVALID_AUDIO_CONTROL_ID);
- }
- #endif
- Audio::AudioTriggerNotificationBus::Handler::BusConnect(Audio::TriggerNotificationIdType{ GetEntityId() });
- }
- void BackgroundMusicComponent::Deactivate()
- {
- Audio::AudioTriggerNotificationBus::Handler::BusDisconnect(Audio::TriggerNotificationIdType{ GetEntityId() });
- }
- void BackgroundMusicComponent::ReportTriggerFinished([[maybe_unused]] Audio::TAudioControlID triggerId)
- {
- if (m_playlist.empty())
- {
- return;
- }
- #if AZ_TRAIT_CLIENT
- m_trackIndex = ++m_trackIndex % m_playlist.size();
- if (m_audioSystem != nullptr)
- {
- const AZStd::string& trackName = m_playlist[m_trackIndex];
- m_currentTrackTriggerId = m_audioSystem->GetAudioTriggerID(trackName.c_str());
- if (m_currentTrackTriggerId != INVALID_AUDIO_CONTROL_ID)
- {
- LmbrCentral::AudioProxyComponentRequestBus::Event(
- GetEntityId(),
- &LmbrCentral::AudioProxyComponentRequests::ExecuteTrigger,
- m_currentTrackTriggerId);
- }
- }
- #endif
- }
- }
|