-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
155 lines (125 loc) · 2.82 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
#include "Player.h"
//Privare functions
void Player::initVariables()
{
this->movementSpeed = 5.f;
this->hpMax = 100;
this->hp = this->hpMax;
this->playerSize = 50.f;
this->outlineSize = this->playerSize / 10.f;
}
void Player::initShape()
{
this->shape.setSize(sf::Vector2f(this->playerSize, this->playerSize));
sf::Color color(0, 220, 0, 255);
this->shape.setFillColor(color);
//color.g -= 50;
//this->shape.setOutlineColor(color);
//
//This code changes the player's size, DO NOT USE IT
//
//this->shape.setOutlineThickness(this->playerSize / 10.f);
//
this->shape.setPosition(sf::Vector2f(560.f, 315.f));
}
//Constructors and Distructors
Player::Player(float x, float y)
{
this->shape.setPosition(x, y);
this->initVariables();
this->initShape();
}
Player::~Player()
{
}
//Accessors
const sf::RectangleShape& Player::getShape() const
{
return this->shape;
}
const int& Player::getHp() const
{
return this->hp;
}
const int& Player::getHpMax() const
{
return this->hpMax;
}
//Functions
void Player::takeDamage(const int damage)
{
if (this->hp > 0)
{
this->hp -= damage;
}
if (this->hp < 0)
{
this->hp = 0;
}
}
void Player::gainHealth(const int health)
{
if (this->hp < this->hpMax)
{
this->hp += health;
}
if (this->hp > this->hpMax)
{
this->hp = this->hpMax;
}
}
void Player::updateInput()
{
//Keybiard input
//Left
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //Left
{
this->shape.move(-this->movementSpeed, 0.f);
}//Right
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
this->shape.move(this->movementSpeed, 0.f);
}
//Up
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
this->shape.move(0.f, -this->movementSpeed);
}//Down
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
this->shape.move(0.f, this->movementSpeed);
}
}
void Player::updateWindowBoundsCollision(const sf::RenderTarget* target)
{
//Left
if (this->shape.getGlobalBounds().left <= 0.f)
{
this->shape.setPosition(0.f, this->shape.getGlobalBounds().top);
}
//Right
if (this->shape.getGlobalBounds().left + this->shape.getGlobalBounds().width >= target->getSize().x)
{
this->shape.setPosition(target->getSize().x - this->shape.getGlobalBounds().width, this->shape.getGlobalBounds().top);
}
//Top
if (this->shape.getGlobalBounds().top <= 0.f)
{
this->shape.setPosition(this->shape.getGlobalBounds().left, 0.f);
}
//Bottom
if (this->shape.getGlobalBounds().top + this->shape.getGlobalBounds().height >= target->getSize().y)
{
this->shape.setPosition(this->shape.getGlobalBounds().left, target->getSize().y - this->shape.getGlobalBounds().height);
}
}
void Player::update(const sf::RenderTarget* target)
{
this->updateInput();
//Window bounds collision
this->updateWindowBoundsCollision(target);
}
void Player::render(sf::RenderTarget* target)
{
target->draw(this->shape);
}