ELEC2645 (2018/19) / Mbed 2 deprecated el17ebs

Dependencies:   mbed FATFileSystem

Revision:
16:c8d68cbd1ae2
Parent:
15:d855e8c666e7
Child:
17:98127ac75195
diff -r d855e8c666e7 -r c8d68cbd1ae2 Ball/Ball.cpp
--- a/Ball/Ball.cpp	Wed May 08 18:51:59 2019 +0000
+++ b/Ball/Ball.cpp	Thu May 09 09:58:51 2019 +0000
@@ -34,8 +34,8 @@
 void Ball::printShotCount(N5110 &lcd) //this is tallied for each level to give user an end score
 {
     char buffer[14];
-    sprintf(buffer,"Shot %i",_shot_count);
-    lcd.printString(buffer,40,0);      
+    sprintf(buffer,"%i",_shot_count);
+    lcd.printString(buffer,70,0);      
 }
 
 void Ball::drawPower(N5110 &lcd, float mag) //draws power bar in top left of screen to indicate how hard a shot is hit
@@ -81,10 +81,13 @@
 
 int Ball::get_total_shot_count() //used at end to add tally to highscore - the lowest shot count is the best score
 {
-    int total_shot_count = _total_shot_count;    
-    return total_shot_count; 
+    return _total_shot_count; 
 }
 
+int Ball::get_shot_count() //returns shot count for current level - is used for tests
+{
+    return _shot_count; 
+}
 bool Ball::check_hole(Coord hole) //returns true when ball is hit in hole and next level begins
 {
     if(_x_pos > hole.x - 1 && _x_pos  < hole.x + 2 && _y_pos > hole.y - 1 && _y_pos < hole.y + 2) {
@@ -99,33 +102,33 @@
     }
 }
 
-void Ball::check_wall_bounce(WallMap map[], int size) //uses information from WallMap array for each level to check for bounces  
-{
+void Ball::check_wall_bounce(const WallMap map[], int size) //uses information from WallMap array to check if ball should bounce
+{        //every frame code runs through array of walls information to check if ball should bounce
     for(int i = 0; i < size; i ++) { //runs through each element in array of wall types to check if bounce condition is met by ball
     
-        if(map[i].wall == LEFT) { 
-            left_bounce(map[i].start,map[i].end);       
+        if(map[i].wall == LEFT) {  ////if current term of wall type struct array is a left wall
+            left_bounce(map[i].start,map[i].end);  //calls left_bounce() using start and end coordinates of the left wall 
         }
-        else if(map[i].wall == RIGHT) {
-            right_bounce(map[i].start,map[i].end);        
+        else if(map[i].wall == RIGHT) {  //if current term of wall type struct array is a right wall
+            right_bounce(map[i].start,map[i].end);  //calls right_bounce() using start and end coordinates of the right wall     
         }
-        else if(map[i].wall == TOP) {
-            top_bounce(map[i].start,map[i].end);
+        else if(map[i].wall == TOP) { //if current term of wall type struct array is a top wall
+            top_bounce(map[i].start,map[i].end); //calls top_bounce() using start and end coordinates of top left wall
         }
-        else if(map[i].wall == BOTTOM) {
-            bottom_bounce(map[i].start,map[i].end);
+        else if(map[i].wall == BOTTOM) { //if current term of wall type struct array is a bottom wall
+            bottom_bounce(map[i].start,map[i].end); //calls bottom_bounce() using start and end coordinates of the bottom wall
         }
-        else if(map[i].wall == BOTTOMLEFT) {
-            bottom_left_bounce(map[i].start,map[i].end);
+        else if(map[i].wall == BOTTOMLEFT) { //if current term of wall type struct array is a bottom left wall
+            bottom_left_bounce(map[i].start,map[i].end); //calls bottom_left_bounce() using start and end coordinates of the bottome left wall
         }
-        else if(map[i].wall == BOTTOMRIGHT) {
-            bottom_right_bounce(map[i].start,map[i].end);
+        else if(map[i].wall == BOTTOMRIGHT) { //if current term of wall type struct array is a bottom right wall
+            bottom_right_bounce(map[i].start,map[i].end); //calls bottom_right_bounce() using start and end coordinates of the bottome right wall
         }
-        else if(map[i].wall == TOPLEFT) {
-            top_left_bounce(map[i].start,map[i].end);
+        else if(map[i].wall == TOPLEFT) {  //if current term of wall type struct array is a top left wall
+            top_left_bounce(map[i].start,map[i].end); //calls top_left_bounce() using start and end coordinates of the top left wall
         }
-        else if(map[i].wall == TOPRIGHT) {
-            top_right_bounce(map[i].start,map[i].end);
+        else if(map[i].wall == TOPRIGHT) { //if current term of wall type struct array is a top right wall
+            top_right_bounce(map[i].start,map[i].end); //calls top_right_bounce() using start and end coordinates of the top right wall
         }
     }
 }
@@ -147,43 +150,43 @@
 
 //private methods
 
-void Ball::left_bounce(Coord start, Coord end) //check for left wall collision
-{
+void Ball::left_bounce(Coord start, Coord end) //checks if there is a collision with left wall using coordinates and bounces accordingly
+{       //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces
     if(_x_pos + _x_vel*10.0f/_frame_rate - 1 < start.x && _x_pos + _x_vel*10.0f/_frame_rate > start.x - 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel < 0){ 
         _x_pos = start.x + 1; 
-        _x_vel = -_x_vel; 
+        _x_vel = -_x_vel;  //left wall bounce causes x velocity to change sign
         _bounce_flag = true;
     }     
 }
 
-void Ball::right_bounce(Coord start, Coord end) //check for right wall collision
-{
+void Ball::right_bounce(Coord start, Coord end) //checks if there is a collision with a right wall using coordinates and bounces accordingly
+{   //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces
     if(_x_pos + _x_vel*10.0f/_frame_rate + 2 > start.x && _x_pos + _x_vel*10.0f/_frame_rate < start.x + 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel > 0){ //right wall x + 1
         _x_pos = start.x - 1;
-        _x_vel = -_x_vel;
+        _x_vel = -_x_vel; //right wall bounce causes x velocity to change sign
         _bounce_flag = true;
     }
 }
 
-void Ball::top_bounce(Coord start, Coord end) //check for top wall collision
-{
+void Ball::top_bounce(Coord start, Coord end) //checks if there is a collision with a top wall using coordinates and bounces accordingly
+{   //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces
     if(_y_pos + _y_vel*10.0f/_frame_rate - 1 < start.y && _y_pos + _y_vel*10.0f/_frame_rate > start.y - 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel < 0){ //top wall y -1
         _y_pos = start.y + 1;
-        _y_vel = -_y_vel;
+        _y_vel = -_y_vel; //top wall bounce causes y velocity to change sign
         _bounce_flag = true;
     }
 }
 
-void Ball::bottom_bounce(Coord start, Coord end) //check for bottom wall collision
-{
+void Ball::bottom_bounce(Coord start, Coord end) //checks if there is a collision with a bottom wall using coordinates and bounces accordingly
+{   //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces
     if(_y_pos + _y_vel*10.0f/_frame_rate + 2 > start.y && _y_pos + _y_vel*10.0f/_frame_rate < start.y + 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel > 0){ //bottom wall 
         _y_pos = start.y - 1;
-        _y_vel = -_y_vel;
+        _y_vel = -_y_vel; //bottom wall bounce causes y velocity to change sign
         _bounce_flag = true;
     }  
 }
 
-void Ball::bottom_left_bounce(Coord start, Coord end) //check for bottom left wall collision
+void Ball::bottom_left_bounce(Coord start, Coord end) //checks if there is a collision with a bottom left wall using coordinates and bounces accordingly
 {
     if((_y_pos + _y_vel*10.0f/_frame_rate + 1) > (_x_pos + _x_vel*10.0f/_frame_rate - 1) + (start.y-start.x) &&  //bottom left bounce conditions
        (_x_pos + _x_vel*10.0f/_frame_rate - 1) >= start.x && (_x_pos + _x_vel*10.0f/_frame_rate - 1) <= end.x && 
@@ -193,39 +196,39 @@
     }     
 }
 
-void Ball::bottom_right_bounce(Coord start, Coord end) //check for bottom right wall collision
-{
+void Ball::bottom_right_bounce(Coord start, Coord end) //checks if there is a collision with a bottom right wall using coordinates and bounces accordingly
+{   //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces
     if((_x_pos + _x_vel*10.0f/_frame_rate + 1) > -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) //bottom right bounce conditions
-    && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x &&  (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x 
-    && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y) { 
+    && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x - 1 &&  (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x + 1
+    && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y + 1) { 
     
-        _x_vel = -_x_vel; //negative wall
+        _x_vel = -_x_vel; //negative wall causes both velocities to change sign then swap
         _y_vel = -_y_vel;
         swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
         _bounce_flag = true;
     }
 }
 
-void Ball::top_left_bounce(Coord start, Coord end) //check for top left wall collision
-{
+void Ball::top_left_bounce(Coord start, Coord end) //checks if there is a collision with a top left wall using coordinates and bounces accordingly
+{   //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces
     if((_x_pos + _x_vel*10.0f/_frame_rate - 1) < -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) //top left bounce conditions
-    && (_x_pos + _x_vel*10.0f/_frame_rate - 1) >= start.x &&  (_x_pos + _x_vel*10.0f/_frame_rate - 1) <= end.x 
-    && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y) { 
+    && (_x_pos + _x_vel*10.0f/_frame_rate - 1) >= start.x - 1&&  (_x_pos + _x_vel*10.0f/_frame_rate - 1) <= end.x + 1
+    && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y + 1) { 
     
-        _x_vel = -_x_vel; //negative wall 
+        _x_vel = -_x_vel; //negative wall causes both velocities to change sign then swap
         _y_vel = -_y_vel;
         swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
         _bounce_flag = true;
     }
 }
 
-void Ball::top_right_bounce(Coord start, Coord end) //check for top right wall collision
-{
+void Ball::top_right_bounce(Coord start, Coord end) //checks if there is a collision with a top right wall using coordinates and bounces accordingly
+{   //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces
     if((_y_pos + _y_vel*10.0f/_frame_rate - 1) < (_x_pos + _x_vel*10.0f/_frame_rate + 1) + (start.y-start.x) //top right bounce conditions
-    && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x && (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x 
-    && (_y_pos + _y_vel*10.0f/_frame_rate - 1) >= start.y && (_y_pos + _y_vel*10.0f/_frame_rate - 1) <= end.y) { 
+    && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x - 1 && (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x + 1 
+    && (_y_pos + _y_vel*10.0f/_frame_rate - 1) >= start.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate - 1) <= end.y + 1) { 
     
         swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
-        _bounce_flag = true;
+        _bounce_flag = true; //used to trigger beep
     }  
 }