ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18zc2

Dependencies:   mbed

Revision:
5:7207c9b70108
Child:
6:b393cfe4e0a7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bullet/Bullet.cpp	Wed May 27 21:13:59 2020 +0000
@@ -0,0 +1,84 @@
+#include "Bullet.h"
+
+Bullet::Bullet()
+{
+
+}
+
+Bullet::~Bullet()
+{
+
+}
+
+void Bullet::init(int x,int size,int speed,int height)
+{
+    _size = size;
+
+    _x = x+(height)*0.5;
+    _y = HEIGHT - size;
+    
+    srand(time(NULL));
+    int direction = rand() % 4; // randomise initial direction. 
+
+    // 4 possibilities. Get random modulo and set velocities accordingly
+    if (direction == 0) {
+        _velocity.x = speed;
+        _velocity.y = speed;
+    } else if (direction == 1) {
+        _velocity.x = speed;
+        _velocity.y = -speed;
+    } else if (direction == 2) {
+        _velocity.x = speed;
+        _velocity.y = speed;
+    } else {
+        _velocity.x = -speed;
+        _velocity.y = -speed;
+    }
+}
+
+void Bullet::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
+    if((_y >= 0)&&(_y <= 24))
+    {
+ //        lcd.clearPixel(_x,_y-1);
+         
+    }
+    //lcd.drawRect(0,0,84,24,FILL_BLACK);
+   // lcd.clearPixel(_x,_y);
+}
+
+void Bullet::update(N5110 &lcd)
+{
+    _x += _velocity.x;
+    _y += _velocity.y;
+    if((_y >= 0)&&(_y <= 24))
+    {
+// lcd.clearPixel(_x,_y-1);
+         
+    }
+}
+
+void Bullet::set_velocity(Vector2D v)
+{
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+Vector2D Bullet::get_velocity()
+{
+    Vector2D v = {_velocity.x,_velocity.y};
+    return v;
+}
+
+Vector2D Bullet::get_pos()
+{
+    Vector2D p = {_x,_y};
+    return p;
+}
+
+void Bullet::set_pos(Vector2D p)
+{
+    _x = p.x;
+    _y = p.y;
+}
\ No newline at end of file