ELEC2645 (2018/19) / Mbed 2 deprecated el17ebs

Dependencies:   mbed FATFileSystem

Revision:
14:08ac9aaa34c3
Parent:
12:7f7fadb5c106
Child:
15:d855e8c666e7
diff -r 681192091568 -r 08ac9aaa34c3 Ball/Ball.cpp
--- a/Ball/Ball.cpp	Mon May 06 10:00:57 2019 +0000
+++ b/Ball/Ball.cpp	Wed May 08 14:54:19 2019 +0000
@@ -58,24 +58,25 @@
     _y_pos = _y_pos + _y_vel*10.0f/_frame_rate;   
     _x_vel = _x_vel*(1.0f-(0.6f/_frame_rate)); //velocity decreases as ball moves 
     _y_vel = _y_vel*(1.0f-(0.6f/_frame_rate)); //inversely proportional to frame rate to ensure ball movement is the same at all frame rates
-    if(_x_vel != 0 && _y_vel != 0 && abs(_x_vel) < 0.2f && abs(_y_vel) < 0.2f) { //to make ball come to complete stop when velocity is small enough
+    if(_x_vel != 0 && _y_vel != 0 && abs(_x_vel) < 0.15f && abs(_y_vel) < 0.15f) { //to make ball come to complete stop when velocity is small enough
         _x_vel = 0;
         _y_vel = 0;
     }  
 }
 
-void Ball::shoot_ball(Gamepad &pad, Vector2D _joy_coord) //checks if shoot coniditons met and A pressed then ball is shot using joystick input
+void Ball::check_shoot_ball(Gamepad &pad, Vector2D _joy_coord) //checks if shoot coniditons met and A pressed then ball is shot using joystick input
 {                                                        //direction of shot is in opposite direction of joystick direction
-    if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < TOL && abs(_y_vel) < TOL){ //If ball stationary and A pressed then shoot                                                                            
+    if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < TOL && abs(_y_vel) < TOL && pad.get_mag() > 0.05f){ //If ball stationary and A pressed then shoot                                                                            
         _x_vel = 6.0f * -_joy_coord.x; //scale x velocity by joystick direction and magnitude
         _y_vel = 6.0f * _joy_coord.y; //scale y velocity by joystick direction and magnitude
         _shot_count ++; //increment shot count
+        pad.tone(392.00 , 0.25); //play note when ball shot (G4)
     }
 }
 
 void Ball::set_total_shot_count(int total_shot_count) //used to initialise value to 0
 {
-    int _total_shot_count = total_shot_count;
+    _total_shot_count = total_shot_count;
 }
 
 int Ball::get_total_shot_count() //used at end to add tally to highscore - the lowest shot count is the best score
@@ -134,6 +135,16 @@
     _frame_rate = frame_rate;
 }
 
+bool Ball::get_bounce_flag()
+{
+    return _bounce_flag;
+}
+
+void Ball::reset_bounce_flag()
+{
+    _bounce_flag = false;    
+}
+
 //private methods
 
 void Ball::left_bounce(Coord start, Coord end) //check for left wall collision
@@ -141,6 +152,7 @@
     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; 
+        _bounce_flag = true;
     }     
 }
 
@@ -149,6 +161,7 @@
     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;
+        _bounce_flag = true;
     }
 }
 
@@ -157,6 +170,7 @@
     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;
+        _bounce_flag = true;
     }
 }
 
@@ -165,46 +179,53 @@
     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;
+        _bounce_flag = true;
     }  
 }
 
 void Ball::bottom_left_bounce(Coord start, Coord end) //check for bottom left wall collision
 {
-    if((_y_pos + _y_vel*10.0f/_frame_rate + 1) > (_x_pos + _x_vel*10.0f/_frame_rate - 1) + (start.y-start.x) && (_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 ) { 
+    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 && 
+       (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= start.y  && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= end.y ) { 
         swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
+        _bounce_flag = true;
     }     
 }
 
 void Ball::bottom_right_bounce(Coord start, Coord end) //check for bottom right wall collision
 {
-    if((_x_pos + _x_vel*10.0f/_frame_rate + 1) > -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) 
+    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_vel = -_x_vel; //negative wall
         _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
 {
-    if((_x_pos + _x_vel*10.0f/_frame_rate - 1) < -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) 
+    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_vel = -_x_vel; //negative wall 
         _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
 {
-    if((_y_pos + _y_vel*10.0f/_frame_rate - 1) < (_x_pos + _x_vel*10.0f/_frame_rate + 1) + (start.y-start.x) 
+    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) { 
     
         swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped
+        _bounce_flag = true;
     }  
 }