Radium Engine  1.5.0
Entity.cpp
1 #include <Engine/Scene/Entity.hpp>
2 
3 #include <Core/Math/LinearAlgebra.hpp>
4 #include <Engine/RadiumEngine.hpp>
5 #include <Engine/Scene/Component.hpp>
6 #include <Engine/Scene/SignalManager.hpp>
7 
8 namespace Ra {
9 namespace Engine {
10 namespace Scene {
11 
12 Entity::Entity( const std::string& name ) :
13  Core::Utils::IndexedObject(),
14  m_transform { Core::Transform::Identity() },
15  m_doubleBufferedTransform { Core::Transform::Identity() },
16  m_name { name } {}
17 
18 Entity::~Entity() {
19  // Ensure components are deleted before the entity for consistent
20  // ordering of signals.
21  m_transformationObservers.detachAll();
22  m_components.clear();
23  RadiumEngine::getInstance()->getSignalManager()->fireEntityDestroyed( ItemEntry( this ) );
24 }
25 
26 void Entity::addComponent( Component* component ) {
27  CORE_ASSERT( getComponent( component->getName() ) == nullptr,
28  "Component \"" << component->getName()
29  << "\" has already been added to the entity." );
30 
31  m_components.emplace_back( std::unique_ptr<Component>( component ) );
32  component->setEntity( this );
33 
34  RadiumEngine::getInstance()->getSignalManager()->fireComponentAdded(
35  ItemEntry( this, component ) );
36 
37  m_isAabbValid = false;
38 }
39 
40 Component* Entity::getComponent( const std::string& name ) {
41  const auto& pos = std::find_if( m_components.begin(),
42  m_components.end(),
43  [name]( const auto& c ) { return c->getName() == name; } );
44 
45  return pos != m_components.end() ? pos->get() : nullptr;
46 }
47 
48 const Component* Entity::getComponent( const std::string& name ) const {
49  const auto& pos = std::find_if( m_components.begin(),
50  m_components.end(),
51  [name]( const auto& c ) { return c->getName() == name; } );
52 
53  return pos != m_components.end() ? pos->get() : nullptr;
54 }
55 const std::vector<std::unique_ptr<Component>>& Entity::getComponents() const {
56  return m_components;
57 }
58 
59 void Entity::removeComponent( const std::string& name ) {
60  const auto& pos = std::find_if( m_components.begin(),
61  m_components.end(),
62  [name]( const auto& c ) { return c->getName() == name; } );
63 
64  CORE_ASSERT( pos != m_components.end(), "Component not found in entity" );
65  m_components.erase( pos );
66 
67  m_isAabbValid = false;
68 }
69 
70 void Entity::swapTransformBuffers() {
71  if ( m_transformChanged ) {
72  m_transform = m_doubleBufferedTransform;
73  m_transformChanged = false;
74  m_transformationObservers.notify( this );
75  }
76 }
77 
78 Core::Aabb Entity::computeAabb() {
79  if ( !m_isAabbValid ) {
80  Core::Aabb aabb;
81 
82  for ( const auto& component : m_components ) {
83  // transform is applied in ro aabb computation ...
84  aabb.extend( component->computeAabb() );
85  }
86 
87  m_aabb = aabb;
88  m_isAabbValid = true;
89  }
90 
91  return m_aabb;
92 }
93 
94 void Entity::invalidateAabb() {
95  m_isAabbValid = false;
96 }
97 
98 } // namespace Scene
99 } // namespace Engine
100 } // namespace Ra
A component is an element that can be updated by a system. It is also linked to some other components...
Definition: Component.hpp:31
virtual const std::string & getName() const
Return the component's name.
Definition: Component.hpp:56
virtual void setEntity(Entity *entity)
Set entity the component is part of. This method is called by the entity.
Definition: Component.hpp:50
Definition: Cage.cpp:3