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

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


	Bouncer class


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

#include "Bouncer.h"

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


//------------------------------------------------------------------------------
//	Bouncer Enemy
//------------------------------------------------------------------------------
Bouncer::Bouncer()
{
	x = 600 <<12;
	y = 96 << 12;
	Frame = 0;
	Active = false;	
	Alive = true;
	DestroyedCounter = 60 * 5;
	Hp = 38; 
	DrawMode = EN_NORMAL;
	FlipMode = GL_FLIP_NONE;
}

Bouncer::~Bouncer()
{
}


void Bouncer::Init( int Palette, glImage* const Sprites )
{
	ColorTable = Palette;		// init needed data for drawing
	Images = Sprites;
}

void Bouncer::Update()
{
	const s32 Delta = BRAD_PI/64;
	static s32 Rotation = 0;
	
	if( Active )
	{
	
		Rotation += Delta;
		x += Dx;
		Dy = -abs(sinLerp( Rotation )) * Radius;
		y = Sy + Dy;
		
		if( (Rotation & (BRAD_PI-1)) == 0 )
		{
			Radius = 20 + qran_range(0,100);
			
		}
		
		if(Counter > 64*3)
		{
			if( (Rotation & (BRAD_PI-1)) == 0 )
			{
				if (qran_range(0,255)> 127) Dx = -Dx;
			}
		
			if( x < 0) Dx = -Dx;
			if( x > (256 << 12) ) Dx = -Dx;
		}
		
		Counter++;
		// animate
		FlipMode = GL_FLIP_NONE;
		s32 Ani = abs( (Rotation & (BRAD_PI-1)));
		if(Ani == 0) Frame = 0;
		
		if( (Ani > 1024) && (Ani < 2048)) Frame = 1;
		
		if( (Ani > 2048) && (Ani < 4096)) Frame = 2;
		
		if( (Ani > 4096)) Frame = 3;
		
		//if( ( Counter & 3 ) == 0 )
		//{
		//	Frame = (Frame + 1) & 3;
		//}
	}
	
}
	

void Bouncer::Draw()
{
	glColorTable( GL_RGB16, ColorTable );
	if( Active )
	{
		if( (DrawMode == EN_WHITE) )
		{
			DrawMode = EN_NORMAL;
			glColorTable( GL_RGB256, Palettes::Instance()->White() );
			glSprite( (x >> 12) - 32, (y >> 12) - 32, 
					  FlipMode, &Images[Frame] 
					);
			glColorTable( GL_RGB16, ColorTable );

		}
		else
		{
			glSprite( (x >> 12) - 32, (y >> 12) - 32, 
					  FlipMode, &Images[Frame] 
					);
		}
		
	}

}


bool Bouncer::Collide( StarFighter &Star )
{
	
	if( Active )
	{
		int rad2 = 32 * 32;
		int cx = (x >> 12) - (Star.x >> 12);
		int cy = (y >> 12) - (Star.y >> 12);
		if( ( (cx * cx) + (cy * cy) ) < (rad2 * (1+Star.IsBursting())) )
		{
			if( Star.IsBursting() )
			{
				Hp--;
				if( (Hp & 3) == 0 ) Sound::Instance()->PlaySFX(E_SFX_HIT);
				if( Hp < 0 )
				{
					Active = false;
					Alive = false;
					Star.Score += 11130;
					Sound::Instance()->PlaySFX(E_SFX_BIGEXP);
					// Add some nice explosions
					Explosion::Instance()->Spawn( x >> 12,
												  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( x >> 12,
												  y >> 12,
												   dx,
												   dy,
												   EX_SMALL_3
												 );
						
					}
					
					x = 600 << 12;
					y = 96 << 12;
	
				}
				if( DrawMode == EN_NORMAL )
				{
					if( (Counter & 1) ) DrawMode = EN_WHITE;
				}
			}
			else
			{
				// kill star
				if( !Star.IsInvincible() ) Star.Kill();
			}
			return true;
		}
	}

	return false;
}



void Bouncer::Spawn()
{
	
	
	x = 300 << 12;
	y = 0;
	Dx = -floattof32(0.75);
	Dy = 0;
	Sy = (191-32) << 12;
	Radius = 100;
		
	Active = true;
	Alive = true;	
	DestroyedCounter = 60 * 5;
	Frame = 0;
	Counter = 0;
	Hp = 264;

	
}

