-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.cpp
194 lines (170 loc) · 3.84 KB
/
GameObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "DevelopState.h"
#include "GameObject.h"
#include <iostream>
#include "UpdateComponent.h"
#include "RectColliderComponent.h"
void GameObject::update(float fps)
{
if (m_Active && m_HasUpdateComponent)
{
for (int i = m_FirstUpdateComponentLocation; i <
m_FirstUpdateComponentLocation +
m_NumberUpdateComponents; i++)
{
shared_ptr<UpdateComponent> tempUpdate =
static_pointer_cast<UpdateComponent>(m_Components[i]);
if (tempUpdate->enabled())
{
tempUpdate->update(fps);
}
}
}
}
void GameObject::draw(RenderWindow& window)
{
if (m_Active && m_HasGraphicsComponent)
{
if (m_Components[m_GraphicsComponentLocation]->enabled())
{
getGraphicsComponent()->draw(window,
getTransformComponent());
}
}
}
shared_ptr<GraphicsComponent> GameObject::getGraphicsComponent()
{
return static_pointer_cast<GraphicsComponent>(
m_Components[m_GraphicsComponentLocation]);
}
shared_ptr<TransformComponent> GameObject::getTransformComponent()
{
return static_pointer_cast<TransformComponent>(
m_Components[m_TransformComponentLocation]);
}
void GameObject::addComponent(shared_ptr<Component> component)
{
m_Components.push_back(component);
component->enableComponent();
if (component->getType() == "state")
{
m_HasStateComponent = true;
m_StateComponentLocation = m_Components.size() - 1;
}
else if (component->getType() == "update")
{
m_HasUpdateComponent = true;
m_NumberUpdateComponents++;
if (m_NumberUpdateComponents == 1)
{
m_FirstUpdateComponentLocation =
m_Components.size() - 1;
}
}
else if (component->getType() == "graphics")
{
// No iteration in the draw method required
m_HasGraphicsComponent = true;
m_GraphicsComponentLocation = m_Components.size() - 1;
}
else if (component->getType() == "transform")
{
// Remember where the Transform component is
m_TransformComponentLocation = m_Components.size() - 1;
}
else if (component->getType() == "collider" &&
component->getSpecificType() == "rect")
{
// Remember where the collider component(s) is
m_HasCollider = true;
m_NumberRectColliderComponents++;
if (m_NumberRectColliderComponents == 1)
{
m_FirstRectColliderComponentLocation =
m_Components.size() - 1;
}
}
}
void GameObject::setActive()
{
m_Active = true;
}
void GameObject::setInactive()
{
m_Active = false;
}
bool GameObject::isActive()
{
return m_Active;
}
void GameObject::setTag(String tag)
{
m_Tag = "" + tag;
}
std::string GameObject::getTag()
{
return m_Tag;
}
void GameObject::start(GameObjectSharer* gos)
{
auto it = m_Components.begin();
auto end = m_Components.end();
for (it;
it != end;
++it)
{
(*it)->start(gos, this);
}
}
// Slow only use in init and start
shared_ptr<Component> GameObject::getComponentByTypeAndSpecificType(
string type, string specificType) {
auto it = m_Components.begin();
auto end = m_Components.end();
for (it;
it != end;
++it)
{
if ((*it)->getType() == type)
{
if ((*it)->getSpecificType() == specificType)
{
return (*it);
}
}
}
#ifdef debuggingErrors
cout <<
"GameObject.cpp::getComponentByTypeAndSpecificType-"
<< "COMPONENT NOT FOUND ERROR!"
<< endl;
#endif
return m_Components[0];
}
FloatRect& GameObject::getEncompassingRectCollider()
{
if (m_HasCollider)
{
return (static_pointer_cast<RectColliderComponent>(
m_Components[m_FirstRectColliderComponentLocation]))
->getColliderRectF();
}
}
string GameObject::getEncompassingRectColliderTag()
{
return static_pointer_cast<RectColliderComponent>(
m_Components[m_FirstRectColliderComponentLocation])->
getColliderTag();
}
shared_ptr<UpdateComponent> GameObject::getFirstUpdateComponent()
{
return static_pointer_cast<UpdateComponent>(
m_Components[m_FirstUpdateComponentLocation]);
}
bool GameObject::hasCollider()
{
return m_HasCollider;
}
bool GameObject::hasUpdateComponent()
{
return m_HasUpdateComponent;
}