ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18jgb

Dependencies:   mbed

Aim/Aim.cpp

Committer:
el18jgb
Date:
2020-05-23
Revision:
21:a0f3651f56c4
Parent:
19:33c77517cb88
Child:
23:0e8155320571

File content as of revision 21:a0f3651f56c4:

#include "Aim.h"

const int curs [5][5] = 
{
    {0,0,1,0,0},
    {0,0,1,0,0},
    {1,1,0,1,1},
    {0,0,1,0,0},
    {0,0,1,0,0},
};

const int bang [5][5] = 
{
    {1,0,0,0,1},
    {0,1,0,1,0},
    {0,0,1,0,0},
    {0,1,0,1,0},
    {1,0,0,0,1},
};


Aim::Aim()
{

}

Aim::~Aim()
{

}
 

void Aim::init()
{
    _y = 5;  // top middle of the screen
    _x = 42;  // 
    _height = 5;
    _width = 5;
    //_state = 0;
   
}

void Aim::draw(N5110 &lcd, int state)
{
    // draw basket 
    bool _state = state;
    
    if (_state == 0){
    lcd.drawSprite(_x,_y,5,5,(int*)curs);
    }
    if (_state == 1){
    lcd.drawSprite(_x,_y,5,5,(int*)bang);//different cursor for a hit shot
    }
}

void Aim::update(Gamepad &pad)
{
    Direction d = pad.get_direction();
    //printf("direction =  %c\n", d);
    mag = pad.get_mag();
    //printf("mag =  %f\n", mag);
    
    _speed = int(mag*8.0f);  

    // North is decrement as origin is at the top-left so decreasing moves up
    if (d == N) {
        _y-=_speed;
    } else if (d == NE) {
        _x+=_speed;
        _y-=_speed;
    } else if (d == E) {
        _x+=_speed;
    } else if (d == SE) {
        _x+=_speed;
        _y+=_speed;
    } else if (d == S) {
        _y+=_speed;
    } else if (d == SW) {
        _x-=_speed;
        _y+=_speed;
    } else if (d == W) {
        _x-=_speed;
    } else if (d == NW) {
        _x-=_speed;
        _y-=_speed;
    }

    // check the x origin to ensure that the paddle doesn't go off screen
    if (_x < 1) {
        _x = 1;
    }
    if (_x > 84 - _width - 1) {
        _x = 84 - _width - 1;
    }
    // check the y origin to ensure that the sprite doesn't go off screen
    if (_y < 1) {
        _y = 1;
    }
    if (_y > 48 - _height - 1) {
        _y = 48 - _height - 1;
    }
    //printf("aim x position =  %d\n", _x);
    //printf("aim y position =  %d\n", _y);
}

void Aim::acc_control(FXOS8700CQ &acc)
{
    float roll = acc.get_roll_angle();
    _x = _x + roll;
    //printf("roll =  %f\n", roll);
    float pitch = acc.get_pitch_angle();
    _y = _y - pitch;
    //printf("pitch =  %f\n", pitch);
    
    // check the x origin to ensure that the paddle doesn't go off screen
    if (_x < 1) {
        _x = 1;
    }
    if (_x > 84 - _width - 1) {
        _x = 84 - _width - 1;
    }
    // check the y origin to ensure that the sprite doesn't go off screen
    if (_y < 1) {
        _y = 1;
    }
    if (_y > 48 - _height - 1) {
        _y = 48 - _height - 1;
    }
    //printf("aim x position =  %d\n", _x);
    //printf("aim y position =  %d\n", _y);
}

Vector2D Aim::get_pos() {
    Vector2D p = {(_x+2),(_y+2)};//middle of the cursor
    return p;    
}

void Aim::set_pos(int x, int y) {
    _x = x;
    _y = y;   
}