Skip to content

Commit 867337e

Browse files
committed
Made player handleInput code more compact
1 parent 2666251 commit 867337e

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

Player.cpp

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,65 +51,37 @@ Player::Player()
5151

5252
void Player::handleInput() {
5353

54-
// If left key pressed then move character left
54+
// If left key pressed then move character left
5555
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::A)) {
5656
sprite.move({-horizontalSpeed, 0.0f});
57-
58-
// jogging left animation
57+
// Waits for set amount of time then plays jogging west animation
5958
timer += 0.08f;
60-
// Will wait for set amount of time
61-
if (timer >= timerMax) {
62-
textureX += 240;
63-
// Call animate function to play the jogging left animation
64-
animate(joggingWestXStart, joggingWestXEnd,
65-
joggingWestYStart, joggingWestYEnd);
66-
timer = 0.0f;
67-
}
59+
if (timer >= timerMax) {textureX += 240; animate(joggingWestXStart, joggingWestXEnd,
60+
joggingWestYStart, joggingWestYEnd); timer = 0.0f;}
6861
}
6962
// If right key pressed then move character right
7063
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::D)) {
7164
sprite.move({horizontalSpeed, 0.0f});
72-
73-
// jogging right animation
65+
// Waits for set amount of time then plays jogging east animation
7466
timer += 0.08f;
75-
// Will wait for set amount of time
76-
if (timer >= timerMax) {
77-
textureX += 240;
78-
// Call animate function to play the jogging right animation
79-
animate(joggingEastXStart, joggingEastXEnd,
80-
joggingEastYStart, joggingEastYEnd);
81-
timer = 0.0f;
82-
}
67+
if (timer >= timerMax) {textureX += 240; animate(joggingEastXStart, joggingEastXEnd,
68+
joggingEastYStart, joggingEastYEnd); timer = 0.0f;}
8369
}
8470
// If up key pressed then move character up
8571
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::W)) {
8672
sprite.move({0.0f, -verticalSpeed});
87-
88-
// jogging up animation
73+
// Waits for set amount of time then plays jogging north animation
8974
timer += 0.08f;
90-
// Will wait for set amount of time
91-
if (timer >= timerMax) {
92-
textureX += 240;
93-
// Call animate function to play the jogging up animation
94-
animate(joggingNorthXStart, joggingNorthXEnd,
95-
joggingNorthYStart, joggingNorthYEnd);
96-
timer = 0.0f;
97-
}
75+
if (timer >= timerMax) {textureX += 240; animate(joggingNorthXStart, joggingNorthXEnd,
76+
joggingNorthYStart, joggingNorthYEnd); timer = 0.0f;}
9877
}
9978
// If down key pressed then move character down
10079
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::S)) {
10180
sprite.move({0.0f, verticalSpeed});
102-
103-
// jogging down animation
81+
// Waits for set amount of time then plays jogging south animation
10482
timer += 0.08f;
105-
// Will wait for set amount of time
106-
if (timer >= timerMax) {
107-
textureX += 240;
108-
// Call animate function to play the jogging down animation
109-
animate(joggingSouthXStart, joggingSouthXEnd,
110-
joggingSouthYStart, joggingSouthYEnd);
111-
timer = 0.0f;
112-
}
83+
if (timer >= timerMax) {textureX += 240; animate(joggingSouthXStart, joggingSouthXEnd,
84+
joggingSouthYStart, joggingSouthYEnd); timer = 0.0f;}
11385
}
11486
}
11587

0 commit comments

Comments
 (0)