ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Ball/Ball.cpp

Committer:
shahidsajid
Date:
2019-04-15
Revision:
3:bd3465a70a5a
Child:
4:55a0509c4874

File content as of revision 3:bd3465a70a5a:

#include "Ball.h"

Ball::Ball()
{

}

Ball::~Ball()
{

}

void Ball::init(int size,int speed)
{
    _size = size;

    _x = 42;
    _y = 15;

    srand(time(NULL));
    int direction = rand() % 4; // randomise initial direction. 

    // 4 possibilities. Get random modulo and set velocities accordingly
    if (direction == 0) {
        _velocity.x = speed;
        _velocity.y = speed;
    } else if (direction == 1) {
        _velocity.x = speed;
        _velocity.y = -speed;
    } else if (direction == 2) {
        _velocity.x = speed;
        _velocity.y = speed;
    } else {
        _velocity.x = -speed;
        _velocity.y = -speed;
    }
}

void Ball::start(N5110 &lcd){
    if (_y!=28){
        _y+=1;
    }
}

void Ball::draw(N5110 &lcd)
{
    lcd.drawCircle(_x,_y,1,FILL_BLACK);
    //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
}

void Ball::update()
{
    _x += _velocity.x;
    _y += _velocity.y;
}

void Ball::set_velocity(Vector2D v)
{
    _velocity.x = v.x;
    _velocity.y = v.y;
}

Vector2D Ball::get_velocity()
{
    Vector2D v = {_velocity.x,_velocity.y};
    return v;
}

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

void Ball::set_pos(Vector2D p)
{
    _x = p.x;
    _y = p.y;
}
void Ball::get_direction(Gamepad &pad){
    _d = pad.get_direction();
}
void Ball::set_field(N5110 &lcd){
    
    lcd.drawCircle(25,2,2,FILL_BLACK); //NE
    lcd.drawCircle(65,2,2,FILL_BLACK);  //NW
    lcd.drawCircle(65,40,2,FILL_BLACK);  //SE
    lcd.drawCircle(25,40,2,FILL_BLACK); //SW
    lcd.drawCircle(3,20,2,FILL_BLACK); //E
    lcd.drawCircle(80,20,2,FILL_BLACK); 
    
    /*
    for(int i=0;i<5;i++){
        get_direction();
        while (_d==N||_d==S){
            get_direction();
        }
        if (d==NE){
            lcd.drawCircle(42,4,2,FILL_BLACK);
    */
}