ELEC2645 (2018/19) / Mbed 2 deprecated el17ebs

Dependencies:   mbed FATFileSystem

Revision:
3:a8960004d261
Child:
4:035448357749
diff -r 81cfa8310f55 -r a8960004d261 Ball/Ball.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ball/Ball.cpp	Fri Mar 29 18:45:40 2019 +0000
@@ -0,0 +1,102 @@
+#include "Ball.h"
+
+//constructor
+
+Ball::Ball()
+{
+
+}
+
+//deconstructor
+
+Ball::~Ball()
+{
+
+}
+
+//public methods
+
+void Ball::init(float x, float y) //ball starts stationary in x and y positions given
+{
+    _x_pos = x;
+    _y_pos = y;
+    _x_vel = 0;
+    _y_vel = 0;
+
+}
+
+void Ball::draw_ball(N5110 &lcd)
+{
+    lcd.drawRect(_x_pos,_y_pos,3,3,FILL_BLACK); //draws ball 
+}
+
+void Ball::draw_aim(N5110 &lcd)
+{
+    lcd.drawLine((_x_pos+1.0f),(_y_pos+1.0f),(_x_pos+1.0f)-(_joy.x*16.0f),(_y_pos+1.0f)-(-_joy.y*16.0f),2); //draws ball 
+}
+
+void Ball::draw_screen(N5110 &lcd) 
+{
+    lcd.drawRect(16,8,51,31,FILL_TRANSPARENT);    
+}
+
+void Ball::move_ball()
+{
+    _x_pos = _x_pos + _x_vel;     
+    _y_pos = _y_pos + _y_vel;   
+    _x_vel = _x_vel*0.9f; //ball slows down 10% each loop 
+    _y_vel = _y_vel*0.9f; 
+    check_hit_wall();
+  
+}
+
+void Ball::shoot_ball(Gamepad &pad)
+{
+    if(pad.check_event(Gamepad::A_PRESSED) == true && _x_vel < 0.1f && _y_vel < 0.1f){ //if ball stationary and a pressed then shoot
+    
+    _x_vel = 8.0f * _joy.x; //scale x velocity by joystick direction and magnitude
+    _y_vel = 8.0f * -_joy.y; //scale y velocity by joystick direction and magnitude
+    }
+
+}
+
+void Ball::set_vel(float x_vel, float y_vel) 
+{
+    _x_vel = x_vel;
+    _y_vel = y_vel;
+}
+
+void Ball::check_hit_wall() 
+{
+    
+    if(_x_pos <= 16) { 
+        _x_pos = 17;
+        _x_vel = -_x_vel;
+    } 
+    
+    else if(_x_pos >= (WIDTH - 18)) {
+        _x_pos = (WIDTH - 20);
+        _x_vel = -_x_vel;
+    }
+    else if(_y_pos <= 8) { 
+        _y_pos = 9;
+        _y_vel = -_y_vel;
+    } 
+    
+    else if(_y_pos >= (HEIGHT - 11)) {
+        _y_pos = (HEIGHT - 13);
+        _y_vel = -_y_vel;
+    }
+}
+
+void Ball::read_joy(Gamepad &pad)
+{
+                                    
+    _coords = pad.get_mapped_coord();
+    _joy.mag = pad.get_mag();
+    _joy.x = _coords.x;
+    _joy.y = _coords.y;
+    
+}
+//private methods
+