SequenceAgentComponent.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "SequenceAgentComponent.h"
  9. #include <AzFramework/API/ApplicationAPI.h>
  10. namespace Maestro
  11. {
  12. //=========================================================================
  13. namespace ClassConverters
  14. {
  15. static bool UpgradeSequenceAgentComponent(AZ::SerializeContext&, AZ::SerializeContext::DataElementNode&);
  16. } // namespace ClassConverters
  17. /*static*/ void SequenceAgentComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  20. if (serializeContext)
  21. {
  22. serializeContext->Class<SequenceAgentComponent, Component>()
  23. ->Field("SequenceComponentEntityIds", &SequenceAgentComponent::m_sequenceEntityIds)
  24. ->Version(2, &ClassConverters::UpgradeSequenceAgentComponent)
  25. ;
  26. }
  27. }
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  29. void SequenceAgentComponent::Init()
  30. {
  31. m_addressToBehaviorVirtualPropertiesMap.clear();
  32. }
  33. void SequenceAgentComponent::Activate()
  34. {
  35. // cache pointers and animatable addresses for animation
  36. //
  37. CacheAllVirtualPropertiesFromBehaviorContext();
  38. ConnectAllSequences();
  39. }
  40. void SequenceAgentComponent::Deactivate()
  41. {
  42. // invalidate all cached pointers and addresses for animation
  43. m_addressToBehaviorVirtualPropertiesMap.clear();
  44. DisconnectAllSequences();
  45. }
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  47. void SequenceAgentComponent::ConnectSequence(const AZ::EntityId& sequenceEntityId)
  48. {
  49. if (m_sequenceEntityIds.find(sequenceEntityId) == m_sequenceEntityIds.end())
  50. {
  51. m_sequenceEntityIds.insert(sequenceEntityId);
  52. // connect to EBus between the given SequenceComponent and me
  53. Maestro::SequenceAgentEventBusId busId(sequenceEntityId, GetEntityId());
  54. SequenceAgentComponentRequestBus::MultiHandler::BusConnect(busId);
  55. }
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  58. void SequenceAgentComponent::DisconnectSequence()
  59. {
  60. const Maestro::SequenceAgentEventBusId *busIdToDisconnect = SequenceAgentComponentRequestBus::GetCurrentBusId();
  61. if (busIdToDisconnect)
  62. {
  63. AZ::EntityId sequenceEntityId = busIdToDisconnect->first;
  64. // we only process DisconnectSequence events sent over an ID'ed bus - otherwise we don't know which SequenceComponent to disconnect
  65. [[maybe_unused]] auto findIter = m_sequenceEntityIds.find(sequenceEntityId);
  66. AZ_Assert(findIter != m_sequenceEntityIds.end(), "A sequence not connected to SequenceAgentComponent on %s is requesting a disconnection", GetEntity()->GetName().c_str());
  67. m_sequenceEntityIds.erase(sequenceEntityId);
  68. // Disconnect from the bus between the SequenceComponent and me
  69. SequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(*busIdToDisconnect);
  70. }
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  73. void SequenceAgentComponent::ConnectAllSequences()
  74. {
  75. // Connect all buses
  76. for (auto iter = m_sequenceEntityIds.begin(); iter != m_sequenceEntityIds.end(); iter++)
  77. {
  78. Maestro::SequenceAgentEventBusId busIdToConnect(*iter, GetEntityId());
  79. SequenceAgentComponentRequestBus::MultiHandler::BusConnect(busIdToConnect);
  80. }
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  83. void SequenceAgentComponent::DisconnectAllSequences()
  84. {
  85. // disconnect all buses
  86. for (auto iter = m_sequenceEntityIds.begin(); iter != m_sequenceEntityIds.end(); iter++)
  87. {
  88. Maestro::SequenceAgentEventBusId busIdToDisconnect(*iter, GetEntityId());
  89. SequenceAgentComponentRequestBus::MultiHandler::BusDisconnect(busIdToDisconnect);
  90. }
  91. }
  92. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  93. void SequenceAgentComponent::GetAnimatedPropertyValue(AnimatedValue& returnValue, const Maestro::SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress)
  94. {
  95. SequenceAgent::GetAnimatedPropertyValue(returnValue, GetEntityId(), animatableAddress);
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  98. bool SequenceAgentComponent::SetAnimatedPropertyValue(const Maestro::SequenceComponentRequests::AnimatablePropertyAddress& animatableAddress, const AnimatedValue& value)
  99. {
  100. return SequenceAgent::SetAnimatedPropertyValue(GetEntityId(), animatableAddress, value);
  101. }
  102. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  103. AZ::Uuid SequenceAgentComponent::GetAnimatedAddressTypeId(const AnimatablePropertyAddress& animatableAddress)
  104. {
  105. return GetVirtualPropertyTypeId(animatableAddress);
  106. }
  107. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  108. void SequenceAgentComponent::GetAssetDuration(AnimatedValue& returnValue, AZ::ComponentId componentId, const AZ::Data::AssetId& assetId)
  109. {
  110. SequenceAgent::GetAssetDuration(returnValue, componentId, assetId);
  111. }
  112. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  113. void SequenceAgentComponent::GetEntityComponents(AZ::Entity::ComponentArrayType& entityComponents) const
  114. {
  115. AZ::Entity* entity = GetEntity();
  116. AZ_Assert(entity, "Expected valid entity.");
  117. if (entity)
  118. {
  119. const AZ::Entity::ComponentArrayType& enabledComponents = entity->GetComponents();
  120. for (AZ::Component* component : enabledComponents)
  121. {
  122. entityComponents.push_back(component);
  123. }
  124. }
  125. }
  126. //=========================================================================
  127. namespace ClassConverters
  128. {
  129. static bool UpgradeSequenceAgentComponent([[maybe_unused]] AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  130. {
  131. if (classElement.GetVersion() == 1)
  132. {
  133. // upgrade V1 to V2 - change element named "SequenceEntityComponentPairIds" to "SequenceComponentEntityIds"
  134. int oldSeqIdNameIdx = classElement.FindElement(AZ::Crc32("SequenceEntityComponentPairIds"));
  135. if (oldSeqIdNameIdx == -1)
  136. {
  137. AZ_Error("Serialization", false, "Failed to find old SequenceEntityComponentPairIds element.");
  138. return false;
  139. }
  140. auto seqIdNameElement = classElement.GetSubElement(oldSeqIdNameIdx);
  141. seqIdNameElement.SetName("SequenceComponentEntityIds");
  142. }
  143. return true;
  144. }
  145. } // namespace ClassConverters
  146. }// namespace Maestro