The classic dueling tanks game for mbed.
Dependencies: 4DGL-uLCD-SE DRV2605 EthernetInterface Game_Synchronizer MMA8452 SDFileSystem SparkfunAnalogJoystick mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Tank/tank.cpp
- Committer:
- jford38
- Date:
- 2015-10-29
- Revision:
- 20:6a58052b0140
- Parent:
- 19:7aa3af04d6a8
- Child:
- 21:edfeb289b21f
File content as of revision 20:6a58052b0140:
#include "tank.h" #include "globals.h" #include "math.h" #include "game_synchronizer.h" extern Game_Synchronizer sync; // sx is the x-coord of the bottom left corner of the tank // sy is the y-coord of the same corner // width is the width of the tank // height is the height of the tank Tank::Tank(int sx, int sy, int width, int height, int color) { x = sx; y = sy; w = width; h = height; tank_color = color; barrel_theta = PI/4.0; barrel_length = w; wheel_rad = 2.0; draw(); } int Tank::min_x(void) { int barrel_extent = (-barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) + w/2.0 : 0; return x + barrel_extent; } int Tank::min_y(void) { return y; } int Tank::max_x(void) { int barrel_extent = (barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) : w/2.0; return x + w/2.0 + barrel_extent; } int Tank::max_y(void) { int barrel_extent = (sin(barrel_theta) > 0) ? barrel_length*sin(barrel_theta) : 0; return y + h + wheel_rad + barrel_extent; } void Tank::barrel_end(int& bx, int& by) { bx = x + w/2.0 + (barrel_length+1)*cos(barrel_theta); by = y+h+wheel_rad + (barrel_length+1)*sin(barrel_theta); } void Tank::reposition(int dx, int dy, float dtheta) { int new_x = x+dx; int new_y = y+dy; float new_theta = abs(barrel_theta+dtheta); float old_theta = barrel_theta; int old_x = x; int old_y = y; int minx = min_x(); int miny = min_y(); int maxx = max_x(); int maxy = max_y(); x = new_x; y = new_y; barrel_theta = (new_theta > PI) ? PI : new_theta; if(min_x() < 0 || max_x() > 128) { x = old_x; y = old_y; barrel_theta = old_theta; draw(); return; } int right_edge = sync.read_pixel(min_x()-1, (min_y()+max_y())/2.0); int left_edge = sync.read_pixel(max_x()+1, (min_y()+max_y())/2.0); if( ( right_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x < old_x ) || ( left_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x > old_x ) ) { x = old_x; y = old_y; barrel_theta = old_theta; draw(); return; } sync.filled_rectangle(minx, miny, maxx, maxy, SKY_COLOR); sync.line(old_x + w/2.0, old_y+h+wheel_rad, old_x + w/2.0 + 1 + barrel_length*cos(old_theta), old_y+h + barrel_length*sin(old_theta), SKY_COLOR); draw(); } void Tank::draw() { 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); sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color); sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK); sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK); }