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 06:36:24 2015 +0000
Revision:
18:18dfc9fb33b5
Parent:
17:7bc7127782e4
Child:
19:7aa3af04d6a8
Most things working! Multi-player and single-player working with accelerometer x controlling the tanks and y controlling the gun. Uses the down button to fire.

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 15:4b27a3a95772 25 if(!in_flight) {return 0;}
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 15:4b27a3a95772 31 return 0;
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 15:4b27a3a95772 35 in_flight = false;
jford38 15:4b27a3a95772 36 return 0;
jford38 15:4b27a3a95772 37 }
jford38 15:4b27a3a95772 38
jford38 15:4b27a3a95772 39 int col = sync.read_pixel(new_x, new_y);
jford38 15:4b27a3a95772 40 if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
jford38 15:4b27a3a95772 41 sync.pixel(x, y, SKY_COLOR);
jford38 15:4b27a3a95772 42 sync.filled_circle(new_x, new_y, 4, SKY_COLOR);
jford38 15:4b27a3a95772 43 in_flight = false;
jford38 15:4b27a3a95772 44
jford38 15:4b27a3a95772 45 if(col == CONVERT_24_TO_16_BPP(TANK_BLUE)) {
jford38 18:18dfc9fb33b5 46 return TANK_BLUE;
jford38 15:4b27a3a95772 47 }
jford38 18:18dfc9fb33b5 48
jford38 18:18dfc9fb33b5 49 if( col == CONVERT_24_TO_16_BPP(TANK_RED)) {
jford38 18:18dfc9fb33b5 50 return TANK_RED;
jford38 18:18dfc9fb33b5 51 }
jford38 18:18dfc9fb33b5 52
jford38 15:4b27a3a95772 53 return 0;
jford38 15:4b27a3a95772 54 }
jford38 15:4b27a3a95772 55 sync.pixel(x, y, SKY_COLOR);
jford38 15:4b27a3a95772 56
jford38 15:4b27a3a95772 57 x = new_x;
jford38 15:4b27a3a95772 58 y = new_y;
jford38 15:4b27a3a95772 59 sync.pixel(x, y, BLACK);
jford38 15:4b27a3a95772 60 return 0;
jford38 15:4b27a3a95772 61 }