/************************************************************************
* level7_crack1# Entity Script
* 
* Author: GD
* Date:   15 June 2002
*
* Desc:    A green door Crack/Door script, this appears as a crack in a 
*          wall, when exposed to a bomb it crumbles.
*
* Usage:  Place in the Landscape Designer.
*
*         Use SetString() to place the identifier of the door you want this one
*         to lead to into the first string of this entity, i.e:
* 
*		      SetString("thisdoor", 0, "targetdoor");
*
*		  Then it should automatically take you to the target door when you go
*         Through this one.
* 
*         CallFunction("door1", false, "Open", "NULL");   To open the Crack
*
*
*         
***********************************************************************/
#include <entity>
#include <general>
#include <animation>
#include <counter>
#include <core>

new doorAnim[20];	// String to store the Identifier of the door animation
new doorArch[20];   // String to hold the sprite of the Door's Arch
new width;		    // Used to store the widht and height of the door
new height;
new Animating = false;
new xoffset = 0;
new yoffset = 0;
new param;			// Will hold the parameter passed to this script
new x;
new y;

//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		// Get the parameter passed to this script from the sprite code
		param = GetParam("this");
		AllocateStrings("this", 1, 20);
				
		// Create the Main Door Animation
		CreateAnim(8, doorAnim);
		
		// Add Frames to Animation depending on what the parameter is
		// Feel free to add more frames to make the animation look better
        if ( param == 'n' )       // North
     	{
	 		 SetDirection("this", north);
        	 AddAnimframe(doorAnim, 4, 0, "level7_crack1nb");
        	 AddAnimframe(doorAnim, 0, 0, "level7_crack1n");
        	 doorArch = "level7_crack1na";
      	}
     	else if ( param == 'e' )  // East
     	{
       	  	 SetDirection("this", east);
	 	 	 AddAnimframe(doorAnim, 0, 4, "level7_crack1eb");
        	 AddAnimframe(doorAnim, 0, 0, "level7_crack1e");
        	 doorArch = "level7_crack1ea";
         	 xoffset = 16;
      	}
        else if ( param == 's' )  // South
        {
        	 SetDirection("this", south);
	 		 AddAnimframe(doorAnim, 4, 0, "level7_crack1sb");
        	 AddAnimframe(doorAnim, 0, 0, "level7_crack1s");
        	 doorArch = "level7_crack1sa";
     	     yoffset = 16;
        }
        else if ( param == 'w' )  // West
        {
	         SetDirection("this", west);
		 	 AddAnimframe(doorAnim, 0, 4, "level7_crack1wb");
        	 AddAnimframe(doorAnim, 0, 0, "level7_crack1w");
        	 doorArch = "level7_crack1wa";
        }
		
		// Set the active distance for this entity
		SetActiveDist("this", 190);
		
		// Set this entity's basic type
		SetType("this", doorType);
		
		// Record the width and height of the main sprite for later use
		width  = GetAnimWidth(doorAnim);
		height = GetAnimHeight(doorAnim);
		x = GetX("this");
        y = GetY("this");
      
		SetOpenFlag("this", false);
		SetInteractingFlag("this", false);
	}
	
	// Draw the door animation without affecting the animation counter
	DrawAnimNoInc(doorAnim, x, y, 1);
		
	if (isOpen("this"))
	{
		// Draw the Arch of the door above everything else
    	PutSprite(doorArch, x + xoffset , y + yoffset, 999999);

		// Create a collsion rectangle around the whole door
		SetCollisionRect("this", 0, false, x, y, x + width, y + height);
		
	    /* Create another collision rectangle inside the door for checking if the 
        player is standing in the doorway */
        SetCollisionRect("this", 1, false, x + xoffset + 3, y + yoffset + 3, \
                         x + xoffset + GetWidth(doorArch) - 3, y + yoffset + GetHeight(doorArch) - 3);
		
		// Check if the Player is standing in the doorway
		if ( CollideAll("this", "player1") )
		{
			SetInteractingFlag("this", true);     // Set the Interacting Flag to true
			CallFunction("_doorlib", true, "MoveToOtherDoor", "NULL");	// Maybe move to another door
		}
		else
			SetInteractingFlag("this", false); 
	}
	else
	{
		// Create a solid collsion rectangle around the whole door
		SetCollisionRect("this", 0, true, x, y, x + width, y + height);
		ClearCollisionRect("this", 1);
	}
	
	if (Animating)
		Animate();    // Move between closed and open states
	
}

//----------------------------------------
// Name: HitByWeapon(wtype[], damage, x, y)
//----------------------------------------
public HitByWeapon(wtype[], damage, x, y)
{
	// Check if an explosion hit the wall
	if (!strcmp( wtype, "explosion"))
		Open();
}

//----------------------------------------
// Name: OpenNow()
//----------------------------------------
public OpenNow()
{
	// opens the crack with no animation 
	SetOpenFlag("this", true);
	SetAnimCount(doorAnim, GetMaxAnim(doorAnim));
	Animating = false;
}

//----------------------------------------
// Name: CloseNow()
//----------------------------------------
public CloseNow()
{
	// closes the crack with no animation 
	SetOpenFlag("this", false);
	SetAnimCount("doorAnim", 0);
	Animating = false;
}
//----------------------------------------
// Name: Open()
//----------------------------------------
public Open()
{
	if (!isOpen("this"))		// You cant open a door which is already open
		Animating = true;
}

//----------------------------------------
// Name: Close()
//----------------------------------------
public Close()
{	
	if (isOpen("this"))         // You cant close a door which is already closed
		Animating = true;
}

//----------------------------------------
// Name: Animate()
//----------------------------------------
Animate()
{
	// Makes the Door open or close
	if (!isOpen("this"))
	{
		SetAnimDirection(doorAnim, 1);     // Make the door animation go fowards
		IncrementAnim(doorAnim);  
		if (GetAnimCount(doorAnim) >= GetMaxAnim(doorAnim))
		{    
			SetOpenFlag("this", true);      // Open the door
			Animating = false;
		}
	}
	else
	{
		SetAnimDirection(doorAnim, 0);     // Make the door animation go backwards
		IncrementAnim(doorAnim);  
		if (GetAnimCount(doorAnim) <= 0)
		{
			SetOpenFlag("this", false);      // Close the door
			Animating = false;
		}        
	}
}

