/************************************************************************
 * level7_door1# Entity Script
 * 
 * Author: GD
 * Date:   22/01/02
 *
 * Desc:   A normal Green Dungeon Door Script
 *
 * 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.
 *
 * Sprites: level7_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 param;			// Will hold the parameter passed to this script
new xoffset = 0;
new yoffset = 0;
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 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, "level7_door1n");
         doorArch = "level7_door1na";
      }
      else if ( param == 'e' )  // East
      {
         SetDirection("this", east);
	 	 AddAnimframe(doorAnim, 0, 0, "level7_door1e");
         doorArch = "level7_door1ea";
         xoffset = 16;
      }
      else if ( param == 's' )  // South
      {
         SetDirection("this", south);
	 	 AddAnimframe(doorAnim, 0, 0, "level7_door1s");
         doorArch = "level7_door1sa";
         yoffset = 16;
      }
      else if ( param == 'w' )  // West
      {
         SetDirection("this", west);
	 	 AddAnimframe(doorAnim, 0, 0, "level7_door1w");
         doorArch = "level7_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", true);
      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);

   // 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);  
}

