-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
39 lines (37 loc) · 908 Bytes
/
Player.cpp
File metadata and controls
39 lines (37 loc) · 908 Bytes
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
#include "Player.h"
Player::Player(sf::Texture* texture, sf::Vector2u imageCount, float switchTime, float speed) :
animate(texture, imageCount, switchTime)
{
this->speed = speed;
row = 0;
faceRight = true;
body.setSize(sf::Vector2f(100.f, 200.f));
body.setPosition({ 300.f,300.f });
body.setOrigin(body.getSize() / 2.f);
body.setTexture(texture);
}
void Player::update(float deltaTime)
{
sf::Vector2f movement(0.f, 0.f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
movement.x -= speed * deltaTime;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
movement.x += speed * deltaTime;
if (movement.x == 0.f)
row = 0;
else
{
row = 1;
if (movement.x > 0.f)
faceRight = true;
else
faceRight = false;
}
animate.update(row, deltaTime, faceRight);
body.setTextureRect(animate.uvRect);
body.move(movement);
}
void Player::Draw(sf::RenderWindow& window)
{
window.draw(body);
}