Skip to content

Commit 0d8d2a6

Browse files
committed
Added attack animation functionality
1 parent 1ed19aa commit 0d8d2a6

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

Player.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ Player::Player()
6666
sprintingSouthEastXStart = 1440; sprintingSouthEastYStart = 1920; sprintingSouthEastXEnd = 240; sprintingSouthEastYEnd = 2160;
6767
sprintingSouthWestXStart = 480; sprintingSouthWestYStart = 2160; sprintingSouthWestXEnd = 1680; sprintingSouthWestYEnd = 2160;
6868
sprintingWestXStart = 1920; sprintingWestYStart = 2160; sprintingWestXEnd = 720; sprintingWestYEnd = 2400;
69+
70+
sprinting = false;
71+
attackEastXStart = 1440; attackEastYStart = 1200; attackEastXEnd = 240; attackEastYEnd = 1440;
72+
attackNorthXStart = 480; attackNorthYStart = 1440; attackNorthXEnd = 1680; attackNorthYEnd = 1440;
73+
attackNorthEastXStart = 1920; attackNorthEastYStart = 1440; attackNorthEastXEnd = 720; attackNorthEastYEnd = 1680;
74+
attackNorthWestXStart = 960; attackNorthWestYStart = 1680; attackNorthWestXEnd = 2160; attackNorthWestYEnd = 1680;
75+
attackSouthXStart = 0; attackSouthYStart = 1920; attackSouthXEnd = 1200; attackSouthYEnd = 1920;
76+
attackSouthEastXStart = 1440; attackSouthEastYStart = 1920; attackSouthEastXEnd = 240; attackSouthEastYEnd = 2160;
77+
attackSouthWestXStart = 480; attackSouthWestYStart = 2160; attackSouthWestXEnd = 1680; attackSouthWestYEnd = 2160;
78+
attackWestXStart = 1920; attackWestYStart = 2160; attackWestXEnd = 720; attackWestYEnd = 2400;
6979
}
7080

7181
void Player::handleInput() {
@@ -183,6 +193,47 @@ void Player::handleInput() {
183193
} else {
184194
moving = false;
185195
}
196+
// If space pressed then attack and change to attack animation
197+
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Space)) {
198+
attacking = true;
199+
timer += 0.08f;
200+
// Check direction user is facing
201+
if (north) {
202+
// Attack animation
203+
if (timer >= timerMax) {textureX += 240; animate(attackNorthXStart, attackNorthXEnd,
204+
attackNorthYStart, attackNorthYEnd); timer = 0.0f;}
205+
} else if (northEast) {
206+
// Attack animation
207+
if (timer >= timerMax) {textureX += 240; animate(attackNorthEastXStart, attackNorthEastXEnd,
208+
attackNorthEastYStart, attackNorthEastYEnd); timer = 0.0f;}
209+
} else if (east) {
210+
// Attack animation
211+
if (timer >= timerMax) {textureX += 240; animate(attackEastXStart, attackEastXEnd,
212+
attackEastYStart, attackEastYEnd); timer = 0.0f;}
213+
} else if (southEast) {
214+
// Attack animation
215+
if (timer >= timerMax) {textureX += 240; animate(attackSouthEastXStart, attackSouthEastXEnd,
216+
attackSouthEastYStart, attackSouthEastYEnd); timer = 0.0f;}
217+
} else if (south) {
218+
// Attack animation
219+
if (timer >= timerMax) {textureX += 240; animate(attackSouthXStart, attackSouthXEnd,
220+
attackSouthYStart, attackSouthYEnd); timer = 0.0f;}
221+
} else if (southWest) {
222+
// Attack animation
223+
if (timer >= timerMax) {textureX += 240; animate(attackSouthWestXStart, attackSouthWestXEnd,
224+
attackSouthWestYStart, attackSouthWestYEnd); timer = 0.0f;}
225+
} else if (west) {
226+
// Attack animation
227+
if (timer >= timerMax) {textureX += 240; animate(attackWestXStart, attackWestXEnd,
228+
attackWestYStart, attackWestYEnd); timer = 0.0f;}
229+
} else if (northWest) {
230+
// Attack animation
231+
if (timer >= timerMax) {textureX += 240; animate(attackNorthWestXStart, attackNorthWestXEnd,
232+
attackNorthWestYStart, attackNorthWestYEnd); timer = 0.0f;}
233+
}
234+
} else {
235+
attacking = false;
236+
}
186237
}
187238

188239
// Reusable function for animating player
@@ -216,7 +267,7 @@ void Player::sprint(bool sprint) {
216267
// Checking for changes to player
217268
void Player::update() {
218269
// If player is not moving then change player to standing pose
219-
if (moving == false) {
270+
if (moving == false && attacking == false) {
220271
if (east) {textureX = standingEastX; textureY = standingEastY;}
221272
if (north) {textureX = standingNorthX; textureY = standingNorthY;}
222273
if (northEast) {textureX = standingNorthEastX; textureY = standingNorthEastY;}

Player.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Player {
1919
float timer, timerMax;
2020
float verticalSpeed, horizontalSpeed;
2121
int finalColumn, finalRow;
22-
bool moving, sprinting, north, northEast, east, southEast, south, southWest, west, northWest;
22+
bool moving, sprinting, attacking, north, northEast, east, southEast, south, southWest, west, northWest;
2323

2424
int standingNorthX, standingNorthY, standingNorthWestX, standingNorthWestY,
2525
standingNorthEastX, standingNorthEastY, standingEastX, standingEastY,
@@ -43,6 +43,15 @@ class Player {
4343
int sprintingSouthWestXStart, sprintingSouthWestYStart, sprintingSouthWestXEnd, sprintingSouthWestYEnd;
4444
int sprintingWestXStart, sprintingWestYStart, sprintingWestXEnd, sprintingWestYEnd;
4545
int sprintingNorthWestXStart, sprintingNorthWestYStart, sprintingNorthWestXEnd, sprintingNorthWestYEnd;
46+
47+
int attackNorthXStart, attackNorthYStart, attackNorthXEnd, attackNorthYEnd;
48+
int attackNorthEastXStart, attackNorthEastYStart, attackNorthEastXEnd, attackNorthEastYEnd;
49+
int attackEastXStart, attackEastYStart, attackEastXEnd, attackEastYEnd;
50+
int attackSouthEastXStart, attackSouthEastYStart, attackSouthEastXEnd, attackSouthEastYEnd;
51+
int attackSouthXStart, attackSouthYStart, attackSouthXEnd, attackSouthYEnd;
52+
int attackSouthWestXStart, attackSouthWestYStart, attackSouthWestXEnd, attackSouthWestYEnd;
53+
int attackWestXStart, attackWestYStart, attackWestXEnd, attackWestYEnd;
54+
int attackNorthWestXStart, attackNorthWestYStart, attackNorthWestXEnd, attackNorthWestYEnd;
4655
};
4756

4857
#endif // PLAYER_HPP

main.exe

1.5 KB
Binary file not shown.

main.obj

1.61 KB
Binary file not shown.

0 commit comments

Comments
 (0)