ELEC2645 (2018/19) / Mbed 2 deprecated el17zl

Dependencies:   mbed

Fork of el17zl by Zhenwen Liao

PushingEngine/PushingEngine.cpp

Committer:
franklzw
Date:
2019-04-11
Revision:
6:6b083e22cb53
Parent:
5:b50ce6160013
Child:
7:6f8aeadc4370

File content as of revision 6:6b083e22cb53:

#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()
{

}

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,int barrier_x,int barrier_y)
{
    // 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);
    
    //draw the barrier 
    lcd.drawSprite(barrier_x,barrier_y,8,8,(int *)barrier);
    
    // boxes
    _b1.draw(lcd);
    _b2.draw(lcd);
    // ppl
    _ppl.draw(lcd);
}

void PushingEngine::update(Gamepad &pad,int barrier_x,int barrier_y) //
{
    check_ppl_box1_touching(pad);
    check_ppl_box2_touching(pad);

    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;}
    
}