Radium Engine  1.5.14
Loading...
Searching...
No Matches
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
14namespace Ra {
15namespace Engine {
16namespace Scene {
17
18class Component;
19class System;
20
22class RA_ENGINE_API Entity : public Core::Utils::IndexedObject
23{
24 public:
25 explicit Entity( const std::string& name = "" );
26
27 // Entities are not copyable.
28 Entity( const Entity& entity ) = delete;
29 Entity& operator=( const Entity& ) = delete;
30
31 ~Entity() override;
32
33 // Name
34 inline const std::string& getName() const;
35 inline void rename( const std::string& name );
36
37 // Transform
38 inline void setTransform( const Core::Transform& transform );
39 inline void setTransform( const Core::Matrix4& transform );
40 const Core::Transform& getTransform() const;
41 const Core::Matrix4& getTransformAsMatrix() const;
42
43 void swapTransformBuffers();
44
46 inline Core::Utils::Observable<const Entity*>& transformationObservers() const;
47
48 // Components
50 void addComponent( Component* component );
51
53 void removeComponent( const std::string& name );
54
56 Component* getComponent( const std::string& name );
57 const Component* getComponent( const std::string& name ) const;
58
59 const std::vector<std::unique_ptr<Component>>& getComponents() const;
60
62 // Component* getComponent( const System& system);
63
64 inline uint getNumComponents() const;
65
66 virtual Core::Aabb computeAabb();
67
68 void invalidateAabb();
69
70 private:
71 Core::Transform m_transform;
72 Core::Transform m_doubleBufferedTransform;
73 mutable std::mutex m_transformMutex;
74
76
77 std::string m_name {};
78 bool m_transformChanged { false };
79
80 bool m_isAabbValid { false };
81 Core::Aabb m_aabb;
82
84 mutable Core::Utils::Observable<const Entity*> m_transformationObservers;
85
87};
88
89inline const std::string& Entity::getName() const {
90 return m_name;
91}
92
93inline void Entity::rename( const std::string& name ) {
94 m_name = name;
95}
96
97inline void Entity::setTransform( const Core::Transform& transform ) {
98 m_transformChanged = true;
99 m_doubleBufferedTransform = transform;
100}
101
102inline void Entity::setTransform( const Core::Matrix4& transform ) {
103 setTransform( Core::Transform( transform ) );
104}
105
106inline const Core::Transform& Entity::getTransform() const {
107 // Radium-V2 : why a mutex on read ? there is no lock on write on this!
108 std::lock_guard<std::mutex> lock( m_transformMutex );
109 return m_transform;
110}
111
112inline const Core::Matrix4& Entity::getTransformAsMatrix() const {
113 // Radium-V2 : why a mutex on read ? there is no lock on write on this!
114 std::lock_guard<std::mutex> lock( m_transformMutex );
115 return m_transform.matrix();
116}
117
118inline uint Entity::getNumComponents() const {
119 return uint( m_components.size() );
120}
121
123 return m_transformationObservers;
124}
125
126} // namespace Scene
127} // namespace Engine
128} // 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:122
uint getNumComponents() const
Get component belonging to a given system.
Definition Entity.hpp:118
T lock(T... args)
Radium Namespaces prefix.
Definition Cage.cpp:3