/*************************************************************************
 * bbirde#.zes.zes Entity Script
 * 
 * Author: KC
 * Date:   March 2003
 *
 * Desc:   blackbird
 *		   flies around after you.
 *
 *         NOTE: This Script REQUIRES the enemy library entity: _enemylib
 *         to be included in the project.
 *
 * Usage:  Just put him somewhere
 *			
 *
 * Sprites: _EnemySheet1.spt
 *         
 ************************************************************************/
#include <entity>
#include <general>
#include <animation>
#include <float>
#include <core>

//   Global Data
new MainImage[20];  // Main Sprite for the enemy
new DeadAnim[20];	     		// String holds animation identifier of death animation
new walkw[20];
new walke[20];
new FallAnim[20];	     		// String holds animation identifier of falling animation
new LastImage[20];       		// holds sprite code of the last drawn image
new float: StunCount;
new float: HitCount;	 		// Counter used when the enemy has been hit
new adj = 4;
new param;	

new float: flytime = 0.00;
new float: timer = 0.00;


//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		param = GetParam("this");
		CreateAnim(16, walkw);
		CreateAnim(16, walke);
		
		if( param == '1')
		{
			AddAnimframe(walkw, 0, 0, "bbirdw2");  // walking west
			AddAnimframe(walkw, 0, 0, "bbirdw3");
			
			AddAnimframe(walke, 0, 0, "bbirde2");  // walking east
			AddAnimframe(walke, 0, 0, "bbirde3");
			LastImage = "bbirde1";
			MainImage = "bbirde1";	
		}
		else if( param == '4')
		{
			AddAnimframe(walkw, 0, 0, "bbirdw5");  // walking west
			AddAnimframe(walkw, 0, 0, "bbirdw6");
			
			AddAnimframe(walke, 0, 0, "bbirde5");  // walking east
			AddAnimframe(walke, 0, 0, "bbirde6");
			LastImage = "bbirde4";
			MainImage = "bbirde4";
		}
		else if( param == '7')
		{
			AddAnimframe(walkw, 0, 0, "bbirdw8");  // walking west
			AddAnimframe(walkw, 0, 0, "bbirdw9");
			
			AddAnimframe(walke, 0, 0, "bbirde8");  // walking east
			AddAnimframe(walke, 0, 0, "bbirde9");
			LastImage = "bbirde7";
			MainImage = "bbirde7";	
		}

		// Create the Death Animation using the enemy library - nmelib
		CreateAnim(8, DeadAnim);
		CallFunction("_enemylib", true, "CreateDeathAnim", "s", DeadAnim );
		
		// Create the Falling Animation using the enemy library
		CreateAnim(8, FallAnim); 
		CallFunction("_enemylib", true, "CreateFallAnim", "s", FallAnim );
		
		// Set some general parameters
		SetActiveDist("this", 320);
		SetType("this", enemyType);
		SetSpeed( "this", 0 );   
		SetDamage("this", 100);
		SetHealth("this", 100);
		SetMaxHealth("this", 100);
		SetActiveFlag("this", true);
		SetVisibleFlag("this", true);
		Stand(false);
		
	}
	
	if (isDead("this"))
		return;
	
	// Check for a collision with the player
	CallFunction("_enemylib", true, "CheckForPlayer", "NULL");
	
	// Call a function for the enemy depending on its state
	switch( GetState("this") )
	{
		case standing:
			Stand(false);
		case walking:
			Walk();
		case hit:
			Hit();
		case dying:
			Die();
		case falling:
			Hover();
		case stunned:
			Stunned();
		case frozen:
			Freeze();
	}
}

//----------------------------------------
// Name: Walk()
//----------------------------------------
Walk()
{	
		
	// Get the width and height of the Current animation
	new width  = GetAnimWidth(walkw);
	new height = GetAnimHeight(walkw);
	SetState("this", walking);
	
	// Move the Enemy
	if (GetPauseLevel() == 0)
	{
		if(NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 60) && !NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 55))
		{
			flytime = 40.00;
		}
		else if(NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 55) && !NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 45))
		{
			flytime = 60.00;
		}
		else if(NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 45) && !NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 35))
		{
			flytime = 70.00;
		}
		else if(NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 35) && !NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 20))
		{
			flytime = 85.00;
		}
		else if(NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 20) && !NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 5))
		{
			flytime = 100.00;
		}
		else if(NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 5))
		{
			flytime = 110.00;
		}
		if(!NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 60))
		{
			SetMoveAngle("this", CalculateAngle(GetX("this"), GetY("this"), GetX("player1"), GetY("player1") ));
		}
		AngleMove("this");	
	}
	
	// Draw the enemy
	new x = GetX("this");
	new y = GetY("this");
	if (isVisible("this"))
	{
		if(GetMoveAngle("this") >= 0 && GetMoveAngle("this") < 90)
		{
			DrawAnim(walkw, x, y , y + height,99999, 255, 255, 255);
			if(param == '1')
			{
				LastImage = "bbirdw2";
			}
			else if(param == '4')
			{
				LastImage = "bbirdw5";
			}
			else if(param == '7')
			{
				LastImage = "bbirdw8";
			}
		}
		if(GetMoveAngle("this") >= 90 && GetMoveAngle("this") < 270)
		{
			DrawAnim(walke, x, y , y + height,99999, 255, 255, 255);
			if(param == '1')
			{
				LastImage = "bbirde2";
			}
			else if(param == '4')
			{
				LastImage = "bbirde5";
			}
			else if(param == '7')
			{
				LastImage = "bbirde8";
			}
		}
		if(GetMoveAngle("this") >= 270 && GetMoveAngle("this") < 360)
		{
			DrawAnim(walkw, x, y , y + height,99999, 255, 255, 255);
			if(param == '1')
			{
				LastImage = "bbirdw2";
			}
			else if(param == '4')
			{
				LastImage = "bbirdw5";
			}
			else if(param == '7')
			{
				LastImage = "bbirdw8";
			}
		}
		PutSprite("shadow1", (x + width / 2) - 8, y + height, 2);
		SetSpeed("this", floatround(flytime));
	}
	
	// Set a collision rectangle around the Enemy
	SetCollisionRect("this", 0, false, x + adj, y + adj, x + width - adj, y + height - adj);
}

//----------------------------------------
// Name: Stand()
//----------------------------------------
Stand(justDraw)
{
	// Get the width and height of the Current animation
	new width  = GetWidth(MainImage);
	new height = GetHeight(MainImage);
	new x = GetX("this");
	new y = GetY("this");
	
	if(justDraw)
	{
		PutSprite(LastImage, x, y, y + height);
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 4, 2);
	}
	else
	{
		// Draw the enemy and its shadow
		if (isVisible("this"))
		{
			PutSprite(LastImage, x, y + 10, y + height,99999);
			PutSprite("shadow1", (x + width / 2) - 8, y + height - 4, 2);
		}
		if(NearPoint(GetX("this"), GetY("this"), GetX("player1"), GetY("player1"), 70))
		{
			SetState("this", falling);
			Hover();
		}
	}
	
	// Set a collision rectangle around the Enemy
	SetCollisionRect("this", 0, false, x + adj, y + 10 + adj, x + width - adj, y + 10 + height - adj);
}

//----------------------------------------
// Name: Hover()
//----------------------------------------
Hover()
{
	new width  = GetWidth(MainImage);
	new height = GetHeight(MainImage);
	new x = GetInitialX("this");
	new y = GetInitialY("this");
	if(timer < 12)
	{
		timer += 12*GetTimeDelta();
		DrawAnim(walke, x, y + 10 - floatround(timer), y + height,99999,255,255,255);
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 4, 2);
	}
	if(timer >= 12)
	{
		SetState("this", walking);
		Walk();
		timer = 0.00;
	}
}
	

//----------------------------------------
// Name: HitByWeapon(wtype[], damage, x, y)
//----------------------------------------
public HitByWeapon(wtype[], damage, x, y)
{
		
	new state = GetState("this");
	if (state == hit || state == dying || state == burning)
		return;
		
	// Check if this enemy was hit by a weapon that can stun
	if ( !strcmp( wtype, "stun" ) && state != stunned && state != frozen)
	{
		StunCount = 32.00;
		SetState("this", stunned);
		return;
	}
	
	// Check if this enemy was hit by an ice weapon
	if ( !strcmp( wtype, "ice" ) )
	{
		return;
	}
	
	HitCount = 0.00;
	CallFunction("_enemylib", true, "BeginHit", "nnn", damage, x, y );
}

//----------------------------------------
// Name: Hit()
//----------------------------------------
Hit()
{
	new colors[5][3] = { {19,125,19}, {253,211,65}, {225,88,5}, {32,211,238}, {238,32,32} };
	new width  = GetWidth(MainImage);
	new height = GetHeight(MainImage);
	
	// Move the enemy if the game is completely unpaused
	if (GetPauseLevel() == 0)
	{
		AngleMove("this");
		AngleCollide("this", 5, 5, 126, 0, width / 2, height / 2);
	}
	new x = GetX("this");
	new y = GetY("this");
	
	// Draw the enemy with different shades of colour becuase they have been hit
	if (isVisible("this"))
	{
		if(x < GetX("player1"))
		{
			DrawAnim(walke, x,  y , y + height, 99999, colors[ floatround(HitCount * 20.0) % 5 ][0], \
		                           			 colors[ floatround(HitCount * 20.0) % 5 ][1], \
		                           			 colors[ floatround(HitCount * 20.0) % 5 ][2], 255, 0, 100);
		}
		if(x >= GetX("player1"))
		{
			DrawAnim(walkw, x,  y , y + height, 99999, colors[ floatround(HitCount * 20.0) % 5 ][0], \
		                           			 colors[ floatround(HitCount * 20.0) % 5 ][1], \
		                           			 colors[ floatround(HitCount * 20.0) % 5 ][2], 255, 0, 100);
		}			
		PutSprite("shadow1", (x + width / 2) - 8, y + height, 2);
	}
	
	// Check the hit counter, if it goes high enough then end this hit state
	HitCount += GetTimeDelta();
	if (HitCount >= 0.23)
	{
		// Leave the Hit state
		SetState("this", walking); 
		SetSpeedMod("this", 0);
	}
}

//----------------------------------------
// Name: Die()
//----------------------------------------
Die()
{		
	// Draw the enemy standing still
	if (GetAnimCount(DeadAnim) < 5)
		Stand(true);   	
	
	// Overlay the death animation over the enemy
	CallFunction("_enemylib", true, "HandleDying", "ss", DeadAnim, LastImage);   
	SetHealth("this", 0);
	SetActiveFlag("this", false);
}

//----------------------------------------
// Name: Stunned()
//----------------------------------------
Stunned()
{
	// Display the enemy stood still
	Stand(true);
	
	// decrement the Stun counter
	StunCount -= 10 * GetTimeDelta();
	CallFunction("_enemylib", true, "Stunned", "n", floatround(StunCount));
}

//----------------------------------------
// Name: Freeze()
//----------------------------------------
Freeze()
{
	// Draw the enemy in a standing position, but draw them blue
	new x = GetX("this");
	new y = GetY("this");
	new width  = GetWidth(MainImage);
	new height = GetHeight(MainImage);
	
	if (isVisible("this"))
	{
		PutSprite(MainImage, x, y, y + height, 0, 100, 100, 255);
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 15, 2);
	}
}