Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Bullet/bullet.cpp
- Committer:
- jford38
- Date:
- 2015-10-29
- Revision:
- 21:edfeb289b21f
- Parent:
- 20:6a58052b0140
- Child:
- 22:3c68eea5a609
File content as of revision 21:edfeb289b21f:
#include "uLCD_4DGL.h"
#include "bullet.h"
#include "game_synchronizer.h"
#include "misc.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;
}
void Bullet::update_position(float dt) {
if(!in_flight) { return; }
time += dt;
x = x0 + vx0*time;
y = y0 + vy0*time + 0.5*(-9.81)*time*time;
}
// return codes:
// BULLET_NO_COLLISION: no collision
// BULLET_OFF_SCREEN: off the side of the screen
// Otherwise, returns the color you've hit in 16bpp format.
int Bullet::time_step(float dt) {
if(!in_flight) { return BULLET_NO_COLLISION; }
int old_x = x;
int old_y = y;
update_position(dt);
if(x == old_x && y == old_y) {
return BULLET_NO_COLLISION;
}
if(x < 0 || x > 128 || y < 0 || y > 128) {
sync.pixel(x, y, SKY_COLOR);
in_flight = false;
return BULLET_OFF_SCREEN;
}
int col = sync.read_pixel(x, y);
if(col != CONVERT_24_TO_16_BPP(SKY_COLOR)) {
sync.pixel(old_x, old_y, SKY_COLOR);
sync.filled_circle(x, y, 4, SKY_COLOR);
in_flight = false;
return col;
}
sync.pixel(old_x, old_y, SKY_COLOR);
sync.pixel(x, y, BLACK);
return BULLET_NO_COLLISION;
}