SimulationEntitiesManager.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/Outcome/Outcome.h>
  12. #include <AzCore/Script/ScriptTimePoint.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <AzFramework/Entity/EntityContextBus.h>
  15. #include <AzFramework/Physics/PhysicsScene.h>
  16. #include <AzFramework/Spawnable/SpawnableEntitiesInterface.h>
  17. #include <SimulationInterfaces/Result.h>
  18. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  19. namespace SimulationInterfaces
  20. {
  21. class SimulationEntitiesManager
  22. : public AZ::Component
  23. , protected SimulationEntityManagerRequestBus::Handler
  24. {
  25. public:
  26. AZ_COMPONENT_DECL(SimulationEntitiesManager);
  27. static void Reflect(AZ::ReflectContext* context);
  28. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  29. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  30. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  31. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  32. SimulationEntitiesManager();
  33. ~SimulationEntitiesManager();
  34. // AZ::Component interface implementation
  35. void Activate() override;
  36. void Deactivate() override;
  37. private:
  38. // SimulationEntityManagerRequestBus interface implementation
  39. AZ::Outcome<EntityNameList, FailedResult> GetEntities(const EntityFilters& filter) override;
  40. AZ::Outcome<EntityState, FailedResult> GetEntityState(const AZStd::string& name) override;
  41. AZ::Outcome<MultipleEntitiesStates, FailedResult> GetEntitiesStates(const EntityFilters& filter) override;
  42. AZ::Outcome<void, FailedResult> SetEntityState(const AZStd::string& name, const EntityState& state) override;
  43. void DeleteEntity(const AZStd::string& name, DeletionCompletedCb completedCb) override;
  44. void DeleteAllEntities(DeletionCompletedCb completedCb) override;
  45. AZ::Outcome<SpawnableList, FailedResult> GetSpawnables() override;
  46. void SpawnEntity(
  47. const AZStd::string& name,
  48. const AZStd::string& uri,
  49. const AZStd::string& entityNamespace,
  50. const AZ::Transform& initialPose,
  51. const bool allowRename,
  52. PreInsertionCb preinsertionCb,
  53. SpawnCompletedCb completedCb) override;
  54. AZ::Outcome<void, FailedResult> ResetAllEntitiesToInitialState() override;
  55. AZ::Outcome<AZStd::string, FailedResult> RegisterNewSimulatedBody(
  56. const AZStd::string& proposedName, const AZ::EntityId& entityId) override;
  57. AZ::Outcome<void, FailedResult> UnregisterSimulatedBody(const AZStd::string& name) override;
  58. AZ::Outcome<void, FailedResult> SetEntityInfo(const AZStd::string& name, const EntityInfo& info) override;
  59. AZ::Outcome<EntityInfo, FailedResult> GetEntityInfo(const AZStd::string& name) override;
  60. AZ::Outcome<Bounds, FailedResult> GetEntityBounds(const AZStd::string& name) override;
  61. AZ::Outcome<AZ::EntityId, FailedResult> GetEntityId(const AZStd::string& name) override;
  62. AZ::Outcome<AZ::EntityId, FailedResult> GetEntityRoot(const AZStd::string& name) override;
  63. // helper methods to filter entities by different filters
  64. AZStd::vector<AZStd::string> FilterEntitiesByCategories(
  65. const AZStd::vector<AZStd::string>& prefilteredEntities, const AZStd::vector<EntityCategory>& categories);
  66. AZStd::vector<AZStd::string> FilterEntitiesByTag(
  67. const AZStd::vector<AZStd::string>& prefilteredEntities, const TagFilter& tagFilter);
  68. AZ::Outcome<AZStd::vector<AZStd::string>, FailedResult> FilterEntitiesByBounds(
  69. const AZStd::vector<AZStd::string>& prefilteredEntities,
  70. const AZStd::shared_ptr<Physics::ShapeConfiguration> shape,
  71. const AZ::Transform& shapePose);
  72. AZ::Outcome<AZStd::vector<AZStd::string>, FailedResult> FilterEntitiesByRegex(
  73. const AZStd::vector<AZStd::string>& prefilteredEntities, const AZStd::string& regex);
  74. //! Registers simulated entity to entity id mapping.
  75. //! Note that the entityId will be registered under unique name.
  76. //! \param entityId The entity id to register
  77. //! \param proposedName Optional user proposed name for the simulated entity
  78. //! \return returns the simulated entity name
  79. AZStd::string AddSimulatedEntity(AZ::EntityId entityId, const AZStd::string& proposedName);
  80. //! Returns the simulated entity name for the given entity id.
  81. AZStd::string GetSimulatedEntityName(AZ::EntityId entityId, const AZStd::string& proposedName) const;
  82. //! Set the state of the entity and their descendants.
  83. void SetEntitiesState(const AZStd::vector<AZ::EntityId>& entityAndDescendants, const EntityState& state);
  84. //! Removes entity info for entity with given name from the simulation interfaces registry, only if exists
  85. void RemoveEntityInfoIfNeeded(const AZStd::string& name);
  86. // Helper method to check if world is loaded
  87. AZ::Outcome<void, FailedResult> IsWorldLoaded();
  88. AzPhysics::SceneEvents::OnSimulationBodyAdded::Handler m_simulationBodyAddedHandler;
  89. AzPhysics::SceneEvents::OnSimulationBodyRemoved::Handler m_simulationBodyRemovedHandler;
  90. AzPhysics::SystemEvents::OnSceneAddedEvent::Handler m_sceneAddedHandler;
  91. AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler m_sceneRemovedHandler;
  92. AzPhysics::SceneHandle m_physicsScenesHandle = AzPhysics::InvalidSceneHandle;
  93. // simulated entity name to tracked entity
  94. AZStd::unordered_map<AZStd::string, AZ::EntityId> m_simulatedEntityToEntityIdMap;
  95. // tracked entity to simulated entity name
  96. AZStd::unordered_map<AZ::EntityId, AZStd::string> m_entityIdToSimulatedEntityMap;
  97. AZStd::unordered_map<AZ::EntityId, EntityState> m_entityIdToInitialState;
  98. // simulated Entity name to prefab root (container entity). Not always equal to tracked entity. Applies only to entities spawned by
  99. // the simulation Interfaces
  100. AZStd::unordered_map<AZStd::string, AZ::EntityId> m_simulatedEntityToPrefabRoot;
  101. // Map holding entityInfo to assigned name
  102. AZStd::unordered_map<AZStd::string, EntityInfo> m_nameToEntityInfo;
  103. // Stores category to name which are forced to be unique by the standard, used for quicker lookup during filtering
  104. AZStd::unordered_map<EntityCategory, AZStd::unordered_set<AZStd::string>> m_categoryToNames;
  105. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, AzFramework::EntitySpawnTicket> m_spawnedTickets;
  106. struct SpawnCompletedCbData
  107. {
  108. AZStd::string m_userProposedName; //! Name proposed by the User in spawn request
  109. SpawnCompletedCb m_completedCb; //! User callback to be called when the entity is registered
  110. PreInsertionCb m_preInsertionCb; //! User callback to be called when entity prefab is added but inactive
  111. };
  112. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, SpawnCompletedCbData> m_spawnCompletedCallbacks;
  113. };
  114. } // namespace SimulationInterfaces