ScriptedEntityTweenerSubtask.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #pragma once
  9. #include <AzCore/Math/Vector3.h>
  10. #include <AzCore/Math/Quaternion.h>
  11. #include <AzCore/std/any.h>
  12. #include <AzCore/std/containers/set.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <ScriptedEntityTweener/ScriptedEntityTweenerEnums.h>
  15. namespace ScriptedEntityTweener
  16. {
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. //! Each subtask performs operations on a virtual address.
  19. class ScriptedEntityTweenerSubtask
  20. {
  21. public:
  22. ScriptedEntityTweenerSubtask(const AZ::EntityId& entityId)
  23. : m_entityId(entityId)
  24. {
  25. Reset();
  26. }
  27. bool Initialize(const AnimationParameterAddressData& animParamData, const AZStd::any& targetValue, const AnimationProperties& properties);
  28. //! Update virtual property based on animation properties, fill out callbacks vector with any callback information that needs to be called this update.
  29. void Update(float deltaTime, AZStd::set<CallbackData>& callbacks);
  30. //! True if active and animating a virtual property
  31. bool IsActive() { return m_isActive; }
  32. void SetPaused(int timelineId, bool isPaused)
  33. {
  34. if (m_animationProperties.m_timelineId == timelineId)
  35. {
  36. m_isPaused = isPaused;
  37. }
  38. }
  39. void SetPlayDirectionReversed(int timelineId, bool isPlayingBackward)
  40. {
  41. if (m_animationProperties.m_timelineId == timelineId)
  42. {
  43. m_animationProperties.m_isPlayingBackward = isPlayingBackward;
  44. }
  45. }
  46. void SetSpeed(int timelineId, float speed)
  47. {
  48. if (m_animationProperties.m_timelineId == timelineId)
  49. {
  50. m_animationProperties.m_playbackSpeedMultiplier = speed;
  51. }
  52. }
  53. void SetInitialValue(const AZ::Uuid& animationId, const AZStd::any& initialValue)
  54. {
  55. if (m_animationProperties.m_animationId == animationId)
  56. {
  57. GetValueFromAny(m_valueInitial, initialValue);
  58. }
  59. }
  60. const int GetTimelineId() const
  61. {
  62. return m_animationProperties.m_timelineId;
  63. }
  64. const AnimationProperties& GetAnimationProperties() const
  65. {
  66. return m_animationProperties;
  67. }
  68. bool GetVirtualPropertyValue(AZStd::any& returnVal, const AnimationParameterAddressData& animParamData);
  69. private:
  70. //! Animated value class that abstracts the supported types
  71. struct EntityAnimatedValue
  72. {
  73. public:
  74. EntityAnimatedValue()
  75. : floatVal(AnimationProperties::UninitializedParamFloat), vectorVal(AZ::Vector3::CreateZero()), quatVal(AZ::Quaternion::CreateIdentity())
  76. {
  77. }
  78. void GetValue(float& outVal) const
  79. {
  80. outVal = floatVal;
  81. }
  82. void GetValue(AZ::Vector3& outVal) const
  83. {
  84. outVal = vectorVal;
  85. }
  86. void GetValue(AZ::Quaternion& outVal) const
  87. {
  88. outVal = quatVal;
  89. }
  90. void SetValue(float val)
  91. {
  92. floatVal = val;
  93. }
  94. void SetValue(AZ::Vector3 val)
  95. {
  96. vectorVal = val;
  97. }
  98. void SetValue(AZ::Quaternion val)
  99. {
  100. quatVal = val;
  101. }
  102. private:
  103. float floatVal;
  104. AZ::Vector3 vectorVal;
  105. AZ::Quaternion quatVal;
  106. };
  107. AnimationProperties m_animationProperties;
  108. // The entity being modified
  109. AZ::EntityId m_entityId;
  110. // The component and property name to be modified. This is only used for displaying warning messages
  111. AnimationParameterAddressData m_animParamData;
  112. // Cached virtual property
  113. AZ::BehaviorEBus::VirtualProperty* m_virtualProperty;
  114. // Type of the virtual property
  115. AZ::Uuid m_virtualPropertyTypeId;
  116. bool m_isActive;
  117. bool m_isPaused;
  118. float m_timeSinceStart;
  119. int m_timesPlayed;
  120. EntityAnimatedValue m_valueInitial;
  121. EntityAnimatedValue m_valueTarget;
  122. void Reset()
  123. {
  124. m_isActive = false;
  125. m_isPaused = false;
  126. m_timeSinceStart = 0.0f;
  127. m_valueInitial = EntityAnimatedValue();
  128. m_valueTarget = EntityAnimatedValue();
  129. m_timesPlayed = 0;
  130. m_animationProperties.Reset();
  131. m_animParamData = AnimationParameterAddressData();
  132. m_virtualPropertyTypeId = AZ::Uuid::CreateNull();
  133. m_virtualProperty = nullptr;
  134. }
  135. //! Cache the virtual property to be animated
  136. bool CacheVirtualProperty(const AnimationParameterAddressData& animParamData);
  137. //! Return whether the virtual property has been cached
  138. bool IsVirtualPropertyCached();
  139. //! Set value from an AZStd::any object
  140. bool GetValueFromAny(EntityAnimatedValue& value, const AZStd::any& anyValue);
  141. //! Set value to an AZStd::any object
  142. bool GetValueAsAny(AZStd::any& anyValue, const EntityAnimatedValue& value);
  143. //! Get value from the virtual address's value
  144. bool GetVirtualValue(EntityAnimatedValue& animatedValue);
  145. //! Set the virtual address's value
  146. bool SetVirtualValue(const EntityAnimatedValue& value);
  147. };
  148. }