Skip to content

Commit 54397ad

Browse files
committed
Added collision for all 8 directions
1 parent 5ddd402 commit 54397ad

File tree

4 files changed

+73
-61
lines changed

4 files changed

+73
-61
lines changed

Player.cpp

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void Player::handleInput(std::vector<sf::FloatRect> wallBounds) {
9494
// Waits for set amount of time then plays Jog north east animation
9595
timer += 0.08f;
9696
// Sprinting animation
97-
if (timer >= timerMax && sprinting) {sprite.move({horizontalSpeed / 1.5f, -verticalSpeed / 1.5f}); textureX += 240; animate(sprintingNorthEastXStart, sprintingNorthEastXEnd,
97+
if (timer >= timerMax && sprinting) {sprite.move({movement.x * 3, movement.y * 3}); textureX += 240; animate(sprintingNorthEastXStart, sprintingNorthEastXEnd,
9898
sprintingNorthEastYStart, sprintingNorthEastYEnd); timer = 0.0f;}
9999
// Jog animation
100100
else if (timer >= timerMax) {textureX += 240; animate(JogNorthEastXStart, JogNorthEastXEnd,
@@ -110,7 +110,7 @@ void Player::handleInput(std::vector<sf::FloatRect> wallBounds) {
110110
// Waits for set amount of time then plays Jog south east animation
111111
timer += 0.08f;
112112
// Sprinting animation
113-
if (timer >= timerMax && sprinting) {sprite.move({(horizontalSpeed / 1.5f) * 3, (verticalSpeed / 1.5f) * 3}); textureX += 240; animate(sprintingSouthEastXStart, sprintingSouthEastXEnd,
113+
if (timer >= timerMax && sprinting) {sprite.move({movement.x * 3, movement.y * 3}); textureX += 240; animate(sprintingSouthEastXStart, sprintingSouthEastXEnd,
114114
sprintingSouthEastYStart, sprintingSouthEastYEnd); timer = 0.0f;}
115115
// Jog animation
116116
else if (timer >= timerMax) {textureX += 240; animate(JogSouthEastXStart, JogSouthEastXEnd,
@@ -119,87 +119,99 @@ void Player::handleInput(std::vector<sf::FloatRect> wallBounds) {
119119
}
120120
// If both left and up key pressed then move character left and up at same time
121121
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)) {
122-
sprite.move({-horizontalSpeed / 1.5f, -verticalSpeed / 1.5f});
123122
moving = true; northWest = true;
124123
north = false; northEast = false; east = false; southEast = false; south = false; southWest = false; west = false;
125-
// Waits for set amount of time then plays Jog north west animation
126-
timer += 0.08f;
127-
// Sprinting animation
128-
if (timer >= timerMax && sprinting) {sprite.move({-horizontalSpeed / 1.5f, -verticalSpeed / 1.5f}); textureX += 240; animate(sprintingNorthWestXStart, sprintingNorthWestXEnd,
129-
sprintingNorthWestYStart, sprintingNorthWestYEnd); timer = 0.0f;}
130-
// Jog animation
131-
else if (timer >= timerMax) {textureX += 240; animate(JogNorthWestXStart, JogNorthWestXEnd,
132-
JogNorthWestYStart, JogNorthWestYEnd); timer = 0.0f;}
124+
sf::Vector2f movement{-horizontalSpeed / 1.5f, -verticalSpeed / 1.5f};
125+
if (!handleCollision(movement, nextBounds, wallBounds)) {
126+
// Waits for set amount of time then plays Jog north west animation
127+
timer += 0.08f;
128+
// Sprinting animation
129+
if (timer >= timerMax && sprinting) {sprite.move({movement.x * 3, movement.y * 3}); textureX += 240; animate(sprintingNorthWestXStart, sprintingNorthWestXEnd,
130+
sprintingNorthWestYStart, sprintingNorthWestYEnd); timer = 0.0f;}
131+
// Jog animation
132+
else if (timer >= timerMax) {textureX += 240; animate(JogNorthWestXStart, JogNorthWestXEnd,
133+
JogNorthWestYStart, JogNorthWestYEnd); timer = 0.0f;}
134+
}
133135
}
134136
// If both left and down key pressed then move character left and down at same time
135137
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)) {
136-
sprite.move({-horizontalSpeed / 1.5f, verticalSpeed / 1.5f});
137138
moving = true; southWest = true;
138139
north = false; northEast = false; east = false; southEast = false; south = false; west = false; northWest = false;
139-
// Waits for set amount of time then plays Jog south west animation
140-
timer += 0.08f;
141-
// Sprinting animation
142-
if (timer >= timerMax && sprinting) {sprite.move({(-horizontalSpeed / 1.5f) * 3, (verticalSpeed / 1.5f) * 3}); textureX += 240; animate(sprintingSouthWestXStart, sprintingSouthWestXEnd,
143-
sprintingSouthWestYStart, sprintingSouthWestYEnd); timer = 0.0f;}
144-
// Jog animation
145-
else if (timer >= timerMax) {textureX += 240; animate(JogSouthWestXStart, JogSouthWestXEnd,
146-
JogSouthWestYStart, JogSouthWestYEnd); timer = 0.0f;}
140+
sf::Vector2f movement{-horizontalSpeed / 1.5f, verticalSpeed / 1.5f};
141+
if (!handleCollision(movement, nextBounds, wallBounds)) {
142+
// Waits for set amount of time then plays Jog south west animation
143+
timer += 0.08f;
144+
// Sprinting animation
145+
if (timer >= timerMax && sprinting) {sprite.move({movement.x * 3, movement.y * 3}); textureX += 240; animate(sprintingSouthWestXStart, sprintingSouthWestXEnd,
146+
sprintingSouthWestYStart, sprintingSouthWestYEnd); timer = 0.0f;}
147+
// Jog animation
148+
else if (timer >= timerMax) {textureX += 240; animate(JogSouthWestXStart, JogSouthWestXEnd,
149+
JogSouthWestYStart, JogSouthWestYEnd); timer = 0.0f;}
150+
}
147151
}
148152
// If right key pressed then move character right
149153
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::D)) {
150-
sprite.move({horizontalSpeed, 0.0f});
151154
moving = true; east = true;
152155
north = false; northEast = false; southEast = false; south = false ;southWest = false; west = false; northWest = false;
153-
// Waits for set amount of time then plays Jog east animation
154-
timer += 0.08f;
155-
// Sprinting animation
156-
if (timer >= timerMax && sprinting) {sprite.move({horizontalSpeed * 3, 0.0f}); textureX += 240; animate(sprintingEastXStart, sprintingEastXEnd,
157-
sprintingEastYStart, sprintingEastYEnd); timer = 0.0f;}
158-
// Jog animation
159-
else if (timer >= timerMax) {textureX += 240; animate(JogEastXStart, JogEastXEnd,
160-
JogEastYStart, JogEastYEnd); timer = 0.0f;}
156+
sf::Vector2f movement{horizontalSpeed, 0.0f};
157+
if (!handleCollision(movement, nextBounds, wallBounds)) {
158+
// Waits for set amount of time then plays Jog east animation
159+
timer += 0.08f;
160+
// Sprinting animation
161+
if (timer >= timerMax && sprinting) {sprite.move({movement.x * 3, 0.0f}); textureX += 240; animate(sprintingEastXStart, sprintingEastXEnd,
162+
sprintingEastYStart, sprintingEastYEnd); timer = 0.0f;}
163+
// Jog animation
164+
else if (timer >= timerMax) {textureX += 240; animate(JogEastXStart, JogEastXEnd,
165+
JogEastYStart, JogEastYEnd); timer = 0.0f;}
166+
}
161167
}
162168
// If up key pressed then move character up
163169
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::W)) {
164-
sprite.move({0.0f, -verticalSpeed});
165170
moving = true; north = true;
166171
northEast = false; east = false; southEast = false; south = false; southWest = false; west = false; northWest = false;
167-
// Waits for set amount of time then plays Jog north animation
168-
timer += 0.08f;
169-
// Sprinting animation
170-
if (timer >= timerMax && sprinting) {sprite.move({0.0f, -verticalSpeed * 3}); textureX += 240; animate(sprintingNorthXStart, sprintingNorthXEnd,
171-
sprintingNorthYStart, sprintingNorthYEnd); timer = 0.0f;}
172-
// Jog animation
173-
else if (timer >= timerMax) {textureX += 240; animate(JogNorthXStart, JogNorthXEnd,
174-
JogNorthYStart, JogNorthYEnd); timer = 0.0f;}
172+
sf::Vector2f movement{0.0f, -verticalSpeed};
173+
if (!handleCollision(movement, nextBounds, wallBounds)) {
174+
// Waits for set amount of time then plays Jog north animation
175+
timer += 0.08f;
176+
// Sprinting animation
177+
if (timer >= timerMax && sprinting) {sprite.move({0.0f, movement.y * 3}); textureX += 240; animate(sprintingNorthXStart, sprintingNorthXEnd,
178+
sprintingNorthYStart, sprintingNorthYEnd); timer = 0.0f;}
179+
// Jog animation
180+
else if (timer >= timerMax) {textureX += 240; animate(JogNorthXStart, JogNorthXEnd,
181+
JogNorthYStart, JogNorthYEnd); timer = 0.0f;}
182+
}
175183
}
176184
// If down key pressed then move character down
177185
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::S)) {
178-
sprite.move({0.0f, verticalSpeed});
179186
moving = true; south = true;
180187
north = false; northEast = false; east = false; southEast = false; southWest = false; west = false; northWest = false;
181-
// Waits for set amount of time then plays Jog south animation
182-
timer += 0.08f;
183-
// Sprinting animation
184-
if (timer >= timerMax && sprinting) {sprite.move({0.0f, verticalSpeed * 3}); textureX += 240; animate(sprintingSouthXStart, sprintingSouthXEnd,
185-
sprintingSouthYStart, sprintingSouthYEnd); timer = 0.0f;}
186-
// Jog animation
187-
else if (timer >= timerMax) {textureX += 240; animate(JogSouthXStart, JogSouthXEnd,
188-
JogSouthYStart, JogSouthYEnd); timer = 0.0f;}
188+
sf::Vector2f movement{0.0f, verticalSpeed};
189+
if (!handleCollision(movement, nextBounds, wallBounds)) {
190+
// Waits for set amount of time then plays Jog south animation
191+
timer += 0.08f;
192+
// Sprinting animation
193+
if (timer >= timerMax && sprinting) {sprite.move({0.0f, movement.y * 3}); textureX += 240; animate(sprintingSouthXStart, sprintingSouthXEnd,
194+
sprintingSouthYStart, sprintingSouthYEnd); timer = 0.0f;}
195+
// Jog animation
196+
else if (timer >= timerMax) {textureX += 240; animate(JogSouthXStart, JogSouthXEnd,
197+
JogSouthYStart, JogSouthYEnd); timer = 0.0f;}
198+
}
189199
}
190200
// If left key pressed then move character left
191201
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::A)) {
192-
sprite.move({-horizontalSpeed, 0.0f});
193202
moving = true; west = true;
194203
north = false; northEast = false; east = false; southEast = false; south = false; southWest = false; northWest = false;
195-
// Waits for set amount of time then plays Jog west animation
196-
timer += 0.08f;
197-
// Sprinting animation
198-
if (timer >= timerMax && sprinting) {sprite.move({-horizontalSpeed * 3, 0.0f}); textureX += 240; animate(sprintingWestXStart, sprintingWestXEnd,
199-
sprintingWestYStart, sprintingWestYEnd); timer = 0.0f;}
200-
// Jog animation
201-
else if (timer >= timerMax) {textureX += 240; animate(JogWestXStart, JogWestXEnd,
202-
JogWestYStart, JogWestYEnd); timer = 0.0f;}
204+
sf::Vector2f movement{-horizontalSpeed, 0.0f};
205+
if (!handleCollision(movement, nextBounds, wallBounds)) {
206+
// Waits for set amount of time then plays Jog west animation
207+
timer += 0.08f;
208+
// Sprinting animation
209+
if (timer >= timerMax && sprinting) {sprite.move({movement.x * 3, 0.0f}); textureX += 240; animate(sprintingWestXStart, sprintingWestXEnd,
210+
sprintingWestYStart, sprintingWestYEnd); timer = 0.0f;}
211+
// Jog animation
212+
else if (timer >= timerMax) {textureX += 240; animate(JogWestXStart, JogWestXEnd,
213+
JogWestYStart, JogWestYEnd); timer = 0.0f;}
214+
}
203215
} else {
204216
moving = false;
205217
}

main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ int main()
3131
// Create walls
3232
std::vector<sf::RectangleShape> walls;
3333
// Create wall 1
34-
sf::Vector2<float> wallPos1(275.f, 200.f); // Set coordinates
34+
sf::Vector2<float> wallPos1(575.f, 200.f); // Set coordinates
3535
sf::RectangleShape wall1(wallPos1);
36-
wall1.setOrigin(wall1.getLocalBounds().size / 2.f);
36+
wall1.setOrigin({wall1.getTextureRect().size.x / 2.0f, wall1.getTextureRect().size.y / 2.0f});
3737
wall1.setPosition(wallPos1);
3838
wall1.setFillColor(sf::Color::Red);
3939
wall1.setScale({0.5f,0.5f});
4040
walls.push_back(wall1);
4141
// Create wall 2
42-
sf::Vector2<float> wallPos2(255.f, 180.f); // Set coordinates
42+
sf::Vector2<float> wallPos2(400.f, 100.f); // Set coordinates
4343
sf::RectangleShape wall2(wallPos2);
44-
wall2.setOrigin(wall2.getLocalBounds().size / 2.f);
44+
wall2.setOrigin({wall2.getTextureRect().size.x / 2.0f, wall2.getTextureRect().size.y / 2.0f});
4545
wall2.setPosition(wallPos2);
4646
wall2.setFillColor(sf::Color::Red);
4747
wall2.setScale({0.5f,0.5f});
@@ -116,7 +116,7 @@ int main()
116116
window.draw(wall);
117117
sf::FloatRect bounds = wall.getGlobalBounds();
118118
bounds.size = wall.getGlobalBounds().size *= 0.2f;
119-
wallBounds.push_back(bounds);
119+
wallBounds.push_back(wall.getGlobalBounds());
120120
}
121121
// Display and update enemies
122122
for (auto& enemy : enemies) {

main.exe

1 KB
Binary file not shown.

main.obj

493 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)