-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
60 lines (54 loc) · 1.84 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
#include "Player.hpp"
Player::Player() {
rect.setSize(sf::Vector2f(30,51));
rect.setPosition(150, 10*80);
rect.setFillColor(sf::Color::Cyan);
sprite.setTextureRect(sf::IntRect(0,0,30,51));
}
void Player::update() {
sprite.setPosition(rect.getPosition());
}
void Player::updateMovement() {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) {
movementSpeed += 2;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) && canMoveUp) {
direction = 1;
if(counterWalking > 2)
counterWalking = 0;
sprite.setTextureRect(sf::IntRect((int)counterWalking * 30,0,30,51));
rect.move(0,-movementSpeed);
counterWalking+=0.5;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S) && canMoveDown) {
direction = 2;
if(counterWalking > 2)
counterWalking = 0;
sprite.setTextureRect(sf::IntRect((int)counterWalking * 30,102,30,51));
rect.move(0,movementSpeed);
counterWalking+=0.5;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) && canMoveLeft) {
direction = 3;
if(counterWalking > 2)
counterWalking = 0;
sprite.setTextureRect(sf::IntRect((int)counterWalking * 30,155,30,51));
rect.move(-movementSpeed, 0);
counterWalking+=0.5;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D) && canMoveRight) {
direction = 4;
if(counterWalking > 2)
counterWalking = 0;
sprite.setTextureRect(sf::IntRect((int)counterWalking * 30,51,30,51));
rect.move(movementSpeed,0);
counterWalking+=0.5;
}
movementSpeed = 2;
}
void Player::resetMoveLimits() {
canMoveDown = canMoveUp = canMoveLeft = canMoveRight = true;
}
void Player::updateView(sf::View &v) {
v.setCenter(sf::Vector2f(rect.getPosition().x, rect.getPosition().y));
}