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

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


	Cancer class


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

#include "Metroid.h"

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

#define MAX_METROIDS 16
#define METROID_MAX_HP 64

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

Metroid::~Metroid()
{
}


void Metroid::Update()
{
	x += Dx;
	Dy = sinLerp( Counter * 230 ) * Radius;
	y = Sy + Dy;

	if( x < MIN_X )
	{
		Active = false;
		x = 300 << 12;
		y = 96 << 12;
	}
	
	Counter++;
	// animate
	if( ( Counter & 3 ) == 0 )
	{
		Frame = Frame + 1;
		if( Frame > 13 ) Frame = 0;
	}

}


//------------------------------------------------------------------------------
//	Metroid Container
//------------------------------------------------------------------------------
//--------------------------------------
//	Ctor and Dtor
//--------------------------------------
MetroidContainer::MetroidContainer()
{
}
	
MetroidContainer::~MetroidContainer()
{
	Metroids.clear();				// clear the bullets
}

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

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

void MetroidContainer::DrawEntities()
{
	
	glColorTable( GL_RGB16, ColorTable );
	numEntities = 0;
	std::vector<Metroid>::iterator iter;
	for( iter = Metroids.begin(); iter != Metroids.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) - 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 MetroidContainer::Collide( StarFighter &Star )
{
	std::vector<Metroid>::iterator iter;
	for( iter = Metroids.begin(); iter != Metroids.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 += 207;
						Sound::Instance()->PlaySFX(E_SFX_MEDEXP1);
						// Add some nice explosions
						Explosion::Instance()->Spawn( iter->x >> 12,
													  iter->y >> 12,
													   0,
													   0,
													   EX_MEDIUM_1
													 );
							
						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_1
													 );
							
						}
						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 MetroidContainer::KillAllEntities()
{
	std::vector<Metroid>::iterator iter;
	for( iter = Metroids.begin(); iter != Metroids.end(); ++iter )
	{
		if( iter->Active )
		{
			iter->Active = false;
			iter->x = 600 << 12;
			iter->y = 96 << 12;
		}
	}
	
}

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

			iter->x = _x << 12;
			iter->Sy = _sy << 12;
			iter->Dx = speed;
			iter->Active = true;
			iter->Frame = 0;
			iter->Counter = 0;
			iter->Radius = _radius;
			iter->Hp = METROID_MAX_HP;
			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 && (Metroids.size() < MAX_METROIDS) )
	{
		Metroid metroid;
		metroid.x = _x << 12;
		metroid.Sy = _sy << 12;
		metroid.Dx = speed;
		metroid.Active = true;
		metroid.Frame = 0;
		metroid.Counter = 0;
		metroid.Radius = _radius;
		metroid.Hp = METROID_MAX_HP;
		Metroids.push_back(metroid);
	}
	
}

