Skip to content

Commit 0067e54

Browse files
committed
Added animation for all 8 directions, fixed issue with spritesheet
1 parent 867337e commit 0067e54

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

Player.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,41 @@ Player::Player()
5050
}
5151

5252
void Player::handleInput() {
53-
54-
// If left key pressed then move character left
55-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::A)) {
53+
// If both right and up key pressed then move character right and up at same time
54+
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::D) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::W)) {
55+
sprite.move({horizontalSpeed, 0.0f});
56+
sprite.move({0.0f, -verticalSpeed});
57+
// Waits for set amount of time then plays jogging north east animation
58+
timer += 0.08f;
59+
if (timer >= timerMax) {textureX += 240; animate(joggingNorthEastXStart, joggingNorthEastXEnd,
60+
joggingNorthEastYStart, joggingNorthEastYEnd); timer = 0.0f;}
61+
}
62+
// If both right and down key pressed then move character right and down at same time
63+
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::D) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::S)) {
64+
sprite.move({horizontalSpeed, 0.0f});
65+
sprite.move({0.0f, verticalSpeed});
66+
// Waits for set amount of time then plays jogging south east animation
67+
timer += 0.08f;
68+
if (timer >= timerMax) {textureX += 240; animate(joggingSouthEastXStart, joggingSouthEastXEnd,
69+
joggingSouthEastYStart, joggingSouthEastYEnd); timer = 0.0f;}
70+
}
71+
// If both left and up key pressed then move character left and up at same time
72+
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::A) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::W)) {
5673
sprite.move({-horizontalSpeed, 0.0f});
57-
// Waits for set amount of time then plays jogging west animation
74+
sprite.move({0.0f, -verticalSpeed});
75+
// Waits for set amount of time then plays jogging north west animation
5876
timer += 0.08f;
59-
if (timer >= timerMax) {textureX += 240; animate(joggingWestXStart, joggingWestXEnd,
60-
joggingWestYStart, joggingWestYEnd); timer = 0.0f;}
77+
if (timer >= timerMax) {textureX += 240; animate(joggingNorthWestXStart, joggingNorthWestXEnd,
78+
joggingNorthWestYStart, joggingNorthWestYEnd); timer = 0.0f;}
79+
}
80+
// If both left and down key pressed then move character left and down at same time
81+
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::A) && sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::S)) {
82+
sprite.move({-horizontalSpeed, 0.0f});
83+
sprite.move({0.0f, verticalSpeed});
84+
// Waits for set amount of time then plays jogging south west animation
85+
timer += 0.08f;
86+
if (timer >= timerMax) {textureX += 240; animate(joggingSouthWestXStart, joggingSouthWestXEnd,
87+
joggingSouthWestYStart, joggingSouthWestYEnd); timer = 0.0f;}
6188
}
6289
// If right key pressed then move character right
6390
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::D)) {
@@ -83,6 +110,14 @@ void Player::handleInput() {
83110
if (timer >= timerMax) {textureX += 240; animate(joggingSouthXStart, joggingSouthXEnd,
84111
joggingSouthYStart, joggingSouthYEnd); timer = 0.0f;}
85112
}
113+
// If left key pressed then move character left
114+
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::A)) {
115+
sprite.move({-horizontalSpeed, 0.0f});
116+
// Waits for set amount of time then plays jogging west animation
117+
timer += 0.08f;
118+
if (timer >= timerMax) {textureX += 240; animate(joggingWestXStart, joggingWestXEnd,
119+
joggingWestYStart, joggingWestYEnd); timer = 0.0f;}
120+
}
86121
}
87122

88123
// Reusable function for animating player

main.exe

1.5 KB
Binary file not shown.

main.obj

1.76 KB
Binary file not shown.

playerSpritesheet.png

13 KB
Loading

0 commit comments

Comments
 (0)