Shell code for the Tanks game
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Diff: Tank/tank.cpp
- Revision:
- 14:36c306e26317
- Child:
- 19:7aa3af04d6a8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tank/tank.cpp Mon Oct 26 02:58:37 2015 +0000 @@ -0,0 +1,77 @@ +#include "tank.h" +#include "globals.h" +#include "math.h" +#include "game_synchronizer.h" + +extern Game_Synchronizer sync; + + +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 new_x, int new_y, float new_theta) { + 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 - (int)(new_theta/PI)*PI; + + 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); +} \ No newline at end of file