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

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


	Singleton Explosion class


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

#include "Explosion.h"

Explosion* Explosion::ms_instance = 0;

//--------------------------------------
//	Ctor and Dtor
//--------------------------------------
Explosion::Explosion()
{
}

Explosion::~Explosion()
{
}

Explosion* Explosion::Instance()
{
	if(ms_instance == 0){
		ms_instance = new Explosion();
	}
	return ms_instance;
}

void Explosion::Release()
{
	if(ms_instance){
		delete ms_instance;
	}
	ms_instance = 0;
}

//--------------------------------------
//	
//--------------------------------------
void Explosion::Init( int Palette, glImage* const Sprites )
{
	
	ColorTable = Palette;		// init needed data for drawing
	Images = Sprites;
	Explosions.reserve( 128 );
	Explosions.resize( 128 );
	
}

//--------------------------------------
//	
//--------------------------------------
void Explosion::Update()
{
	
	std::vector<Explode>::iterator iter;
	for( iter = Explosions.begin(); iter != Explosions.end(); ++iter )
	{
		if( iter->Active )
			iter->Update();
	}
	
}

//--------------------------------------
//	
//--------------------------------------
void Explosion::Draw()
{
	
	glColorTable( GL_RGB256, ColorTable );
	numExplosions = 0;
	std::vector<Explode>::iterator iter;
	for( iter = Explosions.begin(); iter != Explosions.end(); ++iter )
	{
		if( iter->Active )
		{
			numExplosions++;
			
			int frame = iter->Frame + iter->BaseFrame;
			glSprite( (iter->x >> 12) - Images[frame].width/2, 
					  (iter->y >> 12) - Images[frame].height/2, 
					  GL_FLIP_NONE, &Images[frame] 
				    );
			
		}
	}
	
	
}

//--------------------------------------
//	
//--------------------------------------
void Explosion::Spawn(  int _x, int _y, s32 _dx, s32 _dy, int ID )
{
	
	int base = 6;
	int frames = 8;
	
	switch( ID )
	{
		case EX_SMALL_1:
			base = 6;
			frames = 8;
			break;
		case EX_SMALL_2:
			base = 19;
			frames = 5;
			break;
		case EX_SMALL_3:
			base = 32;
			frames = 8;
			break;
		case EX_SMALL_4:
			base = 40;
			frames = 5;
			break;
		case EX_MEDIUM_1:
			base = 0;
			frames = 6;
			break;
		case EX_MEDIUM_2:
			base = 14;
			frames = 5;
			break;
		case EX_MEDIUM_3:
			base = 24;
			frames = 4;
			break;
		case EX_MEDIUM_4:
			base = 28;
			frames = 4;
			break;
		case EX_MEDIUM_5:
			base = 45;
			frames = 4;
			break;
		default:
			base = 6;
			frames = 8;
	}
	
	std::vector<Explode>::iterator iter;
	for( iter = Explosions.begin(); iter != Explosions.end(); ++iter )
	{
		if( !iter->Active )		// insert a bullet on a free slot
		{

			iter->Init( _x << 12 , _y << 12, _dx, _dy, base, frames );
			break;
		
		}
	}
	
}


