not final

Dependencies:   mbed

Bullet/Bullet.cpp

Committer:
ChenZirui
Date:
2020-05-29
Revision:
14:3b4370d5b2c0
Parent:
7:f61ac963eb07

File content as of revision 14:3b4370d5b2c0:

#include "Bullet.h"


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

    _x = 24;
    _y = 28;
    
    srand(time(NULL));
    int direction = rand() % 2; // 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;
    } 
}

void Bullet::draw(N5110 &lcd)
{
    lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
  /*  if((_y >= 0)&&(_y <= 24))
    {
       X=_x;
       Y=_y;   
         
    }
   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);
      X=_x;
      Y=_y;   
    }
     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;
}