/*************************************************************************
 *  block1f.zes Entity Script
 * 
 * Author: KC
 * Date:   5 February 2003
 *
 * Desc:   A script for a fireblock, that can be destroyed with the ice rod.
 *
 *         NOTE: This Script REQUIRES the enemy library entity: _enemylib
 *         to be included in the project.
 *
 * Usage:  
 *
 * Sprites: bszeldasprites.spt
 *         
 ************************************************************************/
#include <entity>
#include <general>
#include <float>
#include <core>
#include <counter>
#include <animation>


new walks[20];
new DeadAnim[20];	     // String holds animation identifier of death animation
new FallAnim[20];	     // String holds animation identifier of falling animation
new LastImage[20];       // holds sprite code of the last drawn image
new float: HitCount;	 // Counter used when the enemy has been hit

//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		// Create the animations
		CreateAnim(6, walks);
		
		// Add Frames to the walking animations
		AddAnimframe(walks, 0, 0, "block1f");  // walking south
		AddAnimframe(walks, 0, 0, "block2f");
		LastImage = "block1f";
		
		// 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", 220);
		SetType("this", enemyType);
		SetSpeed( "this",0);   
		SetDamage("this", 50);
		SetHealth("this",1);	
	}
	
	new width  = GetAnimWidth(walks);
	new height = GetAnimHeight(walks);	
	
	if (!isActive("this") || isDead("this"))
		return;
		
	// Check for a collision with the player
	CallFunction("_enemylib", true, "CheckForPlayer", "NULL");		
	if(Collide("this","player1"))
	{
	CallFunction("player1", false, "BeginKnockBack", "nnnn", GetX("this"), GetY("this"), 0, 0);
	}		
	
	// Call a function for the enemy depending on its state
	switch( GetState("this") )
	{
		case standing:
			Stand(false)
		case walking:
			Stand(false)
		case hit:
			Hit();
		case dying:
			Die();
		case falling:
			CallFunction("_enemylib", true, "Fall", "snn", FallAnim, GetWidth("block1f"), GetHeight("block1f"));
		case burning:
			Stand(false)
		case stunned:
			Stand(false)
		case frozen:
			Freeze();
	}
}
//----------------------------------------
// Name: Walk()
//----------------------------------------
Walk()
{
}
//----------------------------------------
// Name: Stand()
//----------------------------------------
Stand( justDraw )
{
	// Get the width and height of the Current animation
	new width  = GetAnimWidth(walks);
	new height = GetAnimHeight(walks);
	new x = GetX("this");
	new y = GetY("this");
	
	// Draw the enemy and its shadow
	if (isVisible("this"))
	{
		if (justDraw)
			DrawAnimNoInc(walks, x, y, y + height);
		else  
			DrawAnim(walks, x, y, y + height);
		
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 15, 2);
		// Set a collision rectangle around the Enemy
		SetCollisionRect("this", 0, false, x + 1,y + 1, x + width - 2,y + height - 1);	
	}

}

//----------------------------------------
// Name: HitByWeapon(wtype[], damage, x, y)
//----------------------------------------
public HitByWeapon(wtype[], damage, x, y)
{
	new state = GetState("this");
	if (state == hit || state == dying)
		return;
	
	// Check if this enemy was hit by a weapon that can stun
	if ( !strcmp( wtype, "stun" ) && state != stunned && state != frozen )
		return;
	
	// Check if this enemy was hit by a fire weapon
	if ( !strcmp( wtype, "fire" ) )
		return;
	
	// Check if this enemy was hit by an ice weapon
	if ( !strcmp( wtype, "ice" ) )
	{
		// Put this enemy on ice
		CallFunction("_enemylib", true, "BeginFreeze", "s", LastImage);
	}
	if ( !strcmp( wtype, "sword" ) )
		return;		
}

//----------------------------------------
// Name: Hit()
//----------------------------------------
Hit()
{
}
//----------------------------------------
// 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);
	ClearCollisionRect("this",0);
}

//----------------------------------------
// Name: Freeze()
//----------------------------------------
Freeze()
{
	// Draw the enemy in a standing position, but draw them blue
	new x = GetX("this");
	new y = GetY("this");
	new width  = GetAnimWidth(walks);
    new height = GetAnimHeight(walks);
	
	if (isVisible("this"))
	{
		DrawAnimNoInc(walks, x, y, y + height, 0, 100, 100, 255);
		PutSprite("shadow1", (x + width / 2) - 8, y + height - 15, 2);
	}
	CallFunction("_enemylib", true, "KillEnemy", "NULL");
}
