-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
133 lines (112 loc) · 2.54 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
#include "Player.h"
void Player::initVariables()
{
this->movementSpeed = 1.f;
this->cooldownAttackMax = 10.f;
this->cooldownAttack = this->cooldownAttackMax;
this->lives = 3;
this->state = State::IDLE;
}
void Player::initTexture()
{
// load texture from file
if (!this->texture.loadFromFile("Textures/player.png"))
{
std::cout << "ERROR::PLAYER::INITTEXTURE:: Could not load texture file."<< "\n";
}
}
void Player::initSprite()
{
this->sprite.setTexture(this->texture);
this->currentFrame = sf::IntRect(0, 0, 128, 128);
this->sprite.setTextureRect(this->currentFrame);
this->sprite.scale(0.5f, 0.5f);
this->sprite.setPosition(315, 570);
}
Player::Player()
{
this->initVariables();
this->initTexture();
this->initSprite();
}
Player::~Player()
{
}
const sf::Vector2f& Player::getPosition() const
{
return this->sprite.getPosition();
}
const sf::FloatRect Player::getBounds() const
{
return this->sprite.getGlobalBounds();
}
void Player::setPosition(const float x, const float y)
{
this->sprite.setPosition(x, y);
}
const bool Player::canAttack()
{
if (this->cooldownAttack >= this->cooldownAttackMax) {
this->cooldownAttack = 0.f;
return true;
}
return false;
}
void Player::movePlayer(const float dirX, const float dirY)
{
this->sprite.move(this->movementSpeed * dirX, this->movementSpeed * dirY);
}
void Player::updateAttackCooldown()
{
/**
@return void
Defines period of time between shooting bullets
Sets pause between shots
*/
if (this->cooldownAttack < this->cooldownAttackMax)
this->cooldownAttack += 0.5f;
}
void Player::update()
{
if (this->state == State::EXPLODING)
{
if (this->currentFrame.left == 0)
{
// start explosion animation
this->animationTimer.restart();
this->currentFrame.left = 128;
}
else
{
// continue animation
if (this->animationTimer.getElapsedTime().asSeconds() >= 0.3f)
{
this->currentFrame.left += 128;
// stop animation
if (this->currentFrame.left > 1408)
{
if (this->lives != 0)
{
this->lives -= 1;
this->state = State::IDLE;
this->currentFrame.left = 0;
}
if (this->lives == 0)
{
this->currentFrame.left = 768.f;
}
}
this->animationTimer.restart();
}
}
this->sprite.setTextureRect(this->currentFrame);
}
else {
// allows shooting
this->updateAttackCooldown();
}
}
void Player::render(sf::RenderTarget& target)
{
target.draw(this->sprite);
}