/******************************************************************************
*******************************************************************************

	Bubble Fight EX
	relminator
	http://rel.betterwebber.com


	Sparky class


*******************************************************************************
******************************************************************************/


#include "Sparky.h"


#define MIN_X ( -16 << 12 )
#define MIN_Y ( -16 << 12 )

#define MAX_SPARKYS 16

//------------------------------------------------------------------------------
//	Sparky Enemy
//------------------------------------------------------------------------------
Sparky::Sparky()
{
	Frame = 0;
	Active = false;
}

Sparky::~Sparky()
{
}

void Sparky::Update()
{
	x += Dx;
	y += Dy;
	
	if( x < MIN_X )
	{
		Active = false;
		x = 300 << 12;
		y = 96 << 12;
	}
	
	Counter++;
	// animate
	if( ( Counter & 3 ) == 0 )
	{
		Frame = (Frame + 1);
		if( Frame > 2 ) Frame = 0;
	}

}


//------------------------------------------------------------------------------
//	Sparky Container
//------------------------------------------------------------------------------
//--------------------------------------
//	Ctor and Dtor
//--------------------------------------
SparkyContainer::SparkyContainer()
{
}
	
SparkyContainer::~SparkyContainer()
{
	Sparkys.clear();				// clear the bullets
}


void SparkyContainer::Init( int Palette, glImage* const Sprites )
{
	ColorTable = Palette;		// init needed data for drawing
	Images = Sprites;
	Sparkys.resize( MAX_SPARKYS/2 );			// 1/2 of max (speed of iteration)
	Sparkys.reserve( MAX_SPARKYS );		// reserve max for speed.  No need to do this for enemies
}

//--------------------------------------
//	Virtual Functions
//--------------------------------------
void SparkyContainer::UpdateEntities()
{
	
	std::vector<Sparky>::iterator iter;
	for( iter = Sparkys.begin(); iter != Sparkys.end(); ++iter )
	{
		if( iter->Active )
			iter->Update();
	}
	
}

void SparkyContainer::DrawEntities()
{

	glColorTable( GL_RGB16, ColorTable );
	numEntities = 0;
	std::vector<Sparky>::iterator iter;
	for( iter = Sparkys.begin(); iter != Sparkys.end(); ++iter )
	{
		if( iter->Active )
		{
			numEntities++;
			glSprite( (iter->x >> 12) - 10, (iter->y >> 12) - 8, 
					  GL_FLIP_NONE, &Images[iter->Frame] 
					);
		}
	}
	
}

bool SparkyContainer::Collide( StarFighter &Star )
{
	std::vector<Sparky>::iterator iter;
	for( iter = Sparkys.begin(); iter != Sparkys.end(); ++iter )
	{
		if( iter->Active )
		{
			int rad2 = 8 * 8;
			for( int i = 0; i < 6; i++)
			{
				int cx = (iter->x >> 12) - (Star.x >> 12);
				int cy = ((iter->y >> 12)+(i * 16) ) - (Star.y >> 12);
				if( ( (cx * cx) + (cy * cy) ) < rad2  )
				{
					if( !Star.IsBursting() )
					{
						// kill star
						if( !Star.IsInvincible() ) Star.Kill();
					}
					return true;
				}
			}
		}
	}
	return false;
}



void SparkyContainer::KillAllEntities()
{
	std::vector<Sparky>::iterator iter;
	for( iter = Sparkys.begin(); iter != Sparkys.end(); ++iter )
	{
		if( iter->Active )
		{
			iter->Active = false;
			iter->x = 600 << 12;
			iter->y = 96 << 12;
		}
	}
	
}


//--------------------------------------
//	Non-Virtual Functions
//--------------------------------------
void SparkyContainer::Spawn( int _x, int _y )
{
	
	bool SlotFull = true;      		// value to check if all the slots are occupied
	
	std::vector<Sparky>::iterator iter;
	for( iter = Sparkys.begin(); iter != Sparkys.end(); ++iter )
	{
		if( !iter->Active )		// insert a bullet on a free slot
		{

			iter->x = _x << 12;
			iter->y = _y << 12;
			iter->Dx = -floattof32( 0.3 );
			iter->Dy = 0;
			iter->Active = true;
			iter->Frame = 0;
			iter->Counter = 0;
			SlotFull = false;			// there's a free slot so we dont execute the code below
			break;
		
		}
	}
	
	// All the slots are full
	// Only push back a bullet if the slot is full and we have less than numbullets
	if( SlotFull && (Sparkys.size() < MAX_SPARKYS) )
	{
		Sparky spark;
		spark.x = _x << 12;
		spark.y = _y << 12;
		spark.Dx = floattof32( 0.3 );
		spark.Dy = 0;
		spark.Active = true;
		spark.Frame = 0;
		spark.Counter = 0;
		
		Sparkys.push_back(spark);
	}
	
}


