SimulationManager.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "SimulationManager.h"
  9. #include "SimulationInterfaces/SimulationFeatures.h"
  10. #include <AzCore/Component/ComponentApplicationBus.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzFramework/Components/ConsoleBus.h>
  13. #include <AzFramework/Physics/PhysicsSystem.h>
  14. #include <SimulationInterfaces/SimulationFeaturesAggregatorRequestBus.h>
  15. #include <SimulationInterfaces/SimulationInterfacesTypeIds.h>
  16. namespace SimulationInterfaces
  17. {
  18. AZ_COMPONENT_IMPL(SimulationManager, "SimulationManager", SimulationManagerTypeId);
  19. void SimulationManager::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<SimulationManager, AZ::Component>()->Version(0);
  24. }
  25. }
  26. void SimulationManager::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  27. {
  28. provided.push_back(AZ_CRC_CE("SimulationManagerService"));
  29. }
  30. void SimulationManager::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  31. {
  32. incompatible.push_back(AZ_CRC_CE("SimulationManagerService"));
  33. }
  34. void SimulationManager::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  35. {
  36. required.push_back(AZ_CRC_CE("PhysicsService"));
  37. required.push_back(AZ_CRC_CE("SimulationFeaturesAggregator"));
  38. }
  39. void SimulationManager::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  40. {
  41. dependent.push_back(AZ_CRC_CE("SimulationFeaturesAggregator"));
  42. }
  43. SimulationManager::SimulationManager()
  44. {
  45. if (SimulationManagerRequestBusInterface::Get() == nullptr)
  46. {
  47. SimulationManagerRequestBusInterface::Register(this);
  48. }
  49. }
  50. SimulationManager::~SimulationManager()
  51. {
  52. if (SimulationManagerRequestBusInterface::Get() == this)
  53. {
  54. SimulationManagerRequestBusInterface::Unregister(this);
  55. }
  56. }
  57. void SimulationManager::Init()
  58. {
  59. }
  60. void SimulationManager::Activate()
  61. {
  62. AzFramework::LevelSystemLifecycleNotificationBus::Handler::BusDisconnect();
  63. SimulationManagerRequestBus::Handler::BusConnect();
  64. SimulationFeaturesAggregatorRequestBus::Broadcast(
  65. &SimulationFeaturesAggregatorRequests::AddSimulationFeatures,
  66. AZStd::unordered_set<SimulationFeatures>{
  67. SimulationFeatures::SIMULATION_RESET,
  68. SimulationFeatures::SIMULATION_RESET_TIME,
  69. //SimulationFeatures::SIMULATION_RESET_STATE,
  70. SimulationFeatures::SIMULATION_RESET_SPAWNED,
  71. SimulationFeatures::SIMULATION_STATE_PAUSE,
  72. SimulationFeatures::STEP_SIMULATION_SINGLE,
  73. SimulationFeatures::STEP_SIMULATION_MULTIPLE,
  74. SimulationFeatures::STEP_SIMULATION_ACTION});
  75. }
  76. void SimulationManager::Deactivate()
  77. {
  78. SimulationManagerRequestBus::Handler::BusDisconnect();
  79. }
  80. bool SimulationManager::IsSimulationPaused() const
  81. {
  82. return m_isSimulationPaused;
  83. }
  84. bool SimulationManager::IsSimulationStepsActive() const
  85. {
  86. return m_simulationFinishEvent.IsConnected();
  87. }
  88. void SimulationManager::CancelStepSimulation()
  89. {
  90. if (m_simulationFinishEvent.IsConnected())
  91. {
  92. m_simulationFinishEvent.Disconnect();
  93. SetSimulationPaused(true);
  94. m_numberOfPhysicsSteps = 0;
  95. }
  96. }
  97. void SimulationManager::SetSimulationPaused(bool paused)
  98. {
  99. // get az physics system
  100. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  101. AZ_Assert(physicsSystem, "Physics system is not available");
  102. const auto& sceneHandlers = physicsSystem->GetAllScenes();
  103. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  104. AZ_Assert(sceneInterface, "Physics scene interface is not available");
  105. for (auto& scene : sceneHandlers)
  106. {
  107. AZ_Assert(scene, "Physics scene is not available");
  108. scene->SetEnabled(!paused);
  109. m_isSimulationPaused = paused;
  110. }
  111. }
  112. void SimulationManager::StepSimulation(AZ::u64 steps)
  113. {
  114. if (steps == 0)
  115. {
  116. return;
  117. }
  118. m_numberOfPhysicsSteps = steps;
  119. // install handler
  120. m_simulationFinishEvent = AzPhysics::SceneEvents::OnSceneSimulationFinishHandler(
  121. [this](AzPhysics::SceneHandle sceneHandle, float)
  122. {
  123. m_numberOfPhysicsSteps--;
  124. AZ_Printf("SimulationManager", "Physics simulation step finished. Remaining steps: %d", m_numberOfPhysicsSteps);
  125. SimulationManagerNotificationsBus::Broadcast(&SimulationManagerNotifications::OnSimulationStepFinish, m_numberOfPhysicsSteps);
  126. if (m_numberOfPhysicsSteps <= 0)
  127. {
  128. SetSimulationPaused(true);
  129. // remove handler
  130. m_simulationFinishEvent.Disconnect();
  131. }
  132. });
  133. // get default scene
  134. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  135. AZ_Assert(physicsSystem, "Physics system is not available");
  136. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  137. AZ_Assert(sceneInterface, "Physics scene interface is not available");
  138. AzPhysics::SceneHandle defaultScene = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
  139. auto scene = sceneInterface->GetScene(defaultScene);
  140. AZ_Assert(scene, "Default physics scene is not available");
  141. // install handler
  142. scene->RegisterSceneSimulationFinishHandler(m_simulationFinishEvent);
  143. SetSimulationPaused(false);
  144. }
  145. void SimulationManager::ReloadLevel(SimulationManagerRequests::ReloadLevelCallback completionCallback)
  146. {
  147. AzFramework::LevelSystemLifecycleNotificationBus::Handler::BusConnect();
  148. m_reloadLevelCallback = completionCallback;
  149. // We need to delete all entities before reloading the level
  150. DeletionCompletedCb deleteAllCompletion = [](const AZ::Outcome<void, FailedResult>& result)
  151. {
  152. AZ_Trace("SimulationManager", "Delete all entities completed: %s, reload level", result.IsSuccess() ? "true" : "false");
  153. const char* levelName = AZ::Interface<AzFramework::ILevelSystemLifecycle>::Get()->GetCurrentLevelName();
  154. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, "UnloadLevel");
  155. AZStd::string command = AZStd::string::format("LoadLevel %s", levelName);
  156. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequests::ExecuteConsoleCommand, command.c_str());
  157. };
  158. // delete spawned entities
  159. SimulationEntityManagerRequestBus::Broadcast(&SimulationEntityManagerRequests::DeleteAllEntities, deleteAllCompletion);
  160. }
  161. void SimulationManager::OnLoadingComplete(const char* levelName)
  162. {
  163. AZ_Printf("SimulationManager", "Level loading started: %s", levelName);
  164. if (m_reloadLevelCallback)
  165. {
  166. m_reloadLevelCallback();
  167. m_reloadLevelCallback = nullptr;
  168. }
  169. AzFramework::LevelSystemLifecycleNotificationBus::Handler::BusDisconnect();
  170. }
  171. } // namespace SimulationInterfaces