-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
executable file
·102 lines (76 loc) · 1.8 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
#include "player.h"
Player::Player(Sprite::Set& sprites) : Entity(sprites)
{
maxSpeed(500, 500);
maxAcceleration(500, 500);
}
/**
* Plays the crash sound.
*/
void Player::crash()
{
playSound("qrc:/assets/sounds/crash.wav");
}
/**
* Sets the "braked" flag to true.
*/
void Player::brake()
{
braked_ = true;
}
/**
* If the player is inside the game boundaries then accelerates in the x coordinates.
*
* @param a Acceleration.
*/
void Player::accelerate(const float a)
{
braked_ = false;
if (insideBoundaries())
acceleration(a, 0);
}
/**
* Handles the movement.
*
* If braked then creates an opposite acceleration to the movement until stop.
*
* Also, this move does not let it overcome the boundaries of the game.
*
* @param elapsed_time Time elapsed from the last move call.
*/
void Player::move(const float elapsed)
{
if (braked_ && speed_[0] != 0)
acceleration( 100*abs(speed_[0])/speed_[0]*(-1), 0);
if (insideBoundaries()) {
Entity::move(elapsed);
} else if (pos().x() < 0) {
braked_ = false;
acceleration(0, 0);
speed(0, 0);
setPos(0, y());
} else if (pos().x() > scene()->width() - pixmap().width()) {
braked_ = false;
acceleration(0, 0);
speed(0, 0);
setPos(scene()->width() - pixmap().width(), y());
}
}
/**
* Kills player.
*
* @param callback Function that will be called after the die animation ends.
*/
void Player::die(std::function<void ()> callback)
{
playSound("qrc:/sounds/explosion.wav");
animation("die", 0, callback);
Entity::die();
}
/**
* Verifies if the player is inside the boundaries of the game.
*/
bool Player::insideBoundaries() const
{
return (pos().x() >= 0) && (pos().x() <= (scene()->width() - pixmap().width()));
}