/***********************************************
 * _iceblast1.zes
 * 
 * Author: GD
 * Date:   25 June 2002
 *
 * Desc:   An Icey blast which travels across the screen, used
 *		   by the Ice rod weapon.
 *
 * Usage:
 *         
 *     
 * Sprites: _WeaponSheet1.spt
 *    
 ***********************************************/
#include <animation>
#include <entity>
#include <general>
#include <float>

new IceAnim[8][20];		// Strings to hold each ice animation
new float: timeActive = 0.00;
new float: HitCounter = 0.00;
new hitSomthing = false;

main()
{
	if (FirstRun())
	{
		new n;
		
		// Create the animations
		for ( n = 0; n < 8; n+=1 )
		{
			CreateAnim(10, IceAnim[n]);
			AddAnimframe(IceAnim[n], 0,0, "_icerod10");
			AddAnimframe(IceAnim[n], 0,0, "_icerod11");
		}
		
		for ( n = 0; n < 8; n+=2 )
			SetAnimCount(IceAnim[n], 1);
		
		SetSpeed("this", 0);
		SetActiveDist("this", -2);
	}
	else
	{
		// Dont allow this entity to live for too long if it doesnt hit anything
		timeActive += 1 * GetTimeDelta();
		if ( timeActive > 5)
			EndEntity();
		
		if ( hitSomthing )
			EndAnim();
		else	
			MoveIceBlast();	
	}
}

//----------------------------------------
// Name: EndAnim()
//----------------------------------------
EndAnim()
{
	new x = GetX("this");
	new y = GetY("this");
	
	HitCounter += 30 * GetTimeDelta();
	if ( HitCounter > 5)
		EndEntity();
		
	if ( HitCounter < 3 )
	{
		PutSprite("_icerod13", x - 8, y - 8, y);
		PutSprite("_icerod13", x,     y - 8, y);
		PutSprite("_icerod13", x - 8, y, y);
		PutSprite("_icerod13", x, y, y);
	}
	else
	{
		PutSprite("_icerod14", x - 12, y - 12, y);
		PutSprite("_icerod14", x + 4,      y - 12, y);
		PutSprite("_icerod14", x - 12, y + 4, y);
		PutSprite("_icerod14", x + 4, y + 4, y);
	}
}

//----------------------------------------
// Name: MoveIceBlast()
//----------------------------------------
MoveIceBlast()
{
	new xf[2][4] = { {-2,   2, -2, -10}, {-6, 0, 0 ,-6}};
	new yf[2][4] = { {-10, -2,  2,  -2}, {-6, -6, 0, 0}};
	new xp[2];
	new yp[2];
	
	// Move the entity
	new dir = GetDirection("this");
	if ( dir == north )
	{
		SetMoveAngle("this", 90);
		yp[0] = -14;
		yp[1] = -26;
	}
	else if ( dir == east )
	{
		SetMoveAngle("this", 180);
		xp[0] = 14;
		xp[1] = 26;
	}
	else if ( dir == south )
	{
		SetMoveAngle("this", 270);
		yp[0] = 14;
		yp[1] = 26;
	}
	else if ( dir == west )
	{
		SetMoveAngle("this", 0);
		xp[0] = -14;
		xp[1] = -26;
	}
	AngleMove("this");
	
	// Draw the entity
	new x = GetX("this");
	new y = GetY("this");
	new a = GetAnimCount(IceAnim[4]);
	
	if ( isVisible("this") )
	{
		if ( timeActive > 0.2 )
			SetSpeed("this", 220);
		
		if ( timeActive > 0.25 )
		{
			DrawAnim(IceAnim[0], x - xp[0] - 6, y - yp[0] - 6, y);
			DrawAnim(IceAnim[1], x - xp[0],     y - yp[0] - 6, y);
			DrawAnim(IceAnim[2], x - xp[0] - 6, y - yp[0],     y);
			DrawAnim(IceAnim[3], x - xp[0],     y - yp[0],     y);
			
			PutSprite("_icerod12", x - xp[1] - 3, 	y - yp[1] - 3, y);
			PutSprite("_icerod12", x - xp[1] + 1, 	y - yp[1] - 3, y);
			PutSprite("_icerod12", x - xp[1] - 3, 	y - yp[1] + 1, y);
			PutSprite("_icerod12", x - xp[1] + 1, 	y - yp[1] + 1, y);
		}
	
		DrawAnim(IceAnim[4], x + xf[a][0], y + yf[a][0], y);
		DrawAnim(IceAnim[5], x + xf[a][1], y + yf[a][1], y);
		DrawAnim(IceAnim[6], x + xf[a][2], y + yf[a][2], y);
		DrawAnim(IceAnim[7], x + xf[a][3], y + yf[a][3], y);
	}
		
	// Check if it hits a wall or somthing else
	CollisionTest();
}


//----------------------------------------
// Name: CollisionTest()
//----------------------------------------
CollisionTest()
{
	// Check if this has hit an enemy, if it has then set the enemy on fire.
	if (CheckForEnemies())
	{	
		hitSomthing = true;
		HitCounter = 0.00;
	}
			
	// Test for a collision on the mask layer
	if (AngleCollide("this", 3, 3, 126, 0, 0, 0))
	{
		hitSomthing = true;
		HitCounter = 0.00;
	}
}

//----------------------------------------
// Name: CheckForEnemies()
//----------------------------------------
CheckForEnemies()
{
	new x = GetX("this");
	new y = GetY("this");
	new temp[20];
	
	// Go to the start of the Entity List
	StartEntity(40, x, y);
	do
	{
		ToString(GetCurrentEntity(), temp);
		
		// Check this entity is an enemy
		if ( GetType( temp ) == enemyType && !isDead( temp ) )
		{
			// Check this entity is near the fireball
			if (CollidePoint(temp, x, y))
			{
				// Freeze the enemy
				CallFunction( temp, false, "HitByWeapon", "snnn", "ice", 50, x, y);
				return true;
			}
		}
	}while( NextEntity() )
	return false;
}

//----------------------------------------
// Name: EndEntity()
//----------------------------------------
EndEntity()
{
	// Delete the animations used in this entity
	new n;
	for ( n = 0; n < 8; n+=1 )
		DeleteAnim(IceAnim[n]);			
	
	DeleteEntity("this");
}