SimulationManager.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzFramework/Components/ConsoleBus.h>
  12. #include <AzFramework/Physics/PhysicsSystem.h>
  13. #include <SimulationInterfaces/SimulationFeaturesAggregatorRequestBus.h>
  14. #include <SimulationInterfaces/SimulationInterfacesTypeIds.h>
  15. #include <simulation_interfaces/msg/simulator_features.hpp>
  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>{ simulation_interfaces::msg::SimulatorFeatures::SIMULATION_RESET,
  67. simulation_interfaces::msg::SimulatorFeatures::SIMULATION_RESET_TIME,
  68. // simulation_interfaces::msg::SimulatorFeatures::SIMULATION_RESET_STATE,
  69. simulation_interfaces::msg::SimulatorFeatures::SIMULATION_RESET_SPAWNED,
  70. simulation_interfaces::msg::SimulatorFeatures::SIMULATION_STATE_PAUSE,
  71. simulation_interfaces::msg::SimulatorFeatures::STEP_SIMULATION_SINGLE,
  72. simulation_interfaces::msg::SimulatorFeatures::STEP_SIMULATION_MULTIPLE,
  73. simulation_interfaces::msg::SimulatorFeatures::STEP_SIMULATION_ACTION });
  74. }
  75. void SimulationManager::Deactivate()
  76. {
  77. SimulationManagerRequestBus::Handler::BusDisconnect();
  78. }
  79. bool SimulationManager::IsSimulationPaused() const
  80. {
  81. return m_isSimulationPaused;
  82. }
  83. bool SimulationManager::IsSimulationStepsActive() const
  84. {
  85. return m_simulationFinishEvent.IsConnected();
  86. }
  87. void SimulationManager::CancelStepSimulation()
  88. {
  89. if (m_simulationFinishEvent.IsConnected())
  90. {
  91. m_simulationFinishEvent.Disconnect();
  92. SetSimulationPaused(true);
  93. m_numberOfPhysicsSteps = 0;
  94. }
  95. }
  96. void SimulationManager::SetSimulationPaused(bool paused)
  97. {
  98. // get az physics system
  99. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  100. AZ_Assert(physicsSystem, "Physics system is not available");
  101. const auto& sceneHandlers = physicsSystem->GetAllScenes();
  102. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  103. AZ_Assert(sceneInterface, "Physics scene interface is not available");
  104. for (auto& scene : sceneHandlers)
  105. {
  106. AZ_Assert(scene, "Physics scene is not available");
  107. scene->SetEnabled(!paused);
  108. m_isSimulationPaused = paused;
  109. }
  110. }
  111. void SimulationManager::StepSimulation(AZ::u64 steps)
  112. {
  113. if (steps == 0)
  114. {
  115. return;
  116. }
  117. m_numberOfPhysicsSteps = steps;
  118. // install handler
  119. m_simulationFinishEvent = AzPhysics::SceneEvents::OnSceneSimulationFinishHandler(
  120. [this](AzPhysics::SceneHandle sceneHandle, float)
  121. {
  122. m_numberOfPhysicsSteps--;
  123. AZ_Printf("SimulationManager", "Physics simulation step finished. Remaining steps: %d", m_numberOfPhysicsSteps);
  124. SimulationManagerNotificationsBus::Broadcast(
  125. &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