Jason McNeely / Mbed 2 deprecated DuelingTanks

Dependencies:   4DGL-uLCD-SE DRV2605 EthernetInterface Game_Synchronizer MMA8452 SDFileSystem SparkfunAnalogJoystick mbed-rtos mbed wave_player

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tank.cpp Source File

tank.cpp

00001 #include "tank.h"
00002 #include "globals.h"
00003 #include "math.h"
00004 #include "game_synchronizer.h"
00005 #include "uLCD_4DGL.h"
00006 
00007 extern Game_Synchronizer sync;
00008 
00009 // sx is the x-coord of the bottom left corner of the tank
00010 // sy is the y-coord of the same corner
00011 // width is the width of the tank
00012 // height is the height of the tank
00013 Tank::Tank(int sx, int sy, int width, int height, int color) {
00014     x = sx; y = sy;
00015     w = width; h = height;
00016     tank_color = color;
00017     barrel_theta = PI/4.0;
00018     barrel_length = .75*w;
00019     wheel_rad = 2.0;
00020     draw();
00021 }
00022 
00023 // Return the minimum x-coord of your tank's bounding box.
00024 int Tank::min_x(void) {
00025     if(w/2.0+x+barrel_length*cos(barrel_theta)<x){
00026         return w/2.0+x+barrel_length*cos(barrel_theta);
00027         }
00028     else{ 
00029     return x;
00030     }
00031 }    
00032 
00033 // Return the minimum y-coord of your tank's bounding box.
00034 int Tank::min_y(void) {
00035     return y+wheel_rad;
00036 }
00037 
00038 // Return the maximum x-coord of your tank's bounding box.
00039 int Tank::max_x(void) { 
00040     if(w/2.0+w+barrel_length*cos(barrel_theta)>x+w){
00041     return x+w/2+barrel_length*cos(barrel_theta);;
00042     }
00043     else{
00044         return x+w;
00045         }
00046 }
00047 
00048 
00049 // Return the maximum y-coord of your tank's bounding box.
00050 int Tank::max_y(void) {  
00051     return y+h+wheel_rad+barrel_length*sin(barrel_theta);
00052 }
00053 
00054 void Tank::barrel_end(int* bx, int* by) {
00055     // Set the x and y coords of the end of the barrel.
00056     *bx = x + w/2.0 + barrel_length*cos(barrel_theta); 
00057     *by = y+h+wheel_rad + barrel_length*sin(barrel_theta);
00058 }
00059 
00060 void Tank::reposition(int dx, int dy, float dtheta) {
00061       
00062     if(dx!=0 || dy!=0 || dtheta!=0){
00063         x=x+dx;
00064         y=y+dy;
00065         sync.filled_rectangle(x-4, y-1, x+20, y+21, SKY_COLOR);//      Blank out the old tank position, and
00066         
00067         sync.filled_circle(53, 26, 6, TREE);
00068         draw();                                         //      Move the tank dx pixels in the x direction.
00069         sync.filled_circle(53, 26, 6, TREE);
00070         //sync.line(76,23,76,50, BLACK);
00071         //sync.rectangle(74, 20, 75, 50, WHITE);
00072         sync.filled_circle(78, 19, 4, TREE);            //      Move the tank dy pixels in the y direction.
00073         sync.update();
00074     if(0 <= barrel_theta+dtheta && barrel_theta+dtheta <= PI){                          //      Move the tank barrel by an angle dtheta. 
00075         sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), SKY_COLOR);//      Don't allow it to go below parallel.
00076         barrel_theta += dtheta;
00077         sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BLACK);
00078         sync.filled_rectangle(x+2*wheel_rad, y+3*wheel_rad, x+.65*w, y+h+3*wheel_rad, tank_color);
00079         sync.update();
00080     }  
00081     
00082     
00083     //if((x){  //Do collision detection to prevent the tank from hitting things.
00084       //(x)=x+1;
00085       //}  
00086       
00087     //if(x+16>127){  //Do collision detection to prevent the tank from hitting things.
00088       //x=111;
00089       //}
00090       
00091       //int obsticle=sync.read_pixel(x+20,y);
00092       
00093     //if(sync.pixel_eq(obsticle, SKY_COLOR)!=1){
00094         //x=x-1;
00095         //}
00096     //      (obstacles, side of the screen, other tanks, etc.)
00097     }
00098 }
00099     // Example tank draw function. We expect you to get creative on this one!
00100 void Tank::draw() {
00101     //if(x>4 || x<111){
00102     sync.line(x + w/2.0, y+h+wheel_rad, x + w/2.0 + barrel_length*cos(barrel_theta), y+h+wheel_rad + barrel_length*sin(barrel_theta), BARREL_GREY);
00103     sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BARREL_GREY);
00104     sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BARREL_GREY);
00105     sync.filled_circle(x+(w/1.475)-wheel_rad,y+wheel_rad, wheel_rad, BARREL_GREY);
00106     sync.filled_rectangle(x, y+(2*wheel_rad), x+w, y+h+wheel_rad, tank_color);
00107     sync.filled_rectangle(x+2*wheel_rad, y+3*wheel_rad, x+.65*w, y+h+3*wheel_rad, tank_color);
00108     sync.line(x+wheel_rad,y-.5*wheel_rad, x+w-wheel_rad, y-.5*wheel_rad, BARREL_GREY);
00109     sync.line(x+(2*wheel_rad), y+h+wheel_rad, x+.65*w, y+h+wheel_rad, BLACK);
00110     
00111     //}
00112 }