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 ECE2035 Spring 2015 TA

bullet.cpp

Committer:
jford38
Date:
2015-10-26
Revision:
15:4b27a3a95772

File content as of revision 15:4b27a3a95772:

#include "bullet.h"
#include "game_synchronizer.h"
#include "globals.h"
#include "math.h"

extern Game_Synchronizer sync;

Bullet::Bullet(Tank* t) {
    tank = t;
    speed = 30;
    in_flight = false;
}
void Bullet::shoot(void) {
    if(in_flight) {return;}
    time = 0;
    tank->barrel_end(x0, y0);
    x = x0; y = y0;
    vx0 = speed*cos(tank->barrel_theta);
    vy0 = speed*sin(tank->barrel_theta);
    in_flight = true; 
}

int Bullet::time_step(float dt) {
    if(!in_flight) {return 0;}
    time += dt;
    int new_x = x0 + vx0*time;
    int new_y = y0 + vy0*time + 0.5*(-9.81)*time*time;
    
    if(new_x == x && new_y == y) {      // Didn't move!
        return 0;
    }
    
    if(new_x < 0 || new_x > 128 || new_y < 0 || new_y > 128) {
        in_flight = false;
        return 0;
    }
    
    int col = sync.read_pixel(new_x, new_y);
    if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
        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 1;
        }
        return 0;
    }
    sync.pixel(x, y, SKY_COLOR);
            
    x = new_x;
    y = new_y;            
    sync.pixel(x, y, BLACK);
    return 0;
}