-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBall.cpp
More file actions
110 lines (103 loc) · 3.03 KB
/
Ball.cpp
File metadata and controls
110 lines (103 loc) · 3.03 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "Ball.h"
void updateBall(tBall &ball, bool collision_player, int collisionZonePlayer, bool collision_wall) {
updateBallDirection(ball, collision_player, collisionZonePlayer, collision_wall);
updateBallPosition(ball);
}
void updateBallPosition(tBall &ball) {
switch (ball.direction) {
case up_right: {
ball.position.x += 1;
ball.position.y += 1;
break;
}
case mid_right: {
ball.position.x += 1;
break;
}
case down_right: {
ball.position.x += 1;
ball.position.y -= 1;
break;
}
case up_left: {
ball.position.x -= 1;
ball.position.y += 1;
break;
}
case mid_left: {
ball.position.x -= 1;
break;
}
case down_left: {
ball.position.x -= 1;
ball.position.y -= 1;
break;
}
}
}
void updateBallDirection(tBall &ball, bool collision_player, int collisionZonePlayer, bool collision_wall) {
if (collision_player && collision_wall) {
if ((ball.direction == up_right) && (collisionZonePlayer == 1))
ball.direction = down_left;
else if ((ball.direction == up_left) && (collisionZonePlayer == 1))
ball.direction = down_right;
else if ((ball.direction == down_right) && (collisionZonePlayer == 3))
ball.direction = up_left;
else if ((ball.direction == down_left) && (collisionZonePlayer == 3))
ball.direction = up_right;
}
else if (!collision_wall && collision_player) {
if (collisionZonePlayer == 1) {
if (ball.direction == up_left)
ball.direction = up_right;
else if (ball.direction == mid_left)
ball.direction = up_right;
else if (ball.direction == down_left)
ball.direction = mid_right;
else if (ball.direction == up_right)
ball.direction = up_left;
else if (ball.direction == mid_right)
ball.direction = up_left;
else if (ball.direction == down_right)
ball.direction = mid_left;
}
else if (collisionZonePlayer == 2) {
if (ball.direction == up_left)
ball.direction = up_right;
else if (ball.direction == mid_left)
ball.direction = mid_right;
else if (ball.direction == down_left)
ball.direction = down_right;
else if (ball.direction == up_right)
ball.direction = up_left;
else if (ball.direction == mid_right)
ball.direction = mid_left;
else if (ball.direction == down_right)
ball.direction = down_left;
}
else if (collisionZonePlayer == 3) {
if (ball.direction == up_left)
ball.direction = mid_right;
else if (ball.direction == mid_left)
ball.direction = down_right;
else if (ball.direction == down_left)
ball.direction = down_right;
else if (ball.direction == up_right)
ball.direction = mid_left;
else if (ball.direction == mid_right)
ball.direction = down_left;
else if (ball.direction == down_right)
ball.direction = down_left;
}
}
else if (collision_wall && !collision_player) {
if (ball.direction == up_right)
ball.direction = down_right;
else if (ball.direction == up_left)
ball.direction = down_left;
else if (ball.direction == down_right)
ball.direction = up_right;
else if (ball.direction == down_left)
ball.direction = up_left;
}
}