SimulationManager.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/Physics/PhysicsSystem.h>
  12. #include <SimulationInterfaces/SimulationInterfacesTypeIds.h>
  13. namespace SimulationInterfaces
  14. {
  15. AZ_COMPONENT_IMPL(SimulationManager, "SimulationManager", SimulationManagerTypeId);
  16. void SimulationManager::Reflect(AZ::ReflectContext* context)
  17. {
  18. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  19. {
  20. serializeContext->Class<SimulationManager, AZ::Component>()->Version(0);
  21. }
  22. }
  23. void SimulationManager::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  24. {
  25. provided.push_back(AZ_CRC_CE("SimulationManagerService"));
  26. }
  27. void SimulationManager::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  28. {
  29. incompatible.push_back(AZ_CRC_CE("SimulationManagerService"));
  30. }
  31. void SimulationManager::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  32. {
  33. required.push_back(AZ_CRC_CE("PhysicsService"));
  34. }
  35. void SimulationManager::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  36. {
  37. }
  38. SimulationManager::SimulationManager()
  39. {
  40. if (SimulationManagerRequestBusInterface::Get() == nullptr)
  41. {
  42. SimulationManagerRequestBusInterface::Register(this);
  43. }
  44. }
  45. SimulationManager::~SimulationManager()
  46. {
  47. if (SimulationManagerRequestBusInterface::Get() == this)
  48. {
  49. SimulationManagerRequestBusInterface::Unregister(this);
  50. }
  51. }
  52. void SimulationManager::Init()
  53. {
  54. }
  55. void SimulationManager::Activate()
  56. {
  57. SimulationManagerRequestBus::Handler::BusConnect();
  58. }
  59. void SimulationManager::Deactivate()
  60. {
  61. SimulationManagerRequestBus::Handler::BusDisconnect();
  62. }
  63. void SimulationManager::SetSimulationPaused(bool paused)
  64. {
  65. // get az physics system
  66. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  67. AZ_Assert(physicsSystem, "Physics system is not available");
  68. const auto& sceneHandlers = physicsSystem->GetAllScenes();
  69. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  70. AZ_Assert(sceneInterface, "Physics scene interface is not available");
  71. for (auto& scene : sceneHandlers)
  72. {
  73. AZ_Assert(scene, "Physics scene is not available");
  74. scene->SetEnabled(!paused);
  75. }
  76. }
  77. void SimulationManager::StepSimulation(AZ::u32 steps)
  78. {
  79. m_numberOfPhysicsSteps = steps;
  80. // install handler
  81. m_simulationFinishEvent = AzPhysics::SceneEvents::OnSceneSimulationFinishHandler(
  82. [this](AzPhysics::SceneHandle sceneHandle, float)
  83. {
  84. m_numberOfPhysicsSteps--;
  85. AZ_Printf("SimulationManager", "Physics simulation step finished. Remaining steps: %d", m_numberOfPhysicsSteps);
  86. if (m_numberOfPhysicsSteps <= 0)
  87. {
  88. SetSimulationPaused(true);
  89. // remove handler
  90. m_simulationFinishEvent.Disconnect();
  91. }
  92. });
  93. // get default scene
  94. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  95. AZ_Assert(physicsSystem, "Physics system is not available");
  96. auto* sceneInterface = AZ::Interface<AzPhysics::SceneInterface>::Get();
  97. AZ_Assert(sceneInterface, "Physics scene interface is not available");
  98. AzPhysics::SceneHandle defaultScene = sceneInterface->GetSceneHandle(AzPhysics::DefaultPhysicsSceneName);
  99. auto scene = sceneInterface->GetScene(defaultScene);
  100. AZ_Assert(scene, "Default physics scene is not available");
  101. // install handler
  102. scene->RegisterSceneSimulationFinishHandler(m_simulationFinishEvent);
  103. SetSimulationPaused(false);
  104. }
  105. } // namespace SimulationInterfaces