NetworkTestSpawnerComponent.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <NetworkPrefabSpawnerInterface.h>
  8. #include <AzCore/Component/TransformBus.h>
  9. #include <AzCore/Math/Random.h>
  10. #include <Components/NetworkRandomComponent.h>
  11. #include <Components/PerfTest/NetworkTestSpawnerComponent.h>
  12. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  13. #include <Multiplayer/IMultiplayer.h>
  14. #include <Multiplayer/Components/NetBindComponent.h>
  15. #include <Source/AutoGen/NetworkRandomComponent.AutoComponent.h>
  16. #include "NetworkPrefabSpawnerComponent.h"
  17. namespace MultiplayerSample
  18. {
  19. NetworkTestSpawnerComponentController::NetworkTestSpawnerComponentController(NetworkTestSpawnerComponent& parent)
  20. : NetworkTestSpawnerComponentControllerBase(parent)
  21. , m_tickEvent{ [this] { TickEvent(); }, AZ::Name{ "NetworkTestSpawnerComponent" } }
  22. {
  23. }
  24. void NetworkTestSpawnerComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  25. {
  26. if (GetParent().GetNetworkPrefabSpawnerComponent())
  27. {
  28. m_tickEvent.Enqueue(AZ::TimeMs{ 0 }, true);
  29. }
  30. m_currentCount = 0;
  31. m_accumulatedTime = 0.f;
  32. m_sinceLastSpawn = 0.f;
  33. }
  34. void NetworkTestSpawnerComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  35. {
  36. }
  37. void NetworkTestSpawnerComponentController::TickEvent()
  38. {
  39. const float deltaTime = static_cast<float>(m_tickEvent.TimeInQueueMs()) / 1000.f;
  40. m_accumulatedTime += deltaTime;
  41. if (m_accumulatedTime > 1.0f / aznumeric_cast<float>(GetParent().GetSpawnPerSecond()))
  42. {
  43. m_accumulatedTime = 0.f;
  44. AZ::Vector3 randomPoint = AZ::Vector3::CreateZero();
  45. // ShapeComponentRequestsBus is designed in such a way that it's very difficult to use direct component interface instead of the EBus
  46. using ShapeBus = LmbrCentral::ShapeComponentRequestsBus;
  47. ShapeBus::EventResult(randomPoint, GetParent().GetEntityId(), &ShapeBus::Events::GenerateRandomPointInside,
  48. AZ::RandomDistributionType::UniformReal);
  49. AZ::Transform t = GetEntity()->GetTransform()->GetWorldTM();
  50. if (!randomPoint.IsZero())
  51. {
  52. t.SetTranslation(randomPoint);
  53. // Create a random orientation for fun.
  54. float randomAngles[3];
  55. randomAngles[0] = aznumeric_cast<float>(GetNetworkRandomComponentController()->GetRandomUint64() % 180);
  56. randomAngles[1] = aznumeric_cast<float>(GetNetworkRandomComponentController()->GetRandomUint64() % 180);
  57. randomAngles[2] = aznumeric_cast<float>(GetNetworkRandomComponentController()->GetRandomUint64() % 180);
  58. t.SetRotation(AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3::CreateFromFloat3(randomAngles)));
  59. }
  60. PrefabCallbacks callbacks;
  61. callbacks.m_onActivateCallback = [this](
  62. AZStd::shared_ptr<AzFramework::EntitySpawnTicket>&& ticket,
  63. [[maybe_unused]] AzFramework::SpawnableConstEntityContainerView view)
  64. {
  65. m_spawnedObjects.push_back(move(ticket));
  66. };
  67. GetParent().GetNetworkPrefabSpawnerComponent()->SpawnDefaultPrefab(t, callbacks);
  68. m_currentCount++;
  69. if (m_currentCount >= GetParent().GetMaxLiveCount())
  70. {
  71. if (GetParent().GetRespawnEnabled())
  72. {
  73. m_spawnedObjects.pop_front(); // this destroys the prefab instance for this ticket
  74. --m_currentCount;
  75. }
  76. else
  77. {
  78. m_tickEvent.RemoveFromQueue();
  79. }
  80. }
  81. }
  82. }
  83. }