#include "classes.h"
#include <cmath>
using namespace std;

vector<bullet> bullets;

void bullet::draw(BITMAP* dest)
{
    circlefill(dest, x, y, 3, makecol(255,0,0));
}

void bullet::move()
{
    if (dir==0)
    y -= 4;
    if (dir==1)
    x += 4;
    if (dir==2)
    y += 4;
    if (dir==3)
    x -= 4;
}

bool bullet::hit(character& chr)
{
    int dx = abs(chr.X()+16-x);
    int dy = abs(chr.Y()+16-y);
    int dis = dx + dy;

    return (dis<25);
}

void character::shoot()
{
    if (rtime<=0)
    {
        if (mag>0)
        {
            mag--;
            rtime = 50;
            bullets.push_back(bullet(x+16, y+16, dir));
            for (int c = 0; c<6; c++)
            {
                bullets[bullets.size()-1].move();
            }

            if (mag==0 && ammo>0)
            {
                rtime = 700;
                ammo -= mgsz;
                mag = mgsz;
                if (ammo<=0)
                {
                    mag -= ammo;
                    ammo = 0;
                }
            }
        }
    }
}

void character::draw(BITMAP* dest)
{
    draw_sprite(dest, gfc[dir], x, y);
}

void bot::move(player& plr)
{
    update();

    int dis = abs(x-plr.x) + abs(y-plr.y);

    if (dis>60)
    {
        if (x<plr.x)
        {
            x++;
            dir = 1;
        }
        else if (x>plr.x)
        {
            x--;
            dir = 3;
        }
        else if (y<plr.y)
        {
            y++;
            dir = 2;
        }
        else
        {
            y--;
            dir = 0;
        }
    }
    if (abs(x-plr.x)<15)
    {
        if (plr.y>y && dir==2)
        shoot();
        if (plr.y<y && dir==0)
        shoot();
    }
    if (abs(y-plr.y)<15)
    {
        if (plr.x>x && dir==1)
        shoot();
        if (plr.x<x && dir==3)
        shoot();
    }
}

void player::move()
{
    update();

    if (key[KEY_UP] && y>0)
    {
        y--;
        dir = 0;
    }
    if (key[KEY_RIGHT] && x<640)
    {
        x++;
        dir = 1;
    }
    if (key[KEY_DOWN] && y<480)
    {
        y++;
        dir = 2;
    }
    if (key[KEY_LEFT] && x>0)
    {
        x--;
        dir = 3;
    }
    if (key[KEY_SPACE])
    shoot();
}
