Repository navigation
Expand file tree
/
Copy pathPlayer.cpp
More file actions
108 lines (86 loc) · 2.32 KB
/
Copy pathPlayer.cpp
File metadata and controls
108 lines (86 loc) · 2.32 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
#include "Player.h"
Player::Player() {
shape.setSize(sf::Vector2f(50.f, 50.f));
shape.setFillColor(sf::Color::Cyan);
shape.setPosition(375.f, 275.f);
speed = 250.f;
shootCooldown = 0.5f;
shootTimer = shootCooldown;
totalShots = 0;
health = High;
}
void Player::update(float dt) {
if (health == High)
speed = 300.f;
else if (health == Medium)
speed = 200.f;
else
speed = 100.f;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
shape.move(0.f, -speed * dt);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
shape.move(0.f, speed * dt);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
shape.move(-speed * dt, 0.f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
shape.move(speed * dt, 0.f);
shootTimer += dt;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && shootTimer >= shootCooldown) {
sf::Vector2f pos = getPosition();
bullets.push_back(Bullet(pos.x + 25.f, pos.y));
totalShots++;
shootTimer = 0.f;
}
for (auto& b : bullets) {
b.update(dt);
}
for (auto it = bullets.begin(); it != bullets.end(); ) {
if (it->getShape().getPosition().y < 0.f) {
it = bullets.erase(it);
}
else {
++it;
}
}
if (health == High)
shape.setFillColor(sf::Color::Cyan);
else if (health == Medium)
shape.setFillColor(sf::Color::Yellow);
else
shape.setFillColor(sf::Color::Red);
}
void Player::render(sf::RenderWindow& window) {
window.draw(shape);
for (auto& b : bullets) {
window.draw(b.getShape());
}
}
sf::Vector2f Player::getPosition() const {
return shape.getPosition();
}
sf::RectangleShape& Player::getShape() {
return shape;
}
std::vector<Bullet>& Player::getBullets() {
return bullets;
}
int Player::getTotalShots() const {
return totalShots;
}
void Player::setTotalShots(int t) {
totalShots = t;
}
void Player::takeDamage() {
if (health >= Low) {
health = static_cast<HealthState>(health - 1);
}
if (health == Dead)
std::cout << "GAME OVER" << std::endl;
}
void Player::setHealth(Player::HealthState h) {
health = h;
// totalShots = 0;
}
Player::HealthState Player::getHealth() const {
return health;
}