SimpleMotionComponentBusTests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 <AzFramework/Components/TransformComponent.h>
  9. #include <EMotionFX/Source/AnimGraph.h>
  10. #include <EMotionFX/Source/AnimGraphMotionNode.h>
  11. #include <EMotionFX/Source/AnimGraphStateMachine.h>
  12. #include <EMotionFX/Source/BlendTree.h>
  13. #include <EMotionFX/Source/MotionSet.h>
  14. #include <EMotionFX/Source/Motion.h>
  15. #include <EMotionFX/Source/MotionInstance.h>
  16. #include <Integration/Components/ActorComponent.h>
  17. #include <Integration/Components/AnimGraphComponent.h>
  18. #include <Integration/Components/SimpleMotionComponent.h>
  19. #include <Tests/AnimGraphFixture.h>
  20. #include <Tests/Integration/EntityComponentFixture.h>
  21. #include <Tests/TestAssetCode/ActorFactory.h>
  22. #include <Tests/TestAssetCode/JackActor.h>
  23. #include <Tests/TestAssetCode/TestActorAssets.h>
  24. #include <Tests/TestAssetCode/TestMotionAssets.h>
  25. namespace EMotionFX
  26. {
  27. class SimpleMotionComponentBusTests
  28. : public EntityComponentFixture
  29. {
  30. public:
  31. void SetUp() override
  32. {
  33. EntityComponentFixture::SetUp();
  34. m_entity = AZStd::make_unique<AZ::Entity>();
  35. m_entityId = AZ::EntityId(740216387);
  36. m_entity->SetId(m_entityId);
  37. // Actor asset.
  38. AZ::Data::AssetId actorAssetId("{5060227D-B6F4-422E-BF82-41AAC5F228A5}");
  39. AZStd::unique_ptr<Actor> actor = ActorFactory::CreateAndInit<JackNoMeshesActor>();
  40. AZ::Data::Asset<Integration::ActorAsset> actorAsset = TestActorAssets::GetAssetFromActor(actorAssetId, AZStd::move(actor));
  41. Integration::ActorComponent::Configuration actorConf;
  42. actorConf.m_actorAsset = actorAsset;
  43. m_entity->CreateComponent<AzFramework::TransformComponent>();
  44. auto actorComponent = m_entity->CreateComponent<Integration::ActorComponent>(&actorConf);
  45. m_simpleMotionComponent = m_entity->CreateComponent<Integration::SimpleMotionComponent>();
  46. m_entity->Init();
  47. // Motion asset.
  48. m_motionAssetId = AZ::Data::AssetId("{F3CDBB53-D793-449F-A086-2821680E3C38}");
  49. m_simpleMotionComponent->SetMotionAssetId(m_motionAssetId);
  50. AZ::Data::Asset<Integration::MotionAsset> motionAsset = AZ::Data::AssetManager::Instance().CreateAsset<Integration::MotionAsset>(m_motionAssetId);
  51. m_motionSet = aznew MotionSet("motionSet");
  52. Motion* motion = TestMotionAssets::GetJackWalkForward();
  53. AddMotionEntry(motion, "jack_walk_forward_aim_zup");
  54. motionAsset.GetAs<Integration::MotionAsset>()->SetData(motion);
  55. m_simpleMotionComponent->OnAssetReady(motionAsset);
  56. // Actor component
  57. m_entity->Activate();
  58. actorComponent->SetActorAsset(actorAsset);
  59. }
  60. void AddMotionEntry(Motion* motion, const AZStd::string& motionId)
  61. {
  62. m_motion = motion;
  63. EMotionFX::MotionSet::MotionEntry* newMotionEntry = aznew EMotionFX::MotionSet::MotionEntry();
  64. newMotionEntry->SetMotion(m_motion);
  65. m_motionSet->AddMotionEntry(newMotionEntry);
  66. m_motionSet->SetMotionEntryId(newMotionEntry, motionId);
  67. }
  68. void TearDown() override
  69. {
  70. m_motionSet->Clear();
  71. // m_motion->Destroy(); // Will be destroyed through the m_entity destructor
  72. delete m_motionSet;
  73. m_entity.reset();
  74. EntityComponentFixture::TearDown();
  75. }
  76. AZ::Data::AssetId m_motionAssetId;
  77. AZ::EntityId m_entityId;
  78. AZStd::unique_ptr<AZ::Entity> m_entity;
  79. Integration::SimpleMotionComponent* m_simpleMotionComponent = nullptr;
  80. MotionSet* m_motionSet = nullptr;
  81. Motion* m_motion = nullptr;
  82. };
  83. // Test GetMotion
  84. TEST_F(SimpleMotionComponentBusTests, GetMotionTest)
  85. {
  86. AZ::Data::AssetId motionAssetId;
  87. Integration::SimpleMotionComponentRequestBus::EventResult(
  88. motionAssetId,
  89. m_entityId,
  90. &Integration::SimpleMotionComponentRequestBus::Events::GetMotion
  91. );
  92. EXPECT_EQ(m_motionAssetId, motionAssetId);
  93. }
  94. // Test LoopMotion and GetLoopMotion
  95. TEST_F(SimpleMotionComponentBusTests, LoopMotionTest)
  96. {
  97. bool loopMotion;
  98. Integration::SimpleMotionComponentRequestBus::EventResult(
  99. loopMotion,
  100. m_entityId,
  101. &Integration::SimpleMotionComponentRequestBus::Events::GetLoopMotion
  102. );
  103. EXPECT_FALSE(loopMotion);
  104. Integration::SimpleMotionComponentRequestBus::Event(
  105. m_entityId,
  106. &Integration::SimpleMotionComponentRequestBus::Events::LoopMotion,
  107. true
  108. );
  109. Integration::SimpleMotionComponentRequestBus::EventResult(
  110. loopMotion,
  111. m_entityId,
  112. &Integration::SimpleMotionComponentRequestBus::Events::GetLoopMotion
  113. );
  114. EXPECT_TRUE(loopMotion);
  115. }
  116. // Test SetPlaySpeed and GetPlaySpeed
  117. TEST_F(SimpleMotionComponentBusTests, PlaySpeedTest)
  118. {
  119. float playSpeed;
  120. const float defaultPlaySpeed = 1.0f;
  121. const float expectedPlaySpeed = 2.0f;
  122. Integration::SimpleMotionComponentRequestBus::EventResult(
  123. playSpeed,
  124. m_entityId,
  125. &Integration::SimpleMotionComponentRequestBus::Events::GetPlaySpeed
  126. );
  127. EXPECT_EQ(playSpeed, defaultPlaySpeed);
  128. Integration::SimpleMotionComponentRequestBus::Event(
  129. m_entityId,
  130. &Integration::SimpleMotionComponentRequestBus::Events::SetPlaySpeed,
  131. expectedPlaySpeed
  132. );
  133. Integration::SimpleMotionComponentRequestBus::EventResult(
  134. playSpeed,
  135. m_entityId,
  136. &Integration::SimpleMotionComponentRequestBus::Events::GetPlaySpeed
  137. );
  138. EXPECT_EQ(playSpeed, expectedPlaySpeed);
  139. }
  140. // Test GetPlayTime and PlayTime
  141. TEST_F(SimpleMotionComponentBusTests, PlayTimeTest)
  142. {
  143. float playTime;
  144. const float defaultPlayTime = 0.0f;
  145. const float expectedPlayTime = 1.5f;
  146. const float err_margin = 0.1f;
  147. Integration::SimpleMotionComponentRequestBus::EventResult(
  148. playTime,
  149. m_entityId,
  150. &Integration::SimpleMotionComponentRequestBus::Events::GetPlayTime
  151. );
  152. EXPECT_EQ(playTime, defaultPlayTime);
  153. Integration::SimpleMotionComponentRequestBus::Event(
  154. m_entityId,
  155. &Integration::SimpleMotionComponentRequestBus::Events::PlayTime,
  156. expectedPlayTime
  157. );
  158. Integration::SimpleMotionComponentRequestBus::EventResult(
  159. playTime,
  160. m_entityId,
  161. &Integration::SimpleMotionComponentRequestBus::Events::GetPlayTime
  162. );
  163. EXPECT_NEAR(playTime, expectedPlayTime, err_margin);
  164. }
  165. // Test BlendInTime and GetBlendInTime
  166. TEST_F(SimpleMotionComponentBusTests, BlendInTimeTest)
  167. {
  168. float blendInTime;
  169. const float defaultBlendInTime = 0.0f;
  170. const float expectedBlendInTime = 1.0f;
  171. Integration::SimpleMotionComponentRequestBus::EventResult(
  172. blendInTime,
  173. m_entityId,
  174. &Integration::SimpleMotionComponentRequestBus::Events::GetBlendInTime
  175. );
  176. EXPECT_EQ(blendInTime, defaultBlendInTime);
  177. Integration::SimpleMotionComponentRequestBus::Event(
  178. m_entityId,
  179. &Integration::SimpleMotionComponentRequestBus::Events::BlendInTime,
  180. expectedBlendInTime
  181. );
  182. Integration::SimpleMotionComponentRequestBus::EventResult(
  183. blendInTime,
  184. m_entityId,
  185. &Integration::SimpleMotionComponentRequestBus::Events::GetBlendInTime
  186. );
  187. EXPECT_EQ(blendInTime, expectedBlendInTime);
  188. }
  189. // Test BlendInTime and GetBlendInTime
  190. TEST_F(SimpleMotionComponentBusTests, BlendOutTimeTest)
  191. {
  192. float blendOutTime;
  193. const float defaultBlendOutTime = 0.0f;
  194. const float expectedBlendOutTime = 1.0f;
  195. Integration::SimpleMotionComponentRequestBus::EventResult(
  196. blendOutTime,
  197. m_entityId,
  198. &Integration::SimpleMotionComponentRequestBus::Events::GetBlendOutTime
  199. );
  200. EXPECT_EQ(blendOutTime, defaultBlendOutTime);
  201. Integration::SimpleMotionComponentRequestBus::Event(
  202. m_entityId,
  203. &Integration::SimpleMotionComponentRequestBus::Events::BlendOutTime,
  204. expectedBlendOutTime
  205. );
  206. Integration::SimpleMotionComponentRequestBus::EventResult(
  207. blendOutTime,
  208. m_entityId,
  209. &Integration::SimpleMotionComponentRequestBus::Events::GetBlendOutTime
  210. );
  211. EXPECT_EQ(blendOutTime, expectedBlendOutTime);
  212. }
  213. // Test PlayMotion
  214. TEST_F(SimpleMotionComponentBusTests, PlayMotionTest)
  215. {
  216. const MotionInstance* motionInstance = m_simpleMotionComponent->GetMotionInstance();
  217. EXPECT_NE(motionInstance, nullptr);
  218. Integration::SimpleMotionComponentRequestBus::Event(
  219. m_entityId,
  220. &Integration::SimpleMotionComponentRequestBus::Events::PlayMotion
  221. );
  222. const MotionInstance* motionInstanceAfterPlayMotion = m_simpleMotionComponent->GetMotionInstance();
  223. EXPECT_NE(motionInstanceAfterPlayMotion, nullptr);
  224. }
  225. // Test MirrorMotion
  226. TEST_F(SimpleMotionComponentBusTests, MirrorMotionTest)
  227. {
  228. const MotionInstance* motionInstance = m_simpleMotionComponent->GetMotionInstance();
  229. bool mirrorMotion = motionInstance->GetMirrorMotion();
  230. EXPECT_FALSE(mirrorMotion);
  231. Integration::SimpleMotionComponentRequestBus::Event(
  232. m_entityId,
  233. &Integration::SimpleMotionComponentRequestBus::Events::MirrorMotion,
  234. true
  235. );
  236. mirrorMotion = motionInstance->GetMirrorMotion();
  237. EXPECT_TRUE(mirrorMotion);
  238. }
  239. // Test RetargetingMotion
  240. TEST_F(SimpleMotionComponentBusTests, RetargetingMotionTest)
  241. {
  242. const MotionInstance* motionInstance = m_simpleMotionComponent->GetMotionInstance();
  243. bool retargetMotion = motionInstance->GetRetargetingEnabled();
  244. EXPECT_FALSE(retargetMotion);
  245. Integration::SimpleMotionComponentRequestBus::Event(
  246. m_entityId,
  247. &Integration::SimpleMotionComponentRequestBus::Events::RetargetMotion,
  248. true
  249. );
  250. retargetMotion = motionInstance->GetRetargetingEnabled();
  251. EXPECT_TRUE(retargetMotion);
  252. }
  253. // Test ReverseMotion
  254. TEST_F(SimpleMotionComponentBusTests, ReverseMotionTest)
  255. {
  256. const MotionInstance* motionInstance = m_simpleMotionComponent->GetMotionInstance();
  257. EMotionFX::EPlayMode reverseMotion = motionInstance->GetPlayMode();
  258. EXPECT_EQ(reverseMotion, EMotionFX::EPlayMode::PLAYMODE_FORWARD);
  259. Integration::SimpleMotionComponentRequestBus::Event(
  260. m_entityId,
  261. &Integration::SimpleMotionComponentRequestBus::Events::ReverseMotion,
  262. true
  263. );
  264. reverseMotion = motionInstance->GetPlayMode();
  265. EXPECT_EQ(reverseMotion, EMotionFX::EPlayMode::PLAYMODE_BACKWARD);
  266. }
  267. } // end namespace EMotionFX