-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.cpp
160 lines (119 loc) · 3.55 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include "Player.h"
Player::~Player() {
}
Player::Player(int player, std::string name, float spawnX, float spawnY) {
this->name = name;
this->maxHealth = 100;
this->health = this->maxHealth;
this->movementSpeed = 4.f;
this->attackCooldownMax = 40.f;
this->attackCooldown = this->attackCooldownMax;
this->blockCooldownMax = 100.f;
this->blockCooldown = this->blockCooldown;
this->player = player;
this->sprite.setScale(2.f,2.f);
if (this->player == 1) {
if (!texture.loadFromFile("../assets/hrac1.png")) {
std::cout << "Could not load player1 texture" << std::endl;
exit(2);
}
this->sprite.setTexture(texture);
this->sprite.setPosition(spawnX, spawnY);
} else if (this->player == 2) {
if (!texture.loadFromFile("../assets/hrac2.png")) {
std::cout << "Could not load player2 texture" << std::endl;
exit(2);
}
this->sprite.setTexture(texture);
this->sprite.setOrigin(this->sprite.getLocalBounds().width,0.f);
this->sprite.setPosition(spawnX, spawnY);
}
}
const bool Player::canAttack() {
if (this->attackCooldown >= this->attackCooldownMax) {
return true;
}
return false;
}
const bool Player::canBlock() {
if (this->blockCooldown >= this->blockCooldownMax) {
return true;
}
return false;
}
void Player::move(const float dirX, const float dirY) {
this->sprite.move(this->movementSpeed * dirX, this->movementSpeed * dirY);
}
void Player::loseHp(const int value) {
this->health -= value;
if (this->health < 0)
this->health = 0;
}
void Player::setPosition(const float x, const float y) {
this->sprite.setPosition(x, y);
}
void Player::setPosition(const sf::Vector2f pos) {
this->sprite.setPosition(pos);
}
const sf::FloatRect Player::getBounds() const {
return this->sprite.getGlobalBounds();
}
const sf::Vector2f &Player::getPos() const {
return this->sprite.getPosition();
}
void Player::updateAttack() {
if (this->attackCooldown < this->attackCooldownMax)
this->attackCooldown += 0.5f;
}
void Player::updateBlock() {
if (this->blockCooldown < this->blockCooldownMax)
this->blockCooldown += 0.5f;
}
void Player::update() {
this->updateAttack();
this->updateBlock();
}
const int Player::getHealth() const {
return this->health;
}
const int Player::getMaxHealth() const {
return this->maxHealth;
}
void Player::setHealth(const int health) {
this->health = health;
}
const std::string Player::getName() const {
return this->name;
}
void Player::updateTexture(sf::String string) {
if (this->player == 1) {
if (!texture.loadFromFile("../assets/" + string)) {
std::cout << "Could not load player1 texture" << std::endl;
exit(2);
}
this->sprite.setTexture(texture,true);
} else if (this->player == 2) {
if (!texture.loadFromFile("../assets/" + string)) {
std::cout << "Could not load player2 texture" << std::endl;
exit(2);
}
this->sprite.setTexture(texture,true);
this->sprite.setOrigin(this->sprite.getLocalBounds().width,0.f);
}
}
void Player::setAttackCooldown() {
this->attackCooldown = 0.f;
}
void Player::setBlockCooldown() {
this->blockCooldown = 0.f;
}
bool Player::isConnected() const {
return this->connected;
}
void Player::setConnected(bool connected) {
this->connected = connected;
}
sf::Sprite Player::getSprite() {
return this->sprite;
}