ActorComponentRagdollTests.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/Component/Entity.h>
  9. #include <AzFramework/Components/TransformComponent.h>
  10. #include <Integration/Components/ActorComponent.h>
  11. #include <EMotionFX/Source/Allocators.h>
  12. #include <EMotionFX/Source/Node.h>
  13. #include <Tests/Integration/EntityComponentFixture.h>
  14. #include <Tests/TestAssetCode/JackActor.h>
  15. #include <Tests/TestAssetCode/TestActorAssets.h>
  16. #include <Tests/TestAssetCode/ActorFactory.h>
  17. #include <Tests/Mocks/PhysicsSystem.h>
  18. #include <Tests/Mocks/PhysicsRagdoll.h>
  19. namespace EMotionFX
  20. {
  21. class TestRagdollPhysicsRequestHandler
  22. : public AzFramework::RagdollPhysicsRequestBus::Handler
  23. {
  24. public:
  25. TestRagdollPhysicsRequestHandler(Physics::Ragdoll* ragdoll, const AZ::EntityId& entityId)
  26. {
  27. m_ragdoll = ragdoll;
  28. AzFramework::RagdollPhysicsRequestBus::Handler::BusConnect(entityId);
  29. }
  30. ~TestRagdollPhysicsRequestHandler()
  31. {
  32. AzFramework::RagdollPhysicsRequestBus::Handler::BusDisconnect();
  33. }
  34. void EnableSimulation([[maybe_unused]] const Physics::RagdollState& initialState) override {}
  35. void EnableSimulationQueued([[maybe_unused]]const Physics::RagdollState& initialState) override {}
  36. void DisableSimulation() override {}
  37. void DisableSimulationQueued() override {}
  38. Physics::Ragdoll* GetRagdoll() override { return m_ragdoll; }
  39. void GetState([[maybe_unused]] Physics::RagdollState& ragdollState) const override {}
  40. void SetState([[maybe_unused]] const Physics::RagdollState& ragdollState) override {}
  41. void SetStateQueued([[maybe_unused]] const Physics::RagdollState& ragdollState) override {}
  42. void GetNodeState([[maybe_unused]] size_t nodeIndex, [[maybe_unused]] Physics::RagdollNodeState& nodeState) const override {}
  43. void SetNodeState([[maybe_unused]] size_t nodeIndex, [[maybe_unused]] const Physics::RagdollNodeState& nodeState) override {}
  44. Physics::RagdollNode* GetNode([[maybe_unused]] size_t nodeIndex) const override { return nullptr; }
  45. void EnterRagdoll() override {}
  46. void ExitRagdoll() override {}
  47. private:
  48. Physics::Ragdoll* m_ragdoll = nullptr;
  49. };
  50. TEST_F(EntityComponentFixture, ActorComponent_ActivateRagdoll)
  51. {
  52. AZ::EntityId entityId(740216387);
  53. AzPhysics::SceneEvents::OnSceneSimulationFinishEvent sceneFinishSimEvent;
  54. Physics::MockPhysicsSceneInterface mockSceneInterface;
  55. EXPECT_CALL(mockSceneInterface, RegisterSceneSimulationFinishHandler)
  56. .WillRepeatedly([&sceneFinishSimEvent](
  57. [[maybe_unused]]AzPhysics::SceneHandle sceneHandle,
  58. AzPhysics::SceneEvents::OnSceneSimulationFinishHandler& handler)
  59. {
  60. handler.Connect(sceneFinishSimEvent);
  61. });
  62. TestRagdoll testRagdoll;
  63. TestRagdollPhysicsRequestHandler ragdollPhysicsRequestHandler(&testRagdoll, entityId);
  64. EXPECT_CALL(testRagdoll, GetState(::testing::_)).Times(::testing::AnyNumber());
  65. EXPECT_CALL(testRagdoll, GetNumNodes()).WillRepeatedly(::testing::Return(1));
  66. EXPECT_CALL(testRagdoll, IsSimulated()).WillRepeatedly(::testing::Return(true));
  67. EXPECT_CALL(testRagdoll, GetEntityId()).WillRepeatedly(::testing::Return(entityId));
  68. EXPECT_CALL(testRagdoll, GetPosition()).WillRepeatedly(::testing::Return(AZ::Vector3::CreateZero()));
  69. EXPECT_CALL(testRagdoll, GetOrientation()).WillRepeatedly(::testing::Return(AZ::Quaternion::CreateIdentity()));
  70. auto gameEntity = AZStd::make_unique<AZ::Entity>();
  71. gameEntity->SetId(entityId);
  72. AZ::Data::AssetId actorAssetId("{5060227D-B6F4-422E-BF82-41AAC5F228A5}");
  73. AZStd::unique_ptr<Actor> actor = ActorFactory::CreateAndInit<JackNoMeshesActor>();
  74. AZ::Data::Asset<Integration::ActorAsset> actorAsset = TestActorAssets::GetAssetFromActor(actorAssetId, AZStd::move(actor));
  75. Integration::ActorComponent::Configuration actorConf;
  76. actorConf.m_actorAsset = actorAsset;
  77. gameEntity->CreateComponent<AzFramework::TransformComponent>();
  78. auto actorComponent = gameEntity->CreateComponent<Integration::ActorComponent>(&actorConf);
  79. gameEntity->Init();
  80. gameEntity->Activate();
  81. actorComponent->SetActorAsset(actorAsset);
  82. EXPECT_FALSE(actorComponent->IsPhysicsSceneSimulationFinishEventConnected())
  83. << "Scene Finish Simulation handler should not be connected directly after creating the actor instance.";
  84. actorComponent->OnRagdollActivated();
  85. EXPECT_TRUE(actorComponent->IsPhysicsSceneSimulationFinishEventConnected())
  86. << "Scene Finish Simulation handler should be connected after activating the ragdoll.";
  87. actorComponent->OnRagdollDeactivated();
  88. EXPECT_FALSE(actorComponent->IsPhysicsSceneSimulationFinishEventConnected())
  89. << "Scene Finish Simulation handler should not be connected after deactivating the ragdoll.";
  90. actorComponent->OnRagdollActivated();
  91. EXPECT_TRUE(actorComponent->IsPhysicsSceneSimulationFinishEventConnected())
  92. << "Scene Finish Simulation handler should be connected after activating the ragdoll.";
  93. gameEntity->Deactivate();
  94. EXPECT_FALSE(actorComponent->IsPhysicsSceneSimulationFinishEventConnected())
  95. << "Scene Finish Simulation handler should not be connected anymore after deactivating the entire entity.";
  96. }
  97. }