/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include "Maestro_precompiled.h" #include "SequenceAgentComponent.h" #include namespace Maestro { //========================================================================= namespace ClassConverters { static bool UpgradeSequenceAgentComponent(AZ::SerializeContext&, AZ::SerializeContext::DataElementNode&); } // namespace ClassConverters /*static*/ void SequenceAgentComponent::Reflect(AZ::ReflectContext* context) { AZ::SerializeContext* serializeContext = azrtti_cast(context); if (serializeContext) { serializeContext->Class() ->Field("SequenceComponentEntityIds", &SequenceAgentComponent::m_sequenceEntityIds) ->Version(2, &ClassConverters::UpgradeSequenceAgentComponent) ; } } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::Init() { m_addressToBehaviorVirtualPropertiesMap.clear(); } void SequenceAgentComponent::Activate() { // cache pointers and animatable addresses for animation // CacheAllVirtualPropertiesFromBehaviorContext(); ConnectAllSequences(); } void SequenceAgentComponent::Deactivate() { // invalidate all cached pointers and addresses for animation m_addressToBehaviorVirtualPropertiesMap.clear(); DisconnectAllSequences(); } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::ConnectSequence(const AZ::EntityId& sequenceEntityId) { if (m_sequenceEntityIds.find(sequenceEntityId) == m_sequenceEntityIds.end()) { m_sequenceEntityIds.insert(sequenceEntityId); // connect to EBus between the given SequenceComponent and me Maestro::SequenceAgentEventBusId busId(sequenceEntityId, GetEntityId()); SequenceAgentComponentRequestBus::MultiHandler::BusConnect(busId); } } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::DisconnectSequence() { const Maestro::SequenceAgentEventBusId *busIdToDisconnect = SequenceAgentComponentRequestBus::GetCurrentBusId(); if (busIdToDisconnect) { AZ::EntityId sequenceEntityId = busIdToDisconnect->first; // we only process DisconnectSequence events sent over an ID'ed bus - otherwise we don't know which SequenceComponent to disconnect auto findIter = m_sequenceEntityIds.find(sequenceEntityId); AZ_Assert(findIter != m_sequenceEntityIds.end(), "A sequence not connected to SequenceAgentComponent on %s is requesting a disconnection", GetEntity()->GetName().c_str()); m_sequenceEntityIds.erase(sequenceEntityId); // Disconnect from the bus between the SequenceComponent and me SequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(*busIdToDisconnect); } } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::ConnectAllSequences() { // Connect all buses for (auto iter = m_sequenceEntityIds.begin(); iter != m_sequenceEntityIds.end(); iter++) { Maestro::SequenceAgentEventBusId busIdToConnect(*iter, GetEntityId()); SequenceAgentComponentRequestBus::MultiHandler::BusConnect(busIdToConnect); } } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::DisconnectAllSequences() { // disconnect all buses for (auto iter = m_sequenceEntityIds.begin(); iter != m_sequenceEntityIds.end(); iter++) { Maestro::SequenceAgentEventBusId busIdToDisconnect(*iter, GetEntityId()); SequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(busIdToDisconnect); } } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::GetAnimatedPropertyValue(AnimatedValue& returnValue, const Maestro::SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress) { SequenceAgent::GetAnimatedPropertyValue(returnValue, GetEntityId(), animatableAddress); } /////////////////////////////////////////////////////////////////////////////////////////////////////// bool SequenceAgentComponent::SetAnimatedPropertyValue(const Maestro::SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress, const AnimatedValue& value) { return SequenceAgent::SetAnimatedPropertyValue(GetEntityId(), animatableAddress, value); } /////////////////////////////////////////////////////////////////////////////////////////////////////// AZ::Uuid SequenceAgentComponent::GetAnimatedAddressTypeId(const AnimatablePropertyAddress& animatableAddress) { return GetVirtualPropertyTypeId(animatableAddress); } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::GetAssetDuration(AnimatedValue& returnValue, AZ::ComponentId componentId, const AZ::Data::AssetId& assetId) { SequenceAgent::GetAssetDuration(returnValue, componentId, assetId); } void SequenceAgentComponent::GetAssetTypeName(AZStd::string& returnValue, const AnimatablePropertyAddress& animatableAddress) { SequenceAgent::GetAssetTypeName(returnValue, GetEntityId(), animatableAddress); } /////////////////////////////////////////////////////////////////////////////////////////////////////// void SequenceAgentComponent::GetEntityComponents(AZ::Entity::ComponentArrayType& entityComponents) const { AZ::Entity* entity = GetEntity(); AZ_Assert(entity, "Expected valid entity."); if (entity) { const AZ::Entity::ComponentArrayType& enabledComponents = entity->GetComponents(); for (AZ::Component* component : enabledComponents) { entityComponents.push_back(component); } } } //========================================================================= namespace ClassConverters { static bool UpgradeSequenceAgentComponent(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement) { if (classElement.GetVersion() == 1) { // upgrade V1 to V2 - change element named "SequenceEntityComponentPairIds" to "SequenceComponentEntityIds" int oldSeqIdNameIdx = classElement.FindElement(AZ::Crc32("SequenceEntityComponentPairIds")); if (oldSeqIdNameIdx == -1) { AZ_Error("Serialization", false, "Failed to find old SequenceEntityComponentPairIds element."); return false; } auto seqIdNameElement = classElement.GetSubElement(oldSeqIdNameIdx); seqIdNameElement.SetName("SequenceComponentEntityIds"); } return true; } } // namespace ClassConverters }// namespace Maestro