/***********************************************
 * _explosion1.zes
 * 
 * Author: GD
 * Date:   12 June 2002
 *
 * Desc:   An Explosion script for creating explosions
 * 		   on the fly, from things like bombs etc..
 *
 * Usage:  Use CreateEntity, it will delete itself
 *		   When it runs out
 *         
 *         
 * Sprites: _MiscSheet.spt
 *
 ***********************************************/
#include <float>
#include <entity>
#include <general>

// Create an array of strings to hold each sprite name
new ImageArray[16][] = {"_explosion1", "_explosion2", "_explosion3", "_explosion4", "_explosion5", "_explosion6", \
                      "_explosion7", "_explosion8", "_explosion9", "_explosion10", "_explosion11", "_explosion12", \
                      "_explosion13", "_explosion14", "_explosion15", "_explosion16" };

new float: AnimCount = 0.00;   // Animation counter
const MAX_FRAMES = 9;
new BlastRadius = 0;

//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		PlaySound("_bigexplosion.wav", 254);
		SetDamage("this", 150);
		SetActiveDist("this", -2);
	   	SetActiveInGroups("this", true);
	}
	
	if (isActive("this"))
	{
		new CurrentFrame = floatround(AnimCount);
		
		// Draw the Explosion
		if (isVisible("this"))
			DrawExplosion( CurrentFrame );
			
		// Do collision detection
		GetCollisionRect();
			
		// Check the animation hasnt finished
		if (CurrentFrame >= MAX_FRAMES)
			DeleteEntity("this");
			
		// Increment the animation
		AnimCount += 10 * GetTimeDelta();
	}
}

//----------------------------------------
// Name: DrawExplosion()
//----------------------------------------
DrawExplosion( CurrentFrame )
{
	// Using arrays is a smart way of dealing with this, otherwise the code would
	// be really messy and long	
	new x = GetX("this");
	new y = GetY("this");
	new xpos[12];
	new ypos[12];
	new sprite[12];
	new NumItems = 0;
	new n;
	
	// Each frame is made up from several smaller sprites, each sprite needs an x and y
	// coordinate and a sprite code, which is what the bit below does. 
	if ( CurrentFrame == 0 )
	{
		xpos = { -8,   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
		ypos = { -8,   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
		sprite = { 15, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};		                           
	    NumItems = 1;
	    BlastRadius = 8;
	}
	else if ( CurrentFrame == 1 )
	{
		xpos = { -16, 0, -16, 0,  -1,-1,-1,-1,-1,-1,-1,-1 };
		ypos = { -16, -16, 0, 0,  -1,-1,-1,-1,-1,-1,-1,-1 };
		sprite = { 0, 1, 2, 3,  -1,-1,-1,-1,-1,-1,-1,-1};		                           
	    NumItems = 4;
	    BlastRadius = 16;
	}
	else if ( CurrentFrame == 2 )
	{
		xpos = { -16, 0, -16, 0,  -1,-1,-1,-1,-1,-1,-1,-1 };
		ypos = { -16, -16, 0, 0,  -1,-1,-1,-1,-1,-1,-1,-1 };
		sprite = { 4, 5, 6, 7,  -1,-1,-1,-1,-1,-1,-1,-1};		                           
	    NumItems = 4;
	    BlastRadius = 16;
	}
	else if ( CurrentFrame == 3 )
	{
		xpos = { -16, 0, -16, 0,  -1,-1,-1,-1,-1,-1,-1,-1 };
		ypos = { -16, -16, 0, 0,  -1,-1,-1,-1,-1,-1,-1,-1 };
		sprite = { 0, 1, 2, 3,  -1,-1,-1,-1,-1,-1,-1,-1};		                           
	    NumItems = 4;
	    BlastRadius = 16;
	}
	else if ( CurrentFrame == 4 )
	{
		xpos = { -20, 4, -20, 4, -8, -1,-1,-1,-1,-1,-1,-1 };
		ypos = { -20, -20, 4, 4, -8, -1,-1,-1,-1,-1,-1,-1 };
		sprite = { 4, 5, 6, 7, 8,     -1,-1,-1,-1,-1,-1,-1};		                           
	    NumItems = 5;
	    BlastRadius = 20;
	}
	else if ( CurrentFrame == 5 )
	{
		xpos = { -8, -13, -7, 0,  -5, -12, -1,-1,-1,-1,-1,-1 };
		ypos = { -6, -8, -13, -8, 0, -3,   -1,-1,-1,-1,-1,-1 };
		sprite = { 8, 8, 8, 8, 8, 8,       -1,-1,-1,-1,-1,-1};		                           
	    NumItems = 6;
	    BlastRadius = 20;
	}
	else if ( CurrentFrame == 6 )
	{
	    xpos = { -10, -20, -8, 6,  -4, -18, -1,-1,-1,-1,-1,-1 };
		ypos = { -6, -10, -20, -10, 5, 0,  -1,-1,-1,-1,-1,-1 };
		sprite = { 8, 8, 8, 8, 8, 8,       -1,-1,-1,-1,-1,-1};		                           
	    NumItems = 6;
	    BlastRadius = 20;
	}
	else if ( CurrentFrame == 7 )
	{
		xpos = { -6,  2,  -5, 4,  -13, -20, -19, -8, -18, 9, 4,   12};
		ypos = { -20, -11,-4, 10, -6,  -13, -8,  0,   16, 4, -18, -10};
		sprite = { 9, 11, 13, 12, 11,  14,  9,   10,  11, 14, 13,  14};		                           
	    NumItems = 12;
	    BlastRadius = 0;
	}
	else if ( CurrentFrame == 8 )
	{
		xpos = { -20, -6, 1,  11,  -13, 4, -12, -1, -1, -1, -1, -1};
		ypos = { -12, -6, -21,-10, -6,  10, 2,  -1, -1, -1, -1, -1};
		sprite = { 9, 10, 11, 12,  11,  9,  11, -1, -1, -1, -1, -1};		                           
	    NumItems = 7;
	    BlastRadius = 0;
	}
	
	// Now actually Draw each frame
	for ( n = 0; n < NumItems; n++ )
	{
		PutSprite( ImageArray[ sprite[n] ], \
		           x + xpos[n], y + ypos[n], y + 240);
	}	           
}

//----------------------------------------
// Name: GetCollisionRect()
//----------------------------------------
GetCollisionRect()
{
	new x = GetX("this");
	new y = GetY("this");
	
	if ( BlastRadius > 0)
	{
		// Set up a collision rectangle based on the Blast Radius
		SetCollisionRect("this", 0, false, x - BlastRadius, y - BlastRadius, x + BlastRadius, y + BlastRadius);
	}
	else
		ClearCollisionRect("this", 0);
		
	CheckForCollisions();
}

//----------------------------------------
// Name: CheckForCollisions()
//----------------------------------------
CheckForCollisions()
{
	// Go through every entity and see if this blast affects them
	new temp[20];
	new x = GetX("this");
	new y = GetY("this");
	
    StartEntity(100, x, y);
    do // Loop through all the entities within a certain distance
    {
       ToString(GetCurrentEntity(), temp);
       
       // Check for collision between collision rectangles
       if (Collide("this", temp) && isActive(temp))
       {
			CallFunction(temp, false, "HitByWeapon", "snnn", \
                         "explosion", GetDamage("this") , x, y);
       }
    }while( NextEntity() )  
}
