Loading [MathJax]/jax/input/TeX/config.js
Radium Engine  1.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EntityManager.cpp
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>
9#include <algorithm>
10#include <deque>
11#include <ostream>
12#include <utility>
13
14namespace Ra {
15namespace Engine {
16namespace Scene {
17
18using namespace Core::Utils; // log
19
20EntityManager::EntityManager() {
21 auto idx = m_entities.emplace( SystemEntity::createInstance() );
22 auto& ent = m_entities[idx];
23 ent->setIndex( 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() ) );
28}
29
30EntityManager::~EntityManager() {
31 auto& ent = m_entities[0];
32 ent.release();
33 SystemEntity::destroyInstance();
34}
35
36Entity* EntityManager::createEntity( const std::string& name ) {
37 auto idx = m_entities.emplace( new Entity( name ) );
38 auto& ent = m_entities[idx];
39 ent->setIndex( idx );
40
41 std::string entityName = name;
42 if ( name.empty() ) {
43 entityName = "Entity_" + std::to_string( idx.getValue() );
44 ent->rename( entityName );
45 }
46 else {
47 int i = 1;
48 bool mustRename = false;
49 while ( entityExists( entityName ) ) {
50 LOG( logWARNING ) << "Entity `" << entityName << "` already exists";
51 entityName = name + "_" + std::to_string( i++ );
52 mustRename = true;
53 }
54 if ( mustRename ) { ent->rename( entityName ); }
55 }
56
57 m_entitiesName.insert( { ent->getName(), idx } );
58 RadiumEngine::getInstance()->getSignalManager()->fireEntityCreated( ItemEntry( ent.get() ) );
59 return ent.get();
60}
61
62bool EntityManager::entityExists( const std::string& name ) const {
63 return m_entitiesName.find( name ) != m_entitiesName.end();
64}
65
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." );
69
70 auto& ent = m_entities[idx];
71 std::string name = ent->getName();
72 m_entities.remove( idx );
73 m_entitiesName.erase( name );
74}
75
76void EntityManager::removeEntity( Entity* entity ) {
77 removeEntity( entity->getIndex() );
78}
79
80Entity* EntityManager::getEntity( Core::Utils::Index idx ) const {
81 CORE_ASSERT( idx.isValid(), "Trying to access an invalid component." );
82
83 Entity* ent = nullptr;
84
85 if ( m_entities.contains( idx ) ) { ent = m_entities[idx].get(); }
86
87 return ent;
88}
89
90std::vector<Entity*> EntityManager::getEntities() const {
91 std::vector<Entity*> entities;
92 entities.resize( m_entities.size() );
93 std::transform( m_entities.begin(), m_entities.end(), entities.begin(), []( const auto& e ) {
94 return e.get();
95 } );
96
97 return entities;
98}
99
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 + ")";
104 return nullptr;
105 }
106 return m_entities.at( idx->second ).get();
107}
108
109void EntityManager::swapBuffers() {
110 for ( auto& e : m_entities ) {
111 e->swapTransformBuffers();
112 }
113}
114
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 ) );
120 }
121 for ( const auto& idx : indices ) {
122 removeEntity( idx );
123 }
124}
125} // namespace Scene
126} // namespace Engine
127} // 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:4
T push_back(T... args)
T reserve(T... args)
T resize(T... args)
T to_string(T... args)
T transform(T... args)