#include "enemy.h"
#include "collide.h"

EnemyGroup::EnemyGroup()
{
    Clear();
}

EnemyGroup::~EnemyGroup()
{
    Clear();
}

void EnemyGroup::Clear()
{
    group.clear();
    base.Clear();
}

void EnemyGroup::Draw(BITMAP *bmp)
{
    for(unsigned int i=0; i<group.size(); ++i)
    {
	group[i].Draw(bmp);
    }
}

void EnemyGroup::UpdateFrame()
{
    for(unsigned int i=0; i<group.size(); ++i)
    {
	group[i].NextFrame();
    }
}

void EnemyGroup::SetBase(Anim newbase)
{
    base = newbase;
}

void EnemyGroup::AddX(float addx)
{
    for(unsigned int i=0; i<group.size(); ++i)
    {
	group[i].SetX(group[i].X() + addx);
    }
}

void EnemyGroup::AddY(float addy)
{
    for(unsigned int i=0; i<group.size(); ++i)
    {
	group[i].SetY(group[i].Y() + addy);
    }
}

void EnemyGroup::XWrap()
{
    for(unsigned int i=0; i<group.size(); ++i)
    {
	if(group[i].X() > SCREEN_W)
	{
	    group[i].SetX(-group[i].W());
	}
    }
}

void EnemyGroup::YWrap()
{
    for(unsigned int i=0; i<group.size(); ++i)
    {
	if(group[i].Y() > 350)
	{
	    group[i].SetY(-group[i].H());
	}
    }
}

bool EnemyGroup::Collide(CMask *mask, float x, float y)
{
    bool collision = false;

    for(unsigned int i=0; i<group.size(); ++i)
    {
	if(group[i].Active())
	{
	    if(group[i].Collide(mask,x,y))
	    {
		group[i].SetActive(false);
		collision = true;
	    }
	}
    } 

    return collision;
}

const EnemyGroup &EnemyGroup::operator=(const EnemyGroup &src)
{
    if(this != &src)
    {
	group = src.group;
	base = src.base;
    }

    return *this;
}

bool EnemyGroup::Empty()
{
    bool full = false;

    for(unsigned int i=0; i<group.size(); ++i)
    {
	if(group[i].Active()) full = true;
    }

    if(full)    return false;
    return true;
}

void EnemyGroup::GetRandData(float *x, float *y)
{
    bool got_one = false;

    while(!got_one)
    {
	for(unsigned int i=0; i<group.size(); ++i)
	{
	    if(group[i].Active())
	    {
		if(rand() % group.size() == 0)
		{
		    got_one = true;
		    *x = group[i].X();
		    *y = group[i].Y();
		}
	    }
	    if(got_one) break;
	}
    }
}