#include <allegro.h>
#include <assert.h>

#include "bullet.h"
#include "collide.h"


Bullet::Bullet()
{
    x = y = 0;
    active = false;
    bullet = NULL;
    bulletmask = NULL;
    dir = 6;
}

Bullet::~Bullet()
{
    if(bulletmask)  destroy_mask(bulletmask);
    bulletmask = NULL;
}

void Bullet::Set(int direction, float speed)
{
    if(direction == DIR_UP) dir = -speed;
    else    dir = speed;
}

void Bullet::Draw(BITMAP *bmp)
{
    if(active)
    {
	draw_sprite(bmp, bullet, (int) x, (int) y);
    }
}

void Bullet::SetBitmap(BITMAP *bmp)
{
    bullet = bmp;
    if(bulletmask)  destroy_mask(bulletmask);
    bulletmask = create_mask(bmp);
    assert(bulletmask);
}

BITMAP *Bullet::GetBitmap()
{
    return bullet;
}

CMask *Bullet::GetMask()
{
    return bulletmask;
}

void Bullet::Update()
{
    if(active)
    {
	y += dir;
	if(y < 0)   active = false;
    }
}

bool Bullet::Active()
{
    return active;
}

void Bullet::SetActive(bool which)
{
    active = which;
}

void Bullet::SetX(float newx)   { x = newx; }
void Bullet::SetY(float newy)   { y = newy; active = true; }
float Bullet::X()   { return x; }
float Bullet::Y()   { return y; }

const Bullet &Bullet::operator=(const Bullet &src)
{
    if(this != &src)
    {
	x = src.x;
	y = src.y;
	dir = src.dir;
	active = src.active;
	bullet = src.bullet;
	bulletmask = copy_mask(src.bulletmask);
    }

    return *this;
}
