-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
273 lines (226 loc) · 5.28 KB
/
Copy pathGameObject.cpp
File metadata and controls
273 lines (226 loc) · 5.28 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <iostream>
#include "GameObject.h"
#include "GameEvent.h"
#include "EventDispatcher.h"
#include "GameLevel.h"
#include "GameObjectTerrainCollision.h"
GameObject::GameObject()
: m_Sprite()
, m_MovementEnabled(true)
, m_Landed(false)
, m_OnSlope(false)
, m_IgnoringOneSidedPlatforms(false)
, m_Location()
, m_Velocity(0, 0)
, m_MaxVelocity(300, 400)
, m_FiringDirection(1,0)
, m_CollisionRect()
, m_GameLevel(nullptr)
, m_TimeBetweenPainInMs(1500)
, m_CollidesWith( CollidesWith::ALL )
{}
GameObject::~GameObject()
{
m_Dispatcher->unregisterListener(this);
}
void GameObject::init(EventDispatcher* dispatcher, GameLevel* game_level)
{
m_GameLevel = game_level;
setDispatcher(dispatcher);
}
void GameObject::updateLocation(float tick_ms, GameLevel* level, bool announce_collision)
{
if(m_MovementEnabled)
{
setOnSlope(false);
std::vector<CollidableTerrain*> collidables = level->getNearbyCollidables(getCollisionRect());
float delta_x = (m_Velocity.x * tick_ms);
float delta_y = (m_Velocity.y * tick_ms);
bool x_collided = false;
bool y_collided = false;
bool object_x_collided = false;
bool object_y_collided = false;
bool landed = false;
for(size_t i = 0; i < collidables.size(); i++)
{
if(level->resolveCollision(this, collidables[i], Axis::X, delta_x, 0))
{
x_collided = true;
}
if((delta_y == 0 && m_Landed) || level->resolveCollision(this, collidables[i], Axis::Y, 0, delta_y))
{
y_collided = true;
if(getVelocity().y >= 0) // landed on ground
landed = true;
else
setYVelocity(1); // hit head
}
}
if(x_collided == false)
{
m_LastLocation.x = m_Location.x;
m_Location.x += delta_x;
}
if(landed == false && y_collided == false)
{
m_LastLocation.y = m_Location.y;
m_Location.y += delta_y;
}
if( m_CollidesWith != CollidesWith::NONE )
{
if( level->resolveObjectCollision( this, Axis::X, delta_x, 0 ) )
object_x_collided = true;
if( level->resolveObjectCollision( this, Axis::Y, 0, delta_y ) )
object_y_collided = true;
}
if(announce_collision && (y_collided || x_collided || m_OnSlope))
{
GameObjectTerrainCollision e(this);
getDispatcher()->dispatchEvent(&e);
}
setLanded(landed);
// Default action for an item if it goes out of the level
// is to set its velocity to nil
if( m_GameLevel->isLocationInLevel( m_Location ) )
{
m_Velocity.x = 0;
m_Velocity.y = 0;
}
}
}
void GameObject::revertLocation()
{
m_Location.x = m_LastLocation.x;
m_Location.y = m_LastLocation.y;
m_Velocity.x = 0;
m_Velocity.y = 0;
}
void GameObject::revertXLocation()
{
m_Velocity.x = 0;
m_Location.x = m_LastLocation.x;
}
void GameObject::revertYLocation()
{
m_Velocity.y = 0;
m_Location.y = m_LastLocation.y;
}
void GameObject::nudge(float x, float y)
{
m_Location.x += x;
m_Location.y += y;
m_LastLocation.x += x;
m_LastLocation.y += y;
}
void GameObject::setLocation(float x, float y)
{
m_Location.x = x;
m_Location.y = y;
}
sf::Vector2f GameObject::getLocation() const
{
return m_Location;
}
void GameObject::setVelocity(float x, float y)
{
m_Velocity.x = x;
m_Velocity.y = y;
}
sf::Vector2f GameObject::getVelocity() const
{
return m_Velocity;
}
void GameObject::addVelocity(float x, float y)
{
if(abs(m_Velocity.x + x) <= m_MaxVelocity.x &&
abs(m_Velocity.y + y) <= m_MaxVelocity.y)
{
m_Velocity.x += x;
m_Velocity.y += y;
}
}
void GameObject::deductVelocity(float x, float y)
{
if(abs(m_Velocity.x - x) <= m_MaxVelocity.x &&
abs(m_Velocity.y - y) <= m_MaxVelocity.y)
{
m_Velocity.x -= x;
m_Velocity.y -= y;
}
}
void GameObject::setXVelocity(float x)
{
if(abs(x) < m_MaxVelocity.x)
{
m_Velocity.x = x;
}
}
void GameObject::setYVelocity(float y)
{
if(abs(y) < m_MaxVelocity.y)
{
m_Velocity.y = y;
}
}
sf::FloatRect GameObject::getCollisionRect()
{
sf::FloatRect rect;
rect.left = m_Location.x + m_CollisionRect.left;
rect.width = m_CollisionRect.width;
rect.top = m_Location.y + m_CollisionRect.top;
rect.height = m_CollisionRect.height;
return rect;
}
void GameObject::setCollisionRect(sf::IntRect rect)
{
m_CollisionRect.left = (float) rect.left;
m_CollisionRect.top = (float) rect.top;
m_CollisionRect.width = (float) rect.width;
m_CollisionRect.height = (float) rect.height;
}
void GameObject::flipCollisionRect()
{
float t = m_CollisionRect.width;
m_CollisionRect.left += t;
m_CollisionRect.width = -1 * t;
}
void GameObject::setRotation(float a)
{
m_Sprite.setRotation(a);
}
void GameObject::setLanded(bool landed)
{
m_Landed = landed;
}
bool GameObject::hasNotLanded()
{
return !m_Landed;
}
bool GameObject::hasLanded()
{
return m_Landed;
}
void GameObject::setOnSlope(bool on_slope)
{
m_OnSlope = on_slope;
}
bool GameObject::isOnSlope()
{
return m_OnSlope;
}
bool GameObject::isIgnoringOneSiders()
{
return m_IgnoringOneSidedPlatforms;
}
void GameObject::enableMovement()
{
m_MovementEnabled = true;
}
void GameObject::disableMovement()
{
m_MovementEnabled = false;
}
CollidesWith::Type GameObject::getCollisionType() const
{
return m_CollidesWith;
}