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 DRV2605 EthernetInterface Game_Synchronizer MMA8452 SDFileSystem SparkfunAnalogJoystick mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Diff: Bullet/bullet.cpp
- Revision:
- 17:7bc7127782e4
- Parent:
- 15:4b27a3a95772
- Child:
- 18:18dfc9fb33b5
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Bullet/bullet.cpp Wed Oct 28 04:30:22 2015 +0000
@@ -0,0 +1,56 @@
+#include "uLCD_4DGL.h"
+#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;
+}
