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

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


	Cancer class


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


#include "Cancer.h"


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

#define MAX_CANCERS 16

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

Cancer::~Cancer()
{
}

void Cancer::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) & 3;
	}

}


//------------------------------------------------------------------------------
//	Cancer Container
//------------------------------------------------------------------------------
//--------------------------------------
//	Ctor and Dtor
//--------------------------------------
CancerContainer::CancerContainer()
{
}


CancerContainer::~CancerContainer()
{
	Cancers.clear();				// clear the bullets
}

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

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

void CancerContainer::DrawEntities()
{

	glColorTable( GL_RGB16, ColorTable );
	numEntities = 0;
	std::vector<Cancer>::iterator iter;
	for( iter = Cancers.begin(); iter != Cancers.end(); ++iter )
	{
		if( iter->Active )
		{
			numEntities++;
			if( (iter->DrawMode == EN_WHITE) )
			{
				iter->DrawMode = EN_NORMAL;
				glColorTable( GL_RGB256, Palettes::Instance()->White() );
				glSprite( (iter->x >> 12) - 16, (iter->y >> 12) - 16, 
						  GL_FLIP_NONE, &Images[iter->Frame] 
					    );
				glColorTable( GL_RGB16, ColorTable );
	
			}
			else
			{
				glSprite( (iter->x >> 12) - 16, (iter->y >> 12) - 16, 
						  GL_FLIP_NONE, &Images[iter->Frame] 
					    );
				
			}
		}
	}
	
}

bool CancerContainer::Collide( StarFighter &Star )
{
	std::vector<Cancer>::iterator iter;
	for( iter = Cancers.begin(); iter != Cancers.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 += 75;
						Sound::Instance()->PlaySFX(E_SFX_SMALLEXP);
						// Add some nice explosions
						Explosion::Instance()->Spawn( iter->x >> 12,
													  iter->y >> 12,
													   0,
													   0,
													   EX_MEDIUM_2
													 );
							
						for( int i = 0; i < 4; i++ )
						{	
							s32 dx = cosLerp(i * BRAD_PI / 2);
							s32 dy = sinLerp(i * BRAD_PI / 2);
							Explosion::Instance()->Spawn( iter->x >> 12,
													  iter->y >> 12,
													   dx,
													   dy,
													   EX_SMALL_2
													 );
						
						}
						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 CancerContainer::KillAllEntities()
{
	std::vector<Cancer>::iterator iter;
	for( iter = Cancers.begin(); iter != Cancers.end(); ++iter )
	{
		if( iter->Active )
		{
			iter->Active = false;
			iter->x = 600 << 12;
			iter->y = 96 << 12;
		}
	}
	
}

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

			iter->x = _x << 12;
			iter->y = _y << 12;
			iter->Dx = -floattof32( 0.5 );
			iter->Dy = 0;
			iter->Active = true;
			iter->Frame = 0;
			iter->Counter = 0;
			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 && (Cancers.size() < MAX_CANCERS) )
	{
		Cancer cancer;
		cancer.x = _x << 12;
		cancer.y = _y << 12;
		cancer.Dx = floattof32( 0.5 );
		cancer.Dy = 0;
		cancer.Active = true;
		cancer.Frame = 0;
		cancer.Counter = 0;
		cancer.Hp = 40;
		Cancers.push_back(cancer);
	}
	
}

