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@20:6a58052b0140, 2015-10-29 (annotated)
- Committer:
- jford38
- Date:
- Thu Oct 29 02:21:11 2015 +0000
- Revision:
- 20:6a58052b0140
- Parent:
- 19:7aa3af04d6a8
- Child:
- 21:edfeb289b21f
Updated to use separate functions to get button data and accelerometer data separately.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 14:36c306e26317 | 1 | #include "tank.h" |
jford38 | 14:36c306e26317 | 2 | #include "globals.h" |
jford38 | 14:36c306e26317 | 3 | #include "math.h" |
jford38 | 14:36c306e26317 | 4 | #include "game_synchronizer.h" |
jford38 | 14:36c306e26317 | 5 | |
jford38 | 14:36c306e26317 | 6 | extern Game_Synchronizer sync; |
jford38 | 14:36c306e26317 | 7 | |
jford38 | 20:6a58052b0140 | 8 | // sx is the x-coord of the bottom left corner of the tank |
jford38 | 20:6a58052b0140 | 9 | // sy is the y-coord of the same corner |
jford38 | 20:6a58052b0140 | 10 | // width is the width of the tank |
jford38 | 20:6a58052b0140 | 11 | // height is the height of the tank |
jford38 | 14:36c306e26317 | 12 | Tank::Tank(int sx, int sy, int width, int height, int color) { |
jford38 | 14:36c306e26317 | 13 | x = sx; y = sy; |
jford38 | 14:36c306e26317 | 14 | w = width; h = height; |
jford38 | 14:36c306e26317 | 15 | tank_color = color; |
jford38 | 14:36c306e26317 | 16 | barrel_theta = PI/4.0; |
jford38 | 14:36c306e26317 | 17 | barrel_length = w; |
jford38 | 14:36c306e26317 | 18 | wheel_rad = 2.0; |
jford38 | 14:36c306e26317 | 19 | draw(); |
jford38 | 14:36c306e26317 | 20 | } |
jford38 | 14:36c306e26317 | 21 | int Tank::min_x(void) { |
jford38 | 14:36c306e26317 | 22 | int barrel_extent = (-barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) + w/2.0 : 0; |
jford38 | 14:36c306e26317 | 23 | return x + barrel_extent; |
jford38 | 14:36c306e26317 | 24 | } |
jford38 | 14:36c306e26317 | 25 | |
jford38 | 14:36c306e26317 | 26 | int Tank::min_y(void) { return y; } |
jford38 | 14:36c306e26317 | 27 | int Tank::max_x(void) { |
jford38 | 14:36c306e26317 | 28 | int barrel_extent = (barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) : w/2.0; |
jford38 | 14:36c306e26317 | 29 | return x + w/2.0 + barrel_extent; |
jford38 | 14:36c306e26317 | 30 | } |
jford38 | 14:36c306e26317 | 31 | |
jford38 | 14:36c306e26317 | 32 | int Tank::max_y(void) { |
jford38 | 14:36c306e26317 | 33 | int barrel_extent = (sin(barrel_theta) > 0) ? barrel_length*sin(barrel_theta) : 0; |
jford38 | 14:36c306e26317 | 34 | return y + h + wheel_rad + barrel_extent; |
jford38 | 14:36c306e26317 | 35 | } |
jford38 | 14:36c306e26317 | 36 | |
jford38 | 14:36c306e26317 | 37 | void Tank::barrel_end(int& bx, int& by) { |
jford38 | 14:36c306e26317 | 38 | bx = x + w/2.0 + (barrel_length+1)*cos(barrel_theta); |
jford38 | 14:36c306e26317 | 39 | by = y+h+wheel_rad + (barrel_length+1)*sin(barrel_theta); |
jford38 | 14:36c306e26317 | 40 | } |
jford38 | 14:36c306e26317 | 41 | |
jford38 | 19:7aa3af04d6a8 | 42 | void Tank::reposition(int dx, int dy, float dtheta) { |
jford38 | 19:7aa3af04d6a8 | 43 | |
jford38 | 19:7aa3af04d6a8 | 44 | int new_x = x+dx; |
jford38 | 19:7aa3af04d6a8 | 45 | int new_y = y+dy; |
jford38 | 19:7aa3af04d6a8 | 46 | float new_theta = abs(barrel_theta+dtheta); |
jford38 | 19:7aa3af04d6a8 | 47 | |
jford38 | 14:36c306e26317 | 48 | float old_theta = barrel_theta; |
jford38 | 14:36c306e26317 | 49 | int old_x = x; |
jford38 | 14:36c306e26317 | 50 | int old_y = y; |
jford38 | 14:36c306e26317 | 51 | int minx = min_x(); |
jford38 | 14:36c306e26317 | 52 | int miny = min_y(); |
jford38 | 14:36c306e26317 | 53 | int maxx = max_x(); |
jford38 | 14:36c306e26317 | 54 | int maxy = max_y(); |
jford38 | 14:36c306e26317 | 55 | |
jford38 | 19:7aa3af04d6a8 | 56 | x = new_x; y = new_y; barrel_theta = (new_theta > PI) ? PI : new_theta; |
jford38 | 14:36c306e26317 | 57 | |
jford38 | 14:36c306e26317 | 58 | if(min_x() < 0 || max_x() > 128) { |
jford38 | 14:36c306e26317 | 59 | x = old_x; y = old_y; barrel_theta = old_theta; |
jford38 | 14:36c306e26317 | 60 | draw(); |
jford38 | 14:36c306e26317 | 61 | return; |
jford38 | 14:36c306e26317 | 62 | } |
jford38 | 14:36c306e26317 | 63 | |
jford38 | 14:36c306e26317 | 64 | int right_edge = sync.read_pixel(min_x()-1, (min_y()+max_y())/2.0); |
jford38 | 14:36c306e26317 | 65 | int left_edge = sync.read_pixel(max_x()+1, (min_y()+max_y())/2.0); |
jford38 | 14:36c306e26317 | 66 | |
jford38 | 14:36c306e26317 | 67 | if( ( right_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x < old_x ) || |
jford38 | 14:36c306e26317 | 68 | ( left_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x > old_x ) ) { |
jford38 | 14:36c306e26317 | 69 | x = old_x; y = old_y; barrel_theta = old_theta; |
jford38 | 14:36c306e26317 | 70 | draw(); |
jford38 | 14:36c306e26317 | 71 | return; |
jford38 | 14:36c306e26317 | 72 | } |
jford38 | 14:36c306e26317 | 73 | |
jford38 | 14:36c306e26317 | 74 | |
jford38 | 14:36c306e26317 | 75 | sync.filled_rectangle(minx, miny, maxx, maxy, SKY_COLOR); |
jford38 | 14:36c306e26317 | 76 | 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); |
jford38 | 14:36c306e26317 | 77 | draw(); |
jford38 | 14:36c306e26317 | 78 | } |
jford38 | 14:36c306e26317 | 79 | |
jford38 | 14:36c306e26317 | 80 | void Tank::draw() { |
jford38 | 14:36c306e26317 | 81 | 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); |
jford38 | 14:36c306e26317 | 82 | sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color); |
jford38 | 14:36c306e26317 | 83 | sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK); |
jford38 | 14:36c306e26317 | 84 | sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK); |
jford38 | 14:36c306e26317 | 85 | } |