Skip to content

Commit 2666251

Browse files
committed
Updated animate function to fix potential bugs, updated spritesheet
1 parent 775919e commit 2666251

File tree

10 files changed

+58
-795
lines changed

10 files changed

+58
-795
lines changed

Ball.png

-24.1 KB
Binary file not shown.

Player.cpp

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Player::Player()
55
: texture{},
66
sprite( // Create a player sprite
77
[this]() {
8-
if (!texture.loadFromFile("spritesheet.png"))
8+
if (!texture.loadFromFile("playerSpritesheet.png"))
99
throw std::runtime_error("Failed to load sprite sheet");
1010
return sf::Sprite(texture);
1111
}()
@@ -24,37 +24,29 @@ Player::Player()
2424
horizontalSpeed = 2.0f;
2525

2626
// Coordinates for current texture in spritesheet
27-
textureX = 1;
28-
textureY = 1;
27+
textureX = 0;
28+
textureY = 0;
2929

3030
// Timer for animations and delays
3131
timer = 0.0f;
3232
timerMax = 0.5f;
3333

3434
// When x coordinates have reached the final column
35-
finalColumn = 1211;
35+
finalColumn = 2160;
36+
finalRow = 2400;
3637

3738
// Start and end coordinates for animation textures in spritesheet
3839
// These values will need to be changed if the spritesheet changes
39-
walkingDownXStart = 485;
40-
walkingDownYStart = 727;
41-
walkingDownXEnd = 727;
42-
walkingDownYEnd = 969;
43-
44-
walkingUpXStart = 1;
45-
walkingUpYStart = 485;
46-
walkingUpXEnd = 243;
47-
walkingUpYEnd = 727;
48-
49-
walkingLeftXStart = 969;
50-
walkingLeftYStart = 969;
51-
walkingLeftXEnd = 1211;
52-
walkingLeftYEnd = 1211;
53-
54-
walkingRightXStart = 969;
55-
walkingRightYStart = 1;
56-
walkingRightXEnd = 1211;
57-
walkingRightYEnd = 243;
40+
41+
joggingEastXStart = 1920; joggingEastYStart = 0; joggingEastXEnd = 720; joggingEastYEnd = 240;
42+
joggingNorthXStart = 960; joggingNorthYStart = 240; joggingNorthXEnd = 2160; joggingNorthYEnd = 240;
43+
joggingNorthEastXStart = 0; joggingNorthEastYStart = 480; joggingNorthEastXEnd = 1200; joggingNorthEastYEnd = 480;
44+
joggingNorthWestXStart = 1440; joggingNorthWestYStart = 480; joggingNorthWestXEnd = 240; joggingNorthWestYEnd = 720;
45+
joggingSouthXStart = 480; joggingSouthYStart = 720; joggingSouthXEnd = 1680; joggingSouthYEnd = 720;
46+
joggingSouthEastXStart = 1920; joggingSouthEastYStart = 720; joggingSouthEastXEnd = 720; joggingSouthEastYEnd = 960;
47+
joggingSouthWestXStart = 960; joggingSouthWestYStart = 960; joggingSouthWestXEnd = 2160; joggingSouthWestYEnd = 960;
48+
joggingWestXStart = 0; joggingWestYStart = 1200; joggingWestXEnd = 1200; joggingWestYEnd = 1200;
49+
5850
}
5951

6052
void Player::handleInput() {
@@ -63,78 +55,79 @@ void Player::handleInput() {
6355
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::A)) {
6456
sprite.move({-horizontalSpeed, 0.0f});
6557

66-
// Walking left animation
58+
// jogging left animation
6759
timer += 0.08f;
6860
// Will wait for set amount of time
6961
if (timer >= timerMax) {
70-
textureX += 242;
71-
// Call animate function to play the walking left animation
72-
animate(walkingLeftXStart, walkingLeftXEnd,
73-
walkingLeftYStart, walkingLeftYEnd, finalColumn);
62+
textureX += 240;
63+
// Call animate function to play the jogging left animation
64+
animate(joggingWestXStart, joggingWestXEnd,
65+
joggingWestYStart, joggingWestYEnd);
7466
timer = 0.0f;
7567
}
7668
}
7769
// If right key pressed then move character right
78-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::D)) {
70+
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::D)) {
7971
sprite.move({horizontalSpeed, 0.0f});
8072

81-
// Walking right animation
73+
// jogging right animation
8274
timer += 0.08f;
8375
// Will wait for set amount of time
8476
if (timer >= timerMax) {
85-
textureX += 242;
86-
// Call animate function to play the walking right animation
87-
animate(walkingRightXStart, walkingRightXEnd,
88-
walkingRightYStart, walkingRightYEnd, finalColumn);
77+
textureX += 240;
78+
// Call animate function to play the jogging right animation
79+
animate(joggingEastXStart, joggingEastXEnd,
80+
joggingEastYStart, joggingEastYEnd);
8981
timer = 0.0f;
9082
}
9183
}
9284
// If up key pressed then move character up
93-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::W)) {
85+
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::W)) {
9486
sprite.move({0.0f, -verticalSpeed});
9587

96-
// Walking up animation
88+
// jogging up animation
9789
timer += 0.08f;
9890
// Will wait for set amount of time
9991
if (timer >= timerMax) {
100-
textureX += 242;
101-
// Call animate function to play the walking up animation
102-
animate(walkingUpXStart, walkingUpXEnd,
103-
walkingUpYStart, walkingUpYEnd, finalColumn);
92+
textureX += 240;
93+
// Call animate function to play the jogging up animation
94+
animate(joggingNorthXStart, joggingNorthXEnd,
95+
joggingNorthYStart, joggingNorthYEnd);
10496
timer = 0.0f;
10597
}
10698
}
10799
// If down key pressed then move character down
108-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::S)) {
100+
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::S)) {
109101
sprite.move({0.0f, verticalSpeed});
110102

111-
// Walking down animation
103+
// jogging down animation
112104
timer += 0.08f;
113105
// Will wait for set amount of time
114106
if (timer >= timerMax) {
115-
textureX += 242;
116-
// Call animate function to play the walking down animation
117-
animate(walkingDownXStart, walkingDownXEnd,
118-
walkingDownYStart, walkingDownYEnd, finalColumn);
107+
textureX += 240;
108+
// Call animate function to play the jogging down animation
109+
animate(joggingSouthXStart, joggingSouthXEnd,
110+
joggingSouthYStart, joggingSouthYEnd);
119111
timer = 0.0f;
120112
}
121113
}
122114
}
123115

124116
// Reusable function for animating player
125-
void Player::animate(int xStart, int xEnd, int yStart, int yEnd, int finalCol) {
117+
void Player::animate(int xStart, int xEnd, int yStart, int yEnd) {
126118
// If current texture coordinates outside of expected values then use start coordinates
127-
if(textureY == yStart) {if(textureX < xStart) {textureX = xStart;}} // If column is incorrect
128-
else if(textureY == yEnd) {if(textureX > xEnd) {textureX = xStart; textureY = yStart;}}
129-
if(textureY < yStart || textureY > yEnd) {textureX = xStart; textureY = yStart;} // If row is incorrect
119+
if(textureY == yStart) {if(textureX < xStart) {textureX = xStart; textureY = yStart;}} // If column is before start of anim
120+
if(textureY == yEnd) {if(textureX > xEnd) {textureX = xStart; textureY = yStart;}} // If column is past end of anim
121+
if(textureY < yStart || textureY > yEnd) {textureX = xStart; textureY = yStart;} // If row is before or after anim
122+
if(textureY != yStart && textureY != yEnd) {textureX = xStart; textureY = yStart;} // Row value is invalid
130123

131124
// If x is at final column, switch to next row
132-
if (textureX > finalCol) { textureX = 1; textureY += 242;}
133-
// If y is at final column, then use start coordinates
134-
if (textureY > finalCol) {textureX = xStart; textureY = yStart;}
125+
if (textureX > finalColumn) { textureX = 0; textureY += 240;}
126+
// If y is at final row, then use start coordinates
127+
if (textureY > finalRow) {textureX = xStart; textureY = yStart;}
135128

136129
// If x value is valid, then change current sprite texture coordinates
137-
if (textureX <= finalCol) {
130+
if (textureX <= finalColumn) {
138131
sprite.setTextureRect({{textureX, textureY}, {240, 240}});
139132
}
140133
}

Player.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ class Player {
99
void handleInput();
1010
void update();
1111
void draw(sf::RenderWindow& window);
12-
void animate(int xStart, int xEnd, int yStart, int yEnd, int finalCol);
12+
void animate(int xStart, int xEnd, int yStart, int yEnd);
1313
private:
1414
sf::Texture texture;
1515
sf::Sprite sprite;
1616

1717
int textureX, textureY;
1818
float timer, timerMax;
1919
float verticalSpeed, horizontalSpeed;
20-
int finalColumn;
21-
int walkingDownXStart, walkingDownYStart, walkingDownXEnd, walkingDownYEnd;
22-
int walkingUpXStart, walkingUpYStart, walkingUpXEnd, walkingUpYEnd;
23-
int walkingLeftXStart, walkingLeftYStart, walkingLeftXEnd, walkingLeftYEnd;
24-
int walkingRightXStart, walkingRightYStart, walkingRightXEnd, walkingRightYEnd;
20+
int finalColumn, finalRow;
21+
int joggingNorthXStart, joggingNorthYStart, joggingNorthXEnd, joggingNorthYEnd;
22+
int joggingNorthEastXStart, joggingNorthEastYStart, joggingNorthEastXEnd, joggingNorthEastYEnd;
23+
int joggingEastXStart, joggingEastYStart, joggingEastXEnd, joggingEastYEnd;
24+
int joggingSouthEastXStart, joggingSouthEastYStart, joggingSouthEastXEnd, joggingSouthEastYEnd;
25+
int joggingSouthXStart, joggingSouthYStart, joggingSouthXEnd, joggingSouthYEnd;
26+
int joggingSouthWestXStart, joggingSouthWestYStart, joggingSouthWestXEnd, joggingSouthWestYEnd;
27+
int joggingWestXStart, joggingWestYStart, joggingWestXEnd, joggingWestYEnd;
28+
int joggingNorthWestXStart, joggingNorthWestYStart, joggingNorthWestXEnd, joggingNorthWestYEnd;
2529
};
2630

2731
#endif // PLAYER_HPP

Player.obj

435 Bytes
Binary file not shown.

main.exe

512 Bytes
Binary file not shown.

main.obj

283 Bytes
Binary file not shown.

playerSpritesheet.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"FaceEast":{"x":0,"y":0,"rotation":0,"width":240,"height":240},"FaceNorth":{"x":240,"y":0,"rotation":0,"width":240,"height":240},"FaceNorthEast":{"x":480,"y":0,"rotation":0,"width":240,"height":240},"FaceNorthWest":{"x":720,"y":0,"rotation":0,"width":240,"height":240},"FaceSouth":{"x":960,"y":0,"rotation":0,"width":240,"height":240},"FaceSouthEast":{"x":1200,"y":0,"rotation":0,"width":240,"height":240},"FaceSouthWest":{"x":1440,"y":0,"rotation":0,"width":240,"height":240},"FaceWest":{"x":1680,"y":0,"rotation":0,"width":240,"height":240},"JogEast1":{"x":1920,"y":0,"rotation":0,"width":240,"height":240},"JogEast2":{"x":2160,"y":0,"rotation":0,"width":240,"height":240},"JogEast3":{"x":0,"y":240,"rotation":0,"width":240,"height":240},"JogEast4":{"x":240,"y":240,"rotation":0,"width":240,"height":240},"JogEast5":{"x":480,"y":240,"rotation":0,"width":240,"height":240},"JogEast6":{"x":720,"y":240,"rotation":0,"width":240,"height":240},"JogNorth1":{"x":960,"y":240,"rotation":0,"width":240,"height":240},"JogNorth2":{"x":1200,"y":240,"rotation":0,"width":240,"height":240},"JogNorth3":{"x":1440,"y":240,"rotation":0,"width":240,"height":240},"JogNorth4":{"x":1680,"y":240,"rotation":0,"width":240,"height":240},"JogNorth5":{"x":1920,"y":240,"rotation":0,"width":240,"height":240},"JogNorth6":{"x":2160,"y":240,"rotation":0,"width":240,"height":240},"JogNorthEast1":{"x":0,"y":480,"rotation":0,"width":240,"height":240},"JogNorthEast2":{"x":240,"y":480,"rotation":0,"width":240,"height":240},"JogNorthEast3":{"x":480,"y":480,"rotation":0,"width":240,"height":240},"JogNorthEast4":{"x":720,"y":480,"rotation":0,"width":240,"height":240},"JogNorthEast5":{"x":960,"y":480,"rotation":0,"width":240,"height":240},"JogNorthEast6":{"x":1200,"y":480,"rotation":0,"width":240,"height":240},"JogNorthWest1":{"x":1440,"y":480,"rotation":0,"width":240,"height":240},"JogNorthWest2":{"x":1680,"y":480,"rotation":0,"width":240,"height":240},"JogNorthWest3":{"x":1920,"y":480,"rotation":0,"width":240,"height":240},"JogNorthWest4":{"x":2160,"y":480,"rotation":0,"width":240,"height":240},"JogNorthWest5":{"x":0,"y":720,"rotation":0,"width":240,"height":240},"JogNorthWest6":{"x":240,"y":720,"rotation":0,"width":240,"height":240},"JogSouth1":{"x":480,"y":720,"rotation":0,"width":240,"height":240},"JogSouth2":{"x":720,"y":720,"rotation":0,"width":240,"height":240},"JogSouth3":{"x":960,"y":720,"rotation":0,"width":240,"height":240},"JogSouth4":{"x":1200,"y":720,"rotation":0,"width":240,"height":240},"JogSouth5":{"x":1440,"y":720,"rotation":0,"width":240,"height":240},"JogSouth6":{"x":1680,"y":720,"rotation":0,"width":240,"height":240},"JogSouthEast1":{"x":1920,"y":720,"rotation":0,"width":240,"height":240},"JogSouthEast2":{"x":2160,"y":720,"rotation":0,"width":240,"height":240},"JogSouthEast3":{"x":0,"y":960,"rotation":0,"width":240,"height":240},"JogSouthEast4":{"x":240,"y":960,"rotation":0,"width":240,"height":240},"JogSouthEast5":{"x":480,"y":960,"rotation":0,"width":240,"height":240},"JogSouthEast6":{"x":720,"y":960,"rotation":0,"width":240,"height":240},"JogSouthWest1":{"x":960,"y":960,"rotation":0,"width":240,"height":240},"JogSouthWest2":{"x":1200,"y":960,"rotation":0,"width":240,"height":240},"JogSouthWest3":{"x":1440,"y":960,"rotation":0,"width":240,"height":240},"JogSouthWest4":{"x":1680,"y":960,"rotation":0,"width":240,"height":240},"JogSouthWest5":{"x":1920,"y":960,"rotation":0,"width":240,"height":240},"JogSouthWest6":{"x":2160,"y":960,"rotation":0,"width":240,"height":240},"JogWest1":{"x":0,"y":1200,"rotation":0,"width":240,"height":240},"JogWest2":{"x":240,"y":1200,"rotation":0,"width":240,"height":240},"JogWest3":{"x":480,"y":1200,"rotation":0,"width":240,"height":240},"JogWest4":{"x":720,"y":1200,"rotation":0,"width":240,"height":240},"JogWest5":{"x":960,"y":1200,"rotation":0,"width":240,"height":240},"JogWest6":{"x":1200,"y":1200,"rotation":0,"width":240,"height":240},"SprintEast1":{"x":1440,"y":1200,"rotation":0,"width":240,"height":240},"SprintEast2":{"x":1680,"y":1200,"rotation":0,"width":240,"height":240},"SprintEast3":{"x":1920,"y":1200,"rotation":0,"width":240,"height":240},"SprintEast4":{"x":2160,"y":1200,"rotation":0,"width":240,"height":240},"SprintEast5":{"x":0,"y":1440,"rotation":0,"width":240,"height":240},"SprintEast6":{"x":240,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorth1":{"x":480,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorth2":{"x":720,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorth3":{"x":960,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorth4":{"x":1200,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorth5":{"x":1440,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorth6":{"x":1680,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorthEast1":{"x":1920,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorthEast2":{"x":2160,"y":1440,"rotation":0,"width":240,"height":240},"SprintNorthEast3":{"x":0,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthEast4":{"x":240,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthEast5":{"x":480,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthEast6":{"x":720,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthWest1":{"x":960,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthWest2":{"x":1200,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthWest3":{"x":1440,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthWest4":{"x":1680,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthWest5":{"x":1920,"y":1680,"rotation":0,"width":240,"height":240},"SprintNorthWest6":{"x":2160,"y":1680,"rotation":0,"width":240,"height":240},"SprintSouth1":{"x":0,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouth2":{"x":240,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouth3":{"x":480,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouth4":{"x":720,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouth5":{"x":960,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouth6":{"x":1200,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouthEast1":{"x":1440,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouthEast2":{"x":1680,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouthEast3":{"x":1920,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouthEast4":{"x":2160,"y":1920,"rotation":0,"width":240,"height":240},"SprintSouthEast5":{"x":0,"y":2160,"rotation":0,"width":240,"height":240},"SprintSouthEast6":{"x":240,"y":2160,"rotation":0,"width":240,"height":240},"SprintSouthWest1":{"x":480,"y":2160,"rotation":0,"width":240,"height":240},"SprintSouthWest2":{"x":720,"y":2160,"rotation":0,"width":240,"height":240},"SprintSouthWest3":{"x":960,"y":2160,"rotation":0,"width":240,"height":240},"SprintSouthWest4":{"x":1200,"y":2160,"rotation":0,"width":240,"height":240},"SprintSouthWest5":{"x":1440,"y":2160,"rotation":0,"width":240,"height":240},"SprintSouthWest6":{"x":1680,"y":2160,"rotation":0,"width":240,"height":240},"SprintWest1":{"x":1920,"y":2160,"rotation":0,"width":240,"height":240},"SprintWest2":{"x":2160,"y":2160,"rotation":0,"width":240,"height":240},"SprintWest3":{"x":0,"y":2400,"rotation":0,"width":240,"height":240},"SprintWest4":{"x":240,"y":2400,"rotation":0,"width":240,"height":240},"SprintWest5":{"x":480,"y":2400,"rotation":0,"width":240,"height":240},"SprintWest6":{"x":720,"y":2400,"rotation":0,"width":240,"height":240}}

playerSpritesheet.png

1.69 MB
Loading

0 commit comments

Comments
 (0)