ELEC2645 (2018/19) / Mbed 2 deprecated el17ttds

Dependencies:   mbed N5110_tf

Revision:
3:3d35ab70b565
Parent:
2:ee9b361ba6df
Child:
4:3446009e2f38
--- a/Map/Map.cpp	Sun May 05 21:54:07 2019 +0000
+++ b/Map/Map.cpp	Mon May 06 19:50:27 2019 +0000
@@ -1,47 +1,59 @@
 #include "Map.h"
 
 Map::Map() {
-    
-}
 
-Map::~Map() {
-    
 }
 
-void Map::init(int width,int height) {
-    _w = width;
-    _h = height;
-    _x1 = -42 - (_w - 84) / 2;
-    _x2 = 84 + (_w - 84) / 2;
-    _y1 = -24 - (_h - 48) / 2;
-    _y2 = 48 + (_h - 48) / 2;
+void Map::init(int width, int height, int swidth, int sheight) {  // initialises map dimensions
+    _swidth = swidth;
+    _sheight = sheight;
+    _w = 0;
+    _h = 0;
+    _x1 = -(_swidth / 2) - (width - 84) / 2;
+    _x2 = _swidth + (width - 84) / 2; //84 + (_w - 84) / 2;
+    _y1 = -(_sheight) - (height - _sheight) / 2; //-24 - (_h - 48) / 2;
+    _y2 = _sheight + (height - _sheight) / 2; //48 + (_h - 48) / 2;
 }
 
-void Map::write(int pix_x, int pix_y) {
-    
-    _x1 = _x1 - pix_x;
-    _x2 = _x2 - pix_x;
-    _y2 = _y2 + pix_y;
-    _y2 = _y2 + pix_y;
-    
-    if (_x1 > 0) {
+void Map::write(int pix_x, int pix_y) {  // Builds new parameters of map
+
+    _x1 += pix_x;
+    _x2 += pix_x;
+    _y1 += pix_y;
+    _y2 += pix_y;
+
+    if (_x1 < -(_swidth / 2)) {
+        _x1_pos = -1;
+    } else if (_x1 < 0) {
+        _x1_pos = 0;
+        _w = (_swidth / 2) + _x1;
+    } else {
         _x1 = 0;
-    }
-    if (_x2 < 42) {
-        _x2 = 42;
+        _x1_pos = 0;
+        _w = _swidth / 2;
     }
-    if (_y1 > 0) {
+    if (_y1 < -(_sheight / 2)) {
+        _y1_pos = -1;
+    } else if (_y1 < 0) {
+        _y1_pos = 0;
+        _h = (_sheight / 2) + _y1;
+    } else {
         _y1 = 0;
+        _y1_pos = 0;
+        _h = _sheight / 2;
     }
-    if (_y2 < 24) {
-        _y1 = 24;
+    if (_y2 < (_sheight / 2)) {
+      _y2 = (_sheight / 2);
+    }
+    if (_x2 < (_swidth / 2)) {
+      _x2 = (_swidth / 2);
     }
 }
 
 void Map::draw(N5110 &lcd) {
-    
-    lcd.drawRect(_x1,_y1,_w,24,FILL_BLACK);
-    lcd.drawRect(_x1,_y1,48,_h,FILL_BLACK);
-    lcd.drawRect(_x1,_y2,_w,24,FILL_BLACK);
-    lcd.drawRect(_x2,_y1,48,_h,FILL_BLACK);
+
+    lcd.drawRect(_x1_pos,0,_w,_sheight,FILL_BLACK);
+    lcd.drawRect(0,_y1_pos,_swidth,_h,FILL_BLACK);
+    lcd.drawRect(0,_y2,_swidth,_sheight / 2,FILL_BLACK);
+    lcd.drawRect(_x2,0,_swidth / 2,_sheight,FILL_BLACK);
 }
\ No newline at end of file