ELEC2645 (2018/19) / Mbed 2 deprecated el17zl

Dependencies:   mbed

Fork of el17zl by Zhenwen Liao

PushingEngine/PushingEngine.cpp

Committer:
franklzw
Date:
2019-04-09
Revision:
5:b50ce6160013
Parent:
4:750d3f9b54de
Child:
6:6b083e22cb53

File content as of revision 5:b50ce6160013:

#include "PushingEngine.h"

PushingEngine::PushingEngine()
{

}

PushingEngine::~PushingEngine()
{

}

void PushingEngine::init(int box1_x,int box1_y,int box2_x,int box2_y,
                         int ppl_x,int ppl_y)
{
    // 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;}
    //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;}
    //if (pad.check_event(Gamepad::JOY_PRESSED))
       //{_bjoy = 1; }
       //else {_bjoy = 0;}
}

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)
        {
            _s = 1; // can push to left
        } 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)
        {
            _s = 4; // can push up
        } else if((b1_pos.y-ppl_pos.y)==8)
        {
            _s = 3; // can push down
        } 
    } else {_s = 0;}
}

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)
        {
            _r = 1; // can push to left
        } 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)
        {
            _r = 4; // can push up
        } else if((b2_pos.y-ppl_pos.y)==8)
        {
            _r = 3; // can push down
        } 
    } else {_r = 0;}
}


void PushingEngine::draw(N5110 &lcd)
{
    // draw the elements in the LCD buffer
    // pitch
    lcd.drawLine(0,2,83,2,1);
    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);
    // boxes
    _b1.draw(lcd);
    _b2.draw(lcd);
    // ppl
    _ppl.draw(lcd);
}

void PushingEngine::update(Gamepad &pad) //
{
    //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);

}