1 #include <Engine/Scene/EntityManager.hpp>
3 #include <Core/Utils/Log.hpp>
5 #include <Engine/RadiumEngine.hpp>
6 #include <Engine/Scene/SignalManager.hpp>
7 #include <Engine/Scene/SystemDisplay.hpp>
13 using namespace Core::Utils;
15 EntityManager::EntityManager() {
16 auto idx = m_entities.emplace( SystemEntity::createInstance() );
17 auto& ent = m_entities[idx];
19 CORE_ASSERT( ent.get() == SystemEntity::getInstance(),
"Invalid singleton instanciation" );
20 m_entitiesName.insert( { ent->getName(), ent->getIndex() } );
21 RadiumEngine::getInstance()->getSignalManager()->fireEntityCreated(
22 ItemEntry( SystemEntity::getInstance() ) );
25 EntityManager::~EntityManager() {
26 auto& ent = m_entities[0];
28 SystemEntity::destroyInstance();
31 Entity* EntityManager::createEntity(
const std::string& name ) {
32 auto idx = m_entities.emplace(
new Entity( name ) );
33 auto& ent = m_entities[idx];
36 std::string entityName = name;
38 entityName =
"Entity_" + std::to_string( idx.getValue() );
39 ent->rename( entityName );
43 bool mustRename =
false;
44 while ( entityExists( entityName ) ) {
45 LOG( logWARNING ) <<
"Entity `" << entityName <<
"` already exists";
46 entityName = name +
"_" + std::to_string( i++ );
49 if ( mustRename ) { ent->rename( entityName ); }
52 m_entitiesName.insert( { ent->getName(), idx } );
53 RadiumEngine::getInstance()->getSignalManager()->fireEntityCreated( ItemEntry( ent.get() ) );
57 bool EntityManager::entityExists(
const std::string& name )
const {
58 return m_entitiesName.find( name ) != m_entitiesName.end();
61 void EntityManager::removeEntity( Core::Utils::Index idx ) {
62 CORE_ASSERT( idx.isValid() && m_entities.contains( idx ),
63 "Trying to remove an entity that has not been added to the manager." );
65 auto& ent = m_entities[idx];
66 std::string name = ent->getName();
67 m_entities.remove( idx );
68 m_entitiesName.erase( name );
71 void EntityManager::removeEntity( Entity* entity ) {
72 removeEntity( entity->getIndex() );
75 Entity* EntityManager::getEntity( Core::Utils::Index idx )
const {
76 CORE_ASSERT( idx.isValid(),
"Trying to access an invalid component." );
78 Entity* ent =
nullptr;
80 if ( m_entities.contains( idx ) ) { ent = m_entities[idx].get(); }
85 std::vector<Entity*> EntityManager::getEntities()
const {
86 std::vector<Entity*> entities;
87 entities.resize( m_entities.size() );
88 std::transform( m_entities.begin(), m_entities.end(), entities.begin(), [](
const auto& e ) {
95 Entity* EntityManager::getEntity(
const std::string& name )
const {
96 auto idx = m_entitiesName.find( name );
97 if ( idx == m_entitiesName.end() ) {
98 LOG( logDEBUG ) <<
"Trying to access an invalid entity (named: " + name +
")";
101 return m_entities.at( idx->second ).get();
104 void EntityManager::swapBuffers() {
105 for (
auto& e : m_entities ) {
106 e->swapTransformBuffers();
110 void EntityManager::deleteEntities() {
111 std::vector<Core::Utils::Index> indices;
112 indices.reserve( m_entities.size() - 1 );
113 for (
size_t i = 1; i < m_entities.size(); ++i ) {
114 indices.push_back( m_entities.index( i ) );
116 for (
const auto& idx : indices ) {