SimulationEntitiesManager.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/Script/ScriptTimePoint.h>
  12. #include <AzFramework/Entity/EntityContextBus.h>
  13. #include <AzFramework/Physics/PhysicsScene.h>
  14. #include <AzFramework/Spawnable/SpawnableEntitiesInterface.h>
  15. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  16. namespace SimulationInterfaces
  17. {
  18. class SimulationEntitiesManager
  19. : public AZ::Component
  20. , protected SimulationEntityManagerRequestBus::Handler
  21. , protected AZ::TickBus::Handler
  22. {
  23. public:
  24. AZ_COMPONENT_DECL(SimulationEntitiesManager);
  25. static void Reflect(AZ::ReflectContext* context);
  26. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  27. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  28. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  29. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  30. SimulationEntitiesManager();
  31. ~SimulationEntitiesManager();
  32. protected:
  33. // SimulationEntityManagerRequestBus interface implementation
  34. AZ::Outcome<EntityNameList, FailedResult> GetEntities(const EntityFilters& filter) override;
  35. AZ::Outcome<EntityState, FailedResult> GetEntityState(const AZStd::string& name) override;
  36. AZ::Outcome<MultipleEntitiesStates, FailedResult> GetEntitiesStates(const EntityFilters& filter) override;
  37. AZ::Outcome<void, FailedResult> SetEntityState(const AZStd::string& name, const EntityState& state) override;
  38. void DeleteEntity(const AZStd::string& name, DeletionCompletedCb completedCb) override;
  39. void DeleteAllEntities(DeletionCompletedCb completedCb) override;
  40. AZ::Outcome<SpawnableList, FailedResult> GetSpawnables() override;
  41. void SpawnEntity(
  42. const AZStd::string& name,
  43. const AZStd::string& uri,
  44. const AZStd::string& entityNamespace,
  45. const AZ::Transform& initialPose,
  46. const bool allowRename,
  47. SpawnCompletedCb completedCb) override;
  48. void ResetAllEntitiesToInitialState() override;
  49. // AZ::Component interface implementation
  50. void Init() override;
  51. void Activate() override;
  52. void Deactivate() override;
  53. // AZ::TickBus::Handler interface implementation
  54. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  55. private:
  56. //! Registers a new simulated body to the simulation interface.
  57. //! Note that the body handle will be registered under unique name
  58. //! Note that body need to be configured to be registered
  59. //! \param sceneHandle The scene handle to register the body to
  60. //! \param bodyHandle The body handle to register
  61. bool RegisterNewSimulatedBody(AzPhysics::SceneHandle sceneHandle, AzPhysics::SimulatedBodyHandle bodyHandle);
  62. //! Registers a new simulated body to the simulation interface.
  63. //! Returns the list of handles that were not registered
  64. AZStd::vector<AZStd::pair<AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle>> RegisterNewSimulatedBodies(const AZStd::vector<AZStd::pair<AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle>>& handles);
  65. //! Registers simulated entity to entity id mapping.
  66. //! Note that the entityId will be registered under unique name.
  67. //! \param entityId The entity id to register
  68. //! \param proposedName Optional user proposed name for the simulated entity
  69. //! \return returns the simulated entity name
  70. AZStd::string AddSimulatedEntity(AZ::EntityId entityId, const AZStd::string& proposedName);
  71. //! Removes simulated entity from the mapping.
  72. void RemoveSimulatedEntity(AZ::EntityId entityId);
  73. //! Returns the simulated entity name for the given entity id.
  74. AZStd::string GetSimulatedEntityName(AZ::EntityId entityId, const AZStd::string& proposedName) const;
  75. //! Set the state of the entity and their descendants.
  76. void SetEntitiesState(const AZStd::vector<AZ::EntityId>& entityAndDescendants, const EntityState& state);
  77. AzPhysics::SceneEvents::OnSimulationBodyAdded::Handler m_simulationBodyAddedHandler;
  78. AzPhysics::SceneEvents::OnSimulationBodyRemoved::Handler m_simulationBodyRemovedHandler;
  79. AzPhysics::SystemEvents::OnSceneAddedEvent::Handler m_sceneAddedHandler;
  80. AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler m_sceneRemovedHandler;
  81. AzPhysics::SceneHandle m_physicsScenesHandle = AzPhysics::InvalidSceneHandle;
  82. AZStd::vector<AZStd::pair<AzPhysics::SceneHandle, AzPhysics::SimulatedBodyHandle>> m_unconfiguredScenesHandles; //! Set of yet-invalid scenes handles, that are waiting for configuration
  83. AZStd::unordered_map<AZStd::string, AZ::EntityId> m_simulatedEntityToEntityIdMap;
  84. AZStd::unordered_map<AZ::EntityId, AZStd::string> m_entityIdToSimulatedEntityMap;
  85. AZStd::unordered_map<AZ::EntityId, EntityState> m_entityIdToInitialState;
  86. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, AzFramework::EntitySpawnTicket> m_spawnedTickets;
  87. struct SpawnCompletedCbData
  88. {
  89. AZStd::string m_userProposedName; //! Name proposed by the User in spawn request
  90. AZStd::string m_resultedName; //! Name of the entity in the simulation interface
  91. SpawnCompletedCb m_completedCb; //! User callback to be called when the entity is registered
  92. AZ::ScriptTimePoint m_spawnCompletedTime; //! Time at which the entity was spawned
  93. bool m_registered = false; //! Flag to check if the entity was registered
  94. };
  95. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, SpawnCompletedCbData>
  96. m_spawnCompletedCallbacks; //! Callbacks to be called when the entity is registered
  97. };
  98. } // namespace SimulationInterfaces