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
Diff: Bullet/bullet.cpp
- Revision:
- 22:3c68eea5a609
- Parent:
- 21:edfeb289b21f
- Child:
- 23:77049670cae6
--- a/Bullet/bullet.cpp Thu Oct 29 03:56:30 2015 +0000 +++ b/Bullet/bullet.cpp Thu Oct 29 05:14:49 2015 +0000 @@ -6,26 +6,23 @@ extern Game_Synchronizer sync; +// Initialize the bullet. Don't have to do much here. +// Just set the speed. Bullet::Bullet(Tank* t) { tank = t; - speed = 30; + //speed = ???; in_flight = false; } + +// Set the in_flight flag. Initialize values needed for +// the trajectory calculations. void Bullet::shoot(void) { - if(in_flight) {return;} - time = 0; - tank->barrel_end(x0, y0); - x = x0; y = y0; - vx0 = speed*cos(tank->barrel_theta); - vy0 = speed*sin(tank->barrel_theta); - in_flight = true; } +// If the bullet is in flight, calculate its new position +// after a time delta dt. void Bullet::update_position(float dt) { - if(!in_flight) { return; } - time += dt; - x = x0 + vx0*time; - y = y0 + vy0*time + 0.5*(-9.81)*time*time; + } // return codes: @@ -34,31 +31,8 @@ // Otherwise, returns the color you've hit in 16bpp format. int Bullet::time_step(float dt) { - if(!in_flight) { return BULLET_NO_COLLISION; } - int old_x = x; - int old_y = y; - - update_position(dt); - - if(x == old_x && y == old_y) { - return BULLET_NO_COLLISION; - } - - if(x < 0 || x > 128 || y < 0 || y > 128) { - sync.pixel(x, y, SKY_COLOR); - in_flight = false; - return BULLET_OFF_SCREEN; - } - - int col = sync.read_pixel(x, y); - if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { - sync.pixel(old_x, old_y, SKY_COLOR); - sync.filled_circle(x, y, 4, SKY_COLOR); - in_flight = false; - return col; - } - sync.pixel(old_x, old_y, SKY_COLOR); - sync.pixel(x, y, BLACK); - return BULLET_NO_COLLISION; - + // If the bullet hasn't hit anything, + // redraw the bullet at its new location. + // If it has hit something (obstacle, tank, edge of the screen), return + // a descriptive error code. } \ No newline at end of file