ELEC2645 (2018/19) / Mbed 2 deprecated el17zl

Dependencies:   mbed

Fork of el17zl by Zhenwen Liao

Revision:
6:6b083e22cb53
Parent:
5:b50ce6160013
Child:
7:6f8aeadc4370
--- a/PushingEngine/PushingEngine.cpp	Tue Apr 09 12:46:02 2019 +0000
+++ b/PushingEngine/PushingEngine.cpp	Thu Apr 11 13:42:34 2019 +0000
@@ -1,5 +1,19 @@
 #include "PushingEngine.h"
 
+
+const int barrier[8][8]= {
+    {1,1,1,1,1,1,1,1},
+    {1,1,0,0,0,0,1,1},
+    {1,0,0,0,0,0,0,1},
+    {1,0,0,0,0,0,0,1},
+    {1,0,0,0,0,0,0,1},
+    {1,0,0,0,0,0,0,1},
+    {1,1,0,0,0,0,1,1},
+    {1,1,1,1,1,1,1,1},
+};
+
+
+
 PushingEngine::PushingEngine()
 {
 
@@ -11,104 +25,106 @@
 }
 
 void PushingEngine::init(int box1_x,int box1_y,int box2_x,int box2_y,
-                         int ppl_x,int ppl_y)
+                         int ppl_x,int ppl_y )
 {
-    // boxes position on screen 
+    // boxes position on screen
     _b1x = box1_x;
     _b2x = box2_x;
     _b1y = box1_y;
     _b2y = box2_y;
-    
+
     // ppl position on screen
     _pplx = ppl_x;
     _pply = ppl_y;
-    
+
     // inital boxes, crosses and ppl
     _b1.init(_b1x,_b1y);
     _b2.init(_b2x,_b2y);
     _ppl.init(_pplx,_pply);
+    
+    
 
 }
 
 void PushingEngine::read_input(Gamepad &pad)
 {
     //if (pad.check_event(Gamepad::START_PRESSED))
-       //{_bstart = 1;}
-       //else {_bstart = 0;}
+    //{_bstart = 1;}
+    //else {_bstart = 0;}
     //if (pad.check_event(Gamepad::BACK_PRESSED))
-       //{_bback = 1; }
-       //else {_bback = 0;}
-    if (pad.check_event(Gamepad::A_PRESSED))
-       {_ba = 1; }
-       else {_ba = 0;}
-    if (pad.check_event(Gamepad::B_PRESSED))
-       {_bb = 1; }
-       else {_bb = 0;}
-    if (pad.check_event(Gamepad::X_PRESSED))
-       {_bx = 1; }
-       else {_bx = 0;}
-    if (pad.check_event(Gamepad::Y_PRESSED))
-       {_by = 1; }
-       else {_by = 0;}
+    //{_bback = 1; }
+    //else {_bback = 0;}
+    if (pad.check_event(Gamepad::A_PRESSED)) {
+        _ba = 1;
+    } else {
+        _ba = 0;
+    }
+    if (pad.check_event(Gamepad::B_PRESSED)) {
+        _bb = 1;
+    } else {
+        _bb = 0;
+    }
+    if (pad.check_event(Gamepad::X_PRESSED)) {
+        _bx = 1;
+    } else {
+        _bx = 0;
+    }
+    if (pad.check_event(Gamepad::Y_PRESSED)) {
+        _by = 1;
+    } else {
+        _by = 0;
+    }
     //if (pad.check_event(Gamepad::JOY_PRESSED))
-       //{_bjoy = 1; }
-       //else {_bjoy = 0;}
+    //{_bjoy = 1; }
+    //else {_bjoy = 0;}
 }
 
-void  PushingEngine::check_ppl_box1_touching(Gamepad &pad) 
+void PushingEngine::check_ppl_box1_touching(Gamepad &pad)
 {
     Vector2D b1_pos = _b1.get_pos();
     Vector2D ppl_pos = _ppl.get_pos();
-    
-    if (b1_pos.y == ppl_pos.y) 
-    {
-        if((ppl_pos.x-b1_pos.x) ==8)
-        {
+
+    if (b1_pos.y == ppl_pos.y) {
+        if((ppl_pos.x-b1_pos.x) ==8) {
             _s = 1; // can push to left
-        } else if((b1_pos.x-ppl_pos.x)==8)
-        {
+        } else if((b1_pos.x-ppl_pos.x)==8) {
             _s = 2; // can push to right
-        } 
-    } else if (b1_pos.x == ppl_pos.x) 
-    {
-        if((ppl_pos.y-b1_pos.y) ==8)
-        {
+        }
+    } else if (b1_pos.x == ppl_pos.x) {
+        if((ppl_pos.y-b1_pos.y) ==8) {
             _s = 4; // can push up
-        } else if((b1_pos.y-ppl_pos.y)==8)
-        {
+        } else if((b1_pos.y-ppl_pos.y)==8) {
             _s = 3; // can push down
-        } 
-    } else {_s = 0;}
+        }
+    } else {
+        _s = 0;
+    }
 }
 
-void  PushingEngine::check_ppl_box2_touching(Gamepad &pad) //
+void PushingEngine::check_ppl_box2_touching(Gamepad &pad) //
 {
     Vector2D b2_pos = _b2.get_pos();
     Vector2D ppl_pos = _ppl.get_pos();
-    
-    if (b2_pos.y == ppl_pos.y) 
-    {
-        if((ppl_pos.x-b2_pos.x) ==8)
-        {
+
+    if (b2_pos.y == ppl_pos.y) {
+        if((ppl_pos.x-b2_pos.x) ==8) {
             _r = 1; // can push to left
-        } else if((b2_pos.x-ppl_pos.x)==8)
-        {
+        } else if((b2_pos.x-ppl_pos.x)==8) {
             _r = 2; // can push to right
-        } 
-    } else if (b2_pos.x == ppl_pos.x) 
-    {
-        if((ppl_pos.y-b2_pos.y) ==8)
-        {
+        }
+    } else if (b2_pos.x == ppl_pos.x) {
+        if((ppl_pos.y-b2_pos.y) ==8) {
             _r = 4; // can push up
-        } else if((b2_pos.y-ppl_pos.y)==8)
-        {
+        } else if((b2_pos.y-ppl_pos.y)==8) {
             _r = 3; // can push down
-        } 
-    } else {_r = 0;}
+        }
+    } else {
+        _r = 0;
+    }
 }
 
 
-void PushingEngine::draw(N5110 &lcd)
+void PushingEngine::draw(N5110 &lcd,int barrier_x,int barrier_y)
 {
     // draw the elements in the LCD buffer
     // pitch
@@ -116,7 +132,10 @@
     lcd.drawLine(0,3,0,45,1);
     lcd.drawLine(1,45,83,45,1);
     lcd.drawLine(83,3,83,44,1);
-    //lcd.drawSprite(34,28,8,8,(int *)barrier);
+    
+    //draw the barrier 
+    lcd.drawSprite(barrier_x,barrier_y,8,8,(int *)barrier);
+    
     // boxes
     _b1.draw(lcd);
     _b2.draw(lcd);
@@ -124,14 +143,89 @@
     _ppl.draw(lcd);
 }
 
-void PushingEngine::update(Gamepad &pad) //
+void PushingEngine::update(Gamepad &pad,int barrier_x,int barrier_y) //
 {
-    //check_cross_box_matching(pad);
     check_ppl_box1_touching(pad);
     check_ppl_box2_touching(pad);
 
-    _b1.update(_s,_bb,_ba,_bx,_by);
-    _b2.update(_r,_bb,_ba,_bx,_by);
-    _ppl.update(_bb,_ba,_bx,_by);
+    hold_ppl_box1_wall(pad);
+    hold_ppl_box2_wall(pad);
+    
+    _ppl.update(_bb,_ba,_bx,_by,_temp,barrier_x,barrier_y);
+    _b1.update(_s,_bb,_ba,_bx,_by,barrier_x,barrier_y);
+    _b2.update(_r,_bb,_ba,_bx,_by,barrier_x,barrier_y);
+    _temp = 0;
+    
+    ppl_box_cover(pad);
 
 }
+
+void PushingEngine::hold_ppl_box1_wall(Gamepad &pad) //
+{
+    Vector2D b1_pos = _b1.get_pos();
+    Vector2D ppl_pos = _ppl.get_pos();
+
+    if (b1_pos.x == 2) {
+        if(_s == 1) {
+            _temp=1;
+        }
+    }
+    if (b1_pos.x == 74) {
+        if(_s == 2) {
+            _temp=2;
+        }
+    }
+    if (b1_pos.y == 4) {
+        if(_s == 4) {
+            _temp=3;
+        }
+    }
+    if (b1_pos.y == 36) {
+        if(_s == 3) {
+            _temp=4;
+        }
+    }
+}
+
+void PushingEngine::hold_ppl_box2_wall(Gamepad &pad) //
+{
+    Vector2D b2_pos = _b2.get_pos();
+    Vector2D ppl_pos = _ppl.get_pos();
+
+    if (b2_pos.x == 2) {
+        if(_r == 1) {
+            _temp=1;
+        }
+    }
+    if (b2_pos.x == 74) {
+        if(_r == 2) {
+            _temp=2;
+        }
+    }
+    if (b2_pos.y == 4) {
+        if(_r == 4) {
+            _temp=3;
+        }
+    }
+    if (b2_pos.y == 36) {
+        if(_r == 3) {
+            _temp=4;
+        }
+    }
+}
+
+void PushingEngine::ppl_box_cover(Gamepad &pad)
+{
+    Vector2D b1_pos = _b1.get_pos();
+    Vector2D b2_pos = _b2.get_pos();
+    Vector2D ppl_pos = _ppl.get_pos();
+    
+    if (b2_pos.x == ppl_pos.x && b2_pos.y == ppl_pos.y){
+        int _t = 2;
+        }
+    if (b1_pos.x == ppl_pos.x && b1_pos.y == ppl_pos.y){
+        int _t = 1;
+        }
+    else {int _t = 0;}
+    
+}
\ No newline at end of file