Yang Hongxiao 201199678

Dependencies:   mbed

Bullet.cpp

Committer:
YHX
Date:
2020-05-14
Revision:
1:a6ead8050c23

File content as of revision 1:a6ead8050c23:

#include "Bullet.h"

Bullet::Bullet()
{

}

Bullet::~Bullet()
{

}

void Bullet::init(int r,int vv)
{
    _r = r;
    srand(time(NULL));
    _x = rand()%(84-_r);   //the inintial ball fall from the top
    _y = 0;

    srand(time(NULL));
    int direction = rand()%4;     //the direction of the ball has four possibility

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

void Bullet::draw(N5110 &lcd)
{
    lcd.drawRect(_x,_y,_r,_r,FILL_BLACK);      //draw the ball
}

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

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

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

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

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