Skip to content

Commit c5788e6

Browse files
committed
call start() on components
1 parent ea3f7a3 commit c5788e6

File tree

2 files changed

+88
-20
lines changed

2 files changed

+88
-20
lines changed

FluffyEngine/GameObject.cpp

+28-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,41 @@ namespace Fluffy
1515
SetWorldPosition(x, y);
1616
}
1717

18+
GameObject::GameObject(const glm::vec2& position)
19+
{
20+
SetWorldPosition(position);
21+
}
22+
1823
const std::vector<Component*>& GameObject::GetAllComponents() const
1924
{
2025
return m_RawComponents;
2126
}
2227

23-
void GameObject::Update(const float deltaTime)
28+
void GameObject::Start() const
2429
{
25-
for (const auto& components : m_Components)
26-
{
27-
for (auto& component : components.second)
30+
std::ranges::for_each(m_Components,
31+
[](const auto& components)
2832
{
29-
component->Update(deltaTime);
30-
}
31-
}
33+
std::ranges::for_each(components.second,
34+
[](auto& component)
35+
{
36+
component->Start();
37+
});
38+
});
39+
}
40+
41+
void GameObject::Update(const float deltaTime) const
42+
{
43+
std::ranges::for_each(m_Components,
44+
[deltaTime](const auto& components)
45+
{
46+
std::ranges::for_each(components.second,
47+
[deltaTime](auto& component)
48+
{
49+
if (component->IsEnabled())
50+
component->Update(deltaTime);
51+
});
52+
});
3253
}
3354

3455
glm::vec2 GameObject::GetWorldPosition() const

FluffyEngine/GameObject.h

+60-13
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,60 @@ namespace Fluffy
3030
m_RawComponents.push_back(pRawComponent);
3131
}
3232

33+
pRawComponent->Awake();
34+
3335
return reinterpret_cast<ComponentType*>(pRawComponent);
3436
}
3537

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
3852
{
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());
4160
}
4261

43-
template<typename T>
44-
const std::vector<Component*>& GetComponentsOfType()
62+
template<typename ComponentType>
63+
bool TryGetComponent(ComponentType*& pOutComponent)
4564
{
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{};
4887

4988
for (const auto& component : m_Components[name])
5089
{
@@ -56,10 +95,10 @@ namespace Fluffy
5695

5796
const std::vector<Component*>& GetAllComponents() const;
5897

59-
template<typename T>
98+
template<typename ComponentType>
6099
void RemoveAllComponentsOfType()
61100
{
62-
const std::string name{ typeid(T).name() };
101+
const std::string name{ typeid(ComponentType).name() };
63102

64103
for (int i{ m_Components[name].size() }; i > 0; --i)
65104
{
@@ -68,24 +107,30 @@ namespace Fluffy
68107

69108
m_Components.erase(name);
70109
}
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; }
72115

73116
void SetParent(GameObject* pParent, const bool keepWorldPosition);
74117

75-
inline Transform GetTransform() const { return m_LocalTransform; }
118+
inline Transform& GetTransform() { return m_LocalTransform; }
119+
inline const Transform& GetTransform() const { return m_LocalTransform; }
76120
glm::vec2 GetWorldPosition() const;
77121
void SetWorldPosition(const float x, const float y);
78122
void SetWorldPosition(const glm::vec2& position);
79123
void SetLocalPosition(const float x, const float y);
80124
void SetLocalPosition(const glm::vec2& position);
81-
inline bool IsActive() const { return m_IsActive; }
125+
inline bool IsActive() const { return m_IsActive && !m_IsDestroyed; }
82126
inline void SetActive(bool isActive) { m_IsActive = isActive; }
83127
inline void Destroy() { m_IsDestroyed = true; }
84128
inline bool IsDestroyed() const { return m_IsDestroyed; }
85129
//inline bool IsLocalPositionDirty() const { return m_LocalPositionIsDirty; }
86130

87131
GameObject() = default;
88132
GameObject(const float x, const float y);
133+
GameObject(const glm::vec2& position);
89134
~GameObject() = default;
90135
GameObject(const GameObject& other) = delete;
91136
GameObject(GameObject&& other) = delete;
@@ -99,6 +144,8 @@ namespace Fluffy
99144
bool m_IsActive{ true };
100145
bool m_IsDestroyed{ false };
101146

147+
class Scene* m_pScene{ nullptr };
148+
102149
GameObject* m_pParent{ };
103150
std::vector<GameObject*> m_Children{ };
104151

0 commit comments

Comments
 (0)