NetworkCharacterTests.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <CommonHierarchySetup.h>
  8. #include <MockInterfaces.h>
  9. #include <AzCore/Asset/AssetManager.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/Console/Console.h>
  12. #include <AzCore/Name/Name.h>
  13. #include <AzCore/std/smart_ptr/make_shared.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. #include <AzCore/UnitTest/UnitTest.h>
  16. #include <AzFramework/Components/TransformComponent.h>
  17. #include <AzFramework/Physics/Material/PhysicsMaterialSystemComponent.h>
  18. #include <AzFramework/Visibility/EntityVisibilityBoundsUnionSystem.h>
  19. #include <AzTest/AzTest.h>
  20. #include <Multiplayer/Components/NetBindComponent.h>
  21. #include <Multiplayer/Components/NetworkCharacterComponent.h>
  22. #include <Multiplayer/NetworkEntity/EntityReplication/EntityReplicator.h>
  23. #include <Source/SystemComponent.h>
  24. #include <Source/System/PhysXCookingParams.h>
  25. #include <Source/System/PhysXSystem.h>
  26. #include <PhysXCharacters/Components/CharacterControllerComponent.h>
  27. namespace Multiplayer
  28. {
  29. using namespace testing;
  30. using namespace ::UnitTest;
  31. /*
  32. * (Networked) Parent -> (Networked) Child
  33. */
  34. class NetworkCharacterTests : public HierarchyTests
  35. {
  36. public:
  37. void SetUp() override
  38. {
  39. HierarchyTests::SetUp();
  40. // create the asset database
  41. {
  42. AZ::Data::AssetManager::Descriptor desc;
  43. AZ::Data::AssetManager::Create(desc);
  44. }
  45. m_systemEntity = new AZ::Entity();
  46. m_physXSystem = AZStd::make_unique<PhysX::PhysXSystem>(AZStd::make_unique<PhysX::PhysXSettingsRegistryManager>(), PhysX::PxCooking::GetRealTimeCookingParams());
  47. m_physMaterialSystemDescriptor.reset(Physics::MaterialSystemComponent::CreateDescriptor());
  48. m_physMaterialSystemDescriptor->Reflect(m_serializeContext.get());
  49. m_physXSystemDescriptor.reset(PhysX::SystemComponent::CreateDescriptor());
  50. m_physXSystemDescriptor->Reflect(m_serializeContext.get());
  51. m_systemEntity->CreateComponent<Physics::MaterialSystemComponent>();
  52. m_systemEntity->CreateComponent<PhysX::SystemComponent>();
  53. m_systemEntity->Init();
  54. m_systemEntity->Activate();
  55. m_visisbilitySystem = AZStd::make_unique<AzFramework::EntityVisibilityBoundsUnionSystem>();
  56. m_visisbilitySystem->Connect();
  57. AZ::EntityBus::Broadcast(&AZ::EntityBus::Events::OnEntityActivated, m_systemEntity->GetId());
  58. AzFramework::GameEntityContextEventBus::Broadcast(&AzFramework::GameEntityContextEventBus::Events::OnPreGameEntitiesStarted);
  59. m_charControllerDescriptor.reset(PhysX::CharacterControllerComponent::CreateDescriptor());
  60. m_charControllerDescriptor->Reflect(m_serializeContext.get());
  61. m_netCharDescriptor.reset(NetworkCharacterComponent::CreateDescriptor());
  62. m_netCharDescriptor->Reflect(m_serializeContext.get());
  63. m_root = AZStd::make_unique<EntityInfo>(1, "root", NetEntityId{ 1 }, EntityInfo::Role::Root);
  64. m_child = AZStd::make_unique<EntityInfo>(2, "child", NetEntityId{ 2 }, EntityInfo::Role::Child);
  65. CreateNetworkParentChild(*m_root, *m_child);
  66. AZ::Transform rootTransform = AZ::Transform::CreateIdentity();
  67. rootTransform.SetTranslation(AZ::Vector3::CreateOne());
  68. m_root->m_entity->FindComponent<AzFramework::TransformComponent>()->SetWorldTM(rootTransform);
  69. m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetWorldTM(rootTransform);
  70. m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(m_root->m_entity->GetId());
  71. m_child->m_entity->FindComponent<AzFramework::TransformComponent>()->SetLocalTM(AZ::Transform::CreateIdentity());
  72. AZ::EntityBus::Broadcast(&AZ::EntityBus::Events::OnEntityActivated, m_root->m_entity->GetId());
  73. }
  74. void TearDown() override
  75. {
  76. m_child.reset();
  77. m_root.reset();
  78. m_visisbilitySystem->Disconnect();
  79. m_visisbilitySystem.reset();
  80. m_systemEntity->Deactivate();
  81. delete m_systemEntity;
  82. m_physXSystem.reset();
  83. m_netCharDescriptor.reset();
  84. m_charControllerDescriptor.reset();
  85. m_physXSystemDescriptor.reset();
  86. m_physMaterialSystemDescriptor.reset();
  87. AZ::Data::AssetManager::Destroy();
  88. HierarchyTests::TearDown();
  89. }
  90. void PopulateNetworkEntity(const EntityInfo& entityInfo)
  91. {
  92. entityInfo.m_entity->CreateComponent<AzFramework::TransformComponent>();
  93. entityInfo.m_entity->CreateComponent<NetBindComponent>();
  94. entityInfo.m_entity->CreateComponent<NetworkTransformComponent>();
  95. entityInfo.m_entity->CreateComponent<PhysX::CharacterControllerComponent>(
  96. AZStd::make_unique<Physics::CharacterConfiguration>(),
  97. AZStd::make_shared<Physics::BoxShapeConfiguration>());
  98. entityInfo.m_entity->CreateComponent<NetworkCharacterComponent>();
  99. }
  100. void CreateNetworkParentChild(EntityInfo& root, EntityInfo& child)
  101. {
  102. PopulateNetworkEntity(root);
  103. SetupEntity(root.m_entity, root.m_netId, NetEntityRole::Authority);
  104. PopulateNetworkEntity(child);
  105. SetupEntity(child.m_entity, child.m_netId, NetEntityRole::Authority);
  106. // Create an entity replicator for the child entity
  107. const NetworkEntityHandle childHandle(child.m_entity.get(), m_networkEntityTracker.get());
  108. child.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, childHandle);
  109. child.m_replicator->Initialize(childHandle);
  110. // Create an entity replicator for the root entity
  111. const NetworkEntityHandle rootHandle(root.m_entity.get(), m_networkEntityTracker.get());
  112. root.m_replicator = AZStd::make_unique<EntityReplicator>(*m_entityReplicationManager, m_mockConnection.get(), NetEntityRole::Client, rootHandle);
  113. root.m_replicator->Initialize(rootHandle);
  114. root.m_entity->Activate();
  115. child.m_entity->Activate();
  116. }
  117. AZStd::unique_ptr<AZ::ComponentDescriptor> m_physMaterialSystemDescriptor;
  118. AZStd::unique_ptr<AZ::ComponentDescriptor> m_physXSystemDescriptor;
  119. AZStd::unique_ptr<AZ::ComponentDescriptor> m_charControllerDescriptor;
  120. AZStd::unique_ptr<AZ::ComponentDescriptor> m_netCharDescriptor;
  121. AZStd::unique_ptr<PhysX::PhysXSystem> m_physXSystem;
  122. AZStd::unique_ptr<AzFramework::EntityVisibilityBoundsUnionSystem> m_visisbilitySystem;
  123. AZ::Entity* m_systemEntity;
  124. AZStd::unique_ptr<EntityInfo> m_root;
  125. AZStd::unique_ptr<EntityInfo> m_child;
  126. };
  127. TEST_F(NetworkCharacterTests, TestMoveWithVelocity)
  128. {
  129. NetworkCharacterComponentController* controller = dynamic_cast<NetworkCharacterComponentController*>(
  130. m_root->m_entity->FindComponent<NetworkCharacterComponent>()->GetController());
  131. // NO_COUNT suppression here as we expect a MATH_ASSERT which is disabled in Profile
  132. AZ_TEST_START_TRACE_SUPPRESSION;
  133. controller->TryMoveWithVelocity(AZ::Vector3(100.f,100.f,100.f), 1.f);
  134. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT;
  135. m_root->m_entity->FindComponent<NetBindComponent>()->NotifySyncRewindState();
  136. }
  137. }