-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
217 lines (194 loc) · 6.72 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// Created by Goodecheeseburgers on 21/04/2020.
// ShapeShooter. A C++ / SFML space, shape shooter game.
// Copyright (C) 2020 - [email protected]
//
// https://github.com/Cheeseborgers/ShapeShooter
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
#include "Player.h"
// Private functions ---------------------------------------------------------------------------------------------------
void Player::initTexture()
{
// Load texture from file
if (!this->texture.loadFromFile("textures/ship.png"))
{
std::cout << "ERROR::PLAYER::INITTEXTURE: Failed to load texture: ship.png\n";
}
// Resize the sprite
this->sprite.scale(sf::Vector2f(0.1F, 0.1F));
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::initSprite()
{
// Set the texture to the sprite
this->sprite.setTexture(this->texture);
}
// Constructor ---------------------------------------------------------------------------------------------------------
Player::Player()
{
this->velocity = 3.F;
this->points = 0;
this->hpMax = 100;
this->hp = hpMax;
this->attackCoolDownMax = 10.F;
this->attackCoolDown = this->attackCoolDownMax;
this->initTexture();
this->initSprite();
}
// Methods -------------------------------------------------------------------------------------------------------------
void Player::move(const float dirX, const float dirY)
{
/* @return void
* Moves the players sprite in relation to its velocity
* and the given x and y params.
*/
this->sprite.move(this->velocity * dirX, this->velocity * dirY);
}
// ---------------------------------------------------------------------------------------------------------------------
bool Player::canAttack()
{
/* @return bool
* Returns whether the player can attack based on
* the attack player cool down.
* - Resets Cool down and returns true if higher or equal to max.
* - Else returns false leaving the current cool down timer value.
*/
if (this->attackCoolDown >= this->attackCoolDownMax)
{
this->attackCoolDown = 0.F;
return true;
}
return false;
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::updateAttackCoolDown()
{
/* @return void
* Increments the player attack cool down if lower
* than the max
*/
if (this->attackCoolDown < this->attackCoolDownMax)
{
this->attackCoolDown += 0.5F;
}
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::update()
{
/* @return void
* Updates player
* - Update attack cool down
*/
this->updateAttackCoolDown();
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::render(sf::RenderTarget& target)
{
/* @return void
* Draws the player sprite to the target window.
*/
target.draw(this->sprite);
}
// Accessors------------------------------------------------------------------------------------------------------------
const sf::Vector2f &Player::getPosition() const
{
/* @return const sf::Vector2f const
* returns the position of the player
*/
return this->sprite.getPosition();
}
// ---------------------------------------------------------------------------------------------------------------------
sf::FloatRect Player::getBounds() const
{
/* @return sf::FloatRect
* returns global bounds of the
* player.
*/
return this->sprite.getGlobalBounds();
}
// -------------------------------------------------------------------------------------------------------
const int &Player::getHp() const
{
/* @return const int& const
* returns the players hp
*/
return this->hp;
}
// ----------------------------------------------------------------------------------------------------------------
const int &Player::getHpMax() const
{
/* @return const int& const
* returns the players Max hp
*/
return this->hpMax;
}
// ------------------------------------------------------------------------------------------------------------
unsigned Player::getPoints() const
{
/* @return unsigned const
* returns the players points
*/
return this->points;
}
// Modifiers -----------------------------------------------------------------------------------------------------------
void Player::setPosition(sf::Vector2f pos)
{
/* @return void
* sets the position of the player
*/
this->sprite.setPosition(pos);
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::setPosition(const float x, const float y)
{
/* @return void
* sets the position of the player
*/
this->sprite.setPosition(x, y);
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::setPoints(unsigned int value)
{
/* @return void
* sets the players points by adding the value
* to the current total
*/
this->points += value;
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::setHp(const int value)
{
/* @return void
* sets the players hp to the value
*/
this->hp = value;
}
// ---------------------------------------------------------------------------------------------------------------------
void Player::loseHp(const int value)
{
/* @return void
* Decrements the current hp by the given value and ensures it does
* not fall below for display purposes.
* - Sets player hp minus param value
* - Checks and resets hp to 0 if below
*/
this->hp -= value;
if (this->hp < 0)
{
this->hp = 0;
}
}
// ---------------------------------------------------------------------------------------------------------------------