/***********************************************
 * _freezeeffect.zes
 * 
 * Author: GD
 * Date:   25 June 2002
 *
 * Desc:   When an enemy is in the frozen state it needs to 'sparkle' small
 *		   sparkly animations appear around the enemy one after the other. Thats
 *		   what this entity does, it can be applied to an area and it will make
 *		   the sparkly effect appear as long as the parent entity is in the frozen
 * 		   state.
 *
 * Usage:  Used by the _enemylib.zes script
 *         
 * Sprites: _weaponsheet.spt
 *
 ***********************************************/
#include <animation>
#include <entity>
#include <general>
#include <core>

new mainAnim[20];
new Parent[20];
new width = 1;
new height = 1;

new CurX = -1;
new CurY = -1;

main()
{
	if (FirstRun())
	{
		// Get the entity who created this
		GetParent("this", Parent);

		// Create the sparkly animation
		CreateAnim(6, mainAnim);
		AddAnimframe(mainAnim, 0, 0, "_icerod10");
		AddAnimframe(mainAnim, 0, 0, "_icerod11");
		AddAnimframe(mainAnim, 0, 0, "__none");
		SetAnimLoop(mainAnim, false);
	}
	else
	{
		if (CurX != -1)
			DrawAnim(mainAnim, CurX - 4, CurY - 4, GetY("this") + height + 4);
		
		// Check if the animation is finsihed
		if (FinishedAnim(mainAnim))
		{
			// Reset the animation and choose a new spot for it
			SetAnimCount(mainAnim, 0);
			GetRandomSpot();	
		}
		
		// If the parent is no longer froxen then delete this entity
		if ( GetState(Parent) != frozen )
			DeleteEntity("this");
	}
}


//----------------------------------------
// Name: SetArea()
//----------------------------------------
public SetArea( x, y, wid, hei )
{
	SetX("this", x);
	SetY("this", y);
	
	width = wid;
	height = hei;
	GetRandomSpot();
}

//----------------------------------------
// Name: GetRandomSpot()
//----------------------------------------
GetRandomSpot()
{
	new border = 2;
	
	// Get a new random position for the sparkle
	CurX = random(width - border*2)  + GetX("this");
	CurY = random(height - border*2) + GetY("this");
	
	CurX += border;
	CurY += border;
}