SimulationEntitiesManager.h 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. {
  22. public:
  23. AZ_COMPONENT_DECL(SimulationEntitiesManager);
  24. static void Reflect(AZ::ReflectContext* context);
  25. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  26. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  27. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  28. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  29. SimulationEntitiesManager();
  30. ~SimulationEntitiesManager();
  31. protected:
  32. // SimulationEntityManagerRequestBus interface implementation
  33. AZ::Outcome<EntityNameList, FailedResult> GetEntities(const EntityFilters& filter) override;
  34. AZ::Outcome<EntityState, FailedResult> GetEntityState(const AZStd::string& name) override;
  35. AZ::Outcome<MultipleEntitiesStates, FailedResult> GetEntitiesStates(const EntityFilters& filter) override;
  36. AZ::Outcome<void, FailedResult> SetEntityState(const AZStd::string& name, const EntityState& state) override;
  37. void DeleteEntity(const AZStd::string& name, DeletionCompletedCb completedCb) override;
  38. AZ::Outcome<SpawnableList, FailedResult> GetSpawnables() override;
  39. void SpawnEntity(
  40. const AZStd::string& name,
  41. const AZStd::string& uri,
  42. const AZStd::string& entityNamespace,
  43. const AZ::Transform& initialPose,
  44. SpawnCompletedCb completedCb) override;
  45. // AZ::Component interface implementation
  46. void Init() override;
  47. void Activate() override;
  48. void Deactivate() override;
  49. private:
  50. //! Registers simulated entity to entity id mapping.
  51. //! Note that the entityId will be registered under unique name.
  52. //! \param entityId The entity id to register
  53. //! \param proposedName Optional user proposed name for the simulated entity
  54. //! \return returns the simulated entity name
  55. AZStd::string AddSimulatedEntity(AZ::EntityId entityId, const AZStd::string& proposedName);
  56. //! Removes simulated entity from the mapping.
  57. void RemoveSimulatedEntity(AZ::EntityId entityId);
  58. //! Returns the simulated entity name for the given entity id.
  59. AZStd::string GetSimulatedEntityName(AZ::EntityId entityId, const AZStd::string& proposedName) const;
  60. AzPhysics::SceneEvents::OnSimulationBodyAdded::Handler m_simulationBodyAddedHandler;
  61. AzPhysics::SceneEvents::OnSimulationBodyRemoved::Handler m_simulationBodyRemovedHandler;
  62. AzPhysics::SystemEvents::OnSceneAddedEvent::Handler m_sceneAddedHandler;
  63. AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler m_sceneRemovedHandler;
  64. AzPhysics::SceneHandle m_physicsScenesHandle = AzPhysics::InvalidSceneHandle;
  65. AZStd::unordered_map<AZStd::string, AZ::EntityId> m_simulatedEntityToEntityIdMap;
  66. AZStd::unordered_map<AZ::EntityId, AZStd::string> m_entityIdToSimulatedEntityMap;
  67. AZStd::unordered_set<AzPhysics::SimulatedBodyHandle> m_disabledBodies;
  68. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, AzFramework::EntitySpawnTicket> m_spawnedTickets;
  69. struct SpawnCompletedCbData
  70. {
  71. AZStd::string m_userProposedName; //! Name proposed by the User in spawn request
  72. SpawnCompletedCb m_completedCb; //! User callback to be called when the entity is registered
  73. AZ::ScriptTimePoint m_spawnCompletedTime; //! Time at which the entity was spawned
  74. };
  75. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, SpawnCompletedCbData>
  76. m_spawnCompletedCallbacks; //! Callbacks to be called when the entity is registered
  77. };
  78. } // namespace SimulationInterfaces