ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18zc2

Dependencies:   mbed

Bullet/Bullet.cpp

Committer:
ChenZirui
Date:
2020-05-27
Revision:
5:7207c9b70108
Child:
6:b393cfe4e0a7

File content as of revision 5:7207c9b70108:

#include "Bullet.h"

Bullet::Bullet()
{

}

Bullet::~Bullet()
{

}

void Bullet::init(int x,int size,int speed,int height)
{
    _size = size;

    _x = x+(height)*0.5;
    _y = HEIGHT - size;
    
    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 Bullet::draw(N5110 &lcd)
{
    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
    if((_y >= 0)&&(_y <= 24))
    {
 //        lcd.clearPixel(_x,_y-1);
         
    }
    //lcd.drawRect(0,0,84,24,FILL_BLACK);
   // lcd.clearPixel(_x,_y);
}

void Bullet::update(N5110 &lcd)
{
    _x += _velocity.x;
    _y += _velocity.y;
    if((_y >= 0)&&(_y <= 24))
    {
// lcd.clearPixel(_x,_y-1);
         
    }
}

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