/************************************************************************
 * level1_entrance#.zes Entity Script
 * 
 * Author: GD
 * Date:   6 July 2002
 *
 * Desc:   A script for any Dungeon Entrance - dungeon entraces
 *		   are how you initially enter dungeons, they are always
 *		   north facing doors, thier script is basically the same
 *		   as a normal 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: _DoorSheet1.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);
      SetDirection("this", south);
      SetType("this", doorType);
      yoffset = 16;
     
      // Create the Main Animation
      CreateAnim(8, doorAnim);
      
      // Add Frames to Animation depending on what the parameter is
      if ( param == 'a' )       // Green Dungeon Entrance
      {
         AddAnimframe(doorAnim, 0, 0, "level1_entrancea");
         doorArch = "level1_entranceaa";
      }
     
      // 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);  
}

