/***********************************************
 * _fireball1.zes
 * 
 * Author: GD
 * Date:   23 June 2002
 *
 * Desc:   Fire ball which travels is a stright line until it hits
 *		   Somthing. If it hits an enemy it will set the enemy on fire.
 * 		   When the fireball hits somthing it creates another entity
 *		   from _fire1.zes which does the actual burning animation.
 *
 * Usage:  Use with the Fire Rod weapon
 *
 * Sprites: _WeaponSheet1.spt
 *         
 ***********************************************/
#include <entity>
#include <general>
#include <float>

new float: fireCount = 0.00;
new float: timeActive = 0.00;

main()
{
	if (FirstRun())
	{	
		SetSpeed("this", 220);
		SetActiveDist("this", -2);
	}
	
	// Advance the counter
	fireCount += 120 * GetTimeDelta();
	if (fireCount >= 40)
		fireCount = 0.00;
		
	// Dont allow this entity to live for too long if it doesnt hit anything
	timeActive += 1 * GetTimeDelta();
	if ( timeActive > 5)
		DeleteEntity("this");
		
	MoveFireball();
}

//----------------------------------------
// Name: MoveFireball()
//----------------------------------------
MoveFireball()
{
	// Move the fireball
	new dir = GetDirection("this");
	if ( dir == north )
		SetMoveAngle("this", 90);
	else if ( dir == east )
		SetMoveAngle("this", 180);
	else if ( dir == south )
		SetMoveAngle("this", 270);
	else if ( dir == west )
		SetMoveAngle("this", 0);
	AngleMove("this");
	
	// Draw the fireball
	new xpos[3];
	new ypos[3];
	new x = GetX("this");
	new y = GetY("this");
	
	// Set the positions of the 3 sprites according to the counter
	if (fireCount < 10)
	{
		xpos = { 0, 8, 0};
		ypos = { 0, 5, 10};
	}
	else if (fireCount < 20)
	{
		xpos = { 5, 0, 9};
		ypos = { 0, 5, 10};
	}
	else if (fireCount < 30)
	{
		xpos = { 5, 0, 9};
		ypos = { 6, 4, 0};
	}
	else
	{
		xpos = { 0, 8, 0};
		ypos = { 6, 4, 0};
	}
	
	if ( isVisible("this") )
	{
		PutSprite("_firerod10", x + xpos[2], y + ypos[2], y + 8);	
		PutSprite("_firerod11", x + xpos[1], y + ypos[1], y + 8);	
		PutSprite("_firerod12", x + xpos[0], y + ypos[0], y + 8);	
	}
	
	// Check if it hits a wall or somthing else
	CollisionTest();
}


//----------------------------------------
// Name: CollisionTest()
//----------------------------------------
CollisionTest()
{
	new entity[20];
	
	// Check if this has hit an enemy, if it has then set the enemy on fire.
	if (CheckForEnemies())
		DeleteEntity("this");
			
	// Test for a collision on the mask layer
	if (AngleCollide("this", 3, 3, 126, 0, 8, 8))
	{
		// Create a Burning fire entity
		CreateEntity("_fire1a", GetX("this"), GetY("this"), entity);
		DeleteEntity("this");
	}
}

//----------------------------------------
// Name: CheckForEnemies()
//----------------------------------------
CheckForEnemies()
{
	new x = GetX("this") + 6;
	new y = GetY("this") + 6;
	new temp[20];
	
	// Go to the start of the Entity List
	StartEntity(40, x, y);
	
	// Loop through all the entities within a certain distance
	do
	{
		ToString(GetCurrentEntity(), temp);
		
		// Check this entity is an enemy
		if ( GetType( temp ) == enemyType && !isDead( temp ) && isActive( temp ))
		{
			// Check this entity is near the fireball
			if (CollidePoint(temp, x, y))
			{
				// Set this enemy on fire!
				CallFunction( temp, false, "HitByWeapon", "snnn", "fire", 150, x - 6, y - 6);
				return true;
			}
		}
	}while( NextEntity() )
	return false;
}