ScriptedEntityTweenerTask.h 5.3 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. #pragma once
  9. #include <AzCore/Component/EntityId.h>
  10. #include <AzCore/std/containers/unordered_map.h>
  11. #include <AzCore/std/containers/list.h>
  12. #include <ScriptedEntityTweener/ScriptedEntityTweenerEnums.h>
  13. #include "ScriptedEntityTweenerSubtask.h"
  14. namespace ScriptedEntityTweener
  15. {
  16. //! One task per entity id, contains a collection of subtasks that are unique per virtual property.
  17. class ScriptedEntityTweenerTask
  18. {
  19. public: // member functions
  20. ScriptedEntityTweenerTask(AZ::EntityId id);
  21. ~ScriptedEntityTweenerTask();
  22. void AddAnimation(const AnimationParameters& params, bool overwriteQueued = true);
  23. void Update(float deltaTime);
  24. bool GetIsActive();
  25. void Stop(int timelineId);
  26. void SetPaused(const AnimationParameterAddressData& addressData, int timelineId, bool isPaused);
  27. void SetPlayDirectionReversed(const AnimationParameterAddressData& addressData, int timelineId, bool isPlayingBackward);
  28. void SetSpeed(const AnimationParameterAddressData& addressData, int timelineId, float speed);
  29. void SetInitialValue(const AnimationParameterAddressData& addressData, const AZ::Uuid& timelineId, const AZStd::any& initialValue);
  30. void GetVirtualPropertyValue(AZStd::any& returnVal, const AnimationParameterAddressData& addressData);
  31. bool operator<(const ScriptedEntityTweenerTask& other) const
  32. {
  33. return m_entityId < other.m_entityId;
  34. }
  35. bool operator>(const ScriptedEntityTweenerTask& other) const
  36. {
  37. return other.m_entityId < m_entityId;
  38. }
  39. bool operator==(const ScriptedEntityTweenerTask& other) const
  40. {
  41. return m_entityId == other.m_entityId;
  42. }
  43. private: // member functions
  44. AZ::EntityId m_entityId;
  45. //! Unique(per address data) active subtasks being updated
  46. AZStd::unordered_map<AnimationParameterAddressData, ScriptedEntityTweenerSubtask> m_subtasks;
  47. class QueuedSubtaskInfo
  48. {
  49. public:
  50. QueuedSubtaskInfo(AnimationParameters params, float delayTime)
  51. : m_params(params)
  52. , m_currentDelayTime(delayTime)
  53. , m_isPaused(false)
  54. {
  55. }
  56. bool UpdateUntilReady(float deltaTime)
  57. {
  58. if (!m_isPaused)
  59. {
  60. m_currentDelayTime -= deltaTime * m_params.m_animationProperties.m_playbackSpeedMultiplier;
  61. if (m_currentDelayTime <= .0f)
  62. {
  63. m_params.m_animationProperties.m_timeToDelayAnim = .0f;
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. int GetTimelineId()
  70. {
  71. return m_params.m_animationProperties.m_timelineId;
  72. }
  73. const AZ::Uuid& GetAnimationId()
  74. {
  75. return m_params.m_animationProperties.m_animationId;
  76. }
  77. void SetPlaybackSpeed(float speed)
  78. {
  79. m_params.m_animationProperties.m_playbackSpeedMultiplier = speed;
  80. }
  81. AnimationParameters& GetParameters()
  82. {
  83. return m_params;
  84. }
  85. void SetPaused(bool isPaused)
  86. {
  87. m_isPaused = isPaused;
  88. }
  89. void SetInitialValue(const AnimationParameterAddressData& addressData, const AZStd::any initialValue)
  90. {
  91. m_initialValues[addressData] = initialValue;
  92. }
  93. bool HasInitialValue()
  94. {
  95. return !m_initialValues.empty();
  96. }
  97. const AZStd::any& GetInitialValue(const AnimationParameterAddressData& addressData)
  98. {
  99. auto initialValueEntry = m_initialValues.find(addressData);
  100. if (initialValueEntry != m_initialValues.end())
  101. {
  102. return initialValueEntry->second;
  103. }
  104. return m_emptyInitialValue;
  105. }
  106. private:
  107. QueuedSubtaskInfo();
  108. float m_currentDelayTime;
  109. bool m_isPaused;
  110. AZStd::unordered_map<AnimationParameterAddressData, AZStd::any> m_initialValues;
  111. AnimationParameters m_params;
  112. static const AZStd::any m_emptyInitialValue;
  113. };
  114. //! List of AnimationsParameters that need to be delayed before being added to m_subtasks, possibly overriding an animation.
  115. AZStd::list<QueuedSubtaskInfo> m_queuedSubtasks;
  116. AZStd::set<CallbackData> m_callbacks;
  117. bool IsTimelineIdValid(int timelineId)
  118. {
  119. return timelineId != static_cast<int>(AnimationProperties::InvalidTimelineId);
  120. }
  121. bool InitializeSubtask(ScriptedEntityTweenerSubtask& subtask, const AZStd::pair<AnimationParameterAddressData, AZStd::any> initData, AnimationParameters params);
  122. void ExecuteCallbacks(const AZStd::set<CallbackData>& callbacks);
  123. void ClearCallbacks(const AnimationProperties& animationProperties);
  124. };
  125. }