-
Notifications
You must be signed in to change notification settings - Fork 2
/
ball.c
205 lines (184 loc) · 4.72 KB
/
ball.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "ball.h"
#include <vita2d.h>
#include "game.h"
#include "paddle.h"
#include "levels.h"
#include "utils.h"
#include "bricks.h"
void startBall(){
moveBall();
if (checkBallCollisions())
{
if (game.ball.coord.x < game.paddle.coord.x + PADDLE_WIDTH/3)
{
game.ball.x_speed = game.ball.y_speed;
game.ball.y_speed = -game.ball.y_speed;
}
else if (game.ball.coord.x > game.paddle.coord.x + 2*(PADDLE_WIDTH/3))
{
game.ball.x_speed = -game.ball.y_speed;
game.ball.y_speed = -game.ball.y_speed ;
}else{
game.ball.y_speed = -game.ball.y_speed;
}
}
// Check for collisions with blocks //
checkBlockCollisions();
}
// Check to see if a point is within a rectangle //
bool isInsideBrick(int x, int y, brick_coordinate rect)
{
if ( (x >= rect.x) && (x <= rect.x + BLOCK_WIDTH) &&
(y >= rect.y) && (y <= rect.y + BLOCK_HEIGHT) )
{
return true;
}
return false;
}
int isInsideBrickStructured (ball_coordinate pPoint, rectangle_t pRect)
{
return ((pPoint.x >= pRect.coord_upper_left.x ) &&
(pPoint.x <= pRect.coord_lower_right.x) &&
(pPoint.y >= pRect.coord_upper_left.y ) &&
(pPoint.y <= pRect.coord_lower_right.y));
}
void moveBall(){
game.ball.coord.x += game.ball.x_speed;
game.ball.coord.y -= game.ball.y_speed;
//left
if (game.ball.coord.x <= 0 + BALL_DIAMETER/2)
{
game.ball.x_speed = -game.ball.x_speed;
}
//top
if (game.ball.coord.y <= 0 + BALL_DIAMETER/2)
{
game.ball.y_speed = -game.ball.y_speed ;
}
//right
if (game.ball.coord.x >= SCREEN_W- BALL_DIAMETER/2)
{
game.ball.x_speed = -game.ball.x_speed ;
}
// Check to see if ball has passed the paddle //
if ( game.ball.coord.y + BALL_DIAMETER/2 >= SCREEN_H )
{
game.lives-=1;
game.ball.x_speed = BALL_SPEED;
game.ball.y_speed = BALL_SPEED;
game.ball.isMoving = false;
if (game.lives == 0)
{
game.game_over = true;
game.ball.isMoving = false;
}
}
}
// Check to see if the ball is going to hit the paddle //
bool checkBallCollisions()
{
// Check to see if ball is in Y range of the player's paddle. //
// We check its speed to see if it's even moving towards the player's paddle. //
if ( (BALL_SPEED > 0) && (game.ball.coord.y + BALL_DIAMETER/2 >= game.paddle.coord.y) &&
(game.ball.coord.y + BALL_DIAMETER/2 <= game.paddle.coord.y + PADDLE_HEIGHT) ) // side hit
{
// If ball is in the X range of the paddle, return true. //
if ( (game.ball.coord.x + BALL_DIAMETER/2 <= game.paddle.coord.x + PADDLE_WIDTH) && (game.ball.coord.x + BALL_DIAMETER/2 >= game.paddle.coord.x) )
{
return true;
}
}
return false;
}
void checkBlockCollisions()
{
// collision points
int left_x = game.ball.coord.x - BALL_DIAMETER/2;
int left_y = game.ball.coord.y;
int right_x = game.ball.coord.x + BALL_DIAMETER/2;
int right_y = game.ball.coord.y;
int top_x = game.ball.coord.x;
int top_y = game.ball.coord.y - BALL_DIAMETER/2;
int bottom_x = game.ball.coord.x;
int bottom_y = game.ball.coord.y + BALL_DIAMETER/2;
bool top = false;
bool bottom = false;
bool left = false;
bool right = false;
if (game.ball.ballpoints.t <= 5*BLOCK_HEIGHT)
{
int i, j;
for(j = 4; j >= 0; j --)
{
for(i = 0; i < 10; i ++)
{
Brick b = game.level.board[j][i];
// top //
if ( isInsideBrick(top_x, top_y, b.rect.coord_upper_left) && b.state == 1)
{
top = true;
handleBlockCollision(b ,j , i );
}
// bottom //
if ( isInsideBrick(bottom_x, bottom_y, b.rect.coord_upper_left) && b.state == 1)
{
bottom = true;
handleBlockCollision(b ,j , i );
}
// left //
if ( isInsideBrick(left_x, left_y, b.rect.coord_upper_left) && b.state == 1)
{
left = true;
handleBlockCollision(b ,j , i );
}
// right //
if ( isInsideBrick(right_x, right_y, b.rect.coord_upper_left) && b.state == 1)
{
right = true;
handleBlockCollision(b ,j , i );
}
if (top)
{
game.ball.y_speed = -game.ball.y_speed;
}
if (bottom)
{
game.ball.y_speed = -game.ball.y_speed ;
}
if (left)
{
game.ball.x_speed = -game.ball.x_speed ;
}
if (right)
{
game.ball.x_speed = -game.ball.x_speed ;
}
top = false;
bottom = false;
left = false;
right = false;
}
}
}
}
void handleBlockCollision(Brick b, int j, int i)
{
//b.hits--;
// If num_hits is 0, the block needs to be erased //
if (b.state)
{
if (b.hits>1)
{
b.hits--;
temp[j][i]=99;
vita2d_draw_rectangle(b.rect.coord_upper_left.x, b.rect.coord_upper_left.y, BLOCK_WIDTH, BLOCK_HEIGHT, WHITE);
}else
{
//handle collision
b.state=false;
b.block_type=0;
vita2d_draw_rectangle(b.rect.coord_upper_left.x, b.rect.coord_upper_left.y, BLOCK_WIDTH, BLOCK_HEIGHT, TRANS);
temp[j][i]=0;
}
}
}