SimulationEntitiesManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. void DeleteAllEntities(DeletionCompletedCb completedCb) override;
  39. AZ::Outcome<SpawnableList, FailedResult> GetSpawnables() override;
  40. void SpawnEntity(
  41. const AZStd::string& name,
  42. const AZStd::string& uri,
  43. const AZStd::string& entityNamespace,
  44. const AZ::Transform& initialPose,
  45. const bool allowRename,
  46. SpawnCompletedCb completedCb) override;
  47. void ResetAllEntitiesToInitialState() override;
  48. // AZ::Component interface implementation
  49. void Init() override;
  50. void Activate() override;
  51. void Deactivate() override;
  52. private:
  53. //! Registers simulated entity to entity id mapping.
  54. //! Note that the entityId will be registered under unique name.
  55. //! \param entityId The entity id to register
  56. //! \param proposedName Optional user proposed name for the simulated entity
  57. //! \return returns the simulated entity name
  58. AZStd::string AddSimulatedEntity(AZ::EntityId entityId, const AZStd::string& proposedName);
  59. //! Removes simulated entity from the mapping.
  60. void RemoveSimulatedEntity(AZ::EntityId entityId);
  61. //! Returns the simulated entity name for the given entity id.
  62. AZStd::string GetSimulatedEntityName(AZ::EntityId entityId, const AZStd::string& proposedName) const;
  63. //! Set the state of the entity and their descendants.
  64. void SetEntitiesState(const AZStd::vector<AZ::EntityId>& entityAndDescendants, const EntityState& state);
  65. AzPhysics::SceneEvents::OnSimulationBodyAdded::Handler m_simulationBodyAddedHandler;
  66. AzPhysics::SceneEvents::OnSimulationBodyRemoved::Handler m_simulationBodyRemovedHandler;
  67. AzPhysics::SystemEvents::OnSceneAddedEvent::Handler m_sceneAddedHandler;
  68. AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler m_sceneRemovedHandler;
  69. AzPhysics::SceneHandle m_physicsScenesHandle = AzPhysics::InvalidSceneHandle;
  70. AZStd::unordered_map<AZStd::string, AZ::EntityId> m_simulatedEntityToEntityIdMap;
  71. AZStd::unordered_map<AZ::EntityId, AZStd::string> m_entityIdToSimulatedEntityMap;
  72. AZStd::unordered_map<AZ::EntityId, EntityState> m_entityIdToInitialState;
  73. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, AzFramework::EntitySpawnTicket> m_spawnedTickets;
  74. struct SpawnCompletedCbData
  75. {
  76. AZStd::string m_userProposedName; //! Name proposed by the User in spawn request
  77. SpawnCompletedCb m_completedCb; //! User callback to be called when the entity is registered
  78. AZ::ScriptTimePoint m_spawnCompletedTime; //! Time at which the entity was spawned
  79. };
  80. AZStd::unordered_map<AzFramework::EntitySpawnTicket::Id, SpawnCompletedCbData>
  81. m_spawnCompletedCallbacks; //! Callbacks to be called when the entity is registered
  82. };
  83. } // namespace SimulationInterfaces