Radium Engine  1.5.20
Loading...
Searching...
No Matches
EntityManager.cpp
1#include <Engine/Scene/EntityManager.hpp>
2
3#include <Core/Utils/Log.hpp>
4
5#include <Engine/RadiumEngine.hpp>
6#include <Engine/Scene/SignalManager.hpp>
8
9namespace Ra {
10namespace Engine {
11namespace Scene {
12
13using namespace Core::Utils; // log
14
15EntityManager::EntityManager() {
16 auto idx = m_entities.emplace( SystemEntity::createInstance() );
17 auto& ent = m_entities[idx];
18 ent->setIndex( 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() ) );
23}
24
25EntityManager::~EntityManager() {
26 auto& ent = m_entities[0];
27 ent.release();
28 SystemEntity::destroyInstance();
29}
30
31Entity* EntityManager::createEntity( const std::string& name ) {
32 auto idx = m_entities.emplace( new Entity( name ) );
33 auto& ent = m_entities[idx];
34 ent->setIndex( idx );
35
36 std::string entityName = name;
37 if ( name.empty() ) {
38 entityName = "Entity_" + std::to_string( idx.getValue() );
39 ent->rename( entityName );
40 }
41 else {
42 int i = 1;
43 bool mustRename = false;
44 while ( entityExists( entityName ) ) {
45 LOG( logWARNING ) << "Entity `" << entityName << "` already exists";
46 entityName = name + "_" + std::to_string( i++ );
47 mustRename = true;
48 }
49 if ( mustRename ) { ent->rename( entityName ); }
50 }
51
52 m_entitiesName.insert( { ent->getName(), idx } );
53 RadiumEngine::getInstance()->getSignalManager()->fireEntityCreated( ItemEntry( ent.get() ) );
54 return ent.get();
55}
56
57bool EntityManager::entityExists( const std::string& name ) const {
58 return m_entitiesName.find( name ) != m_entitiesName.end();
59}
60
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." );
64
65 auto& ent = m_entities[idx];
66 std::string name = ent->getName();
67 m_entities.remove( idx );
68 m_entitiesName.erase( name );
69}
70
71void EntityManager::removeEntity( Entity* entity ) {
72 removeEntity( entity->getIndex() );
73}
74
75Entity* EntityManager::getEntity( Core::Utils::Index idx ) const {
76 CORE_ASSERT( idx.isValid(), "Trying to access an invalid component." );
77
78 Entity* ent = nullptr;
79
80 if ( m_entities.contains( idx ) ) { ent = m_entities[idx].get(); }
81
82 return ent;
83}
84
85std::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 ) {
89 return e.get();
90 } );
91
92 return entities;
93}
94
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 + ")";
99 return nullptr;
100 }
101 return m_entities.at( idx->second ).get();
102}
103
104void EntityManager::swapBuffers() {
105 for ( auto& e : m_entities ) {
106 e->swapTransformBuffers();
107 }
108}
109
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 ) );
115 }
116 for ( const auto& idx : indices ) {
117 removeEntity( idx );
118 }
119}
120} // namespace Scene
121} // namespace Engine
122} // namespace Ra
T begin(T... args)
T empty(T... args)
T end(T... args)
T erase(T... args)
T find(T... args)
T insert(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T push_back(T... args)
T reserve(T... args)
T resize(T... args)
T to_string(T... args)
T transform(T... args)