ELEC2645 (2018/19) / Mbed 2 deprecated el17aj

Dependencies:   mbed

CrossHairs/CrossHairs.cpp

Committer:
adat80
Date:
2019-04-21
Revision:
1:3916f272663e
Child:
5:8bd09c675f28

File content as of revision 1:3916f272663e:

#include "CrossHairs.h"


int crossHairsSprite[7][7] =   {
    { 0,0,1,1,1,0,0 },
    { 0,1,0,0,0,1,0 },
    { 1,0,0,1,0,0,1 },
    { 1,0,1,1,1,0,1 },
    { 1,0,0,1,0,0,1 },
    { 0,1,0,0,0,1,0 },
    { 0,0,1,1,1,0,0 },
};

CrossHairs::CrossHairs()
{

}

CrossHairs::~CrossHairs()
{

}

void CrossHairs::init(int speed)
{

    _x = WIDTH/2 -  (7/2);
    _y = HEIGHT/2 - (7/2);
    _speed = speed;


   
}

void CrossHairs::draw(N5110 &lcd)
{
    // x origin, y origin, rows, cols, sprite
    lcd.drawSprite(_x,_y,7,7,(int *)crossHairsSprite);
 
}

void CrossHairs::update(float angle,float mag, int fps)
{
    //Correcting JoyStick to match screen angles
    angle = angle - 90.0f;
    
    //Conversion To Radians
    double angleRads = (angle/360.0f)*2.0f*3.14f;
    
    
    
    _x+= _speed*mag*cos(angleRads);
    _y += _speed*mag*sin(angleRads);
    
    //check if cross hairs are outside screen
    int halfCrossHairs = 3;
    if (_x < -halfCrossHairs) {
        _x = -halfCrossHairs;
    } else if (_x > 84-halfCrossHairs) {
        _x = 84-halfCrossHairs;
    }
    if (_y < -halfCrossHairs) {
        _y = -halfCrossHairs;
    } else if (_y > 48-halfCrossHairs) {
        _y = 48-halfCrossHairs;
    }
        
    
    //_x = WIDTH/2 +  10.0*mag*cos(angleRads);
    //_y = HEIGHT/2 + 10.0*mag*sin(angleRads);
}



Vector2D CrossHairs::get_pos()
{
    Vector2D p = {_x,_y};
    return p;
}

void CrossHairs::set_pos(Vector2D p)
{
    _x = p.x;
    _y = p.y;
}