Musallam Bseiso / Friendly
Revision:
4:879c4cb8a7e4
Child:
5:0cd7f779418a
diff -r c054c5e52370 -r 879c4cb8a7e4 Friendly.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Friendly.cpp	Wed Mar 15 14:34:43 2017 +0000
@@ -0,0 +1,68 @@
+#include "Friendly.h"
+
+//  Class for friendly ship (the one you control)
+
+Friendly::Friendly()
+{
+}
+
+Friendly::~Friendly()
+{
+}
+
+void Friendly::init(int x,int height,int width)     // initialization
+{
+    _x = 2;     // starting x position (fixed)
+    _y = HEIGHT/2 - height/2;  // starting y position (centre)
+}
+
+void Friendly::draw(N5110 &lcd)     // draws friendly ship in screen buffer
+{
+    lcd.drawLine(_x,_y,_x,_y+5,1);
+    lcd.setPixel(_x+1,_y);
+    lcd.setPixel(_x+1,_y+5);
+    lcd.setPixel(_x+2,_y+1);
+    lcd.setPixel(_x+2,_y+4);
+    lcd.setPixel(_x+3,_y+1);
+    lcd.setPixel(_x+3,_y+4);
+    lcd.setPixel(_x+4,_y+2);
+    lcd.setPixel(_x+4,_y+3);
+}
+
+void Friendly::update(Direction d,float mag)
+{
+    _speed = int(mag*10.0f); 
+
+    // four-directional movement
+    if (d == N) {
+        _y-=_speed;
+    } else if (d == S) {
+        _y+=_speed;
+    } else if (d == W) {
+        _x-=_speed;
+    } else if (d == E) {
+        _x+=_speed;
+    }
+
+    // position check so the ship doesn't go out of bounds
+    if (_y < 1) {
+        _y = 1;
+    }
+    
+    if (_y > 41) {
+        _y = 41;
+    }
+    
+    if (_x < 1) {
+        _x = 1;
+    }
+    
+    if (_x > WIDTH/1.5 - _width - 1) {
+        _x = WIDTH/1.5 - _width - 1;
+    }
+}
+
+Vector2D Friendly::get_pos() {
+    Vector2D p = {_x,_y};
+    return p;    
+}
\ No newline at end of file