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
bullet.cpp@15:4b27a3a95772, 2015-10-26 (annotated)
- Committer:
- jford38
- Date:
- Mon Oct 26 04:12:35 2015 +0000
- Revision:
- 15:4b27a3a95772
Updates. Idk. Leave me alone. It's late.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 15:4b27a3a95772 | 1 | #include "bullet.h" |
jford38 | 15:4b27a3a95772 | 2 | #include "game_synchronizer.h" |
jford38 | 15:4b27a3a95772 | 3 | #include "globals.h" |
jford38 | 15:4b27a3a95772 | 4 | #include "math.h" |
jford38 | 15:4b27a3a95772 | 5 | |
jford38 | 15:4b27a3a95772 | 6 | extern Game_Synchronizer sync; |
jford38 | 15:4b27a3a95772 | 7 | |
jford38 | 15:4b27a3a95772 | 8 | Bullet::Bullet(Tank* t) { |
jford38 | 15:4b27a3a95772 | 9 | tank = t; |
jford38 | 15:4b27a3a95772 | 10 | speed = 30; |
jford38 | 15:4b27a3a95772 | 11 | in_flight = false; |
jford38 | 15:4b27a3a95772 | 12 | } |
jford38 | 15:4b27a3a95772 | 13 | void Bullet::shoot(void) { |
jford38 | 15:4b27a3a95772 | 14 | if(in_flight) {return;} |
jford38 | 15:4b27a3a95772 | 15 | time = 0; |
jford38 | 15:4b27a3a95772 | 16 | tank->barrel_end(x0, y0); |
jford38 | 15:4b27a3a95772 | 17 | x = x0; y = y0; |
jford38 | 15:4b27a3a95772 | 18 | vx0 = speed*cos(tank->barrel_theta); |
jford38 | 15:4b27a3a95772 | 19 | vy0 = speed*sin(tank->barrel_theta); |
jford38 | 15:4b27a3a95772 | 20 | in_flight = true; |
jford38 | 15:4b27a3a95772 | 21 | } |
jford38 | 15:4b27a3a95772 | 22 | |
jford38 | 15:4b27a3a95772 | 23 | int Bullet::time_step(float dt) { |
jford38 | 15:4b27a3a95772 | 24 | if(!in_flight) {return 0;} |
jford38 | 15:4b27a3a95772 | 25 | time += dt; |
jford38 | 15:4b27a3a95772 | 26 | int new_x = x0 + vx0*time; |
jford38 | 15:4b27a3a95772 | 27 | int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time; |
jford38 | 15:4b27a3a95772 | 28 | |
jford38 | 15:4b27a3a95772 | 29 | if(new_x == x && new_y == y) { // Didn't move! |
jford38 | 15:4b27a3a95772 | 30 | return 0; |
jford38 | 15:4b27a3a95772 | 31 | } |
jford38 | 15:4b27a3a95772 | 32 | |
jford38 | 15:4b27a3a95772 | 33 | if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) { |
jford38 | 15:4b27a3a95772 | 34 | in_flight = false; |
jford38 | 15:4b27a3a95772 | 35 | return 0; |
jford38 | 15:4b27a3a95772 | 36 | } |
jford38 | 15:4b27a3a95772 | 37 | |
jford38 | 15:4b27a3a95772 | 38 | int col = sync.read_pixel(new_x, new_y); |
jford38 | 15:4b27a3a95772 | 39 | if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) { |
jford38 | 15:4b27a3a95772 | 40 | sync.pixel(x, y, SKY_COLOR); |
jford38 | 15:4b27a3a95772 | 41 | sync.filled_circle(new_x, new_y, 4, SKY_COLOR); |
jford38 | 15:4b27a3a95772 | 42 | in_flight = false; |
jford38 | 15:4b27a3a95772 | 43 | |
jford38 | 15:4b27a3a95772 | 44 | if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) { |
jford38 | 15:4b27a3a95772 | 45 | return 1; |
jford38 | 15:4b27a3a95772 | 46 | } |
jford38 | 15:4b27a3a95772 | 47 | return 0; |
jford38 | 15:4b27a3a95772 | 48 | } |
jford38 | 15:4b27a3a95772 | 49 | sync.pixel(x, y, SKY_COLOR); |
jford38 | 15:4b27a3a95772 | 50 | |
jford38 | 15:4b27a3a95772 | 51 | x = new_x; |
jford38 | 15:4b27a3a95772 | 52 | y = new_y; |
jford38 | 15:4b27a3a95772 | 53 | sync.pixel(x, y, BLACK); |
jford38 | 15:4b27a3a95772 | 54 | return 0; |
jford38 | 15:4b27a3a95772 | 55 | } |