4

Ball.cpp

Committer:
eencae
Date:
2017-02-08
Revision:
0:a695d2b64167
Child:
1:201ef6494656

File content as of revision 0:a695d2b64167:

#include "Ball.h"

Ball::Ball()
{

}

Ball::~Ball()
{

}

void Ball::init(int radius,int speed,Gamepad &pad)
{
    _radius = radius;

    _x = WIDTH/2 -  _radius/2;
    _y = HEIGHT/2 - _radius/2;

    float rand1 = pad.read_pot();
    Vector2D rand2 = pad.get_coord();
    int rand3 = int(1.0e6f*(rand1 + (rand2.x/rand2.y)));
    srand(rand3);

    int direction = rand() % 4; // randomise initial direction. Need to add seed in future

    // 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::draw(N5110 &lcd)
{

    //lcd.drawCircle(_x,_y,_radius,1);
    lcd.drawRect(_x,_y,_radius,_radius,1);
}

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

int Ball::get_radius()
{
    return _radius;
}

// Private Methods