forked from alexxsxnchez/BrickBreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.ino
189 lines (183 loc) · 4.96 KB
/
ball.ino
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
static ball ballObj;
static ball *ballPtr = &ballObj;
void initBall() {
ballPtr -> x = INIT_BALL_X;
ballPtr -> y = INIT_BALL_Y;
ballPtr -> xSpeed = 0;
ballPtr -> ySpeed = 0;
}
void drawBall() {
int xSpeed = ballPtr -> xSpeed;
int ySpeed = ballPtr -> ySpeed;
if(xSpeed || ySpeed) {
bool ballLost = checkForCollision();
if(ballLost) {
loseLife();
respawn();
return;
}
showBall(false);
ballPtr -> x += ballPtr -> xSpeed;
ballPtr -> y += ballPtr -> ySpeed;
}
else {
ballPtr -> ySpeed = 2;
}
showBall(true);
}
void showBall(boolean makeVisible) {
int pattern;
if(makeVisible) {
pattern = iptnSolid;
}
else {
pattern = iptnBlank;
}
OrbitOledSetFillPattern(OrbitOledGetStdPattern(pattern));
int x = ballPtr -> x;
int y = ballPtr -> y;
OrbitOledMoveTo(y, x);
OrbitOledFillRect(y, x);
}
// helper function for checkForCollision()
int myRound(double n) {
double whole = floor(n);
double remainder = n - whole;
if(remainder >= 0.5) {
return (int) whole + 1;
}
return (int) whole;
}
bool checkForCollision() {
int x = ballPtr -> x;
int y = ballPtr -> y;
int xSpeed = ballPtr -> xSpeed;
int ySpeed = ballPtr -> ySpeed;
int n = max(abs(xSpeed), abs(ySpeed));
double sumX = 0;
double sumY = 0;
int xMultiplier = xSpeed / abs(xSpeed);
int yMultiplier = ySpeed / abs(ySpeed);
for(int i = 1; i <= n; i++) {
sumX += (double) abs(xSpeed) / (double) n;
sumY += (double) abs(ySpeed) / (double) n;
if(myRound(sumX) == 1) {
x += 1 * xMultiplier;
sumX -= 1;
}
if(myRound(sumY) == 1) {
y += 1 * yMultiplier;
sumY -= 1;
}
if(y <= 0) {
return true;
}
bool wallBounce = isWallCollision(x, y);
if(isBrickCollision(x, y) || wallBounce) {
return false;
}
if(isPaddleCollision(x, y)) {
return false;
}
}
return false;
}
bool isWallCollision(int x, int y) {
if(x < 0 || x > OLED_HEIGHT - 1 || y > OLED_WIDTH - 1) {
if(y > OLED_WIDTH - 1) {
ballPtr -> ySpeed *= -1;
}
if(x < 0 || x > OLED_HEIGHT - 1) {
ballPtr -> xSpeed *= -1;
}
return true;
}
return false;
}
bool isBrickCollision(int x, int y) {
brick *brick = getBrickAt(x, y);
if(brick != NULL) {
lowerBrickHealth(brick);
int brickX = brick -> x;
int brickY = brick -> y;
int xSpeed = ballPtr -> xSpeed;
int ySpeed = ballPtr -> ySpeed;
int edgeCaseX = brickX + BRICK_WIDTH;
int edgeCaseY = brickY - BRICK_HEIGHT;
if(x == edgeCaseX && y == edgeCaseY) {
lowerBrickHealth(getBrickAt(x + BRICK_WIDTH + 1, y));
lowerBrickHealth(getBrickAt(x, y - BRICK_HEIGHT - 1));
lowerBrickHealth(getBrickAt(x + BRICK_WIDTH + 1, y - BRICK_HEIGHT - 1));
if(abs(xSpeed) == abs(ySpeed)) {
xSpeed *= -1;
ySpeed *= -1;
}
else if (abs(xSpeed) > abs(ySpeed)) {
xSpeed *= -1;
}
else {
ySpeed *= -1;
}
}
else if (abs(y - (brickY - 1)) >= abs(x - (brickX + 1))) {
ySpeed *= -1; //ERROR
}
else {
xSpeed *= -1;
}
ballPtr -> xSpeed = xSpeed;
ballPtr -> ySpeed = ySpeed;
return true;
}
return false;
}
bool isPaddleCollision(int x, int y) {
paddle *paddlePtr = getPaddlePtr();
if(y == paddlePtr -> y) {
int paddleX;
if(paddlePtr -> x < 0) {
paddleX = (paddlePtr -> x % -OLED_HEIGHT) + OLED_HEIGHT - 1;
}
else {
paddleX = paddlePtr -> x % OLED_HEIGHT;
}
if(x < paddleX) {
paddleX -= OLED_HEIGHT;
}
int collisionX = x - paddleX;
switch(collisionX) {
case 0:
ballPtr -> xSpeed = -2;
ballPtr -> ySpeed = 1;
break;
case 1:
case 2:
ballPtr -> xSpeed = -2;
ballPtr -> ySpeed = 2;
break;
case 3:
ballPtr -> xSpeed = -1;
ballPtr -> ySpeed = 2;
break;
case 4:
ballPtr -> xSpeed = 0;
ballPtr -> ySpeed = 2;
break;
case 5:
ballPtr -> xSpeed = 1;
ballPtr -> ySpeed = 2;
break;
case 6:
case 7:
ballPtr -> xSpeed = 2;
ballPtr -> ySpeed = 2;
break;
case 8:
ballPtr -> xSpeed = 2;
ballPtr -> ySpeed = 1;
break;
}
return true;
}
return false;
}