FINAL VERSION

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Paddle.cpp Source File

Paddle.cpp

00001 #include "Paddle.h"
00002 
00003 FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
00004     
00005 Paddle::Paddle()
00006 {
00007 
00008 }
00009 
00010 Paddle::~Paddle()
00011 {
00012 
00013 }
00014 
00015 void Paddle::init(int y,int height,int width)
00016 {
00017     _x = WIDTH/2 - width/2;  // initialised in the centre of the screen
00018     _y = y;  // fixed at the bottom of the screen, initialised by _paddley in engine
00019     _height = height; // paddle height, defined at init
00020     _width = width; // paddle width, defined at init
00021     _sens = 0.5; // default sensitivity of tilt/joystick
00022     _speed = 4;  // default speed = _speed * _sens
00023     _lives = 6;  // number of lives = number of LEDs
00024     _tilt = false;  //used to choose between motion methods
00025 
00026 }
00027 
00028 
00029 void Paddle::draw(N5110 &lcd)
00030 {
00031     lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);   // draw paddle at specified coordinates
00032 }
00033 
00034 
00035 void Paddle::update(Direction d,float mag)   // update the paddle's properties
00036 {
00037     if (_tilt == false) {
00038         _speed = int(mag*10.0f);  // scale is arbitrary
00039 
00040         // update x value depending on direction of movement
00041         // West is decrement as origin is at the top-left so decreasing moves left
00042         if (d == W) {
00043             _x-=_speed * _sens;   // adjust the x coordinate accordingly. Sensitivity scales the speed of the paddle
00044         } else if (d == E) {
00045             _x+=_speed * _sens;
00046         }
00047 
00048         // censure that the paddle doesn't go off screen
00049         if (_x < 1) {
00050             _x = 1;
00051         }
00052         if (_x > WIDTH - _width - 1) {
00053             _x = WIDTH - _width - 1;
00054         }
00055     } else if (_tilt == true) {
00056         accelerometer.init();     // initialise the accelerometer
00057         Data values = accelerometer.get_values();     // retrieve a struct of values
00058         float roll_rad = atan2(values.ay,values.az);  // use values to calculate the roll angle in radians
00059         _speed = int((roll_rad*(360/3.14159265))*0.3f);  // convert to degrees and scale
00060         _x -= _speed * _sens;  // adjust the x coordinate accordingly
00061     }
00062 
00063     // ensure that the paddle doesn't go off screen
00064     if (_x < 1) {
00065         _x = 1;
00066     }
00067     if (_x > WIDTH - _width - 1) {
00068         _x = WIDTH - _width - 1;
00069     }
00070 }
00071 
00072 
00073 int Paddle::get_lives()  // returns the integer _lives
00074 {
00075     return _lives;
00076 }
00077 
00078 
00079 void Paddle::lose_life()  // decrements the member variable _lives
00080 {
00081     _lives--;
00082 }
00083 
00084 
00085 void Paddle::inc_life()  // increment the member variable _lives
00086 {
00087     _lives++;
00088 }
00089 
00090 
00091 void Paddle::reset_lives()
00092 {
00093     _lives = 6;
00094 }
00095 
00096 
00097 Vector2D Paddle::get_pos()  // returns the vector of the paddle's current position
00098 {
00099     Vector2D p = {_x,_y};
00100     return p;
00101 }
00102 
00103 
00104 void Paddle::set_tilt()    // sets the paddle motion option to tilt i.e. the member variable _tilt = true
00105 {
00106     _tilt = true;
00107 }
00108 
00109 
00110 void Paddle::set_joy()     // sets the paddle motion option to joystick
00111 {
00112     _tilt = false;
00113 }
00114 
00115 
00116 void Paddle::recentre()    // moves the paddle back to the centre of the screen (used after victory screen to restart)
00117 {
00118     _x = WIDTH/2 - PADDLE_WIDTH/2;  // centre of the screen
00119 }
00120 
00121 
00122 void Paddle::set_sens(float sens)  // sets the member variable _sens to an input
00123 {
00124     _sens = sens;
00125 }