Skip to content

Commit fbc92de

Browse files
committed
Added sprint functionality for all 8 directions
1 parent 034aedb commit fbc92de

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

Player.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,47 +59,70 @@ Player::Player()
5959

6060
sprinting = false;
6161
sprintingEastXStart = 1440; sprintingEastYStart = 1200; sprintingEastXEnd = 240; sprintingEastYEnd = 1440;
62+
sprintingNorthXStart = 480; sprintingNorthYStart = 1440; sprintingNorthXEnd = 1680; sprintingNorthYEnd = 1440;
63+
sprintingNorthEastXStart = 1920; sprintingNorthEastYStart = 1440; sprintingNorthEastXEnd = 720; sprintingNorthEastYEnd = 1680;
64+
sprintingNorthWestXStart = 960; sprintingNorthWestYStart = 1680; sprintingNorthWestXEnd = 2160; sprintingNorthWestYEnd = 1680;
65+
sprintingSouthXStart = 0; sprintingSouthYStart = 1920; sprintingSouthXEnd = 1200; sprintingSouthYEnd = 1920;
66+
sprintingSouthEastXStart = 1440; sprintingSouthEastYStart = 1920; sprintingSouthEastXEnd = 240; sprintingSouthEastYEnd = 2160;
67+
sprintingSouthWestXStart = 480; sprintingSouthWestYStart = 2160; sprintingSouthWestXEnd = 1680; sprintingSouthWestYEnd = 2160;
68+
sprintingWestXStart = 1920; sprintingWestYStart = 2160; sprintingWestXEnd = 720; sprintingWestYEnd = 2400;
6269
}
6370

6471
void Player::handleInput() {
6572
// If both right and up key pressed then move character right and up at same time
6673
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)) {
6774
moving = true; northEast = true;
6875
north = false; east = false; southEast = false; south = false; southWest = false; west = false; northWest = false;
69-
sprite.move({horizontalSpeed / 1.5f, 0.0f}); sprite.move({0.0f, -verticalSpeed / 1.5f});
76+
sprite.move({horizontalSpeed / 1.5f, -verticalSpeed / 1.5f});
7077
// Waits for set amount of time then plays jogging north east animation
7178
timer += 0.08f;
72-
if (timer >= timerMax) {textureX += 240; animate(joggingNorthEastXStart, joggingNorthEastXEnd,
79+
// Sprinting animation
80+
if (timer >= timerMax && sprinting) {sprite.move({horizontalSpeed / 1.5f, -verticalSpeed / 1.5f}); textureX += 240; animate(sprintingNorthEastXStart, sprintingNorthEastXEnd,
81+
sprintingNorthEastYStart, sprintingNorthEastYEnd); timer = 0.0f;}
82+
// Jogging animation
83+
else if (timer >= timerMax) {textureX += 240; animate(joggingNorthEastXStart, joggingNorthEastXEnd,
7384
joggingNorthEastYStart, joggingNorthEastYEnd); timer = 0.0f;}
7485
}
7586
// If both right and down key pressed then move character right and down at same time
7687
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)) {
7788
moving = true; southEast = true;
7889
north = false; northEast = false; east = false; south = false; southWest = false; west = false; northWest = false;
79-
sprite.move({horizontalSpeed / 1.5f, 0.0f}); sprite.move({0.0f, verticalSpeed / 1.5f});
90+
sprite.move({horizontalSpeed / 1.5f, verticalSpeed / 1.5f});
8091
// Waits for set amount of time then plays jogging south east animation
8192
timer += 0.08f;
82-
if (timer >= timerMax) {textureX += 240; animate(joggingSouthEastXStart, joggingSouthEastXEnd,
93+
// Sprinting animation
94+
if (timer >= timerMax && sprinting) {sprite.move({(horizontalSpeed / 1.5f) * 3, (verticalSpeed / 1.5f) * 3}); textureX += 240; animate(sprintingSouthEastXStart, sprintingSouthEastXEnd,
95+
sprintingSouthEastYStart, sprintingSouthEastYEnd); timer = 0.0f;}
96+
// Jogging animation
97+
else if (timer >= timerMax) {textureX += 240; animate(joggingSouthEastXStart, joggingSouthEastXEnd,
8398
joggingSouthEastYStart, joggingSouthEastYEnd); timer = 0.0f;}
8499
}
85100
// If both left and up key pressed then move character left and up at same time
86101
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)) {
87-
sprite.move({-horizontalSpeed / 1.5f, 0.0f}); sprite.move({0.0f, -verticalSpeed / 1.5f});
102+
sprite.move({-horizontalSpeed / 1.5f, -verticalSpeed / 1.5f});
88103
moving = true; northWest = true;
89104
north = false; northEast = false; east = false; southEast = false; south = false; southWest = false; west = false;
90105
// Waits for set amount of time then plays jogging north west animation
91106
timer += 0.08f;
92-
if (timer >= timerMax) {textureX += 240; animate(joggingNorthWestXStart, joggingNorthWestXEnd,
107+
// Sprinting animation
108+
if (timer >= timerMax && sprinting) {sprite.move({-horizontalSpeed / 1.5f, -verticalSpeed / 1.5f}); textureX += 240; animate(sprintingNorthWestXStart, sprintingNorthWestXEnd,
109+
sprintingNorthWestYStart, sprintingNorthWestYEnd); timer = 0.0f;}
110+
// Jogging animation
111+
else if (timer >= timerMax) {textureX += 240; animate(joggingNorthWestXStart, joggingNorthWestXEnd,
93112
joggingNorthWestYStart, joggingNorthWestYEnd); timer = 0.0f;}
94113
}
95114
// If both left and down key pressed then move character left and down at same time
96115
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)) {
97-
sprite.move({-horizontalSpeed / 1.5f, 0.0f}); sprite.move({0.0f, verticalSpeed / 1.5f});
116+
sprite.move({-horizontalSpeed / 1.5f, verticalSpeed / 1.5f});
98117
moving = true; southWest = true;
99118
north = false; northEast = false; east = false; southEast = false; south = false; west = false; northWest = false;
100119
// Waits for set amount of time then plays jogging south west animation
101120
timer += 0.08f;
102-
if (timer >= timerMax) {textureX += 240; animate(joggingSouthWestXStart, joggingSouthWestXEnd,
121+
// Sprinting animation
122+
if (timer >= timerMax && sprinting) {sprite.move({(-horizontalSpeed / 1.5f) * 3, (verticalSpeed / 1.5f) * 3}); textureX += 240; animate(sprintingSouthWestXStart, sprintingSouthWestXEnd,
123+
sprintingSouthWestYStart, sprintingSouthWestYEnd); timer = 0.0f;}
124+
// Jogging animation
125+
else if (timer >= timerMax) {textureX += 240; animate(joggingSouthWestXStart, joggingSouthWestXEnd,
103126
joggingSouthWestYStart, joggingSouthWestYEnd); timer = 0.0f;}
104127
}
105128
// If right key pressed then move character right
@@ -109,8 +132,10 @@ void Player::handleInput() {
109132
north = false; northEast = false; southEast = false; south = false ;southWest = false; west = false; northWest = false;
110133
// Waits for set amount of time then plays jogging east animation
111134
timer += 0.08f;
135+
// Sprinting animation
112136
if (timer >= timerMax && sprinting) {sprite.move({horizontalSpeed * 3, 0.0f}); textureX += 240; animate(sprintingEastXStart, sprintingEastXEnd,
113137
sprintingEastYStart, sprintingEastYEnd); timer = 0.0f;}
138+
// Jogging animation
114139
else if (timer >= timerMax) {textureX += 240; animate(joggingEastXStart, joggingEastXEnd,
115140
joggingEastYStart, joggingEastYEnd); timer = 0.0f;}
116141
}
@@ -121,7 +146,11 @@ void Player::handleInput() {
121146
northEast = false; east = false; southEast = false; south = false; southWest = false; west = false; northWest = false;
122147
// Waits for set amount of time then plays jogging north animation
123148
timer += 0.08f;
124-
if (timer >= timerMax) {textureX += 240; animate(joggingNorthXStart, joggingNorthXEnd,
149+
// Sprinting animation
150+
if (timer >= timerMax && sprinting) {sprite.move({0.0f, -verticalSpeed * 3}); textureX += 240; animate(sprintingNorthXStart, sprintingNorthXEnd,
151+
sprintingNorthYStart, sprintingNorthYEnd); timer = 0.0f;}
152+
// Jogging animation
153+
else if (timer >= timerMax) {textureX += 240; animate(joggingNorthXStart, joggingNorthXEnd,
125154
joggingNorthYStart, joggingNorthYEnd); timer = 0.0f;}
126155
}
127156
// If down key pressed then move character down
@@ -131,7 +160,11 @@ void Player::handleInput() {
131160
north = false; northEast = false; east = false; southEast = false; southWest = false; west = false; northWest = false;
132161
// Waits for set amount of time then plays jogging south animation
133162
timer += 0.08f;
134-
if (timer >= timerMax) {textureX += 240; animate(joggingSouthXStart, joggingSouthXEnd,
163+
// Sprinting animation
164+
if (timer >= timerMax && sprinting) {sprite.move({0.0f, verticalSpeed * 3}); textureX += 240; animate(sprintingSouthXStart, sprintingSouthXEnd,
165+
sprintingSouthYStart, sprintingSouthYEnd); timer = 0.0f;}
166+
// Jogging animation
167+
else if (timer >= timerMax) {textureX += 240; animate(joggingSouthXStart, joggingSouthXEnd,
135168
joggingSouthYStart, joggingSouthYEnd); timer = 0.0f;}
136169
}
137170
// If left key pressed then move character left
@@ -141,7 +174,11 @@ void Player::handleInput() {
141174
north = false; northEast = false; east = false; southEast = false; south = false; southWest = false; northWest = false;
142175
// Waits for set amount of time then plays jogging west animation
143176
timer += 0.08f;
144-
if (timer >= timerMax) {textureX += 240; animate(joggingWestXStart, joggingWestXEnd,
177+
// Sprinting animation
178+
if (timer >= timerMax && sprinting) {sprite.move({-horizontalSpeed * 3, 0.0f}); textureX += 240; animate(sprintingWestXStart, sprintingWestXEnd,
179+
sprintingWestYStart, sprintingWestYEnd); timer = 0.0f;}
180+
// Jogging animation
181+
else if (timer >= timerMax) {textureX += 240; animate(joggingWestXStart, joggingWestXEnd,
145182
joggingWestYStart, joggingWestYEnd); timer = 0.0f;}
146183
} else {
147184
moving = false;
@@ -167,6 +204,7 @@ void Player::animate(int xStart, int xEnd, int yStart, int yEnd) {
167204
}
168205
}
169206

207+
// Sprint movement boolean, to be called true in main when user double tabs movement keys
170208
void Player::sprint(bool sprint) {
171209
if (sprint) {
172210
sprinting = true;

Player.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class Player {
3838
int sprintingNorthXStart, sprintingNorthYStart, sprintingNorthXEnd, sprintingNorthYEnd;
3939
int sprintingNorthEastXStart, sprintingNorthEastYStart, sprintingNorthEastXEnd, sprintingNorthEastYEnd;
4040
int sprintingEastXStart, sprintingEastYStart, sprintingEastXEnd, sprintingEastYEnd;
41+
int sprintingSouthEastXStart, sprintingSouthEastYStart, sprintingSouthEastXEnd, sprintingSouthEastYEnd;
42+
int sprintingSouthXStart, sprintingSouthYStart, sprintingSouthXEnd, sprintingSouthYEnd;
43+
int sprintingSouthWestXStart, sprintingSouthWestYStart, sprintingSouthWestXEnd, sprintingSouthWestYEnd;
44+
int sprintingWestXStart, sprintingWestYStart, sprintingWestXEnd, sprintingWestYEnd;
45+
int sprintingNorthWestXStart, sprintingNorthWestYStart, sprintingNorthWestXEnd, sprintingNorthWestYEnd;
4146
};
4247

4348
#endif // PLAYER_HPP

main.exe

2 KB
Binary file not shown.

main.obj

1.96 KB
Binary file not shown.

0 commit comments

Comments
 (0)