Shell code for the Tanks game
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Tank/tank.cpp@14:36c306e26317, 2015-10-26 (annotated)
- Committer:
- jford38
- Date:
- Mon Oct 26 02:58:37 2015 +0000
- Revision:
- 14:36c306e26317
- Child:
- 19:7aa3af04d6a8
Split things into multiple files. Working pretty well now!
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 | 14:36c306e26317 | 8 | |
jford38 | 14:36c306e26317 | 9 | Tank::Tank(int sx, int sy, int width, int height, int color) { |
jford38 | 14:36c306e26317 | 10 | x = sx; y = sy; |
jford38 | 14:36c306e26317 | 11 | w = width; h = height; |
jford38 | 14:36c306e26317 | 12 | tank_color = color; |
jford38 | 14:36c306e26317 | 13 | barrel_theta = PI/4.0; |
jford38 | 14:36c306e26317 | 14 | barrel_length = w; |
jford38 | 14:36c306e26317 | 15 | wheel_rad = 2.0; |
jford38 | 14:36c306e26317 | 16 | draw(); |
jford38 | 14:36c306e26317 | 17 | } |
jford38 | 14:36c306e26317 | 18 | int Tank::min_x(void) { |
jford38 | 14:36c306e26317 | 19 | int barrel_extent = (-barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) + w/2.0 : 0; |
jford38 | 14:36c306e26317 | 20 | return x + barrel_extent; |
jford38 | 14:36c306e26317 | 21 | } |
jford38 | 14:36c306e26317 | 22 | |
jford38 | 14:36c306e26317 | 23 | int Tank::min_y(void) { return y; } |
jford38 | 14:36c306e26317 | 24 | int Tank::max_x(void) { |
jford38 | 14:36c306e26317 | 25 | int barrel_extent = (barrel_length*cos(barrel_theta) > w/2.0) ? barrel_length*cos(barrel_theta) : w/2.0; |
jford38 | 14:36c306e26317 | 26 | return x + w/2.0 + barrel_extent; |
jford38 | 14:36c306e26317 | 27 | } |
jford38 | 14:36c306e26317 | 28 | |
jford38 | 14:36c306e26317 | 29 | int Tank::max_y(void) { |
jford38 | 14:36c306e26317 | 30 | int barrel_extent = (sin(barrel_theta) > 0) ? barrel_length*sin(barrel_theta) : 0; |
jford38 | 14:36c306e26317 | 31 | return y + h + wheel_rad + barrel_extent; |
jford38 | 14:36c306e26317 | 32 | } |
jford38 | 14:36c306e26317 | 33 | |
jford38 | 14:36c306e26317 | 34 | void Tank::barrel_end(int& bx, int& by) { |
jford38 | 14:36c306e26317 | 35 | bx = x + w/2.0 + (barrel_length+1)*cos(barrel_theta); |
jford38 | 14:36c306e26317 | 36 | by = y+h+wheel_rad + (barrel_length+1)*sin(barrel_theta); |
jford38 | 14:36c306e26317 | 37 | } |
jford38 | 14:36c306e26317 | 38 | |
jford38 | 14:36c306e26317 | 39 | void Tank::reposition(int new_x, int new_y, float new_theta) { |
jford38 | 14:36c306e26317 | 40 | float old_theta = barrel_theta; |
jford38 | 14:36c306e26317 | 41 | int old_x = x; |
jford38 | 14:36c306e26317 | 42 | int old_y = y; |
jford38 | 14:36c306e26317 | 43 | int minx = min_x(); |
jford38 | 14:36c306e26317 | 44 | int miny = min_y(); |
jford38 | 14:36c306e26317 | 45 | int maxx = max_x(); |
jford38 | 14:36c306e26317 | 46 | int maxy = max_y(); |
jford38 | 14:36c306e26317 | 47 | |
jford38 | 14:36c306e26317 | 48 | x = new_x; y = new_y; barrel_theta = new_theta - (int)(new_theta/PI)*PI; |
jford38 | 14:36c306e26317 | 49 | |
jford38 | 14:36c306e26317 | 50 | if(min_x() < 0 || max_x() > 128) { |
jford38 | 14:36c306e26317 | 51 | x = old_x; y = old_y; barrel_theta = old_theta; |
jford38 | 14:36c306e26317 | 52 | draw(); |
jford38 | 14:36c306e26317 | 53 | return; |
jford38 | 14:36c306e26317 | 54 | } |
jford38 | 14:36c306e26317 | 55 | |
jford38 | 14:36c306e26317 | 56 | int right_edge = sync.read_pixel(min_x()-1, (min_y()+max_y())/2.0); |
jford38 | 14:36c306e26317 | 57 | int left_edge = sync.read_pixel(max_x()+1, (min_y()+max_y())/2.0); |
jford38 | 14:36c306e26317 | 58 | |
jford38 | 14:36c306e26317 | 59 | if( ( right_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x < old_x ) || |
jford38 | 14:36c306e26317 | 60 | ( left_edge != CONVERT_24_TO_16_BPP(SKY_COLOR) && new_x > old_x ) ) { |
jford38 | 14:36c306e26317 | 61 | x = old_x; y = old_y; barrel_theta = old_theta; |
jford38 | 14:36c306e26317 | 62 | draw(); |
jford38 | 14:36c306e26317 | 63 | return; |
jford38 | 14:36c306e26317 | 64 | } |
jford38 | 14:36c306e26317 | 65 | |
jford38 | 14:36c306e26317 | 66 | |
jford38 | 14:36c306e26317 | 67 | sync.filled_rectangle(minx, miny, maxx, maxy, SKY_COLOR); |
jford38 | 14:36c306e26317 | 68 | 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 | 69 | draw(); |
jford38 | 14:36c306e26317 | 70 | } |
jford38 | 14:36c306e26317 | 71 | |
jford38 | 14:36c306e26317 | 72 | void Tank::draw() { |
jford38 | 14:36c306e26317 | 73 | 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 | 74 | sync.filled_rectangle(x, y+wheel_rad, x+w, y+h+wheel_rad, tank_color); |
jford38 | 14:36c306e26317 | 75 | sync.filled_circle(x+wheel_rad, y+wheel_rad, wheel_rad, BLACK); |
jford38 | 14:36c306e26317 | 76 | sync.filled_circle(x+w-wheel_rad, y+wheel_rad, wheel_rad, BLACK); |
jford38 | 14:36c306e26317 | 77 | } |