/***********************************************
 * i_lantern#.zes	lantern script
 * 
 * Author: Luke X 
 * Date:   31st December 1969
 *
 * Desc:   
 *
 * Usage:  
 *     
 * Sprites: i_lantern.spt
 *         
 ***********************************************/
#include <entity>
#include <general>
#include <counter>

new magicNeeded = 0;		// magic needed to fire the weapon
		
	
main()
{
	if (FirstRun())
	{
		// Set this entity's basic type
		SetType("this", weaponType);
		// Create 2 string for the item descriptions
		AllocateStrings("this", 2, 100);
		SetString("this", 0,  "You got the lantern! Now you can see in the dark");		// For Chests
		SetString("this", 1,  "Lantern");	// For menu
		SetImage("this", "i_lantern");
		SetOwnedFlag("this", true);
		magicNeeded = 1;
	}
	SetActiveDist("this", -1); 

		
}

//----------------------------------------
// Name: Init()
//----------------------------------------
public Init()
{
	
    if (magicNeeded > 0)
    {
	    // Make sure we have enough magic to do this
	    if ( GetCounterValue( "magicMeter" ) < magicNeeded )
	    {
	    	// Player does have enough magic
	    	PlaySound("_error.wav", 240);
	    	SetState("player1", standing);
	    	return;	
	    }
	    else
	    {
	    	// Decrease magic level
	    	IncCounterTarget("magicMeter", -magicNeeded);	
	    }
    }
	   
		
    // Set the Position and Direction of this entity to match the player's
    SetX("this", GetX("player1"));
    SetY("this", GetY("player1"));
    SetDirection("this", GetDirection("player1"));
    PlaySound("_fireball.wav");
    
    CreateFlame();
}

CreateFlame()
{
	new entity[20];
	new x = GetX("player1");
	new y = GetY("player1");
	new dir = GetDirection("player1");
	
	if ( dir == north )
	{
		y -= 18;
	}
	else if ( dir == east )
	{
		x += 18;
	}
	else if ( dir == south )
	{
		y += 18;
	}
	else if ( dir == west )
	{
		x -= 18;
	}
	
	// Create a flame entity
   CreateEntity("_flame", x, y, entity);
	SetDirection(entity, dir);
}

//----------------------------------------
// Name: DrawWeapon()
//----------------------------------------
public DrawWeapon()
{
	// Get the Correct Animation
	new dir = GetDirection("this");

   new x = GetX("this");
   new y = GetY("this");
   new head[20];
   new body[20];

   if ( dir == north )
   {
		body = "__pstn1";
		head = "___phn1";
   }
   else if ( dir == east )
   {
		body = "__pste1";
		head = "___phe1";
   }
   else if ( dir == south )
   {
   	body = "__psts1";
		head = "___phs1";
   }
   else if ( dir == west )
   {
   	body = "__pstw1";
		head = "___phw1";
   }
	new xa = GetX("player1");
	new string[16];

	// Draw the Player animation
	if (isVisible("player1"))
	{
		// Draw the Player
		PutSprite(body, x, y, y + 16);
		PutSprite(head, x, y - 8, y + 16);
		PutSprite("shadow1", x, y, 2);
   }

   SetState("player1", standing);
}

public draw_light(){
   new dir = GetDirection("player1");
	new xa = GetX("player1");
	new ya = GetY("player1");
	new rot;
	 
 	xa = (xa - GetWorldX()) * 2;
	ya = (ya - GetWorldY()) * 2;

	if ( dir == south ) 
	{
		rot = 0;
		ya += 24;
		xa -= 14;
	}

	else if ( dir == west )
	{
		rot = 90;
		xa -= 52;
		ya -= 14;
	}
	else if ( dir == north )
	{
		rot = 180;
		ya -= 64;
		xa -= 14;
	}
	else if ( dir == east )
	{
		rot = 270;
		xa += 24;
		ya -= 14;
	}
   
	//DrawImage("o_lamplight", xa, ya, 255, 255, 255, 160, rot, 100);
	//DrawImage("o_lamplight", xa, ya, 255, 255, 255, 128, rot, 200);
	if ( GetCounterValue("darkness") > 0  ) {
		DrawImage("o_lampdark", xa, ya, 255, 255, 255, 255, rot, 200);
		//above
		DrawRectangle(0, 0, 640, ya - 32, 0, 0, 0);
		//below
		DrawRectangle(0, ya + 96, 640, 480, 0, 0, 0);
		//left
		DrawRectangle(0, ya - 32, xa - 32, ya + 96, 0, 0, 0);
		//right
		DrawRectangle(xa + 96, ya - 32, 640, ya + 96, 0, 0, 0);
	}

}