-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPlayer.cpp
107 lines (87 loc) · 3.02 KB
/
CPlayer.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
#include "CApplication.h"
#include "CPlayer.h"
extern CApplication Game;
CPlayer::CPlayer(sf::Texture& playerTexture, sf::Texture& missileTexture)
{
m_Shots.reserve(MAX_ALLOWED_SHOTS);
m_Player.setTexture(playerTexture);
m_Missile.setTexture(missileTexture);
m_Health.setFillColor(sf::Color::Green);
m_Health.setPosition(Game.m_Window->getSize().x * 0.725f, Game.m_Window->getSize().y * 0.02f);
m_HealthBackground.setFillColor(sf::Color::Color(50, 50, 50, 200));
m_HealthBackground.setSize({200.0f + 2.0f, 15.0f + 2.0f});
m_HealthBackground.setPosition(m_Health.getPosition().x- 1, m_Health.getPosition().y - 1);
}
CPlayer::~CPlayer()
{
}
void CPlayer::update(float& dt) {
std::string itemCollide = Game.m_Items->collide(m_Player);
if(itemCollide == "Rock") {
m_fHealth -= Game.m_Items->ROCK_DAMAGE;
if(m_fHealth <= 0) {
onPlayerDie();
return Game.Game_destroyPlayer();
}
// Set health color according to the percentage
m_Health.setFillColor(sf::Color::Color(255*(1.0f - m_fHealth), 255*m_fHealth, 0));
} else if(itemCollide == "Health") {
m_fHealth += Game.m_Items->HEALTH_POINTS;
if(m_fHealth > 1.0f)
m_fHealth = 1.0f;
// Set health color according to the percentage
m_Health.setFillColor(sf::Color::Color(255 * (1.0f - m_fHealth), 255 * m_fHealth, 0));
}
std::vector<sf::Sprite>::iterator it;
for(it = m_Shots.begin(); it != m_Shots.end();) {
if(it->getPosition().x > Game.m_WindowResolution.width) {
it = m_Shots.erase(it);
} else if(Game.m_Items->collide(*it) == "Rock") {
it = m_Shots.erase(it);
onPlayerShootRock();
} else {
moveShot(dt, (*it));
it++;
}
}
}
void CPlayer::shoot() {
if(m_Shots.size() >= MAX_ALLOWED_SHOTS || m_LastShoot.getElapsedTime().asSeconds() < MIN_SHOT_WAIT)
return;
sf::Sprite missile = sf::Sprite(m_Missile);
missile.setPosition(m_Player.getGlobalBounds().width + 5, m_Player.getPosition().y - 1);
m_Shots.push_back(missile);
m_LastShoot.restart();
}
void CPlayer::move(float& dt, signed short int direction) {
float old_y = m_Velocity.y;
m_Velocity.y = (direction * m_Units) * dt;
// Create a "slow down" effect if direction is 0
if(direction == 0 && dt < 1) {
m_Velocity.y = old_y > 0 ? old_y - dt*(float)9.8 : old_y + dt*(float)9.8;
}
if(m_Player.getPosition().y + m_Velocity.y >= Game.m_WindowResolution.height - m_Player.getGlobalBounds().height - 5
|| m_Player.getPosition().y + m_Velocity.y <= m_Player.getGlobalBounds().height + 5)
return;
m_Player.move(0, m_Velocity.y);
}
void CPlayer::moveShot(float& dt, sf::Sprite& missile) {
missile.move(m_Units*dt, 0);
}
void CPlayer::draw() {
Game.m_Window->draw(m_HealthBackground);
m_Health.setSize({200.0f * m_fHealth, 15.0f});
Game.m_Window->draw(m_Health);
Game.m_Window->draw(m_Player);
std::vector<sf::Sprite>::iterator it;
for(it = m_Shots.begin(); it != m_Shots.end(); it++) {
Game.m_Window->draw(*it);
}
}
void CPlayer::onPlayerShootRock() {
(*Game.m_ScoreManager) += 1;
Game.m_SoundManager->playSound("hit.ogg");
}
void CPlayer::onPlayerDie() {
Game.m_ScoreManager->reset();
}