ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Revision:
21:f3b0ce18b44f
Parent:
20:01efa2bce75d
Child:
26:716bcd47f3ca
--- a/CaMove/CaMove.cpp	Wed May 08 15:48:44 2019 +0000
+++ b/CaMove/CaMove.cpp	Thu May 09 06:22:53 2019 +0000
@@ -1,6 +1,8 @@
 #include "CaMove.h"
 #include "AniPaths.h"
 
+Serial f(USBTX,USBRX);
+
 //Constructor
 CaMove::CaMove(PinName Button, 
                PinName Pot_h, 
@@ -21,6 +23,8 @@
     delete _ver;
 };
 
+//Initilizing Functions
+//Player
 void CaMove::init(int x,int y,Direction D) {
     _h = x;
     _v = y;
@@ -30,35 +34,69 @@
     _treg = -1;
 };
 
+//AI Chasers
+void CaMove::AIinit(int chaser) {
+    enabled = false;
+    _ch = 0;
+    _cv = 0;
+    switch (chaser) {
+        case 2:
+        increment = 2;
+        break;
+        case 1:
+        increment = 0;
+        break;
+        case 4:
+        increment = 5;
+        break;
+        default:
+        increment = 3;
+        break;
+    }
+};
+
+//Player Functions
+
 void CaMove::move(Bitmap &lcd) {
     //If statements to check joystick movement and has in bluilt collsion detection
     //collsion detection check 4 sides for black pixels
-    bool c1 = (lcd.getPixel((_h-1),(_v+12)) == 0 &&  lcd.getPixel((_h-1),(_v+1)) == 0) ; 
-    bool c2 = (lcd.getPixel((_h+10),(_v+12)) == 0 && lcd.getPixel((_h+10),(_v+1)) == 0);
-    bool c3 = (lcd.getPixel((_h+1),(_v-1)) == 0 && lcd.getPixel((_h+8),(_v-1)) == 0); 
-    bool c4 = (lcd.getPixel((_h+1),(_v+14)) == 0 && lcd.getPixel((_h+8),(_v+14)) == 0);
-    this->check_collision(c1,c2,c3,c4);
+    bool left = (lcd.getPixel((_h-1),(_v+12)) == 0 &&  lcd.getPixel((_h-1),(_v+1)) == 0) ; 
+    bool right = (lcd.getPixel((_h+10),(_v+12)) == 0 && lcd.getPixel((_h+10),(_v+1)) == 0);
+    bool down = (lcd.getPixel((_h+1),(_v-1)) == 0 && lcd.getPixel((_h+8),(_v-1)) == 0); 
+    bool up = (lcd.getPixel((_h+1),(_v+14)) == 0 && lcd.getPixel((_h+8),(_v+14)) == 0);
+    this->check_collision(left,right,down,up);
     if (_itr == 3) {
         _itr = -1;
     }
     _itr++;
     this->rend(lcd);
 }
-    
-void CaMove::check_collision(bool c1,bool c2,bool c3,bool c4) {
-    if (_hoz->read() > 0.6f && c1) {
+
+bool CaMove::in_screen() {
+    if (_h + 5 >= 84 || (_h + 5) <= 0 || _v + 7 >= 48 || (_v + 8) <= 0) {
+        return false;
+    } else {
+        return true;
+    }
+};
+
+//Aiding Player Functions
+
+void CaMove::check_collision(bool left,bool right,bool down,bool up) {
+    //Checks Input on Joystick and Verifies if charater can move in that direction
+    if (_hoz->read() > 0.6f && left) {
         _h = _h - 2;
         _fc = Lt;
     }
-    if (_hoz->read() < 0.4f && c2) {
+    if (_hoz->read() < 0.4f && right) {
         _h = _h + 2;
         _fc = Rt;
     }
-    if (_ver->read() > 0.6f && c3) {
+    if (_ver->read() > 0.6f && down) {
         _v = _v - 2;
         _fc = Fd;
     }
-    if (_ver->read() < 0.4f && c4) {
+    if (_ver->read() < 0.4f && up) {
         _v = _v + 2;
         _fc = Bd;
     }   
@@ -68,34 +106,23 @@
     //Switch case allows charater to move diagonally and retains its facing direction when stationary
     switch (_fc) {
             case Lt:
-            lcd.renderBMP(L[_itr],_h,_v);
-            break;
-            
+            lcd.renderBMP(L[_itr],_h,_v); //render Charater on Screen
+            break;       
             case Rt:
             lcd.renderBMP(R[_itr],_h,_v);
             break;
-            
             case Fd:
             lcd.renderBMP(F[_itr],_h,_v);
             break;
-            
             case Bd:
             lcd.renderBMP(B[_itr],_h,_v);
             break;
         }
 };
 
-
-bool CaMove::in_screen() {
-    if (_h + 5 >= 84 || (_h + 5) <= 0 || _v + 7 >= 48 || (_v + 8) <= 0) {
-        return false;
-    } else {
-        return true;
-    }
-};
-
-//Sets values into vector
-void CaMove::set_region(int xmin, int ymin, int xl, int yl) {
+//Interactive Region Function pack
+//Setting regions
+void CaMove::set_region(int xmin, int ymin, int xl, int yl) { //Sets values into vector
     _vreg.push_back((xmin + xl));
     _vreg.push_back(xmin);
     _vreg.push_back((ymin + yl));
@@ -106,27 +133,40 @@
     _vreg.clear();
 };
 
+//Interupting Function
 void CaMove::intercheck() {
-    _trg = false;
+    _trg = false;         //reset everytime called
     LowerH = false;
     UpperH = false;
     MidH = false;
     LowerV = false;
     UpperV = false;
     MidV = false;
-    if (_vreg.size() > 0) {
+    if (_vreg.size() > 0) {       //checks all regions for vector
         for(int Vvalue = 0; Vvalue < _vreg.size(); Vvalue = Vvalue +4) {
-            this->set_statements(Vvalue);
-            this->check_sides(Vvalue);
+            this->set_statements(Vvalue);  //returns checked regions if in an Region
+            this->check_sides(Vvalue);     //focuses on regions in facing direction
         }
     }
 };
 
+
+//Internal Functions
+void CaMove::set_statements(int Vvalue) {
+    //If Statements check that sides position with all x- y region ranges to see if charater has hit that region.
+    LowerH = (_h - 1) <= _vreg[Vvalue] && (_h-1) >= _vreg[Vvalue + 1];
+    UpperH = (_h + 10) <= _vreg[Vvalue] && (_h + 10) >= _vreg[Vvalue + 1];
+    MidH = (_h + 5) <= _vreg[Vvalue] && (_h + 5) >= _vreg[Vvalue + 1];
+    LowerV = (_v - 1) >= _vreg[Vvalue + 3] && (_v - 1) <= _vreg[Vvalue + 2];
+    UpperV = (_v+14) >= _vreg[Vvalue + 3] && (_v + 14) <= _vreg[Vvalue + 2];
+    MidV = (_v+7) >= _vreg[Vvalue + 3] && (_v + 7) <= _vreg[Vvalue + 2];
+}
+
 void CaMove::check_sides(int Vvalue) {
     switch (_fc) {        //Function only will check the facing side for interative regions
             case Lt:           //L
                 if (LowerH && MidV) {
-                    this->set_treg(Vvalue);
+                    this->set_treg(Vvalue); //returns region number
                 };
             break;
             case Rt:          //R
@@ -147,6 +187,8 @@
         }
 }
 
+//Aiding Functions
+
 void CaMove::set_treg(int Vvalue) {
     //The region number is found depending on its position in the vector
     //e.g elements 0 to 3 are in region 0 and elements 4 to 7 are in region 1
@@ -154,18 +196,10 @@
     _treg = Vvalue / 4;
 }
 
-void CaMove::set_statements(int Vvalue) {
-    //If Statements check that sides position with all x- y region ranges to see if charater has hit that region.
-    LowerH = (_h - 1) <= _vreg[Vvalue] && (_h-1) >= _vreg[Vvalue + 1];
-    UpperH = (_h + 10) <= _vreg[Vvalue] && (_h + 10) >= _vreg[Vvalue + 1];
-    MidH = (_h + 5) <= _vreg[Vvalue] && (_h + 5) >= _vreg[Vvalue + 1];
-    LowerV = (_v - 1) >= _vreg[Vvalue + 3] && (_v - 1) <= _vreg[Vvalue + 2];
-    UpperV = (_v+14) >= _vreg[Vvalue + 3] && (_v + 14) <= _vreg[Vvalue + 2];
-    MidV = (_v+7) >= _vreg[Vvalue + 3] && (_v + 7) <= _vreg[Vvalue + 2];
-}
+//Accessors
 
 //Functions revert to default values evrytime they are called. 
-bool CaMove::is_trg() {
+bool CaMove::is_trg() {  
     bool temp = _trg;
     _trg = false;
     return temp;
@@ -178,33 +212,25 @@
     return temp;
 }
 
-//AI Components
-void CaMove::AIinit(int chaser) {
-    enabled = false;
-    _ch = 0;
-    _cv = 0;
-    switch (chaser) {
-        case 2:
-        increment = 2;
-        break;
-        case 1:
-        increment = 0;
-        break;
-        case 4:
-        increment = 5;
-        break;
-        default:
-        increment = 3;
-        break;
-    }
-};
+
 
-void CaMove::spawn(int x, int y) {
+//Chaser Functions
+
+void CaMove::spawn(int x, int y) { //Chaser Appears
     enabled = true;
     _ch = x;
     _cv = y;
 }
 
+
+bool CaMove::is_caught() {
+    if (abs(_ch - _h) < 10 && abs(_cv - _v) < 14 && enabled) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
 void CaMove::chase(Bitmap &lcd, int girl) {
     if (enabled) {
         if (_cv < _v) {
@@ -221,6 +247,8 @@
     }
 }
 
+//Internal Functions
+
 void CaMove::render_chaser(Bitmap &lcd,int chaser) {
     switch (chaser) {
         case 2:
@@ -233,13 +261,4 @@
         lcd.renderBMP(AIM[_itr],_ch,_cv);
         break;
     }
-}
-
-bool CaMove::is_caught() {
-    if (abs(_ch - _h) < 10 && abs(_cv - _v) < 14 && enabled) {
-        return true;
-    } else {
-        return false;
-    }
-}
-
+}
\ No newline at end of file