-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
588 lines (537 loc) · 13.9 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include "Player.h"
/// <summary>
/// Gets unit vector using the formula (the vector divided by its length)
/// x = x / sqrt(x*x + y*y)
/// y = y / sqrt(x*x + y*y)
/// </summary>
/// <param name=""></param>
/// <returns></returns>
sf::Vector2f Player::getUnitVector(sf::Vector2f vector)
{
float magnitude = sqrt(vector.x * vector.x + vector.y * vector.y);
if (magnitude != 0)
{
return sf::Vector2f(vector.x / magnitude, vector.y / magnitude);
}
else
{
return sf::Vector2f(0, 0);
}
}
float const Player::GRAVITY = (9.81f);
/// <summary>
/// Default constructor
/// </summary>
Player::Player()
: m_coefficientOfFriction(0.8f)
, m_force(0)
, m_maxForce(300)
, m_forceIncrement(8)
, m_position(0.0f, 0.0f)
, m_velocity(0.0f, 0.0f)
, m_acceleration(0.0f, 0.0f)
, m_texture()
, m_sprite()
, m_playerState(PlayerState::Ground)
, m_animState(AnimState::Idle)
, m_gravity(GRAVITY * PIXEL_TO_UNIT)
, m_animator(thor::Animator<sf::Sprite, AnimState>())
, m_textBox()
{
initSound();
initSprite();
m_standPlatform = nullptr;
m_textBox.setString("0" + TEXT);
}
Player::Player(sf::Vector2f const & pos)
: m_coefficientOfFriction(0.8f)
, m_force(0)
, m_maxForce(300)
, m_forceIncrement(8)
, m_position(pos)
, m_velocity(0.0f, 0.0f)
, m_acceleration(0.0f, 0.0f)
, m_texture()
, m_sprite()
, m_playerState(PlayerState::Ground)
, m_animState(AnimState::Idle)
, m_gravity(GRAVITY * PIXEL_TO_UNIT)
, m_animator(thor::Animator<sf::Sprite, AnimState>())
, m_textBox()
{
initSound();
initSprite();
m_standPlatform = nullptr;
m_textBox.setString("0" + TEXT);
}
/// <summary>
/// Default destructor
/// </summary>
Player::~Player()
{
}
/// <summary>
/// main player update loop
/// </summary>
/// <param name="dt"> delta time, time since last update </param>
void Player::update(const double & dt)
{
m_positionPrev = m_position;
/* SEB */
//acceleration = -coeffFriction*g*unitVelocity
m_acceleration.x = -m_coefficientOfFriction * m_gravity * getUnitVector(m_velocity).x;
//Velocity = Velocity + acceleration* time
m_velocity.x += m_acceleration.x * dt;
//Position = Position + Velocity* time + 0.5*acceleration*(time)2
m_position.x += m_velocity.x * dt + (0.5f * m_acceleration.x * (dt * dt));
/*-------------------------------------------------------------*/
processInput();
trackAnimStates();
m_animator.update(sf::seconds(dt));
m_animator.animate(m_sprite);
adjustSprite();
switch (m_playerState)
{
case Player::PlayerState::Ground:
if (m_standPlatform != nullptr)
{
m_velocity.y = 0.0f;
m_position.y = m_standPlatform->getBounds().top - (m_sprite.getGlobalBounds().height * (1.0f + SCALE));
}
else
{
m_playerState = PlayerState::Fall;
}
break;
case Player::PlayerState::Jump:
m_velocity.y += m_gravity * dt;
m_position.y += m_velocity.y * dt + (0.5f * m_gravity * (dt*dt));
m_velocity.y += m_gravity * dt;
m_position.y += m_velocity.y * dt + (0.5f * m_gravity * (dt*dt));
if (m_velocity.y >= 1.0f)
{
m_playerState = PlayerState::Fall;
}
if (m_standPlatform != nullptr && m_standPlatformPrev != nullptr &&
m_standPlatformPrev->getBounds().top > m_standPlatform->getBounds().top)
{
m_heightTravelled += (m_positionPrev.y - m_position.y)* 0.01f;
m_textBox.setString(std::to_string(static_cast<int>(m_heightTravelled)) + TEXT);
}
break;
case Player::PlayerState::Fall:
m_velocity.y += m_gravity * dt;
m_position.y += m_velocity.y * dt + (0.5f * m_gravity * (dt*dt));
m_velocity.y += m_gravity * dt;
m_position.y += m_velocity.y * dt + (0.5f * m_gravity * (dt*dt));
break;
default:
break;
}
}
/// <summary>
/// main player draw
/// </summary>
/// <param name="window"> target draw window </param>
void Player::draw(sf::RenderWindow & window)
{
window.draw(m_textBox);
window.draw(m_sprite);
}
/// <summary>
/// Loads texture from passed file path
/// </summary>
/// <param name="filePath"> file path and file name </param>
/// <returns> returns true if loaded successfully, else false </returns>
bool Player::loadTexture(const sf::String & filePath)
{
bool loaded = m_texture.loadFromFile(filePath);
m_sprite.setTexture(m_texture);
sf::FloatRect rect = m_sprite.getGlobalBounds();
m_sprite.setOrigin(rect.width / 2.0f, rect.height / 2.0f);
if ( SIZE.x > static_cast<float>(m_texture.getSize().x))
{
m_sprite.setScale(SIZE.x, m_sprite.getScale().y);
}
if (SIZE.y > static_cast<float>(m_texture.getSize().y))
{
m_sprite.setScale(m_sprite.getScale().x, SIZE.y);
}
return loaded;
}
bool Player::loadFont(const sf::String & filePath)
{
bool loaded = m_font.loadFromFile(filePath);
m_textBox.setFont(m_font);
m_textBox.setPosition(700.0f, 30.0f);
return loaded;
}
void Player::addAnimTextures(TextureCollection ptrTextureCollection)
{
m_textureMap = TextureCollection(ptrTextureCollection);
int animCount = 0;
/** IDLE ANIMATION **/
addAnimRects(199, 288, 5, 4);
m_animator.addAnimation(AnimState::Idle, *(m_animations.at(animCount)), IDLE_DUR);
animCount++;
/** RUNNING ANIMATION **/
addAnimRects(313, 297, 5, 4);
m_animator.addAnimation(AnimState::Run, *(m_animations.at(animCount)), RUN_DUR);
animCount++;
/** JUMP START ANIMATION **/
addAnimRects(237, 366, 5, 4);
m_animator.addAnimation(AnimState::JumpStart, *(m_animations.at(animCount)), JUMP_START_DUR);
animCount++;
/** JUMP LOOP ANIMATION **/
addAnimRects(234, 344, 5, 4);
m_animator.addAnimation(AnimState::JumpLoop, *(m_animations.at(animCount)), JUMP_LOOP_DUR);
animCount++;
/** FALL START ANIMATION **/
addAnimRects(279, 360, 5, 4);
m_animator.addAnimation(AnimState::FallStart, *(m_animations.at(animCount)), FALL_START_DUR);
animCount++;
/** FALL LOOP ANIMATION **/
addAnimRects(278, 357, 5, 4);
m_animator.addAnimation(AnimState::FallLoop, *(m_animations.at(animCount)), FALL_LOOP_DUR);
}
void Player::addAnimRects(int width, int height, int framesWidth, int framesHeight)
{
std::unique_ptr<thor::FrameAnimation> ptrAnimation;
ptrAnimation.reset(new thor::FrameAnimation());
sf::IntRect textRect(0, 0, width, height);
for (int x = 0; x < (width * framesWidth); x += width)
{
for (int y = 0; y < (height * framesHeight); y += height)
{
textRect.left = x; textRect.top = y;
ptrAnimation->addFrame(1.0f, textRect);
}
}
m_animations.push_back(std::move(ptrAnimation));
}
/// <summary>
/// takes input based on current player state
/// </summary>
void Player::processInput()
{
switch (m_playerState)
{
case Player::PlayerState::Ground:
lateralMovement(1.0f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
playSound();
m_velocity.y += JUMP_FORCE.y * PIXEL_TO_UNIT;
m_playerState = PlayerState::Jump;
}
break;
case Player::PlayerState::Jump:
lateralMovement(0.7f);
break;
case Player::PlayerState::Fall:
lateralMovement(0.7f);
break;
default:
break;
}
}
void Player::trackAnimStates()
{
switch (m_playerState)
{
case Player::PlayerState::Ground:
if (m_velocity.x != 0)
{
if (m_animState != AnimState::Run)
{
if (m_animator.isPlayingAnimation())
{
m_animator.stopAnimation();
}
m_animState = AnimState::Run;
m_sprite.setTexture(m_textureMap[m_animState]);
m_sprite.setTextureRect(sf::IntRect(0, 0, 1, 1));
m_animator.playAnimation(m_animState, true);
}
}
else
{
if (m_animState != AnimState::Idle)
{
if (m_animator.isPlayingAnimation())
{
m_animator.stopAnimation();
}
m_animState = AnimState::Idle;
m_sprite.setTexture(m_textureMap[m_animState]);
m_sprite.setTextureRect(sf::IntRect(0, 0, 1, 1));
m_animator.playAnimation(m_animState, true);
}
}
break;
case Player::PlayerState::Jump:
if (m_animState == AnimState::Idle || m_animState == AnimState::Run)
{
if (m_animState != AnimState::JumpStart)
{
if (m_animator.isPlayingAnimation())
{
m_animator.stopAnimation();
}
m_animState = AnimState::JumpStart;
m_sprite.setTexture(m_textureMap[m_animState]);
m_sprite.setTextureRect(sf::IntRect(0, 0, 1, 1));
m_animator.playAnimation(m_animState, false);
}
}
else if(m_animState == AnimState::JumpStart)
{
if (m_animator.isPlayingAnimation())
{
m_animator.stopAnimation();
}
m_animState = AnimState::JumpLoop;
m_sprite.setTexture(m_textureMap[m_animState]);
m_sprite.setTextureRect(sf::IntRect(0, 0, 1, 1));
m_animator.playAnimation(m_animState, true);
}
break;
case Player::PlayerState::Fall:
if (m_velocity.y >= 0)
{
if (m_animState != AnimState::FallStart)
{
if (m_animator.isPlayingAnimation())
{
m_animator.stopAnimation();
}
m_animState = AnimState::FallStart;
m_sprite.setTexture(m_textureMap[m_animState]);
m_sprite.setTextureRect(sf::IntRect(0, 0, 1, 1));
m_animator.playAnimation(m_animState, false);
}
}
if (m_animState == AnimState::FallStart)
{
if (m_animState != AnimState::FallLoop)
{
if (m_animator.isPlayingAnimation())
{
m_animator.stopAnimation();
}
m_animState = AnimState::FallLoop;
m_sprite.setTexture(m_textureMap[m_animState]);
m_sprite.setTextureRect(sf::IntRect(0, 0, 1, 1));
m_animator.playAnimation(m_animState, true);
}
}
else if (m_velocity.y == 0.0f)
{
if (m_animState != AnimState::Idle)
{
if (m_animator.isPlayingAnimation())
{
m_animator.stopAnimation();
}
m_animState = AnimState::Idle;
m_sprite.setTexture(m_textureMap[m_animState]);
m_sprite.setTextureRect(sf::IntRect(0, 0, 1, 1));
m_animator.playAnimation(m_animState, true);
}
}
break;
default:
break;
}
//switch (m_animState)
//{
//case Player::AnimState::Idle:
// break;
//case Player::AnimState::Run:
// break;
//case Player::AnimState::JumpStart:
// break;
//case Player::AnimState::JumpLoop:
// break;
//case Player::AnimState::FallStart:
// break;
//case Player::AnimState::FallLoop:
// break;
//default:
// break;
//}
}
void Player::lateralMovement(float num)
{
bool pressed = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if (m_force < m_maxForce && m_force < 0)
{
m_force += (m_forceIncrement * 2 * num);
}
else if (m_force < m_maxForce)
{
m_force += m_forceIncrement * num;
}
pressed = true;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if (m_force > -m_maxForce && m_force > 0)
{
m_force -= (m_forceIncrement * 2 * num);
}
else
{
if (m_force > -m_maxForce)
{
m_force -= m_forceIncrement * num;
}
}
pressed = true;
}
else
{
/*
if (m_force > 0.0f && m_velocity.y == 0.0f)
{
m_force -= m_forceIncrement;
}
else if (m_force < 0.0f && m_velocity.y == 0.0f)
{
m_force += m_forceIncrement;
}
/**/
m_force = 0.0f;
if (m_velocity.x <= 10.0f && m_velocity.x >= -10.0f)
{
m_velocity.x = 0.0f;
}
}
if (pressed)
{
m_velocity = (sf::Vector2f(m_force * 1.5, m_velocity.y));
}
}
void Player::initSound()
{
if (!m_soundbuffer[0].loadFromFile(".\\resources\\sounds\\Jump1.wav"))
{
std::string s("Error player texture (");
s += ".\\resources\\sounds\\WALK.wav";
s += ") was not loaded";
throw std::exception(s.c_str());
}
m_jump[0].setBuffer(m_soundbuffer[0]);
if (!m_soundbuffer[1].loadFromFile(".\\resources\\sounds\\Jump2.wav"))
{
std::string s("Error player texture (");
s += ".\\resources\\sounds\\WALK.wav";
s += ") was not loaded";
throw std::exception(s.c_str());
}
m_jump[1].setBuffer(m_soundbuffer[1]);
if (!m_soundbuffer[2].loadFromFile(".\\resources\\sounds\\Jump4.wav"))
{
std::string s("Error player texture (");
s += ".\\resources\\sounds\\WALK.wav";
s += ") was not loaded";
throw std::exception(s.c_str());
}
m_jump[2].setBuffer(m_soundbuffer[2]);
}
void Player::playSound()
{
int random = rand() % 3 + 1;
switch (random)
{
case 1:
m_jump[0].play();
break;
case 2:
m_jump[1].play();
break;
case 3:
m_jump[2].play();
break;
default:
break;
}
}
Player::PlayerState Player::getPlayerState() const
{
return m_playerState;
}
sf::FloatRect Player::getBounds() const
{
return m_sprite.getGlobalBounds();
}
void Player::land(std::shared_ptr<Platform> & landPlatform, const double & dt)
{
if (m_standPlatform != nullptr)
{
m_standPlatformPrev = std::shared_ptr<Platform>(m_standPlatform);
}
m_standPlatform = std::shared_ptr<Platform>(landPlatform);
m_playerState = PlayerState::Ground;
m_position.y = m_standPlatform->getBounds().top - m_sprite.getGlobalBounds().height;
}
void Player::checkland()
{
if (m_playerState == PlayerState::Ground && m_standPlatform != nullptr)
{
sf::FloatRect playerBox, standPlatform;
playerBox = m_sprite.getGlobalBounds();
standPlatform = m_standPlatform->getBounds();
if (
playerBox.left + playerBox.width - LEDGE_BOX_OFFSET < standPlatform.left ||
playerBox.left + LEDGE_BOX_OFFSET > standPlatform.left + standPlatform.width
)
{
m_standPlatform = nullptr;
m_playerState = PlayerState::Fall;
}
}
}
void Player::initSprite()
{
m_sprite.setScale(SCALE, SCALE);
sf::FloatRect rect = m_sprite.getGlobalBounds();
m_offset.x = rect.width / 2.0f;
m_offset.y = rect.height / 2.0f;
m_sprite.setOrigin(m_offset);
}
void Player::adjustSprite()
{
if (m_velocity.x < 0.0f)
{
m_sprite.setScale(-SCALE, SCALE);
m_offset.x = m_sprite.getGlobalBounds().width;
m_offset.x /= 2.0f;
m_offset.y = m_sprite.getGlobalBounds().height;
m_offset.y /= 2.0f;
m_sprite.setOrigin(m_offset);
m_sprite.setPosition(m_position.x + m_offset.x * (SCALE), m_position.y + m_offset.y);
}
else if (m_velocity.x > 0.0f)
{
m_sprite.setScale(SCALE, SCALE);
m_offset.x = m_sprite.getGlobalBounds().width;
m_offset.x /= 2.0f;
m_offset.y = m_sprite.getGlobalBounds().height;
m_offset.y /= 2.0f;
m_sprite.setOrigin(m_offset);
m_offset.x = -m_offset.x;
m_sprite.setPosition(m_position.x + m_offset.x * (SCALE), m_position.y + m_offset.y);
}
else
{
m_sprite.setPosition(m_position.x + m_offset.x * (SCALE), m_position.y + m_offset.y);
}
}
void Player::wallBounce(const sf::FloatRect & wall)
{
m_force = -(m_force*1.009f);
m_velocity = (sf::Vector2f(m_force * 1.5, m_velocity.y));
}