ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

CaMove/CaMove.cpp

Committer:
rottenegg
Date:
2019-04-23
Revision:
8:e3a76a808a4c
Parent:
5:860087ff295e
Child:
9:ac396c818874

File content as of revision 8:e3a76a808a4c:

#include "CaMove.h"
#include "AniPaths.h"
//Constructor
CaMove::CaMove(PinName Button, 
               PinName Pot_h, 
               PinName Pot_v)
        : //Creating Class Objects
        _check(new InterruptIn(Button)), 
        _hoz(new AnalogIn(Pot_h)),
        _ver(new AnalogIn(Pot_v))
    {
    _check->mode(PullDown);
    _check->rise(callback(this,&CaMove::intercheck));
};
//Destructor
CaMove::~CaMove() {
    delete _check;
    delete _hoz;
    delete _ver;
};

void CaMove::init(int x,int y,Direction D) {
    _h = x;
    _v = y;
    _itr = 0;
    _fc = D;
    _trg = false;
    _treg = -1;
};

void CaMove::move(N5110 &lcd) {
    //If statements to check joystick movement and has in bluilt collsion detection
    //collsion detection check 4 sides for black pixels
    bool pi = (lcd.getPixel((_h-1),(_v+7)) == 0) && (lcd.getPixel((_h+10),(_v+7)) == 0) &&
              (lcd.getPixel((_h+5),(_v-1)) == 0) && (lcd.getPixel((_h+5),(_v+14)) == 0);
    if (_hoz->read() > 0.6f && pi) {
        _h = _h - 2;
        _fc = Lt;
    }
    if (_hoz->read() < 0.4f && pi) {
        _h = _h + 2;
        _fc = Rt;
    }
    if (_ver->read() > 0.6f && pi) {
        _v = _v - 2;
        _fc = Fd;
    }
    if (_ver->read() < 0.4f && pi) {
        _v = _v + 2;
        _fc = Bd;
    }
    if (_itr == 3) {
        _itr = -1;
    }
    _itr++;
    //Switch case allows charater to move diagonally and retains its facing direction when stationary
    switch (_fc) {
            case Lt:
            Bitmap::renderBMP(L[_itr],lcd,_h,_v);
            break;
            
            case Rt:
            Bitmap::renderBMP(R[_itr],lcd,_h,_v);
            break;
            
            case Fd:
            Bitmap::renderBMP(F[_itr],lcd,_h,_v);
            break;
            
            case Bd:
            Bitmap::renderBMP(B[_itr],lcd,_h,_v);
            break;
        }
};


bool CaMove::in_screen() {
    if (_h >= 84 || (_h + 9) <= 0 || _v <= 0 || (_v + 13) >= 48) {
        return false;
    } else {
        return true;
    }
};
//Sets values into vector
void CaMove::set_region(int xmin, int ymin, int xl, int yl) {
    _vreg.push_back((xmin + xl));
    _vreg.push_back(xmin);
    _vreg.push_back((ymin + yl));
    _vreg.push_back(ymin);
};

void CaMove::delete_regions() {
    _vreg.clear();
};

void CaMove::intercheck() {
    _trg = false;
    if (_vreg.size() > 0) {
        for(int Vvalue = 0; Vvalue < _vreg.size(); Vvalue = Vvalue +4) {
            //Function only will check the facing side for interative regions
            //If Statements check that sides position with all x- y region ranges to see if charater has hit that region.
            bool LowerH = (_h - 1) <= _vreg[Vvalue] && (_h-1) >= _vreg[Vvalue + 1];
            bool UpperH = (_h + 10) <= _vreg[Vvalue] && (_h + 10) >= _vreg[Vvalue + 1];
            bool MidH = (_h + 5) <= _vreg[Vvalue] && (_h + 5) >= _vreg[Vvalue + 1];
            bool LowerV = (_v - 1) >= _vreg[Vvalue + 3] && (_v - 1) <= _vreg[Vvalue + 2];
            bool UpperV = (_v+14) >= _vreg[Vvalue + 3] && (_v + 14) <= _vreg[Vvalue + 2];
            bool MidV = (_v+7) >= _vreg[Vvalue + 3] && (_v + 7) <= _vreg[Vvalue + 2];
            //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
            switch (_fc) {
                case Lt:           //L
                    if (LowerH && MidV) {
                        _trg = true;
                        _treg = Vvalue / 4;
                    };
                break;
                
                case Rt:          //R
                    if (UpperH && MidV) {
                        _trg = true;
                        _treg = Vvalue / 4;
                    }
                break;
                
                case Fd:         //F
                    if (MidH && LowerV) {
                        _trg = true;
                        _treg = Vvalue / 4;
                    }
                break;
                
                case Bd:         //B
                    if (MidH && UpperV) {
                        _trg = true;
                        _treg = Vvalue / 4;
                    }
                break;
            }
        }
    }
};
//Functions revert to default values evrytime they are called. 
bool CaMove::is_trg() {
    bool temp = _trg;
    _trg = false;
    return temp;

};

int CaMove::get_treg() {
    int temp = _treg;
    _treg = -1;
    return temp;
}