ELEC2645 (2018/19) / Mbed 2 deprecated fy14lkaa

Dependencies:   mbed

Revision:
28:4786e81ce3e3
Child:
91:f9e2ff484014
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Alien/Alien.cpp	Sat Apr 20 00:48:05 2019 +0000
@@ -0,0 +1,70 @@
+#include "Alien.h"
+
+
+Alien::Alien()
+{
+
+}
+
+Alien::~Alien()
+{
+
+}
+
+void Alien::init(int size,int speed)
+{
+    
+    _size = size;
+    
+    
+   
+    
+    
+    srand(time(NULL));
+    int direction = rand() % 8; // 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 Alien::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
+}
+ 
+    void Alien::update()
+{
+    _x += _velocity.x;
+    _y += _velocity.y;
+    }
+    
+    void Alien::set_velocity(Vector2D v)
+{
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+Vector2D Alien::get_velocity()
+{
+    Vector2D v = {_velocity.x,_velocity.y};
+    return v;
+}
+
+Vector2D Alien::get_pos()
+{
+    Vector2D p = {_x,_y};
+    return p;
+}