-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
145 lines (114 loc) · 2.52 KB
/
Player.cpp
File metadata and controls
145 lines (114 loc) · 2.52 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
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
#include "Player.h"
#include<iostream>
using namespace sf;
Player::Player(std::string nameTexture, std::string controls, int health)
{
this->controls = controls;
this->speed = 200.0f;
this->jumpHeight = 100.0f;
row = 0;
faceRight = true;
texture = new Texture;
texture->loadFromFile(nameTexture);
body.setSize(Vector2f(50.0f, 50.0f));
body.setOrigin(body.getSize() / 2.0f);
body.setPosition({ 206.0f, 206.0f });
body.setPosition(50.0f, 500.0f);
body.setTexture(texture);
animation = new Animation(texture, Vector2u(3, 2), 0.3f);
this->health = health;
this->winner = false;
}
Player::~Player()
{
}
void Player::Update(float deltaTime)
{ //Velocity tiene que ver con la gravedad
velocity.x = 0.0f;
if(controls == "letters"){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
velocity.x -= speed;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
velocity.x += speed;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && canJump)
{
canJump = false;
velocity.y = -sqrtf(2.0f * 981.0f * jumpHeight);
}
}
else if(controls == "arrows"){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
velocity.x -= speed;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
velocity.x += speed;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && canJump)
{
canJump = false;
velocity.y = -sqrtf(2.0f * 981.0f * jumpHeight);
}
}
velocity.y += 981.0f * deltaTime;
if (velocity.x == 0.0f)
{
row = 0;
}
else
{
row = 1;
if (velocity.x > 0.0f)
faceRight = true;
else
faceRight = false;
}
animation->Update(row, deltaTime, faceRight);
body.setTextureRect(animation->uvRect);
body.move(velocity * deltaTime);
}
void Player::Draw(RenderWindow& window)
{
window.draw(body);
}
void Player::OnCollision(Vector2f direction)
{
if (direction.x < 0.0f)
{
//Colision on the left
velocity.x = 0.0f;
}
else if (direction.x > 0.0f)
{
//Colision on the right
velocity.x = 0.0f;
}
if (direction.y < 0.0f)
{
//Colision on the bottom(creo que es abajo)
velocity.y = 0.0f;
canJump = true;
}
else if (direction.y > 0.0f)
{
//Colision con arriba
velocity.y = 0.0f;
}
}
void Player::SetPosition(sf::Vector2f position) {
body.setPosition(position);
this->initialPosition = position;
}
void Player::die() {
health--;
body.setPosition(initialPosition);
}
bool Player::alive() {
return (health >= 1);
}
int Player::getHealth() {
return this->health;
}
void Player::setWin(bool state) {
this->winner = state;
}
bool Player::isWinner() {
return winner;
}