Revenge of the Mouse
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer LCD_fonts MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Diff: Bullet/bullet.cpp
- Revision:
- 21:edfeb289b21f
- Parent:
- 20:6a58052b0140
- Child:
- 22:3c68eea5a609
diff -r 6a58052b0140 -r edfeb289b21f Bullet/bullet.cpp --- a/Bullet/bullet.cpp Thu Oct 29 02:21:11 2015 +0000 +++ b/Bullet/bullet.cpp Thu Oct 29 03:56:30 2015 +0000 @@ -21,42 +21,44 @@ in_flight = true; } -int Bullet::time_step(float dt) { - if(!in_flight) {return CONVERT_24_TO_16_BPP(SKY_COLOR);} +void Bullet::update_position(float dt) { + if(!in_flight) { return; } time += dt; - int new_x = x0 + vx0*time; - int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time; + x = x0 + vx0*time; + y = y0 + vy0*time + 0.5*(-9.81)*time*time; +} + +// return codes: +// BULLET_NO_COLLISION: no collision +// BULLET_OFF_SCREEN: off the side of the screen +// 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; - if(new_x == x && new_y == y) { // Didn't move! - return CONVERT_24_TO_16_BPP(SKY_COLOR); - } + update_position(dt); - if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { - sync.pixel(x, y, SKY_COLOR); - in_flight = false; - return CONVERT_24_TO_16_BPP(BLACK); + if(x == old_x && y == old_y) { + return BULLET_NO_COLLISION; } - int col = sync.read_pixel(new_x, new_y); - if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { + if(x < 0 || x > 128 || y < 0 || y > 128) { sync.pixel(x, y, SKY_COLOR); - sync.filled_circle(new_x, new_y, 4, SKY_COLOR); in_flight = false; - - if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { - return col; - } - - if( col == CONVERT_24_TO_16_BPP(TANK_RED)) { - return col; - } - - return CONVERT_24_TO_16_BPP(BLACK); + return BULLET_OFF_SCREEN; } - sync.pixel(x, y, SKY_COLOR); - - x = new_x; - y = new_y; + + 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 CONVERT_24_TO_16_BPP(SKY_COLOR); -} + return BULLET_NO_COLLISION; + +} \ No newline at end of file