-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
410 lines (357 loc) · 15.2 KB
/
Player.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Tilemap.h"
#include "FileStream.h"
float Player::getTop()
{
return m_top;
}
float Player::getBottom()
{
return m_bottom;
}
float Player::getLeft()
{
return m_left;
}
float Player::getRight()
{
return m_right;
}
void Player::setSoundLevel(int i)
{
if(i <= 0)
m_walking.setVolume(0);
else
m_walking.setVolume(i/2);
}
void Player::setPosition(sf::Vector2f pos)
{
m_rect.setPosition(pos);
m_sprite.setPosition(pos);
}
void Player::setInvis()
{
m_sprite.setTextureRect(sf::IntRect(0, 0, 0, 0));
vis = false;
}
void Player::setVis()
{
m_sprite.setTextureRect(sf::IntRect(0, 0, 32, 32));
vis = true;
}
bool Player::getVis()
{
return vis;
}
Player::Player()
{
m_rect.setSize(sf::Vector2f(32, 32));
m_rect.setFillColor(sf::Color::Red);
m_texture.loadFromFile("Assets/Player.png");
m_texture.setSmooth(true);
m_sprite.setTexture(m_texture);
m_sprite.setTextureRect(sf::IntRect(0, 0, 32, 32));
frameCounter = 0; switchFrame = 100; frameSpeed = 500;
source = sf::Vector2i(0, 0);
m_grassBuffer.loadFromFile("Assets/grass.flac");
m_walking.setBuffer(m_grassBuffer);
soundInt = 0;
setSoundLevel(0);
m_top = m_rect.getPosition().y;
m_bottom = m_rect.getPosition().y + m_rect.getSize().y;
m_left = m_rect.getPosition().x;
m_right = m_rect.getPosition().x + m_rect.getSize().x;
m_isMovingRight = m_isMovingLeft = false;
m_movSpeedConst = 0.0010f;
m_movSpeedMax = 0.23f;
m_gravity = 0.388;
m_inAir = 0;
m_inAirMax = 0.3f;
m_onGround = m_inAir = false;
m_onCrate = false;
m_movement = sf::Vector2f(0, 0);
vis = true;
}
void Player::HandleEvents(sf::Keyboard::Key key, bool isPressed)
{
if (key == sf::Keyboard::Key::D || key == sf::Keyboard::Key::Right)
m_isMovingRight = isPressed;
if (key == sf::Keyboard::Key::A || key == sf::Keyboard::Key::Left)
m_isMovingLeft = isPressed;
}
void Player::Update(Tilemap &t, sf::RenderWindow &window, BackgroundProcesses &b)
{
m_top = m_rect.getPosition().y;
m_bottom = m_rect.getPosition().y + m_rect.getSize().y;
m_left = m_rect.getPosition().x;
m_right = m_rect.getPosition().x + m_rect.getSize().x;
//collision with portal
if(t.getLevelMap(m_bottom/64, m_left/64) == 999 || t.getLevelMap(m_bottom/64, m_right/64) == 999 || t.getLevelMap(m_top/64, m_left/64) == 999 || t.getLevelMap(m_top/64, m_right/64) == 999)
{
setPosition(sf::Vector2f(0, 0));
t.setMap(window);
}
//respawning when falling through the bottom of the map!!
if(m_bottom >= 30*64)
{
setPosition(sf::Vector2f(0, 0));
t.setMap(t.getLevel(), window);
}
//downward collision on tilemap
if(m_bottom / 64 < 0 || m_right < 0)
m_onGround = false;
else if(int(m_bottom) % 64 != 0)
m_onGround = false;
else if( (t.getLevelMap(m_bottom/64, m_left/64) >= 100 && t.getLevelMap(m_bottom/64, m_left/64) < 200) || (t.getLevelMap(m_bottom/64, m_left/64) >= 500 && t.getLevelMap(m_bottom/64, m_left/64) < 600) || (t.getLevelMap(m_bottom/64, m_right/64) >= 100 && t.getLevelMap(m_bottom/64, m_right/64) < 200) || (t.getLevelMap(m_bottom/64, m_right/64) >= 500 && t.getLevelMap(m_bottom/64, m_right/64) < 600) )
m_onGround = true;
else
m_onGround = false;
//downward collision on platform
for(int i = 0; i < t.getPlatformSize(); i++)
{
if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i))
{}
else if(int(m_bottom) <= t.getPlatformTop(i))
m_onGround = true;
}
int whichCrate = -1;
for(int i = 0; i < t.getCrateSize(); i++)
{
//downward collision on crate
if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
{}
else if(m_bottom >= t.getCrateTop(i) && m_bottom - 2 <= t.getCrateTop(i) && m_right - 2 > t.getCrateLeft(i) && m_left + 2 < t.getCrateRight(i))
{
m_onGround = true;
m_onCrate = true;
whichCrate = i;
break;
}
else
{
m_onCrate = false;
}
}
for(int i = 0; i < t.getCrateSize(); i++)
{
//downward collision on crate
/*if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
{}
else if(m_bottom >= t.getCrateTop(i) && m_bottom - 2 <= t.getCrateTop(i) && m_right - 2 > t.getCrateLeft(i) && m_left + 2 < t.getCrateRight(i))
{
m_onGround = true;
m_onCrate = true;
}
else
{
m_onCrate = false;
} */
if(i != whichCrate)
{
//not colliding with crate
if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
{}
//on top of crate
else if(m_top <= t.getCrateBottom(i) && m_top >= (t.getCrateBottom(i) - 2))
{
t.setCratePos(i, sf::Vector2f(t.getCrateLeft(i), m_top - (t.getCrateBottom(i) - t.getCrateTop(i))));
}
//pushing crate right
else if(m_right >= t.getCrateLeft(i) && m_left < t.getCrateLeft(i))
{
if(m_movement.x > 0)
t.setCratePos(i, sf::Vector2f(m_right, t.getCrateTop(i)));
}
//pushing crate left
else if(m_left <= t.getCrateRight(i) && m_left > t.getCrateLeft(i))
{
if(m_movement.x < 0)
t.setCratePos(i, sf::Vector2f(m_left - (t.getCrateRight(i) - t.getCrateLeft(i)), t.getCrateTop(i)));
}
}
}
}
void Player::Move(sf::Time &deltaTime, Tilemap &t)
{
//moving right and slowing down
if(m_isMovingRight == true)
m_movement.x += m_movSpeedConst * deltaTime.asMilliseconds();
else if(m_isMovingRight == false && m_movement.x > 0)
{
m_movement.x -= m_movSpeedConst * 1.5 * deltaTime.asMilliseconds();
if(m_movement.x < 0)
m_movement.x = 0;
}
//moving left and slowing down
if(m_isMovingLeft == true)
m_movement.x -= m_movSpeedConst * deltaTime.asMilliseconds();
else if(m_isMovingLeft == false && m_movement.x < 0)
{
m_movement.x += m_movSpeedConst* 1.5 * deltaTime.asMilliseconds();
if(m_movement.x > 0)
m_movement.x = 0;
}
//not over max speed
if(m_movement.x >= m_movSpeedMax)
m_movement.x = m_movSpeedMax;
else if(m_movement.x <= -m_movSpeedMax)
m_movement.x = -m_movSpeedMax;
//don't move left right if colliding with wall
if(int(m_bottom) % 64 == 0 && (t.getLevelMap((m_bottom + 2)/64, m_left/64) == 192 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 191 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 191 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 190 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 192 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 592 || t.getLevelMap((m_bottom + 2)/64, m_left/64) == 591 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 591 || t.getLevelMap((m_bottom + 2)/64, m_right/64) == 590))
{}
else if((t.getLevelMap(m_bottom/64, m_left/64) == 192 || t.getLevelMap(m_bottom/64, m_left/64) == 292 || t.getLevelMap(m_bottom/64, m_left/64) == 392 || t.getLevelMap(m_bottom/64, m_left/64) == 490 || t.getLevelMap(m_bottom/64, m_left/64) == 592 || t.getLevelMap(m_bottom/64, m_left/64) == 191 || t.getLevelMap(m_bottom/64, m_left/64) == 291 || t.getLevelMap(m_bottom/64, m_left/64) == 391 || t.getLevelMap(m_bottom/64, m_left/64) == 491 || t.getLevelMap(m_bottom/64, m_left/64) == 591) && m_movement.x < 0)
m_movement.x = 0;
else if((t.getLevelMap(m_bottom/64, m_right/64) == 190 || t.getLevelMap(m_bottom/64, m_right/64) == 290 || t.getLevelMap(m_bottom/64, m_right/64) == 390 || t.getLevelMap(m_bottom/64, m_right/64) == 492 || t.getLevelMap(m_bottom/64, m_right/64) == 590 || t.getLevelMap(m_bottom/64, m_right/64) == 191 || t.getLevelMap(m_bottom/64, m_right/64) == 291 || t.getLevelMap(m_bottom/64, m_right/64) == 391 || t.getLevelMap(m_bottom/64, m_right/64) == 491 || t.getLevelMap(m_bottom/64, m_right/64) == 591) && m_movement.x > 0)
m_movement.x = 0;
else if((t.getLevelMap((m_top + 2)/64, m_left/64) == 192 || t.getLevelMap((m_top + 2)/64, m_left/64) == 292 || t.getLevelMap((m_top + 2)/64, m_left/64) == 392 || t.getLevelMap((m_top + 2)/64, m_left/64) == 490 || t.getLevelMap((m_top + 2)/64, m_left/64) == 592 || t.getLevelMap((m_top + 2)/64, m_left/64) == 191 || t.getLevelMap((m_top + 2)/64, m_left/64) == 291 || t.getLevelMap((m_top + 2)/64, m_left/64) == 391 || t.getLevelMap((m_top + 2)/64, m_left/64) == 491 || t.getLevelMap((m_top + 2)/64, m_left/64) == 591) && m_movement.x < 0)
m_movement.x = 0;
else if((t.getLevelMap((m_top + 2)/64, m_right/64) == 190 || t.getLevelMap((m_top + 2)/64, m_right/64) == 290 || t.getLevelMap((m_top + 2)/64, m_right/64) == 390 || t.getLevelMap((m_top + 2)/64, m_right/64) == 492 || t.getLevelMap((m_top + 2)/64, m_right/64) == 590 || t.getLevelMap((m_top + 2)/64, m_right/64) == 191 || t.getLevelMap((m_top + 2)/64, m_right/64) == 291 || t.getLevelMap((m_top + 2)/64, m_right/64) == 391 || t.getLevelMap((m_top + 2)/64, m_right/64) == 491 || t.getLevelMap((m_top + 2)/64, m_right/64) == 591) && m_movement.x > 0)
m_movement.x = 0;
//don't move if colliding with a colliding crate!
for(int i = 0; i < t.getCrateSize(); i++)
{
if(t.getCrateOnLeft(i) == true || t.getCrateOnRight(i) == true)
{
if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
{}
else if(m_right >= t.getCrateLeft(i) && (m_right + m_left)/2 <= t.getCrateLeft(i) && m_movement.x > 0 &&(m_bottom - 2) >= t.getCrateTop(i))
m_movement.x = 0;
else if(m_left <= t.getCrateRight(i) && (m_right + m_left)/2 >= t.getCrateLeft(i) && m_movement.x < 0 && (m_bottom - 2) >= t.getCrateTop(i))
m_movement.x = 0;
}
}
//jumping
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
m_jumped = true;
//if hit top of block, fall
if((t.getLevelMap(m_top/64, (m_left + 2)/64) == 190 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 190 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 191 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 191 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 192 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 192 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 290 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 290 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 291 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 291 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 292 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 292 ||
t.getLevelMap(m_top/64, (m_left + 2)/64) == 390 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 390 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 391 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 391 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 392 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 392 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 490 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 490 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 491 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 491 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 492 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 492 ||
t.getLevelMap(m_top/64, (m_left + 2)/64) == 590 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 590 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 591 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 591 || t.getLevelMap(m_top/64, (m_left + 2)/64) == 592 || t.getLevelMap(m_top/64, (m_right - 2)/64) == 592) && m_movement.y < 0)
m_movement.y = 0;
//setting jump stuff
if(m_jumped == true && m_onGround == true)
{
m_movement.y = -m_gravity;
m_jumped = false;
}
else
{
m_jumped = false;
if(!m_onGround)
m_movement.y += m_gravity * 0.003 * deltaTime.asMilliseconds();
else
m_movement.y = 0;
if(m_movement.y > 0.5)
m_movement.y = 0.5;
}
//move if on a platform
for(int i = 0; i < t.getPlatformSize(); i++)
{
//not touching
if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i))
{}
else if(m_bottom - 2 > t.getPlatformTop(i))
{}
else if(m_left > t.getPlatformLeft(i) && m_right < t.getPlatformRight(i))
m_movement.x += t.getPlatformMovement(i).x;
else if( m_left < t.getPlatformLeft(i) && (m_left + m_right)/2 > t.getPlatformLeft(i) && int(m_movement.x) == 0)
m_movement.x += t.getPlatformMovement(i).x;
else if( m_right > t.getPlatformRight(i) && (m_left + m_right)/2 < t.getPlatformRight(i) && int(m_movement.x) == 0)
m_movement.x += t.getPlatformMovement(i).x;
}
//move if on a crate (movement will be non-zero if crate is on a platform so...)
/*for(int i = 0; i < t.getCrateSize(); i++)
{
if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
{}
else if(int(m_bottom) <= t.getCrateTop(i))
m_movement.x += t.getCrateMovement(i).x;
}*/
//the actual move part
m_rect.move(m_movement.x, m_movement.y);
m_sprite.move(m_movement.x, m_movement.y);
//change the movement after actually moving so it doesn't mess up earlier processes
for(int i = 0; i < t.getPlatformSize(); i++)
{
//not touching
if(m_right < t.getPlatformLeft(i) || m_left > t.getPlatformRight(i) || m_top > t.getPlatformBottom(i) || m_bottom < t.getPlatformTop(i))
{}
else if(m_bottom - 2 > t.getPlatformTop(i))
{}
else if(m_left > t.getPlatformLeft(i) && m_right < t.getPlatformRight(i))
m_movement.x -= t.getPlatformMovement(i).x;
else if( m_left < t.getPlatformLeft(i) && (m_left + m_right)/2 > t.getPlatformLeft(i) && int(m_movement.x) == 0)
m_movement.x -= t.getPlatformMovement(i).x;
else if( m_right > t.getPlatformRight(i) && (m_left + m_right)/2 < t.getPlatformRight(i) && int(m_movement.x) == 0)
m_movement.x -= t.getPlatformMovement(i).x;
}
//change the movement after actually moving the crate so it doesn't mess up earlier processes
/*for(int i = 0; i < t.getCrateSize(); i++)
{
if(m_right < t.getCrateLeft(i) || m_left > t.getCrateRight(i) || m_top > t.getCrateBottom(i) || m_bottom < t.getCrateTop(i))
{}
else if(int(m_bottom) <= t.getCrateTop(i))
m_movement.x -= t.getCrateMovement(i).x;
}*/
/*if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) //FOR GOD MODE!!
m_movement.y = -0.25;
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
m_movement.y = 0.25;
else
m_movement.y = 0;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
m_movement.x = -0.25;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
m_movement.x = 0.25; */
}
void Player::Animate(sf::Time &deltaTime)
{
if(m_isMovingRight || m_isMovingLeft)
{
frameCounter += frameSpeed * deltaTime.asMilliseconds() * 0.001;
if(frameCounter >= switchFrame)
{
frameCounter = 0;
source.x++;
if(source.x * 32 >= 32*3)
source.x = 0;
}
}
if(m_isMovingRight)
source.y = 2; //source corresponds with .png
else if(m_isMovingLeft)
source.y = 1;
else if(m_onGround)
{
source.y = 0;
source.x = 1;
}
else if(!m_onGround)
{
//source.y = 3; Take out the looking back while falling: Quality of life change for CHRIS ZITNIK - WHAT A GUY
source.y = 0;
source.x = 1;
}
m_sprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32)); //make sure that texture is 32!!
}
void Player::Sound(sf::Time &deltaTime)
{
if((m_isMovingRight || m_isMovingLeft) && m_onGround)
{
if(soundInt >= m_grassBuffer.getDuration().asSeconds()*1.2)
{
m_walking.play();
soundInt = 0;
}
}
if(soundInt <= m_grassBuffer.getDuration().asSeconds()*1.2)
soundInt += deltaTime.asSeconds();
}
void Player::Draw(sf::RenderWindow &window)
{
//window.draw(m_rect);
window.draw(m_sprite);
}