ELEC2645 (2018/19) / Mbed 2 deprecated henririgby98

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Missiles.cpp Source File

Missiles.cpp

00001 #include "Missiles.h"
00002 
00003 Missiles::Missiles()
00004 {
00005 
00006 }
00007 
00008 Missiles::~Missiles()
00009 {
00010 
00011 }
00012 
00013 void Missiles::init(int size)
00014 {
00015     _speed = 3;  // sets initial speed of missile to 3
00016     _size = size;
00017     srand(time(NULL));
00018     int N = rand() % 4;  //randomises a value of N between 0 and 3
00019     if (N == 0) {
00020         _y = HEIGHT;  //sets _y value to bottom of lcd display
00021         _x = rand() % 80 +4;  //randomises _x value
00022         south();  //calls south function
00023     } else if (N == 1){
00024                _y = 0;  //sets _y value to top of lcd display
00025                _x = rand() % 80 +4;
00026                north(); //calls north function
00027     } else if (N == 2){
00028                _y = rand() % 44 +4;  //randomises _y value
00029                _x = 0;  //sets _x value to left side of lcd display
00030                west();  //calls west function
00031      } else {
00032             _y = rand() % 44 +4;
00033             _x = WIDTH;  //sets _x value to right side of lcd display
00034             east();  //calls east function
00035     }
00036 }
00037 
00038 void Missiles::south()
00039 {
00040     srand(time(NULL));
00041     int direction = rand() % 2; //randomises value for direction between 0-1
00042         if (direction == 0) {
00043                   _velocity.x = ((2*_speed)^1/2)/2;  //sets x velocity equation means that no strafing will occur
00044                   _velocity.y = -((2*_speed)^1/2)/2;  //sets y velocity
00045         } else {
00046                   _velocity.x = -((2*_speed)^1/2)/2;
00047                   _velocity.y = -((2*_speed)^1/2)/2;
00048         }
00049 }
00050 
00051 void Missiles::north()
00052 {
00053            srand(time(NULL));
00054            int direction = rand() % 2;
00055             if (direction == 0) {
00056                        _velocity.x = ((2*_speed)^1/2)/2;
00057                        _velocity.y = ((2*_speed)^1/2)/2;
00058             } else {
00059                     _velocity.x = -((2*_speed)^1/2)/2;
00060                     _velocity.y = ((2*_speed)^1/2)/2;
00061             }
00062 }
00063 
00064 void Missiles::west()
00065 {
00066            srand(time(NULL));
00067            int direction = rand() % 2;
00068            if (direction == 0) {
00069                        _velocity.x = ((2*_speed)^1/2)/2;
00070                        _velocity.y = ((2*_speed)^1/2)/2;
00071            } else {
00072                   _velocity.x = ((2*_speed)^1/2)/2;
00073                   _velocity.y = -((2*_speed)^1/2)/2;
00074            }
00075 }
00076 
00077 void Missiles::east()
00078 {
00079         int direction = rand() % 2;
00080         if (direction == 0) {
00081                    _velocity.x = -((2*_speed)^1/2)/2;
00082                    _velocity.y = ((2*_speed)^1/2)/2;
00083         } else {
00084                _velocity.x = -((2*_speed)^1/2)/2;
00085                _velocity.y = -((2*_speed)^1/2)/2;
00086         }
00087 }
00088 
00089 void Missiles::update()
00090 {
00091     _x += _velocity.x;  //sets _x by adding velocity in x direction
00092     _y += _velocity.y;  //sets _y by adding velocity in y direction
00093 }
00094 
00095 void Missiles::draw(N5110 &lcd)
00096 {
00097     lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);  //draws square representing missile
00098 }
00099 
00100 void Missiles::set_velocity(Vector2D v)
00101 {
00102     _velocity.x = v.x;  //sets the velocity in the x direction
00103     _velocity.y = v.y;  //sets the velocity in the y direction
00104 }
00105 
00106 Vector2D Missiles::get_velocity()
00107 {
00108     Vector2D v = {_velocity.x,_velocity.y};  //sets 2D velocity
00109     return v;
00110 }
00111 
00112 Vector2D Missiles::get_pos()
00113 {
00114     Vector2D p = {_x,_y};  //gets the position by taking both _x and _y coordinates
00115     return p;
00116 }
00117 
00118 void Missiles::set_pos(Vector2D p)
00119 {
00120     _x = p.x;  //sets _x by taking the x component of position
00121     _y = p.y;  //sets _y by taking the y component of position
00122 }
00123 
00124 int Missiles::set_score()
00125 {
00126     _score = 0;  //sets the value of _score to equal 0
00127     return _score;
00128 }
00129 
00130 void Missiles::add_score()
00131 {
00132     _score++;  //increases the value of score by 1
00133 }
00134 
00135 int Missiles::get_score()
00136 {
00137     return _score;  //returns the value of _score
00138 }