For Nikhil

Dependencies:   4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Committer:
jford38
Date:
Wed Oct 28 08:40:55 2015 +0000
Revision:
19:7aa3af04d6a8
Parent:
18:18dfc9fb33b5
Child:
20:6a58052b0140
Finished! I think. Haha :D

Who changed what in which revision?

UserRevisionLine numberNew 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 15:4b27a3a95772 4 #include "globals.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 15:4b27a3a95772 24 int Bullet::time_step(float dt) {
jford38 19:7aa3af04d6a8 25 if(!in_flight) {return CONVERT_24_TO_16_BPP(SKY_COLOR);}
jford38 15:4b27a3a95772 26 time += dt;
jford38 15:4b27a3a95772 27 int new_x = x0 + vx0*time;
jford38 15:4b27a3a95772 28 int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time;
jford38 15:4b27a3a95772 29
jford38 15:4b27a3a95772 30 if(new_x == x && new_y == y) { // Didn't move!
jford38 19:7aa3af04d6a8 31 return CONVERT_24_TO_16_BPP(SKY_COLOR);
jford38 15:4b27a3a95772 32 }
jford38 15:4b27a3a95772 33
jford38 15:4b27a3a95772 34 if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) {
jford38 19:7aa3af04d6a8 35 sync.pixel(x, y, SKY_COLOR);
jford38 15:4b27a3a95772 36 in_flight = false;
jford38 19:7aa3af04d6a8 37 return CONVERT_24_TO_16_BPP(BLACK);
jford38 15:4b27a3a95772 38 }
jford38 15:4b27a3a95772 39
jford38 15:4b27a3a95772 40 int col = sync.read_pixel(new_x, new_y);
jford38 15:4b27a3a95772 41 if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
jford38 15:4b27a3a95772 42 sync.pixel(x, y, SKY_COLOR);
jford38 15:4b27a3a95772 43 sync.filled_circle(new_x, new_y, 4, SKY_COLOR);
jford38 15:4b27a3a95772 44 in_flight = false;
jford38 15:4b27a3a95772 45
jford38 15:4b27a3a95772 46 if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) {
jford38 19:7aa3af04d6a8 47 return col;
jford38 15:4b27a3a95772 48 }
jford38 18:18dfc9fb33b5 49
jford38 18:18dfc9fb33b5 50 if( col == CONVERT_24_TO_16_BPP(TANK_RED)) {
jford38 19:7aa3af04d6a8 51 return col;
jford38 18:18dfc9fb33b5 52 }
jford38 18:18dfc9fb33b5 53
jford38 19:7aa3af04d6a8 54 return CONVERT_24_TO_16_BPP(BLACK);
jford38 15:4b27a3a95772 55 }
jford38 15:4b27a3a95772 56 sync.pixel(x, y, SKY_COLOR);
jford38 15:4b27a3a95772 57
jford38 15:4b27a3a95772 58 x = new_x;
jford38 15:4b27a3a95772 59 y = new_y;
jford38 15:4b27a3a95772 60 sync.pixel(x, y, BLACK);
jford38 19:7aa3af04d6a8 61 return CONVERT_24_TO_16_BPP(SKY_COLOR);
jford38 15:4b27a3a95772 62 }