-
Notifications
You must be signed in to change notification settings - Fork 1
/
ball.c
195 lines (171 loc) · 5.07 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
/**
Ball module source code
@team 128
@author Ambrose Ledbrook - 79172462
@author Josh Jarvis - 28803714
@date 13-oct-2018
@brief This module provides the ball functionality to the game through the use
of the provided api module boing. It includes methods to intialise,
update, send, receive and check collisons for the ball.
*/
// Including modules used in this module
#include "boing.h"
#include "tinygl.h"
#include "comms.h"
#include "paddle.h"
#include "ball.h"
#include <stdlib.h>
// A boing_state_t variable which holds the postion and direction of the ball
static boing_state_t ball;
/**
Generates a random value between 0 and 1 which corresponds to the directions
North west and North east. This is used to make the ball seem more random.
@return A random boing_dir_t which is either DIR_NW, DIR_SW
*/
boing_dir_t get_rand_dir(void) {
// Generating number
uint8_t index = rand() % NUM_START_DIRS;
boing_dir_t dir = DIR_N;
// Getting direction from index
switch(index) {
case 0 :
dir = DIR_NW;
break;
case 1 :
dir = DIR_SW;
break;
}
return dir;
}
/**
Intialising the state and postion of the ball
*/
void ball_init(void) {
ball = boing_init(BOTTOM_COLUMN, CENTER_ROW, get_rand_dir());
}
/**
Updating the postion of the ball through the use of the boing_update method
*/
void ball_update(void) {
// Removing current ball from ledmat
tinygl_draw_point(ball.pos, BALL_NOT_VISIBLE);
// Updating ball postion
ball = boing_update(ball);
// showing ball in its new postion
tinygl_draw_point(ball.pos, BALL_VISIBLE);
}
/**
Swithcing the direction of the ball when it hits the paddle
*/
void bounce_ball (void) {
switch(ball.dir) {
case DIR_SE :
ball.dir = DIR_SW;
break;
case DIR_NE :
ball.dir = DIR_NW;
break;
default :
break;
}
}
/**
Reversing the direction os the ball.
*/
void ball_reverse(void) {
// Removing current ball from ledmat
tinygl_draw_point(ball.pos, BALL_NOT_VISIBLE);
// Updating ball postion
ball = boing_reverse(ball);
// showing ball in its new postion
tinygl_draw_point(ball.pos, BALL_VISIBLE);
}
/**
Used to check if the ball has hit the paddle. Also handling the ball
bouncing off the paddle.
@return 0 if ball is on back of screen 1 otherwise
*/
uint8_t check_paddle(void) {
// Checking the ball is on the bottom layer of the ledmat
if (ball.pos.x == BOTTOM_COLUMN) {
// Checking if the ball is within the paddle
if (check_ball(ball.pos)) {
return 1;
} else {
// Ball is not within the paddle and the game is over
return 0;
}
// Checking if the ball is one layer above the paddle
} else if (ball.pos.x == ABOVE_PADDLE_COLUMN) {
// Checking edge cases where the ball hits the side of the paddle
if (((paddle.left.y + 1) == ball.pos.y) && ball.dir == DIR_SE) {
ball_reverse();
} else if (((paddle.right.y - 1) == ball.pos.y) && ball.dir == DIR_NE) {
ball_reverse();
}
// Checking if ball is over the paddle
if (check_ball(ball.pos)) {
bounce_ball();
}
}
return 1;
}
/**
Sending the postion and direction of the ball to the other board over
through the use of the comms module.
*/
void send_ball_pos(void) {
// Sending the ball
send_ball(ball);
// Removing the ball from the ledmat
tinygl_draw_point(ball.pos, BALL_NOT_VISIBLE);
}
/**
Processing a ball that was received from the other board. Takes a y-coordinate
and a direction and updates the ball to match.
@param pos the y coordinate received for the ball
@param dir the direction received for the ball
*/
void receive_ball(uint8_t pos, uint8_t dir) {
// Setting x and y postions of the ball
ball.pos.x = TOP_COLUMN;
ball.pos.y = pos;
// setting the direction of the ball
switch (dir) {
case NORTH_EAST :
ball.dir = DIR_NE;
break;
case EAST :
ball.dir = DIR_E;
break;
case SOUTH_EAST :
ball.dir = DIR_SE;
break;
}
// Showing the ball on the ledmat
tinygl_draw_point(ball.pos, BALL_VISIBLE);
}
/**
Checking if the ball needs to be sent to the other board. The ball will be
sent to the other board if its x-coordinate is 0 which is the top row of the
ledmat.
@return 1 if the ball is to be sent 0 otherwise
*/
uint8_t check_send(void) {
// Checking if send conditions are met
if (ball.pos.x == TOP_COLUMN && (ball.dir == DIR_W || ball.dir == DIR_SW || ball.dir == DIR_NW)) {
return 1;
} else {
return 0;
}
}
/**
Resetting the postion of the ball to default. Used when the game is restarted
*/
void reset_ball(void) {
ball.pos.x = BOTTOM_COLUMN;
// Getting center postion of the paddle
ball.pos.y = get_paddle_center();
// Getting random direction
ball.dir = get_rand_dir();
}