Radium Engine  1.5.0
System.cpp
1 #include <Engine/Scene/System.hpp>
2 
3 #include <Engine/Scene/Component.hpp>
4 #include <Engine/Scene/Entity.hpp>
5 
6 namespace Ra {
7 namespace Engine {
8 namespace Scene {
9 
10 void System::registerComponent( const Entity* ent, Component* component ) {
11  // Perform checks on debug
12 #if defined( DEBUG )
13  CORE_ASSERT( component->getEntity() == ent, "Component does not belong to entity" );
14  for ( const auto& pair : m_components ) {
15  CORE_ASSERT( pair.second != component, "Component already registered" );
16  if ( pair.first == ent ) {
17  CORE_ASSERT( pair.second->getName() != component->getName(),
18  "A component with the same name is already associated with this entity" );
19  }
20  }
21 #endif // DEBUG
22  m_components.emplace_back( ent, component );
23  component->setSystem( this );
24 }
25 
26 void System::unregisterComponent( const Entity* ent, Component* component ) {
27  CORE_ASSERT( component->getEntity() == ent, "Component does not belong to entity" );
28  CORE_UNUSED( ent );
29  const auto& pos =
30  std::find_if( m_components.begin(), m_components.end(), [component]( const auto& pair ) {
31  return pair.second == component;
32  } );
33 
34  CORE_ASSERT( pos != m_components.end(), "Component is not registered." );
35  CORE_ASSERT( pos->first == ent, "Component belongs to a different entity" );
36  component->setSystem( nullptr );
37  m_components.erase( pos );
38 }
39 
40 void System::unregisterAllComponents( const Entity* entity ) {
41  std::vector<std::pair<const Entity*, Component*>>::iterator pos;
42  while ( ( pos = std::find_if(
43  m_components.begin(), m_components.end(), [entity]( const auto& pair ) {
44  return pair.first == entity;
45  } ) ) != m_components.end() ) {
46  m_components.erase( pos );
47  }
48 }
49 
50 std::vector<Component*> System::getEntityComponents( const Entity* entity ) {
51  std::vector<Component*> comps;
52  for ( const auto& ec : m_components ) {
53  if ( ec.first == entity ) { comps.push_back( ec.second ); }
54  }
55  return comps;
56 }
57 
58 } // namespace Scene
59 } // namespace Engine
60 } // 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 Entity * getEntity() const
Return the entity the component belongs to.
Definition: Component.hpp:53
virtual void setSystem(System *system)
Set the system to which the component belongs.
Definition: Component.hpp:59
An entity is an scene element. It ties together components with a transform.
Definition: Entity.hpp:23
std::vector< Component * > getEntityComponents(const Entity *entity)
Definition: System.cpp:50
std::vector< std::pair< const Entity *, Component * > > m_components
List of active components.
Definition: System.hpp:103
virtual void unregisterAllComponents(const Entity *entity)
Definition: System.cpp:40
virtual void registerComponent(const Entity *entity, Component *component)
Definition: System.cpp:10
virtual void unregisterComponent(const Entity *entity, Component *component)
Definition: System.cpp:26
Definition: Cage.cpp:3