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

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


	Blob class


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

#include "Blob.h"

#define MIN_X ( -20 << 12 )
#define MIN_Y ( -18 << 12 )

#define MAX_BLOBS 16

//------------------------------------------------------------------------------
//	Blob Enemy
//------------------------------------------------------------------------------
Blob::Blob()
{
	Frame = 0;
	Active = false;
	Hp = 40;
	DrawMode = EN_NORMAL;
	
}

Blob::~Blob()
{
}


void Blob::Update()
{
	Sx += Dx;
	x = Sx + sinLerp( Counter * 230 ) * Radius;
	if( x < MIN_X )
	{
		Active = false;
		x = 300 << 12;
		y = 96 << 12;
	}

	Counter++;
	// animate
	if( ( Counter & 3 ) == 0 )
	{
		Frame = (Frame + 1) & 3;
	}

}


//------------------------------------------------------------------------------
//	Blob Container
//------------------------------------------------------------------------------
//--------------------------------------
//	Ctor and Dtor
//--------------------------------------
BlobContainer::BlobContainer()
{
}
	
BlobContainer::~BlobContainer()
{
	Blobs.clear();				// clear the bullets
}

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

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

void BlobContainer::DrawEntities()
{
	
	glColorTable( GL_RGB16, ColorTable );
	numEntities = 0;
	std::vector<Blob>::iterator iter;
	for( iter = Blobs.begin(); iter != Blobs.end(); ++iter )
	{
		if( iter->Active )
		{
			numEntities++;
			if( (iter->DrawMode == EN_WHITE) )
			{
				iter->DrawMode = EN_NORMAL;
				glColorTable( GL_RGB256, Palettes::Instance()->Green() );
				glSprite( (iter->x >> 12) - 19, (iter->y >> 12) - 18, 
					      GL_FLIP_NONE, &Images[iter->Frame] 
					    );
				glColorTable( GL_RGB16, ColorTable );
	
			}
			else
			{
				glSprite( (iter->x >> 12) - 19, (iter->y >> 12) - 18, 
						  GL_FLIP_NONE, &Images[iter->Frame] 
					    );
			}
			
		}
	}
	
}

bool BlobContainer::Collide( StarFighter &Star )
{
	std::vector<Blob>::iterator iter;
	for( iter = Blobs.begin(); iter != Blobs.end(); ++iter )
	{
		if( iter->Active )
		{
			int rad2 = 16 * 16;
			int cx = (iter->x >> 12) - (Star.x >> 12);
			int cy = (iter->y >> 12) - (Star.y >> 12);
			if( ( (cx * cx) + (cy * cy) ) < (rad2 * (1+Star.IsBursting())) )
			{
				if( Star.IsBursting() )
				{
					iter->Hp--;
					if( (iter->Hp & 3) == 0 ) Sound::Instance()->PlaySFX(E_SFX_HIT);
					if( iter->Hp < 0 )
					{
						iter->Active = false;
						Star.Score += 100;
						Sound::Instance()->PlaySFX(E_SFX_SMALLEXP);
						// Add some nice explosions
						Explosion::Instance()->Spawn( iter->x >> 12,
													  iter->y >> 12,
													   0,
													   0,
													   EX_MEDIUM_4
													 );
							
						for( int i = 0; i < 4; i++ )
						{	
							s32 dx = cosLerp(BRAD_PI/4 + i * BRAD_PI / 2);
							s32 dy = sinLerp(BRAD_PI/4 + i * BRAD_PI / 2);
							Explosion::Instance()->Spawn( iter->x >> 12,
													  iter->y >> 12,
													   dx,
													   dy,
													   EX_SMALL_4
													 );
							
						}
						iter->x = 600 << 12;
						iter->y = 96 << 12;
						
					}
					if( iter->DrawMode == EN_NORMAL )
					{
						if( (iter->Counter & 1) ) iter->DrawMode = EN_WHITE;
					}
				}
				else
				{
					// kill star
					if( !Star.IsInvincible() ) Star.Kill();
				}
				
				return true;
			}
		}
	}
	return false;
}


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



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

			iter->Sx = _x << 12;
			iter->y = _y << 12;
			iter->Dx = speed;
			iter->Dy = 0;
			iter->Active = true;
			iter->Frame = 0;
			iter->Counter = 0;
			iter->Radius = _radius;
			iter->Hp = 40;
			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 && (Blobs.size() < MAX_BLOBS) )
	{
		Blob blob;
		blob.Sx = _x << 12;
		blob.y = _y << 12;
		blob.Dx = speed;
		blob.Dy = 0;
		blob.Active = true;
		blob.Frame = 0;
		blob.Counter = 0;
		blob.Radius = _radius;
		blob.Hp = 40;
		Blobs.push_back(blob);
	}
	
}

