-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
113 lines (109 loc) · 2.79 KB
/
player.cpp
File metadata and controls
113 lines (109 loc) · 2.79 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
109
110
111
112
113
#include "player.hpp"
//Private functions
void Player::initTexture(){
if(!this->texture.loadFromFile("Resources/Sprites/player.png"))
std::cout<<"ERROR LOADING TEXTURE FOR PLAYER!\n";
if(!this->font.loadFromFile("../Fonts/Roboto-Black.ttf"))
std::cout<<"ERROR LOADING FONT IN PLAYER.CPP\n";
}
void Player::initSprite(){
//Player
this->sprite.setTexture(this->texture);
this->sprite.scale(0.1f, 0.1f);
this->sprite.setPosition(600,800-(sprite.getGlobalBounds().height /2.f));
//Hp bar
this->hpBarFull.setPosition(650,this->sprite.getPosition().y);
this->hpBarCurrent.setPosition(650, this->sprite.getPosition().y);
this->hpStatus.setPosition(673, this->sprite.getPosition().y + 16);
}
void Player::initVariables(){
//input
this->writeCooldownMax = 3.f;
this->writeCooldown = this->writeCooldownMax;
this->hpMax = 20;
this->hp = hpMax;
//HP BAR
std::stringstream ss;
this->hpStatus.setFont(this->font);
this->hpStatus.setCharacterSize(18);
ss << hp << " / " << hpMax;
this->hpStatus.setString(ss.str());
this->hpBarFull.setFillColor(sf::Color::Transparent);
this->hpBarFull.setOutlineColor(sf::Color::White);
this->hpBarFull.setOutlineThickness(0.8f);
this->hpBarCurrent.setFillColor(sf::Color::Red);
this->hpBarFull.setSize(sf::Vector2f(100.f, 14.f));
this->hpBarCurrent.setSize(sf::Vector2f(100.f, 13.f));
//score
this->tempPoints = 0;
this->points = 0;
}
//Constructors
Player::Player(){
this->initTexture();
this->initVariables();
this->initSprite();
}
Player::~Player(){
}
//Accessors and Modifiers
int Player::getHpMax(){
return this->hpMax;
}
int Player::getHp(){
return this->hp;
}
void Player::addHp(int amount){
this->hp +=amount;
}
void Player::setHpStatus(std::string status){
this->hpStatus.setString(status);
}
std::string Player::getHpStatus(){
return this->hpStatus.getString();
}
sf::RectangleShape* Player::getHpBar(){
return &this->hpBarCurrent;
}
void Player::setHpBar(sf::RectangleShape* hpBarCurrent){
this->hpBarCurrent = *hpBarCurrent;
}
void Player::setCooldown(float val){
this->writeCooldown = val;
}
void Player::addPoints(int val){
this->points += val;
}
int Player::getTempPoints(){
return this->tempPoints;
}
void Player::addTempPoints(int val){
this->tempPoints += val;
}
int Player::getPoints(){
return this->points;
}
sf::Vector2f Player::getPos(){
return this->sprite.getPosition();
}
//Functions
bool Player::canWrite(){
if(this->writeCooldown >= writeCooldownMax){
this->writeCooldown = 0.f;
return true;
}
return false;
}
void Player::updateCooldown(){
if(this->writeCooldown < this->writeCooldownMax)
this->writeCooldown += 1.f;
}
void Player::update(){
this->updateCooldown();
}
void Player::render(sf::RenderTarget* target){
target->draw(this->sprite);
target->draw(this->hpBarCurrent);
target->draw(this->hpBarFull);
target->draw(this->hpStatus);
}