-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
208 lines (185 loc) · 5.99 KB
/
Copy pathGameObject.cpp
File metadata and controls
208 lines (185 loc) · 5.99 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "GameObject.h"
#include "DevelopState.h"
#include "UpdateComponent.h"
#include "RectColliderComponent.h"
#include <iostream>
#include <stdexcept>
using namespace std;
using namespace sf;
void GameObject::update(float dt)
{
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(dt);
}
}
}
}
void GameObject::draw(RenderWindow& window)
{
if (m_Active && m_HasGraphicsComponent)
{
if (m_Components[m_GraphicsComponentLocation]->enabled())
{
getGraphicsComponent()->draw(window, getTransformComponent());
}
}
}
// These three accessors index m_Components by a cached position that stays -1
// until the matching component is added. Indexing a vector with -1 is undefined
// behaviour, and it is genuinely reachable: a level file naming a component the
// factory does not recognise produces an object with none of them. Fail with a
// message that names the object instead of reading 16 bytes off the front of
// the array.
shared_ptr<GraphicsComponent> GameObject::getGraphicsComponent()
{
if (m_GraphicsComponentLocation < 0)
{
throw std::runtime_error(
std::string("GameObject::getGraphicsComponent - object tagged \"") + m_TagName + "\" has no graphics component"
);
}
return static_pointer_cast<GraphicsComponent>(m_Components[m_GraphicsComponentLocation]);
}
shared_ptr<TransformComponent> GameObject::getTransformComponent()
{
if (m_TransformComponentLocation < 0)
{
throw std::runtime_error(
std::string("GameObject::getTransformComponent - object tagged \"") + m_TagName + "\" has no transform component"
);
}
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() == ComponentType::Update)
{
m_HasUpdateComponent = true;
m_NumberUpdateComponents++;
if (m_NumberUpdateComponents == 1)
{
m_FirstUpdateComponentLocation = m_Components.size() - 1;
}
}
else if (component->getType() == ComponentType::Graphics)
{
m_HasGraphicsComponent = true;
m_GraphicsComponentLocation = m_Components.size() - 1;
}
else if (component->getType() == ComponentType::Transform)
{
m_TransformComponentLocation = m_Components.size() - 1;
}
else if (component->getType() == ComponentType::Collider && component->getSpecificType() == ComponentSpecificType::Rect)
{
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() const
{
return m_Active;
}
void GameObject::setTag(string tag)
{
m_TagName = tag;
m_Tag = toObjectTag(tag);
}
ObjectTag GameObject::getTag() const
{
return m_Tag;
}
const std::string& GameObject::getTagName() const
{
return m_TagName;
}
void GameObject::start(GameObjectSharer* gos)
{
auto it = m_Components.begin();
auto end = m_Components.end();
for (; it != end; ++it)
{
(*it)->start(gos, this);
}
}
shared_ptr<Component> GameObject::getComponentByTypeAndSpecificType(ComponentType type, ComponentSpecificType specificType)
{
auto it = m_Components.begin();
auto end = m_Components.end();
for (; it != end; ++it)
{
if ((*it)->getType() == type)
{
if ((*it)->getSpecificType() == specificType)
{
return (*it);
}
}
}
// This used to return m_Components[0]. Every caller immediately
// static_pointer_cast's the result to a concrete component type, so
// returning the wrong component is undefined behaviour that shows up later
// as inexplicable movement or a crash in an unrelated system.
throw std::runtime_error(
std::string("GameObject::getComponentByTypeAndSpecificType - object tagged \"") + m_TagName +
"\" has no component of type \"" + toString(type) + "\" / \"" + toString(specificType) + "\""
);
}
FloatRect& GameObject::getEncompassingRectCollider()
{
if (m_HasCollider)
{
return (static_pointer_cast<RectColliderComponent>(
m_Components[m_FirstRectColliderComponentLocation]
))->getColliderRectF();
}
// Previously this fell off the end of the function, which is undefined
// behaviour: the caller got a reference to whatever happened to be in the
// return register. Callers are expected to gate on hasCollider(); this
// degenerate rect intersects nothing, so a missed check misbehaves
// visibly instead of corrupting memory.
static FloatRect noCollider(0.f, 0.f, 0.f, 0.f);
return noCollider;
}
string GameObject::getEncompassingRectColliderTag()
{
return (static_pointer_cast<RectColliderComponent>(m_Components[m_FirstRectColliderComponentLocation]))->getColliderTag();
}
shared_ptr<UpdateComponent> GameObject::getFirstUpdateComponent()
{
if (m_FirstUpdateComponentLocation < 0)
{
throw std::runtime_error(
std::string("GameObject::getFirstUpdateComponent - object tagged \"") + m_TagName + "\" has no update component"
);
}
return static_pointer_cast<UpdateComponent>(m_Components[m_FirstUpdateComponentLocation]);
}
bool GameObject::hasCollider() const
{
return m_HasCollider;
}
bool GameObject::hasUpdateComponent() const
{
return m_HasUpdateComponent;
}