Radium Engine  1.5.0
Entity.hpp
1 #pragma once
2 
3 #include <Engine/RaEngine.hpp>
4 
5 #include <mutex>
6 #include <string>
7 #include <thread>
8 #include <vector>
9 
10 #include <Core/Types.hpp>
11 #include <Core/Utils/IndexedObject.hpp>
12 #include <Core/Utils/Observable.hpp>
13 
14 namespace Ra {
15 namespace Engine {
16 namespace Scene {
17 
18 class Component;
19 class System;
20 
22 class RA_ENGINE_API Entity : public Core::Utils::IndexedObject
23 {
24  public:
25  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26  explicit Entity( const std::string& name = "" );
27 
28  // Entities are not copyable.
29  Entity( const Entity& entity ) = delete;
30  Entity& operator=( const Entity& ) = delete;
31 
32  ~Entity() override;
33 
34  // Name
35  inline const std::string& getName() const;
36  inline void rename( const std::string& name );
37 
38  // Transform
39  inline void setTransform( const Core::Transform& transform );
40  inline void setTransform( const Core::Matrix4& transform );
41  const Core::Transform& getTransform() const;
42  const Core::Matrix4& getTransformAsMatrix() const;
43 
44  void swapTransformBuffers();
45 
47  inline Core::Utils::Observable<const Entity*>& transformationObservers() const;
48 
49  // Components
51  void addComponent( Component* component );
52 
54  void removeComponent( const std::string& name );
55 
57  Component* getComponent( const std::string& name );
58  const Component* getComponent( const std::string& name ) const;
59 
60  const std::vector<std::unique_ptr<Component>>& getComponents() const;
61 
63  // Component* getComponent( const System& system);
64 
65  inline uint getNumComponents() const;
66 
67  virtual Core::Aabb computeAabb();
68 
69  void invalidateAabb();
70 
71  private:
72  Core::Transform m_transform;
73  Core::Transform m_doubleBufferedTransform;
74  mutable std::mutex m_transformMutex;
75 
76  std::vector<std::unique_ptr<Component>> m_components;
77 
78  std::string m_name {};
79  bool m_transformChanged { false };
80 
81  bool m_isAabbValid { false };
82  Core::Aabb m_aabb;
83 
85  mutable Core::Utils::Observable<const Entity*> m_transformationObservers;
86 
88 };
89 
90 inline const std::string& Entity::getName() const {
91  return m_name;
92 }
93 
94 inline void Entity::rename( const std::string& name ) {
95  m_name = name;
96 }
97 
98 inline void Entity::setTransform( const Core::Transform& transform ) {
99  m_transformChanged = true;
100  m_doubleBufferedTransform = transform;
101 }
102 
103 inline void Entity::setTransform( const Core::Matrix4& transform ) {
104  setTransform( Core::Transform( transform ) );
105 }
106 
107 inline const Core::Transform& Entity::getTransform() const {
108  // Radium-V2 : why a mutex on read ? there is no lock on write on this!
109  std::lock_guard<std::mutex> lock( m_transformMutex );
110  return m_transform;
111 }
112 
113 inline const Core::Matrix4& Entity::getTransformAsMatrix() const {
114  // Radium-V2 : why a mutex on read ? there is no lock on write on this!
115  std::lock_guard<std::mutex> lock( m_transformMutex );
116  return m_transform.matrix();
117 }
118 
119 inline uint Entity::getNumComponents() const {
120  return uint( m_components.size() );
121 }
122 
124  return m_transformationObservers;
125 }
126 
127 } // namespace Scene
128 } // namespace Engine
129 } // 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
An entity is an scene element. It ties together components with a transform.
Definition: Entity.hpp:23
Core::Utils::Observable< const Entity * > & transformationObservers() const
get a ref to transformation observers to add/remove an observer
Definition: Entity.hpp:123
uint getNumComponents() const
Get component belonging to a given system.
Definition: Entity.hpp:119
Definition: Cage.cpp:3