/*************************************************************************
 * teethenemy.zes Entity Script
 * 
 * Author: KC
 * Date:   March 2003
 *
 * Desc:   Teeth enemy
 *		   hops 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] = "teeth1s";  // Main Sprite for the enemy
new DeadAnim[20];	     		// String holds animation identifier of death animation
new walksw[20];
new walkse[20];
new walknw[20];
new walkne[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;		     		// Collision rectangle adjustment value

// Variables used for bouncing
new float: BounceVelocity = 80.00;
new float: BounceAmount = 1.00;
new float: gravity = 1100.00;
new float: timer = 0.00;
new numval;


//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		CreateAnim(3, walksw);
		
		AddAnimframe(walksw, 0, 0, "teeth2s");  // walking southwest
		AddAnimframe(walksw, 0, 0, "teeth3s");
		
		CreateAnim(3, walkse);
		
		AddAnimframe(walkse, 0, 0, "teeth1s");  // walking southwest
		AddAnimframe(walkse, 0, 0, "teeth4s");	
		
		CreateAnim(3, walknw);
		
		AddAnimframe(walknw, 0, 0, "teeth2n");  // walking southwest
		AddAnimframe(walknw, 0, 0, "teeth4n");
		
		CreateAnim(3, walkne);
		
		AddAnimframe(walkne, 0, 0, "teeth1n");  // walking southwest
		AddAnimframe(walkne, 0, 0, "teeth3n");
		
		LastImage = walkse;
		numval = 1;
		
		// Create the Death Animation using the enemy library - nmelib
		CreateAnim(6, 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", 65);   
		SetDamage("this", 100);
		SetHealth("this", 100);
		SetMaxHealth("this", 100);
		SetActiveFlag("this", true);
		SetState("this", standing);
		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:
			CallFunction("_enemylib", true, "Fall", "snn", FallAnim, GetWidth("teeth1s"), GetHeight("teeth1s"));
		case stunned:
			Stunned();
		case frozen:
			Freeze();
	}
		
	// Check for holes in the ground
	CallFunction("_enemylib", true, "CheckForHoles", "s", FallAnim);
}

//----------------------------------------
// Name: Walk()
//----------------------------------------
Walk()
{	
		
	// Get the width and height of the Current animation
	new width  = GetAnimWidth(walksw);
	new height = GetAnimHeight(walksw);
	SetState("this", walking);
	
	// Move the Enemy
	if (GetPauseLevel() == 0)
	{	
		// Decrease the bounce velocity
		if(BounceAmount > 0)
		{
			BounceVelocity -= (gravity * GetTimeDelta());
			BounceAmount += (BounceVelocity * GetTimeDelta());
		}
		// Check if the enemy hits the floor yet after bouncing
		if ( BounceAmount <= 0 )
		{
			// Start a new bounce
			BounceAmount = 1.00;
			BounceVelocity = 80.00;
		}
		
		if(timer < 1.4)
		{
			if(numval == 1)
			{
				SetMoveAngle("this", 45);
			}
			else if(numval == 2)
			{
				SetMoveAngle("this", 135);
			}
			else if(numval == 3)
			{
				SetMoveAngle("this", 225);
			}
			else if(numval == 4)
			{
				SetMoveAngle("this", 315);
			}
			timer += GetTimeDelta();
			// Check for Collisions - if there are none then move the enemy
			if (!AngleCollide("this", 6, 5, 240, true, width / 2, 24))
			{
				AngleMove("this");	
			}
		}
		else if(timer >= 1.4)
		{
			SetState("this", standing);
			timer = 0.00;
			Stand(false);
		}
	}
	
	// Draw the enemy
	new x = GetX("this");
	new y = GetY("this");
	if (isVisible("this"))
	{
		if(GetMoveAngle("this") >= 180 && GetMoveAngle("this") < 270)
		{
			PutSprite("teeth4s", x, y - floatround(BounceAmount), y + height);
			LastImage = walkse;
		}
		else if(GetMoveAngle("this") >= 270 && GetMoveAngle("this") < 360)
		{
			PutSprite("teeth3s", x, y - floatround(BounceAmount), y + height);
			LastImage = walksw;
		}
		else if(GetMoveAngle("this") >= 0 && GetMoveAngle("this") < 90)
		{
			PutSprite("teeth4n", x, y - floatround(BounceAmount), y + height);
			LastImage = walknw;
		}
		else if(GetMoveAngle("this") >= 90 && GetMoveAngle("this") < 180)
		{
			PutSprite("teeth3n", x, y - floatround(BounceAmount), y + height);
			LastImage = walkne;
		}
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 15, 2);
	}
	
	// Set a collision rectangle around the Enemy
	SetCollisionRect("this", 0, false, x + adj, y + 8 + 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");
	
	// Draw the enemy and its shadow
	if (isVisible("this"))
	{
		if (justDraw)
		{
			DrawAnimNoInc(LastImage, x, y, y + height);
		}
		else
		{
			DrawAnim(LastImage, x, y, y + height);
			if(random(500) == 1)
			{
				SetState("this", walking);
				numval = random(4);
				Walk();
			}
		}
		
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 16, 2);
	}
	
	// Set a collision rectangle around the Enemy
	SetCollisionRect("this", 0, false, x + adj, y + 8 + adj, x + width - adj, y + height - adj);
}

//----------------------------------------
// 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 = float(damage);
		SetState("this", stunned);
		return;
	}
	
	// Check if this enemy was hit by a fire weapon
	if ( !strcmp( wtype, "fire" ) )
	{
		// fire attacks harm armos a lot - crank up the damage
		// but go into a hit state instead of a burning state
		damage = 200;
	}
	
	// Check if this enemy was hit by an ice weapon
	if ( !strcmp( wtype, "ice" ) )
	{
		// Put this enemy on ice
		CallFunction("_enemylib", true, "BeginFreeze", "s", MainImage);
		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  = GetAnimWidth(walksw);
	new height = GetAnimHeight(walksw);
	
	// 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(GetMoveAngle("this") >= 180 && GetMoveAngle("this") < 270)
		{
			PutSprite("teeth4s", x,  y - floatround(BounceAmount), y + height, 0, 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(GetMoveAngle("this") >= 270 && GetMoveAngle("this") < 360)
		{
			PutSprite("teeth3s", x,  y - floatround(BounceAmount), y + height, 0, 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(GetMoveAngle("this") >= 0 && GetMoveAngle("this") < 90)
		{
			PutSprite("teeth4n", x,  y - floatround(BounceAmount), y + height, 0, 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(GetMoveAngle("this") >= 90 && GetMoveAngle("this") < 180)
		{
			PutSprite("teeth3n", x,  y - floatround(BounceAmount), y + height, 0, 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 - 15, 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, MainImage);   
	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);
	BounceAmount = 1.00;
	
	if (isVisible("this"))
	{
		PutSprite(MainImage, x, y, y + height, 0, 100, 100, 255);
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 15, 2);
	}
}