SequenceAgentComponent.cpp 6.1 KB

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