1#include <Engine/Scene/EntityManager.hpp>
3#include <Core/Utils/Log.hpp>
5#include <Engine/RadiumEngine.hpp>
6#include <Engine/Scene/SignalManager.hpp>
13using namespace Core::Utils;
15EntityManager::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() ) );
25EntityManager::~EntityManager() {
26 auto& ent = m_entities[0];
28 SystemEntity::destroyInstance();
31Entity* EntityManager::createEntity(
const std::string& name ) {
32 auto idx = m_entities.emplace(
new Entity( name ) );
33 auto& ent = m_entities[idx];
39 ent->rename( entityName );
43 bool mustRename =
false;
44 while ( entityExists( entityName ) ) {
45 LOG( logWARNING ) <<
"Entity `" << entityName <<
"` already exists";
49 if ( mustRename ) { ent->rename( entityName ); }
52 m_entitiesName.
insert( { ent->getName(), idx } );
53 RadiumEngine::getInstance()->getSignalManager()->fireEntityCreated( ItemEntry( ent.get() ) );
57bool EntityManager::entityExists(
const std::string& name )
const {
58 return m_entitiesName.
find( name ) != m_entitiesName.
end();
61void 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];
67 m_entities.remove( idx );
68 m_entitiesName.
erase( name );
71void EntityManager::removeEntity( Entity* entity ) {
72 removeEntity( entity->getIndex() );
75Entity* 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(); }
87 entities.
resize( m_entities.size() );
88 std::transform( m_entities.begin(), m_entities.end(), entities.
begin(), [](
const auto& e ) {
95Entity* 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();
104void EntityManager::swapBuffers() {
105 for (
auto& e : m_entities ) {
106 e->swapTransformBuffers();
110void EntityManager::deleteEntities() {
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 ) {
hepler function to manage enum as underlying types in VariableSet