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
Bullet/bullet.cpp@21:edfeb289b21f, 2015-10-29 (annotated)
- Committer:
- jford38
- Date:
- Thu Oct 29 03:56:30 2015 +0000
- Revision:
- 21:edfeb289b21f
- Parent:
- 20:6a58052b0140
- Child:
- 22:3c68eea5a609
Final Version (I hope). Gonna start stripping out the code to make the shell I reckon.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 17:7bc7127782e4 | 1 | #include "uLCD_4DGL.h" |
jford38 | 15:4b27a3a95772 | 2 | #include "bullet.h" |
jford38 | 15:4b27a3a95772 | 3 | #include "game_synchronizer.h" |
jford38 | 20:6a58052b0140 | 4 | #include "misc.h" |
jford38 | 15:4b27a3a95772 | 5 | #include "math.h" |
jford38 | 15:4b27a3a95772 | 6 | |
jford38 | 15:4b27a3a95772 | 7 | extern Game_Synchronizer sync; |
jford38 | 15:4b27a3a95772 | 8 | |
jford38 | 15:4b27a3a95772 | 9 | Bullet::Bullet(Tank* t) { |
jford38 | 15:4b27a3a95772 | 10 | tank = t; |
jford38 | 15:4b27a3a95772 | 11 | speed = 30; |
jford38 | 15:4b27a3a95772 | 12 | in_flight = false; |
jford38 | 15:4b27a3a95772 | 13 | } |
jford38 | 15:4b27a3a95772 | 14 | void Bullet::shoot(void) { |
jford38 | 15:4b27a3a95772 | 15 | if(in_flight) {return;} |
jford38 | 15:4b27a3a95772 | 16 | time = 0; |
jford38 | 15:4b27a3a95772 | 17 | tank->barrel_end(x0, y0); |
jford38 | 15:4b27a3a95772 | 18 | x = x0; y = y0; |
jford38 | 15:4b27a3a95772 | 19 | vx0 = speed*cos(tank->barrel_theta); |
jford38 | 15:4b27a3a95772 | 20 | vy0 = speed*sin(tank->barrel_theta); |
jford38 | 15:4b27a3a95772 | 21 | in_flight = true; |
jford38 | 15:4b27a3a95772 | 22 | } |
jford38 | 15:4b27a3a95772 | 23 | |
jford38 | 21:edfeb289b21f | 24 | void Bullet::update_position(float dt) { |
jford38 | 21:edfeb289b21f | 25 | if(!in_flight) { return; } |
jford38 | 15:4b27a3a95772 | 26 | time += dt; |
jford38 | 21:edfeb289b21f | 27 | x = x0 + vx0*time; |
jford38 | 21:edfeb289b21f | 28 | y = y0 + vy0*time + 0.5*(-9.81)*time*time; |
jford38 | 21:edfeb289b21f | 29 | } |
jford38 | 21:edfeb289b21f | 30 | |
jford38 | 21:edfeb289b21f | 31 | // return codes: |
jford38 | 21:edfeb289b21f | 32 | // BULLET_NO_COLLISION: no collision |
jford38 | 21:edfeb289b21f | 33 | // BULLET_OFF_SCREEN: off the side of the screen |
jford38 | 21:edfeb289b21f | 34 | // Otherwise, returns the color you've hit in 16bpp format. |
jford38 | 21:edfeb289b21f | 35 | |
jford38 | 21:edfeb289b21f | 36 | int Bullet::time_step(float dt) { |
jford38 | 21:edfeb289b21f | 37 | if(!in_flight) { return BULLET_NO_COLLISION; } |
jford38 | 21:edfeb289b21f | 38 | int old_x = x; |
jford38 | 21:edfeb289b21f | 39 | int old_y = y; |
jford38 | 15:4b27a3a95772 | 40 | |
jford38 | 21:edfeb289b21f | 41 | update_position(dt); |
jford38 | 15:4b27a3a95772 | 42 | |
jford38 | 21:edfeb289b21f | 43 | if(x == old_x && y == old_y) { |
jford38 | 21:edfeb289b21f | 44 | return BULLET_NO_COLLISION; |
jford38 | 15:4b27a3a95772 | 45 | } |
jford38 | 15:4b27a3a95772 | 46 | |
jford38 | 21:edfeb289b21f | 47 | if(x < 0 || x > 128 || y < 0 || y > 128) { |
jford38 | 15:4b27a3a95772 | 48 | sync.pixel(x, y, SKY_COLOR); |
jford38 | 15:4b27a3a95772 | 49 | in_flight = false; |
jford38 | 21:edfeb289b21f | 50 | return BULLET_OFF_SCREEN; |
jford38 | 15:4b27a3a95772 | 51 | } |
jford38 | 21:edfeb289b21f | 52 | |
jford38 | 21:edfeb289b21f | 53 | int col = sync.read_pixel(x, y); |
jford38 | 21:edfeb289b21f | 54 | if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { |
jford38 | 21:edfeb289b21f | 55 | sync.pixel(old_x, old_y, SKY_COLOR); |
jford38 | 21:edfeb289b21f | 56 | sync.filled_circle(x, y, 4, SKY_COLOR); |
jford38 | 21:edfeb289b21f | 57 | in_flight = false; |
jford38 | 21:edfeb289b21f | 58 | return col; |
jford38 | 21:edfeb289b21f | 59 | } |
jford38 | 21:edfeb289b21f | 60 | sync.pixel(old_x, old_y, SKY_COLOR); |
jford38 | 15:4b27a3a95772 | 61 | sync.pixel(x, y, BLACK); |
jford38 | 21:edfeb289b21f | 62 | return BULLET_NO_COLLISION; |
jford38 | 21:edfeb289b21f | 63 | |
jford38 | 21:edfeb289b21f | 64 | } |