ScriptedEntityTweenerSystemComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <AzCore/RTTI/BehaviorContext.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <ScriptedEntityTweener/ScriptedEntityTweenerBus.h>
  12. #include <ScriptedEntityTweener/ScriptedEntityTweenerEnums.h>
  13. #include "ScriptedEntityTweenerSystemComponent.h"
  14. namespace ScriptedEntityTweener
  15. {
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. //! ScriptedEntityTweenerNotificationBusHandler Behavior context handler class
  18. class ScriptedEntityTweenerNotificationBusHandler :
  19. public ScriptedEntityTweenerNotificationsBus::Handler,
  20. public AZ::BehaviorEBusHandler
  21. {
  22. public:
  23. AZ_EBUS_BEHAVIOR_BINDER(ScriptedEntityTweenerNotificationBusHandler, "{118D3961-17C7-46E9-B9AC-61682D57E8D3}", AZ::SystemAllocator,
  24. OnComplete, OnUpdate, OnLoop, RemoveCallback, OnTimelineAnimationStart);
  25. void OnComplete(int callbackId) override
  26. {
  27. Call(FN_OnComplete, callbackId);
  28. }
  29. void OnUpdate(int callbackId, const AZStd::any& currentVal, float progressPercent) override
  30. {
  31. Call(FN_OnUpdate, callbackId, currentVal, progressPercent);
  32. }
  33. void OnLoop(int callbackId) override
  34. {
  35. Call(FN_OnLoop, callbackId);
  36. }
  37. void RemoveCallback(int callbackId) override
  38. {
  39. Call(FN_RemoveCallback, callbackId);
  40. }
  41. void OnTimelineAnimationStart(int timelineId, const AZ::Uuid& uuid, const AZStd::string& componentName, const AZStd::string& propertyName) override
  42. {
  43. Call(FN_OnTimelineAnimationStart, timelineId, uuid, componentName, propertyName);
  44. }
  45. };
  46. void ScriptedEntityTweenerSystemComponent::Reflect(AZ::ReflectContext* context)
  47. {
  48. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  49. {
  50. serialize->Class<ScriptedEntityTweenerSystemComponent, AZ::Component>()
  51. ->Version(0)
  52. ;
  53. }
  54. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  55. if (behaviorContext)
  56. {
  57. behaviorContext->EBus<ScriptedEntityTweenerBus>("ScriptedEntityTweenerBus")
  58. ->Event("AnimateEntity", &ScriptedEntityTweenerBus::Events::AnimateEntityScript)
  59. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::List)
  60. ->Event("SetOptionalParams", &ScriptedEntityTweenerBus::Events::SetOptionalParams)
  61. ->Event("Stop", &ScriptedEntityTweenerBus::Events::Stop)
  62. ->Event("Pause", &ScriptedEntityTweenerBus::Events::Pause)
  63. ->Event("Resume", &ScriptedEntityTweenerBus::Events::Resume)
  64. ->Event("SetPlayDirectionReversed", &ScriptedEntityTweenerBus::Events::SetPlayDirectionReversed)
  65. ->Event("SetSpeed", &ScriptedEntityTweenerBus::Events::SetSpeed)
  66. ->Event("SetInitialValue", &ScriptedEntityTweenerBus::Events::SetInitialValue)
  67. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::List)
  68. ->Event("GetVirtualPropertyValue", &ScriptedEntityTweenerBus::Events::GetVirtualPropertyValue);
  69. behaviorContext->EBus<ScriptedEntityTweenerNotificationsBus>("ScriptedEntityTweenerNotificationBus")
  70. ->Handler<ScriptedEntityTweenerNotificationBusHandler>();
  71. behaviorContext->Enum<(int)ScriptedEntityTweener::EasingMethod::Linear>("ScriptedEntityTweenerEasingMethod_Linear")
  72. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Quad>("ScriptedEntityTweenerEasingMethod_Quad")
  73. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Cubic>("ScriptedEntityTweenerEasingMethod_Cubic")
  74. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Quart>("ScriptedEntityTweenerEasingMethod_Quart")
  75. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Quint>("ScriptedEntityTweenerEasingMethod_Quint")
  76. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Sine>("ScriptedEntityTweenerEasingMethod_Sine")
  77. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Expo>("ScriptedEntityTweenerEasingMethod_Expo")
  78. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Circ>("ScriptedEntityTweenerEasingMethod_Circ")
  79. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Elastic>("ScriptedEntityTweenerEasingMethod_Elastic")
  80. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Back>("ScriptedEntityTweenerEasingMethod_Back")
  81. ->Enum<(int)ScriptedEntityTweener::EasingMethod::Bounce>("ScriptedEntityTweenerEasingMethod_Bounce")
  82. ->Enum<(int)ScriptedEntityTweener::EasingType::In>("ScriptedEntityTweenerEasingType_In")
  83. ->Enum<(int)ScriptedEntityTweener::EasingType::Out>("ScriptedEntityTweenerEasingType_Out")
  84. ->Enum<(int)ScriptedEntityTweener::EasingType::InOut>("ScriptedEntityTweenerEasingType_InOut");
  85. }
  86. }
  87. void ScriptedEntityTweenerSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  88. {
  89. provided.push_back(AZ_CRC_CE("ScriptedEntityTweenerService"));
  90. }
  91. void ScriptedEntityTweenerSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  92. {
  93. incompatible.push_back(AZ_CRC_CE("ScriptedEntityTweenerService"));
  94. }
  95. void ScriptedEntityTweenerSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  96. {
  97. (void)required;
  98. }
  99. void ScriptedEntityTweenerSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  100. {
  101. (void)dependent;
  102. }
  103. void ScriptedEntityTweenerSystemComponent::Init()
  104. {
  105. }
  106. void ScriptedEntityTweenerSystemComponent::Activate()
  107. {
  108. ScriptedEntityTweenerBus::Handler::BusConnect();
  109. AZ::TickBus::Handler::BusConnect();
  110. }
  111. void ScriptedEntityTweenerSystemComponent::Deactivate()
  112. {
  113. ScriptedEntityTweenerBus::Handler::BusDisconnect();
  114. AZ::TickBus::Handler::BusDisconnect();
  115. }
  116. void ScriptedEntityTweenerSystemComponent::AnimateEntity(const AZ::EntityId& entityId, const AnimationParameters& params)
  117. {
  118. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  119. if (animationTask == m_animationTasks.end())
  120. {
  121. animationTask = m_animationTasks.insert(ScriptedEntityTweenerTask(entityId)).first;
  122. }
  123. animationTask->AddAnimation(params);
  124. }
  125. void ScriptedEntityTweenerSystemComponent::SetOptionalParams(float timeIntoAnimation, float duration, int easingMethod, int easingType, float delayTime, int timesToPlay, bool isFrom, bool isPlayingBackward, const AZ::Uuid& animationId, int timelineId, int onCompleteCallbackId, int onUpdateCallbackId, int onLoopCallbackId)
  126. {
  127. AnimationParameters& params = m_tempParams;
  128. params.m_animationProperties.m_easeMethod = static_cast<EasingMethod>(easingMethod);
  129. params.m_animationProperties.m_easeType = static_cast<EasingType>(easingType);
  130. params.m_animationProperties.m_timeIntoAnimation = timeIntoAnimation;
  131. params.m_animationProperties.m_timeDuration = duration;
  132. params.m_animationProperties.m_timeToDelayAnim = delayTime;
  133. params.m_animationProperties.m_timesToPlay = timesToPlay;
  134. params.m_animationProperties.m_isFrom = isFrom;
  135. params.m_animationProperties.m_isPlayingBackward = isPlayingBackward;
  136. params.m_animationProperties.m_animationId = animationId;
  137. params.m_animationProperties.m_timelineId = timelineId;
  138. params.m_animationProperties.m_onCompleteCallbackId = onCompleteCallbackId;
  139. params.m_animationProperties.m_onUpdateCallbackId = onUpdateCallbackId;
  140. params.m_animationProperties.m_onLoopCallbackId = onLoopCallbackId;
  141. }
  142. void ScriptedEntityTweenerSystemComponent::AnimateEntityScript(const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, const AZStd::any& paramTarget)
  143. {
  144. AnimationParameters& params = m_tempParams;
  145. AnimationParameterAddressData data(componentName, virtualPropertyName);
  146. params.m_animationParameters.emplace(data, paramTarget);
  147. AnimateEntity(entityId, params);
  148. m_tempParams.Reset();
  149. }
  150. void ScriptedEntityTweenerSystemComponent::Stop(int timelineId, const AZ::EntityId& entityId)
  151. {
  152. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  153. if (animationTask == m_animationTasks.end())
  154. {
  155. return;
  156. }
  157. animationTask->Stop(timelineId);
  158. }
  159. void ScriptedEntityTweenerSystemComponent::Pause(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName)
  160. {
  161. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  162. if (animationTask == m_animationTasks.end())
  163. {
  164. return;
  165. }
  166. AnimationParameterAddressData data(componentName, virtualPropertyName);
  167. animationTask->SetPaused(data, timelineId, true);
  168. }
  169. void ScriptedEntityTweenerSystemComponent::Resume(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName)
  170. {
  171. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  172. if (animationTask == m_animationTasks.end())
  173. {
  174. return;
  175. }
  176. AnimationParameterAddressData data(componentName, virtualPropertyName);
  177. animationTask->SetPaused(data, timelineId, false);
  178. }
  179. void ScriptedEntityTweenerSystemComponent::SetPlayDirectionReversed(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, bool rewind)
  180. {
  181. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  182. if (animationTask == m_animationTasks.end())
  183. {
  184. return;
  185. }
  186. AnimationParameterAddressData data(componentName, virtualPropertyName);
  187. animationTask->SetPlayDirectionReversed(data, timelineId, rewind);
  188. }
  189. void ScriptedEntityTweenerSystemComponent::SetSpeed(int timelineId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, float speed)
  190. {
  191. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  192. if (animationTask == m_animationTasks.end())
  193. {
  194. return;
  195. }
  196. AnimationParameterAddressData data(componentName, virtualPropertyName);
  197. animationTask->SetSpeed(data, timelineId, speed);
  198. }
  199. void ScriptedEntityTweenerSystemComponent::SetInitialValue(const AZ::Uuid& animationId, const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName, const AZStd::any& initialValue)
  200. {
  201. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  202. if (animationTask == m_animationTasks.end())
  203. {
  204. return;
  205. }
  206. AnimationParameterAddressData data(componentName, virtualPropertyName);
  207. animationTask->SetInitialValue(data, animationId, initialValue);
  208. }
  209. AZStd::any ScriptedEntityTweenerSystemComponent::GetVirtualPropertyValue(const AZ::EntityId& entityId, const AZStd::string& componentName, const AZStd::string& virtualPropertyName)
  210. {
  211. AZStd::any toReturn;
  212. AnimationParameterAddressData data(componentName, virtualPropertyName);
  213. auto animationTask = m_animationTasks.find(ScriptedEntityTweenerTask(entityId));
  214. if (animationTask != m_animationTasks.end())
  215. {
  216. animationTask->GetVirtualPropertyValue(toReturn, data);
  217. }
  218. else
  219. {
  220. ScriptedEntityTweenerTask task(entityId);
  221. task.GetVirtualPropertyValue(toReturn, data);
  222. }
  223. return toReturn;
  224. }
  225. void ScriptedEntityTweenerSystemComponent::Reset()
  226. {
  227. m_animationTasks.clear();
  228. }
  229. void ScriptedEntityTweenerSystemComponent::OnTick(float deltaTime, AZ::ScriptTimePoint /*time*/)
  230. {
  231. for (auto& animTask : m_animationTasks)
  232. {
  233. animTask.Update(deltaTime);
  234. }
  235. //TODO: Possible optimization: Logic to remove "stale" animation tasks, use a "garbage collection" method to defer set removal operations (which is O(n))
  236. for (auto it = m_animationTasks.begin(); it != m_animationTasks.end(); )
  237. {
  238. if (!((*it).GetIsActive()))
  239. {
  240. it = m_animationTasks.erase(it);
  241. }
  242. else
  243. {
  244. ++it;
  245. }
  246. }
  247. }
  248. int ScriptedEntityTweenerSystemComponent::GetTickOrder()
  249. {
  250. return AZ::TICK_LAST;
  251. }
  252. }