@@ -30,21 +30,60 @@ namespace Fluffy
30
30
m_RawComponents.push_back (pRawComponent);
31
31
}
32
32
33
+ pRawComponent->Awake ();
34
+
33
35
return reinterpret_cast <ComponentType*>(pRawComponent);
34
36
}
35
37
36
- template <typename T>
37
- T* GetComponentOfType ()
38
+ template <typename ComponentType>
39
+ ComponentType* GetComponent ()
40
+ {
41
+ const std::string name{ typeid (ComponentType).name () };
42
+
43
+ const auto & components{ m_Components.find (name) };
44
+ if (components == m_Components.end () || components->second .empty ())
45
+ return nullptr ;
46
+
47
+ return reinterpret_cast <ComponentType*>(components->second .at (0 ).get ());
48
+ }
49
+
50
+ template <typename ComponentType>
51
+ const ComponentType* GetComponent () const
38
52
{
39
- const std::string name{ typeid (T).name () };
40
- return reinterpret_cast <T*>(m_Components[name][0 ].get ());
53
+ const std::string name{ typeid (ComponentType).name () };
54
+
55
+ const auto & components{ m_Components.find (name) };
56
+ if (components == m_Components.end () || components->second .empty ())
57
+ return nullptr ;
58
+
59
+ return reinterpret_cast <const ComponentType*>(components->second .at (0 ).get ());
41
60
}
42
61
43
- template <typename T >
44
- const std::vector<Component*>& GetComponentsOfType ( )
62
+ template <typename ComponentType >
63
+ bool TryGetComponent (ComponentType*& pOutComponent )
45
64
{
46
- const std::string name{ typeid (T).name () };
47
- std::vector<T*> components{};
65
+ pOutComponent = GetComponent<ComponentType>();
66
+ return pOutComponent != nullptr ;
67
+ }
68
+
69
+ template <typename ComponentType>
70
+ bool TryGetComponent (const ComponentType*& pOutComponent) const
71
+ {
72
+ pOutComponent = GetComponent<ComponentType>();
73
+ return pOutComponent != nullptr ;
74
+ }
75
+
76
+ template <typename ComponentType>
77
+ bool HasComponent () const
78
+ {
79
+ return GetComponent<ComponentType>() != nullptr ;
80
+ }
81
+
82
+ template <typename ComponentType>
83
+ const std::vector<Component*>& GetComponents ()
84
+ {
85
+ const std::string name{ typeid (ComponentType).name () };
86
+ std::vector<ComponentType*> components{};
48
87
49
88
for (const auto & component : m_Components[name])
50
89
{
@@ -56,10 +95,10 @@ namespace Fluffy
56
95
57
96
const std::vector<Component*>& GetAllComponents () const ;
58
97
59
- template <typename T >
98
+ template <typename ComponentType >
60
99
void RemoveAllComponentsOfType ()
61
100
{
62
- const std::string name{ typeid (T ).name () };
101
+ const std::string name{ typeid (ComponentType ).name () };
63
102
64
103
for (int i{ m_Components[name].size () }; i > 0 ; --i)
65
104
{
@@ -68,24 +107,30 @@ namespace Fluffy
68
107
69
108
m_Components.erase (name);
70
109
}
71
- void Update (const float deltaTime);
110
+
111
+ void Start () const ;
112
+ void Update (const float deltaTime) const ;
113
+ inline class Scene * GetScene () const { return m_pScene; }
114
+ inline void SetCurrentScene (class Scene * pScene) { m_pScene = pScene; }
72
115
73
116
void SetParent (GameObject* pParent, const bool keepWorldPosition);
74
117
75
- inline Transform GetTransform () const { return m_LocalTransform; }
118
+ inline Transform& GetTransform () { return m_LocalTransform; }
119
+ inline const Transform& GetTransform () const { return m_LocalTransform; }
76
120
glm::vec2 GetWorldPosition () const ;
77
121
void SetWorldPosition (const float x, const float y);
78
122
void SetWorldPosition (const glm::vec2& position);
79
123
void SetLocalPosition (const float x, const float y);
80
124
void SetLocalPosition (const glm::vec2& position);
81
- inline bool IsActive () const { return m_IsActive; }
125
+ inline bool IsActive () const { return m_IsActive && !m_IsDestroyed ; }
82
126
inline void SetActive (bool isActive) { m_IsActive = isActive; }
83
127
inline void Destroy () { m_IsDestroyed = true ; }
84
128
inline bool IsDestroyed () const { return m_IsDestroyed; }
85
129
// inline bool IsLocalPositionDirty() const { return m_LocalPositionIsDirty; }
86
130
87
131
GameObject () = default ;
88
132
GameObject (const float x, const float y);
133
+ GameObject (const glm::vec2& position);
89
134
~GameObject () = default ;
90
135
GameObject (const GameObject& other) = delete ;
91
136
GameObject (GameObject&& other) = delete ;
@@ -99,6 +144,8 @@ namespace Fluffy
99
144
bool m_IsActive{ true };
100
145
bool m_IsDestroyed{ false };
101
146
147
+ class Scene * m_pScene{ nullptr };
148
+
102
149
GameObject* m_pParent{ };
103
150
std::vector<GameObject*> m_Children{ };
104
151
0 commit comments