/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace SimulationInterfaces { class SimulationEntitiesManager : public AZ::Component , protected SimulationEntityManagerRequestBus::Handler { public: AZ_COMPONENT_DECL(SimulationEntitiesManager); static void Reflect(AZ::ReflectContext* context); static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided); static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible); static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required); static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent); SimulationEntitiesManager(); ~SimulationEntitiesManager(); // AZ::Component interface implementation void Activate() override; void Deactivate() override; private: // SimulationEntityManagerRequestBus interface implementation AZ::Outcome GetEntities(const EntityFilters& filter) override; AZ::Outcome GetEntityState(const AZStd::string& name) override; AZ::Outcome GetEntitiesStates(const EntityFilters& filter) override; AZ::Outcome SetEntityState(const AZStd::string& name, const EntityState& state) override; void DeleteEntity(const AZStd::string& name, DeletionCompletedCb completedCb) override; void DeleteAllEntities(DeletionCompletedCb completedCb) override; AZ::Outcome GetSpawnables() override; void SpawnEntity( const AZStd::string& name, const AZStd::string& uri, const AZStd::string& entityNamespace, const AZ::Transform& initialPose, const bool allowRename, PreInsertionCb preinsertionCb, SpawnCompletedCb completedCb) override; AZ::Outcome ResetAllEntitiesToInitialState() override; AZ::Outcome RegisterNewSimulatedBody( const AZStd::string& proposedName, const AZ::EntityId& entityId) override; AZ::Outcome UnregisterSimulatedBody(const AZStd::string& name) override; AZ::Outcome SetEntityInfo(const AZStd::string& name, const EntityInfo& info) override; AZ::Outcome GetEntityInfo(const AZStd::string& name) override; AZ::Outcome GetEntityBounds(const AZStd::string& name) override; AZ::Outcome GetEntityId(const AZStd::string& name) override; AZ::Outcome GetEntityRoot(const AZStd::string& name) override; // helper methods to filter entities by different filters AZStd::vector FilterEntitiesByCategories( const AZStd::vector& prefilteredEntities, const AZStd::vector& categories); AZStd::vector FilterEntitiesByTag( const AZStd::vector& prefilteredEntities, const TagFilter& tagFilter); AZ::Outcome, FailedResult> FilterEntitiesByBounds( const AZStd::vector& prefilteredEntities, const AZStd::shared_ptr shape, const AZ::Transform& shapePose); AZ::Outcome, FailedResult> FilterEntitiesByRegex( const AZStd::vector& prefilteredEntities, const AZStd::string& regex); //! Registers simulated entity to entity id mapping. //! Note that the entityId will be registered under unique name. //! \param entityId The entity id to register //! \param proposedName Optional user proposed name for the simulated entity //! \return returns the simulated entity name AZStd::string AddSimulatedEntity(AZ::EntityId entityId, const AZStd::string& proposedName); //! Returns the simulated entity name for the given entity id. AZStd::string GetSimulatedEntityName(AZ::EntityId entityId, const AZStd::string& proposedName) const; //! Set the state of the entity and their descendants. void SetEntitiesState(const AZStd::vector& entityAndDescendants, const EntityState& state); //! Removes entity info for entity with given name from the simulation interfaces registry, only if exists void RemoveEntityInfoIfNeeded(const AZStd::string& name); // Helper method to check if world is loaded AZ::Outcome IsWorldLoaded(); AzPhysics::SceneEvents::OnSimulationBodyAdded::Handler m_simulationBodyAddedHandler; AzPhysics::SceneEvents::OnSimulationBodyRemoved::Handler m_simulationBodyRemovedHandler; AzPhysics::SystemEvents::OnSceneAddedEvent::Handler m_sceneAddedHandler; AzPhysics::SystemEvents::OnSceneRemovedEvent::Handler m_sceneRemovedHandler; AzPhysics::SceneHandle m_physicsScenesHandle = AzPhysics::InvalidSceneHandle; // simulated entity name to tracked entity AZStd::unordered_map m_simulatedEntityToEntityIdMap; // tracked entity to simulated entity name AZStd::unordered_map m_entityIdToSimulatedEntityMap; AZStd::unordered_map m_entityIdToInitialState; // simulated Entity name to prefab root (container entity). Not always equal to tracked entity. Applies only to entities spawned by // the simulation Interfaces AZStd::unordered_map m_simulatedEntityToPrefabRoot; // Map holding entityInfo to assigned name AZStd::unordered_map m_nameToEntityInfo; // Stores category to name which are forced to be unique by the standard, used for quicker lookup during filtering AZStd::unordered_map> m_categoryToNames; AZStd::unordered_map m_spawnedTickets; struct SpawnCompletedCbData { AZStd::string m_userProposedName; //! Name proposed by the User in spawn request SpawnCompletedCb m_completedCb; //! User callback to be called when the entity is registered PreInsertionCb m_preInsertionCb; //! User callback to be called when entity prefab is added but inactive }; AZStd::unordered_map m_spawnCompletedCallbacks; }; } // namespace SimulationInterfaces