123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- /*
- * 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 "AudioMultiPositionComponent.h"
- #include <AzCore/Component/TransformBus.h>
- #include <AzCore/RTTI/BehaviorContext.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <LmbrCentral/Audio/AudioProxyComponentBus.h>
- namespace LmbrCentral
- {
- //=========================================================================
- void AudioMultiPositionComponent::Reflect(AZ::ReflectContext* context)
- {
- if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serializeContext->Class<AudioMultiPositionComponent, AZ::Component>()
- ->Version(0)
- ->Field("Entity Refs", &AudioMultiPositionComponent::m_entityRefs)
- ->Field("Behavior Type", &AudioMultiPositionComponent::m_behaviorType)
- ;
- }
- if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Enum<static_cast<AZ::u32>(Audio::MultiPositionBehaviorType::Separate)>("MultiPositionBehaviorType_Separate")
- ->Enum<static_cast<AZ::u32>(Audio::MultiPositionBehaviorType::Blended)>("MultiPositionBehaviorType_Blended")
- ;
- behaviorContext->EBus<AudioMultiPositionComponentRequestBus>("Multi-Position Audio Requests", "AudioMultiPositionComponentRequestBus")
- ->Attribute(AZ::Script::Attributes::Category, "Audio")
- ->Event("Add Entity", &AudioMultiPositionComponentRequestBus::Events::AddEntity, "AddEntity")
- ->Event("RemoveEntity", &AudioMultiPositionComponentRequestBus::Events::RemoveEntity)
- ->Event("SetBehaviorType", &AudioMultiPositionComponentRequestBus::Events::SetBehaviorType)
- ;
- }
- }
- //=========================================================================
- AudioMultiPositionComponent::AudioMultiPositionComponent(const AZStd::vector<AZ::EntityId>& entities, Audio::MultiPositionBehaviorType type)
- : m_behaviorType(type)
- {
- // Remove duplicates when building the game entity.
- AZStd::unordered_set<AZ::EntityId> setOfEntities(entities.begin(), entities.end());
- m_entityRefs.assign(setOfEntities.begin(), setOfEntities.end());
- m_entityPositions.reserve(m_entityRefs.size());
- }
- //=========================================================================
- void AudioMultiPositionComponent::Activate()
- {
- for (const auto& entityId : m_entityRefs)
- {
- AZ::EntityBus::MultiHandler::BusConnect(entityId);
- }
- AudioMultiPositionComponentRequestBus::Handler::BusConnect(GetEntityId());
- }
- //=========================================================================
- void AudioMultiPositionComponent::Deactivate()
- {
- AudioMultiPositionComponentRequestBus::Handler::BusDisconnect();
- AZ::EntityBus::MultiHandler::BusDisconnect();
- }
- //=========================================================================
- void AudioMultiPositionComponent::SetBehaviorType(Audio::MultiPositionBehaviorType type)
- {
- if (m_behaviorType != type)
- {
- m_behaviorType = type;
- // Re-send if all entities are activated...
- if (m_entityPositions.size() == m_entityRefs.size())
- {
- SendMultiplePositions();
- }
- }
- }
- //=========================================================================
- void AudioMultiPositionComponent::AddEntity(const AZ::EntityId& entityId)
- {
- auto iter = AZStd::find(m_entityRefs.begin(), m_entityRefs.end(), entityId);
- if (iter == m_entityRefs.end())
- {
- m_entityRefs.push_back(entityId);
- AZ::EntityBus::MultiHandler::BusConnect(entityId);
- }
- }
- //=========================================================================
- void AudioMultiPositionComponent::RemoveEntity(const AZ::EntityId& entityId)
- {
- auto iter = AZStd::find(m_entityRefs.begin(), m_entityRefs.end(), entityId);
- if (iter != m_entityRefs.end())
- {
- m_entityRefs.erase(iter);
- AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
- }
- }
- //=========================================================================
- void AudioMultiPositionComponent::OnEntityActivated(const AZ::EntityId& entityId)
- {
- AZ_Assert(m_entityPositions.size() < m_entityRefs.size(), "Multi-Position Audio: Seen more entities activated than entities being tracked.\n");
- AZ::Vector3 position;
- AZ::TransformBus::EventResult(position, entityId, &AZ::TransformBus::Events::GetWorldTranslation);
- m_entityPositions.push_back(AZStd::make_pair(entityId, position));
- if (m_entityPositions.size() == m_entityRefs.size())
- {
- SendMultiplePositions();
- }
- }
- //=========================================================================
- void AudioMultiPositionComponent::OnEntityDeactivated(const AZ::EntityId& entityId)
- {
- auto iter = AZStd::find_if(m_entityPositions.begin(), m_entityPositions.end(),
- [&entityId](const auto& entityPositionPair) -> bool
- {
- return entityPositionPair.first == entityId;
- }
- );
- if (iter != m_entityPositions.end())
- {
- m_entityPositions.erase(iter);
- }
- }
- //=========================================================================
- void AudioMultiPositionComponent::SendMultiplePositions()
- {
- // Sends the vector of positions out to be processed.
- Audio::MultiPositionParams params;
- params.m_type = m_behaviorType;
- for (const auto& entityPosPair : m_entityPositions)
- {
- params.m_positions.emplace_back(entityPosPair.second);
- }
- AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetMultiplePositions, params);
- }
- } // namespace LmbrCentral
|