-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractions.cpp
More file actions
45 lines (40 loc) · 1.63 KB
/
interactions.cpp
File metadata and controls
45 lines (40 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "interactions.hpp"
#include "constants.hpp"
// Resolve potential collision between the ball and a brick
void handle_collision(Player& player, Brick& brick) {
auto collision = player.getGlobalBounds().findIntersection(brick.getGlobalBounds());
if (collision.has_value()) {
sf::Vector2f collisionCenter {collision->left + (collision->width / 2.f), collision->top + (collision->height / 2.f)};
sf::Vector2f playerCenter {player.getGlobalBounds().left + (player.getGlobalBounds().width / 2.f), player.getGlobalBounds().top + (player.getGlobalBounds().height / 2.f)};
float angle = atan2(collisionCenter.y - playerCenter.y, collisionCenter.x - playerCenter.x) * 180.f / M_PI;
if (angle >= 45.f && angle < 135.f)
{
player.setCollisionDirections(Constants::DIRECTION_BOTTOM);
player.nudgeUp(collision->height);
}
else if (angle >= -45.f && angle < 45.f)
{
player.setCollisionDirections(Constants::DIRECTION_RIGHT);
player.nudgeLeft(collision->width);
}
else if (angle >= -135.f && angle < -45.f)
{
SharedResources::Instance().playBrickSound();
brick.destroy();
player.setCollisionDirections(Constants::DIRECTION_TOP);
player.nudgeDown(collision->height);
}
else if (angle >= 135.f || angle < -135.f)
{
player.setCollisionDirections(Constants::DIRECTION_LEFT);
player.nudgeRight(collision->width);
}
}
}
void handle_collision(Player& player, Coin& coin) {
auto collision = player.getGlobalBounds().findIntersection(coin.getGlobalBounds());
if (collision.has_value()) {
coin.playCoinSound();
coin.destroy();
}
}