ELEC2645 (2018/19) / Mbed 2 deprecated el17zl

Dependencies:   mbed

Fork of el17zl by Zhenwen Liao

Revision:
3:9fa31396d89d
Parent:
2:9f0d9516a6cd
Child:
4:750d3f9b54de
--- a/PushingEngine/PushingEngine.cpp	Sun Mar 24 10:45:30 2019 +0000
+++ b/PushingEngine/PushingEngine.cpp	Sun Mar 24 22:41:22 2019 +0000
@@ -30,7 +30,7 @@
     _c1y = cross1_y;
     _c2y = cross2_y;
     
-    // inital boxes cross and ppl
+    // inital boxes, crosses and ppl
     _b1.init(_b1x,_b1y);
     _b2.init(_b2x,_b2y);
     _ppl.init(_pplx,_pply);
@@ -40,148 +40,177 @@
 
 void PushingEngine::read_input(Gamepad &pad)
 {
-    _d = pad.get_direction(); // return direction of joystick
-    _mag = pad.get_mag(); 
+    _bstart = pad.check_event(Gamepad::START_PRESSED);
+    _bback = pad.check_event(Gamepad::BACK_PRESSED);
+    _ba = pad.check_event(Gamepad::A_PRESSED);
+    _bb = pad.check_event(Gamepad::B_PRESSED);
+    _bx = pad.check_event(Gamepad::X_PRESSED);
+    _by = pad.check_event(Gamepad::Y_PRESSED);
+    _bjoy = pad.check_event(Gamepad::JOY_PRESSED);
+    _djoy = pad.get_direction();
 }
 
 void PushingEngine::draw(N5110 &lcd)
 {
     // draw the elements in the LCD buffer
     // pitch
-    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
-    // scores
-    print_scores(lcd);
+    lcd.drawLine(0,4,83,4,1);
+    lcd.drawLine(0,5,0,47,1);
+    lcd.drawLine(1,47,83,47,1);
+    lcd.drawLine(83,5,83,46,1);
     // boxes
     _b1.draw(lcd);
     _b2.draw(lcd);
     // ppl
     _ppl.draw(lcd);
-    //cross
+    // cross
     _c1.draw(lcd);
     _c2.draw(lcd);
 }
 
-void PushingEngine::update(Gamepad &pad)
+void PushingEngine::update(Gamepad &pad) //
 {
     check_goal(pad);
-    // important to update boxes and crosses before checking collisions so can
+    // important to update paddles and ball before checking collisions so can
     // correct for it before updating the display
-    _b1.update(_d,_mag);
-    _b2.update(_d,_mag);
+    _b1.update();
+    _b2.update();
     _ppl.update();
-    _c1.update();
-    _c2.update();
 
-    //check_wall_collision(pad);
-    //check_paddle_collisions(pad);
+    check_cross_box_matching(pad);
+    check_cross_box_touching(pad);
 }
 
-void PushingEngine::check_wall_collision(Gamepad &pad)
+void PushingEngine::check_cross_box_matching(Gamepad &pad) 
 {
-    // read current ball attributes
-    Vector2D box1_pos = _b1.get_pos();
-    Vector2D box2_pos = _b2.get_pos();
-
-    // check if topleft touch wall
-    if (box1_pos.y <= 1 ||box1_pos.x <= 1) {  //  1 due to 1 pixel boundary
-        box1_pos.y = 1;  // keep the position at (1,1)
-        // audio feedback
-        pad.tone(750.0,0.1);
-    }
-    // check if hit bottom wall
-    else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47
-        // hit bottom
-        ball_pos.y = (HEIGHT-1) - _ball_size;  // stops ball going off screen
-        ball_velocity.y = -ball_velocity.y;
-        // audio feedback
-        pad.tone(750.0,0.1);
-    }
-
-    // update ball parameters
-    _ball.set_velocity(ball_velocity);
-    _ball.set_pos(ball_pos);
+    vvvv
 }
 
-void PushingEngine::check_paddle_collisions(Gamepad &pad)
+int  PushingEngine::check_ppl_box1_touching(Gamepad &pad) 
 {
-    // read current ball attributes
-    Vector2D ball_pos = _ball.get_pos();
-    Vector2D ball_velocity = _ball.get_velocity();
-
-    // check p1 first
-    Vector2D p1_pos = _p1.get_pos();
+    Vector2D b1_pos = _b1.get_pos();
+    Vector2D ppl_pos = _ppl.get_pos();
 
-    // see if ball has hit the paddle by checking for overlaps
-    if (
-        (ball_pos.y >= p1_pos.y) && //top
-        (ball_pos.y <= p1_pos.y + _paddle_height) && //bottom
-        (ball_pos.x >= _p1x) && //left
-        (ball_pos.x <= _p1x + _paddle_width)  //right
-    ) {
-        // if it has, fix position and reflect x velocity
-        ball_pos.x = _p1x + _paddle_width;
-        ball_velocity.x = -ball_velocity.x;
-        // audio feedback
-        pad.tone(1000.0,0.1);
+    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)
+        {
+            _s = 2; // can push to right
+        }
     }
 
-    // check p2 next
-    Vector2D p2_pos = _p2.get_pos();
-
-    // see if ball has hit the paddle by checking for overlaps
-    if (
-        (ball_pos.y >= p2_pos.y) && //top
-        (ball_pos.y <= p2_pos.y + _paddle_height) && //bottom
-        (ball_pos.x + _ball_size >= _p2x) && //left
-        (ball_pos.x + _ball_size <= _p2x + _paddle_width)  //right
-    ) {
-        // if it has, fix position and reflect x velocity
-        ball_pos.x = _p2x - _ball_size;
-        ball_velocity.x = -ball_velocity.x;
-        // audio feedback
-        pad.tone(1000.0,0.1);
-    }
-
-    // write new attributes
-    _ball.set_velocity(ball_velocity);
-    _ball.set_pos(ball_pos);
-}
-
-void PongEngine::check_goal(Gamepad &pad)
-{
-    Vector2D ball_pos = _ball.get_pos();
-    // P2 has scored
-    if (ball_pos.x + _ball_size < 0) {
-        _p2.add_score();
-        _ball.init(_ball_size,_speed);
-        pad.tone(1500.0,0.5);
-        pad.leds_on();
-        wait(0.5);
-        pad.leds_off();
-    }
-
-    // P1 has scored
-    if (ball_pos.x > WIDTH) {
-        _p1.add_score();
-        _ball.init(_ball_size,_speed);
-        pad.tone(1500.0,0.5);
-        pad.leds_on();
-        wait(0.5);
-        pad.leds_off();
+    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)
+        {
+            _s = 3; // can push down
+        }
     }
 }
 
-void PongEngine::print_scores(N5110 &lcd)
+int  PushingEngine::check_ppl_box2_touching(Gamepad &pad) //
 {
-    // get scores from paddles
-    int p1_score = _p1.get_score();
-    int p2_score = _p2.get_score();
+    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)
+        {
+            _r = 1; // can push to left
+        } else if((b2_pos.x-ppl_pos.x)=8)
+        {
+            _r = 2; // can push to right
+        }
+    }
+
+    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)
+        {
+            _r = 3; // can push down
+        }
+    }
+}
+
+void PushingEngine::box1_moving(Gamepad &pad) 
+{
+    Vector2D b1_pos = _b1.get_pos();
+    Vector2D ppl_pos = _ppl.get_pos();
+
+    read_input(pad);
+    check_ppl_box1_touching(pad);
+
+    if(_bx == 1 && _s == 1 )
+    {
+        b1_pos.x=b1_pos.x-8;
+        ppl_pos.x=ppl_pos.x-8;
+    }
+
+    if(_bb == 1 && _s == 2 )
+    {
+        b1_pos.x=b1_pos.x+8;
+        ppl_pos.x=ppl_pos.x+8;
+    }
 
-    // print to LCD i
-    char buffer1[14];
-    sprintf(buffer1,"%2d",p1_score);
-    lcd.printString(buffer1,WIDTH/2 - 20,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
-    char buffer2[14];
-    sprintf(buffer2,"%2d",p2_score);
-    lcd.printString(buffer2,WIDTH/2 + 4,1);
-}
\ No newline at end of file
+    if(_ba == 1 && _s == 3 )
+    {
+        b1_pos.y=b1_pos.y+8;
+        ppl_pos.y=ppl_pos.y+8;
+    }
+
+    if(_by == 1 && _s == 4 )
+    {
+        b1_pos.y=b1_pos.y-8;
+        ppl_pos.y=ppl_pos.y-8;
+    }
+
+    _b1.set_pos(b1_pos);
+    _ppl.set_pos(ppl_pos);
+}
+
+void PushingEngine::box2_moving(Gamepad &pad) 
+{
+    Vector2D b2_pos = _b2.get_pos();
+    Vector2D ppl_pos = _ppl.get_pos();
+
+    read_input(pad);
+    check_ppl_box1_touching(pad);
+
+    if(_bx == 1 && _s == 1 )
+    {
+        b2_pos.x=b2_pos.x-8;
+        ppl_pos.x=ppl_pos.x-8;
+    }
+
+    if(_bb == 1 && _s == 2 )
+    {
+        b2_pos.x=b2_pos.x+8;
+        ppl_pos.x=ppl_pos.x+8;
+    }
+
+    if(_ba == 1 && _s == 3 )
+    {
+        b2_pos.y=b2_pos.y+8;
+        ppl_pos.y=ppl_pos.y+8;
+    }
+
+    if(_by == 1 && _s == 4 )
+    {
+        b2_pos.y=b2_pos.y-8;
+        ppl_pos.y=ppl_pos.y-8;
+    }
+
+    _b2.set_pos(b2_pos);
+    _ppl.set_pos(ppl_pos);
+}
+