1#include <Core/CoreMacros.hpp>
2#include <Core/Utils/Log.hpp>
3#include <Engine/RadiumEngine.hpp>
4#include <Engine/Scene/Entity.hpp>
5#include <Engine/Scene/EntityManager.hpp>
6#include <Engine/Scene/ItemEntry.hpp>
7#include <Engine/Scene/SignalManager.hpp>
18using namespace Core::Utils;
20EntityManager::EntityManager() {
21 auto idx = m_entities.emplace( SystemEntity::createInstance() );
22 auto& ent = m_entities[idx];
24 CORE_ASSERT( ent.get() == SystemEntity::getInstance(),
"Invalid singleton instanciation" );
25 m_entitiesName.
insert( { ent->getName(), ent->getIndex() } );
26 RadiumEngine::getInstance()->getSignalManager()->fireEntityCreated(
27 ItemEntry( SystemEntity::getInstance() ) );
30EntityManager::~EntityManager() {
31 auto& ent = m_entities[0];
33 SystemEntity::destroyInstance();
36Entity* EntityManager::createEntity(
const std::string& name ) {
37 auto idx = m_entities.emplace(
new Entity( name ) );
38 auto& ent = m_entities[idx];
44 ent->rename( entityName );
48 bool mustRename =
false;
49 while ( entityExists( entityName ) ) {
50 LOG( logWARNING ) <<
"Entity `" << entityName <<
"` already exists";
54 if ( mustRename ) { ent->rename( entityName ); }
57 m_entitiesName.
insert( { ent->getName(), idx } );
58 RadiumEngine::getInstance()->getSignalManager()->fireEntityCreated( ItemEntry( ent.get() ) );
62bool EntityManager::entityExists(
const std::string& name )
const {
63 return m_entitiesName.
find( name ) != m_entitiesName.
end();
66void EntityManager::removeEntity( Core::Utils::Index idx ) {
67 CORE_ASSERT( idx.isValid() && m_entities.contains( idx ),
68 "Trying to remove an entity that has not been added to the manager." );
70 auto& ent = m_entities[idx];
72 m_entities.remove( idx );
73 m_entitiesName.
erase( name );
76void EntityManager::removeEntity( Entity* entity ) {
77 removeEntity( entity->getIndex() );
80Entity* EntityManager::getEntity( Core::Utils::Index idx )
const {
81 CORE_ASSERT( idx.isValid(),
"Trying to access an invalid component." );
83 Entity* ent =
nullptr;
85 if ( m_entities.contains( idx ) ) { ent = m_entities[idx].get(); }
92 entities.
resize( m_entities.size() );
93 std::transform( m_entities.begin(), m_entities.end(), entities.
begin(), [](
const auto& e ) {
100Entity* EntityManager::getEntity(
const std::string& name )
const {
101 auto idx = m_entitiesName.
find( name );
102 if ( idx == m_entitiesName.
end() ) {
103 LOG( logDEBUG ) <<
"Trying to access an invalid entity (named: " + name +
")";
106 return m_entities.at( idx->second ).get();
109void EntityManager::swapBuffers() {
110 for (
auto& e : m_entities ) {
111 e->swapTransformBuffers();
115void EntityManager::deleteEntities() {
117 indices.
reserve( m_entities.size() - 1 );
118 for (
size_t i = 1; i < m_entities.size(); ++i ) {
119 indices.
push_back( m_entities.index( i ) );
121 for (
const auto& idx : indices ) {
hepler function to manage enum as underlying types in VariableSet