/***********************************************
 * Changes:  
 *	21/05/2004 [lukex]: New file.
 ***********************************************/
#include <animation>
#include <entity>
#include <general>
#include <float>

new param;
new shield = 0;
new autoblock = true; // true = Z3 Style Shield - false = Z4 Style Shield
new active = false;
main()
{
	if (FirstRun())
	{
		SetType("this", itemType);
		param = GetParam("this");
		
		SetOwnedFlag("this", true);
		
		// Create 2 string for the weapons descriptions
		AllocateStrings("this", 2, 64);	
		SetString("this", 0, "You got the level 1 Shield!"); 	// For Chests
		if ( !autoblock )
			SetImage("this", "w_shield1");
		SetActiveDist("this", -2);
		SetActiveInGroups("this", true);
		SetString("this", 1, "Shield lv1"); 				// For menu			

	}
	new x = GetX("player1");
	new y = GetY("player1");
	SetPosition("this", x, y);
		
	CallFunction("console", false, "PrintNumber", "sn", "active", active);
	if ( isVisible("player1") ) 
	{
		new playerstate = GetState("player1");
		
		if ( playerstate == walking || playerstate == standing )
		{
			if ( autoblock )
			{
				DrawShield(x, y, GetDirection("player1"));
			}
			else if ( active )
			{
				DrawInactiveShield(x, y, GetDirection("player1"));
			}
		}
	}
}

//----------------------------------------
// Name: Init()
//----------------------------------------
public Init()
{

}

public MenuSelected()
{
	active = true;
	return -1;
}
public MenuUnselected()
{
	active = false;
	return -1;
}
//----------------------------------------
// Name: DrawWeapon()
//----------------------------------------
public DrawWeapon( keyHeld )
{
	new x = GetX("player1");
	new y = GetY("player1");
	new dir = GetDirection("player1");

	CallFunction("player1", false, "Stand", "nn", x, y);// Draw the Player
	DrawShield(x, y, dir);// Draw the Shield
	
	if ( keyHeld )
		SetState("player1", using);
	else
		SetState("player1", standing);	
}

//----------------------------------------
// Name: DrawWeapon()
//----------------------------------------
public DrawShield(x, y, dir )
{
	new rot;
	new depth = y + 16;
	new shield_image[20];
	
	if ( dir == north)
	{
		shield_image = "w_shield2";
		x += 9;
		depth = y;
	}
	else if ( dir == east)
	{
		shield_image = "w_shield3";
		x += 16;
		y += 2;
		rot = 180;
	}
	else if ( dir == south)
	{
		shield_image = "w_shield1";
		y += 4;
	}
	else if ( dir == west)
	{
		shield_image = "w_shield3";
		x -= 2;
		y += 2;   
	}
	SetCollisionRect("this", 0, false, x - 1, y - 1, x + GetWidth(shield_image) + 1, y + GetHeight(shield_image) + 1 );
	PutSprite(shield_image, x, y, depth, 0, 255, 255, 255, 255, rot );
	CheckForEnemies(x, y);
}
//----------------------------------------
// Name: DrawWeapon()
//----------------------------------------
public DrawInactiveShield(x, y, dir )
{
	
	new xoff;
	new yoff;
	new rot;
	new depth = y + 16;
	new shield_image[20];
	if ( !autoblock )
		dir += 1;
		
	if ( dir == north)
	{
		shield_image = "w_shield2";
		x += 9;
		depth = y;
	}
	else if ( dir == east)
	{
		shield_image = "w_shield3";
		x += 16;
		y += 2;
		rot = 180;
	}
	else if ( dir == south)
	{
		shield_image = "w_shield1";
		y += 4;
	}
	else if ( dir == west)
	{
		shield_image = "w_shield3";
		x -= 2;
		y += 2;   
	}

	PutSprite(shield_image, x, y, depth, 0, 255, 255, 255, 255, rot );
}

CheckForEnemies(x, y)
{
	new temp[20];
	
	// Go to the start of the Entity List
	StartEntity(64, x + 8, y + 8);
	
	// 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 arrow
			if ( Collide("this", temp) )
			{
				CallFunction( temp, false, "HitByWeapon", "snnn", "shield", 200, x - 3, y - 3);
			}
		}
	}while( NextEntity() )
}