ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18zc_

Dependencies:   mbed

Bullet/Bullet.cpp

Committer:
ChenZirui
Date:
2020-05-29
Revision:
15:7ca2d1b2bd0e
Parent:
7:f61ac963eb07
Child:
16:f8a6834a0289

File content as of revision 15:7ca2d1b2bd0e:

#include "Bullet.h"


void Bullet::init(int x,int size,int speed,int y)
{
    _size = size; // the size of bullet

    _x = x;       //initial horizontal coordinate
    _y = y;       //initial vertical coordinate
    
    srand(time(NULL));
    int direction = rand() % 2; // randomise initial direction. 

    // 2 random direction
    if (direction == 0) {
        _velocity.x = speed;
        _velocity.y = -speed;
    } else if (direction == 1) {
        _velocity.x = -speed;
        _velocity.y = -speed;
    } 
}

void Bullet::draw(N5110 &lcd)  // bullet drawing function
{
    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
}

void Bullet::update(N5110 &lcd) // bullet updating
{
    _x += _velocity.x;
    _y += _velocity.y;

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

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

Vector2D Bullet::get_pos()           //bullet  position reading
{
    Vector2D p = {_x,_y};
    return p;
}

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