SimplePlayerSpawnerTests.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <CommonBenchmarkSetup.h>
  8. #include <MockInterfaces.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzCore/UnitTest/UnitTest.h>
  12. #include <AzFramework/Components/TransformComponent.h>
  13. #include <AzTest/AzTest.h>
  14. #include <Multiplayer/Components/SimplePlayerSpawnerComponent.h>
  15. namespace Multiplayer
  16. {
  17. using namespace testing;
  18. using namespace ::UnitTest;
  19. class SimplePlayerSpawnerTests
  20. : public LeakDetectionFixture
  21. {
  22. public:
  23. void SetUp() override
  24. {
  25. m_componentApplicationRequests = AZStd::make_unique<BenchmarkComponentApplicationRequests>();
  26. AZ::Interface<AZ::ComponentApplicationRequests>::Register(m_componentApplicationRequests.get());
  27. // register components involved in testing
  28. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  29. m_transformDescriptor.reset(AzFramework::TransformComponent::CreateDescriptor());
  30. m_transformDescriptor->Reflect(m_serializeContext.get());
  31. m_simplePlayerSpawnerComponentDescriptor.reset(SimplePlayerSpawnerComponent::CreateDescriptor());
  32. m_simplePlayerSpawnerComponentDescriptor->Reflect(m_serializeContext.get());
  33. }
  34. void TearDown() override
  35. {
  36. AZ::Interface<AZ::ComponentApplicationRequests>::Unregister(m_componentApplicationRequests.get());
  37. m_componentApplicationRequests.reset();
  38. m_simplePlayerSpawnerComponentDescriptor.reset();
  39. m_transformDescriptor.reset();
  40. m_serializeContext.reset();
  41. }
  42. void CreateSimplePlayerSpawner(AZ::Entity& entity, AZStd::vector<AZ::EntityId> spawnPoints)
  43. {
  44. auto spawner = entity.CreateComponent<SimplePlayerSpawnerComponent>();
  45. spawner->m_spawnPoints = spawnPoints;
  46. entity.Init();
  47. entity.Activate();
  48. }
  49. void CreateSpawnPoint(AZ::Entity& spawnPointEntity, const AZ::Vector3& position)
  50. {
  51. auto transform = spawnPointEntity.CreateComponent<AzFramework::TransformComponent>();
  52. transform->SetWorldTM(AZ::Transform::CreateTranslation(position));
  53. spawnPointEntity.Init();
  54. spawnPointEntity.Activate();
  55. }
  56. AZStd::unique_ptr<BenchmarkComponentApplicationRequests> m_componentApplicationRequests;
  57. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  58. AZStd::unique_ptr<AZ::ComponentDescriptor> m_transformDescriptor;
  59. AZStd::unique_ptr<AZ::ComponentDescriptor> m_simplePlayerSpawnerComponentDescriptor;
  60. };
  61. TEST_F(SimplePlayerSpawnerTests, SpawnLocations)
  62. {
  63. AZ::Entity spawnPoint1(AZ::EntityId(1));
  64. AZ::Entity spawnPoint2(AZ::EntityId(2));
  65. AZ::Entity spawnPoint3(AZ::EntityId(3));
  66. CreateSpawnPoint(spawnPoint1, AZ::Vector3(1.0f, 0, 0));
  67. CreateSpawnPoint(spawnPoint2, AZ::Vector3(2.0f, 0, 0));
  68. CreateSpawnPoint(spawnPoint3, AZ::Vector3(3.0f, 0, 0));
  69. AZ::Entity simplePlayerSpawnerEntity;
  70. CreateSimplePlayerSpawner(simplePlayerSpawnerEntity, { spawnPoint1.GetId(), spawnPoint2.GetId(), spawnPoint3.GetId() });
  71. auto simplePlayerSpawner = AZ::Interface<ISimplePlayerSpawner>::Get();
  72. ASSERT_NE(simplePlayerSpawner, nullptr);
  73. uint32_t numSpawnPoints = simplePlayerSpawner->GetSpawnPointCount();
  74. EXPECT_EQ(numSpawnPoints, 3);
  75. uint32_t currentSpawnPointIndex = simplePlayerSpawner->GetNextSpawnPointIndex();
  76. EXPECT_EQ(currentSpawnPointIndex, 0);
  77. AZ::Transform nextSpawnPoint = simplePlayerSpawner->GetNextSpawnPoint();
  78. EXPECT_EQ(nextSpawnPoint.GetTranslation().GetX(), 1.0f);
  79. simplePlayerSpawner->SetNextSpawnPointIndex(++currentSpawnPointIndex);
  80. nextSpawnPoint = simplePlayerSpawner->GetNextSpawnPoint();
  81. EXPECT_EQ(nextSpawnPoint.GetTranslation().GetX(), 2.0f);
  82. simplePlayerSpawner->SetNextSpawnPointIndex(++currentSpawnPointIndex);
  83. nextSpawnPoint = simplePlayerSpawner->GetNextSpawnPoint();
  84. EXPECT_EQ(nextSpawnPoint.GetTranslation().GetX(), 3.0f);
  85. // Attempt to set out-of-bound spawn point and see that it's not saved
  86. simplePlayerSpawner->SetNextSpawnPointIndex(99);
  87. uint32_t spawnPointIndex = simplePlayerSpawner->GetNextSpawnPointIndex();
  88. EXPECT_EQ(spawnPointIndex, 2); // Spawn point stays at #3 (aka index 2)
  89. }
  90. TEST_F(SimplePlayerSpawnerTests, NoSpawnPoints)
  91. {
  92. AZ::Entity simplePlayerSpawnerEntity;
  93. CreateSimplePlayerSpawner(simplePlayerSpawnerEntity, {});
  94. auto simplePlayerSpawner = AZ::Interface<ISimplePlayerSpawner>::Get();
  95. ASSERT_NE(simplePlayerSpawner, nullptr);
  96. uint32_t numSpawnPoints = simplePlayerSpawner->GetSpawnPointCount();
  97. EXPECT_EQ(numSpawnPoints, 0);
  98. // No spawn points. The next spawn position will be the world origin.
  99. AZ::Transform nextSpawnPoint = simplePlayerSpawner->GetNextSpawnPoint();
  100. EXPECT_EQ(nextSpawnPoint, AZ::Transform::CreateIdentity());
  101. }
  102. } // namespace Multiplayer