/************************************************************************
* level1_door2# Entity Script
* 
* Author: GD
* Date:   15 June 2002
*
* Desc:   A green door script which can be opened or closed, uses a parameter
*		   so it can be one of 4 directions.
*
* 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 door
*
*
* Sprites: level1_Dungeon.spt
*         
*         
***********************************************************************/
#include <entity>
#include <general>
#include <animation>
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
        if ( param == 'n' )       // North
     	{
	 		 SetDirection("this", north);
        	 AddAnimframe(doorAnim, 0, 0, "level1_door2n");
        	 AddAnimframe(doorAnim, 0, 8, "level1_door2nb");
        	 AddAnimframe(doorAnim, 0, 0, "level1_door1n");
        	 doorArch = "level1_door1na";
      	}
     	else if ( param == 'e' )  // East
     	{
       	  	 SetDirection("this", east);
	 	 	 AddAnimframe(doorAnim, 0, 0, "level1_door2e");
        	 AddAnimframe(doorAnim, 0, 0, "level1_door2eb");
        	 AddAnimframe(doorAnim, 0, 0, "level1_door1e");
       	  	 doorArch = "level1_door1ea";
         	 xoffset = 16;
      	}
        else if ( param == 's' )  // South
        {
        	 SetDirection("this", south);
	 		 AddAnimframe(doorAnim, 0, 0, "level1_door2s");
        	 AddAnimframe(doorAnim, 0, 0, "level1_door2sb");
        	 AddAnimframe(doorAnim, 0, 0, "level1_door1s");
     	     doorArch = "level1_door1sa";
     	     yoffset = 16;
        }
        else if ( param == 'w' )  // West
        {
	         SetDirection("this", west);
		 	 AddAnimframe(doorAnim, 0, 0, "level1_door2w");
        	 AddAnimframe(doorAnim, 8, 0, "level1_door2wb");
        	 AddAnimframe(doorAnim, 0, 0, "level1_door1w");
	         doorArch = "level1_door1wa";
        }
		
		// 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 Arch of the door above everything else
    PutSprite(doorArch, x + xoffset , y + yoffset, 999999);
	
	// Draw the door animation without affecting the animation counter
	DrawAnimNoInc(doorAnim, x, y, 1);
		
	if (isOpen("this"))
	{
		// 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 + 2, y + 2, x + width - 2, y + height - 2);
		ClearCollisionRect("this", 1);
	}
	
	if (Animating)
		Animate();    // Move between closed and open states
	
}

//----------------------------------------
// 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;
			PlaySound("_dooropen.wav");
		}
	}
	else
	{
		SetAnimDirection(doorAnim, 0);     // Make the door animation go backwards
		IncrementAnim(doorAnim);  
		if (GetAnimCount(doorAnim) <= 0)
		{
			SetOpenFlag("this", false);      // Close the door
			Animating = false;
			PlaySound("_doorclose.wav");
		}        
	}
}

