/*************************************************************************
 * _doorlib Door Library Script
 * 
 * Author: GD
 * Date:   28 June 2002
 *
 * Desc:   A Door Library, contains functions used by doors
 *
 * Usage:  Call these functions from other entity scripts using the
 *         CallFunction() function.
 *
 *         
 ************************************************************************/
#include <entity>
#include <general>
#include <core>

//----------------------------------------
// Name: MoveToOtherDoor()
//----------------------------------------
public MoveToOtherDoor()
{
	// This function is designed to move the player from one door (this entity) to another
	// Door, The other door's identifier should have been set as the first string in this
	// door via SetString("this", 0, "doorid");
	
	// Get the target door from this doors first string
	new TargetDoor[20];
	GetString("this", 0, TargetDoor);

	// Check if the first string is blank
	if ( strlen(TargetDoor) > 1 )
	{
		// We should try and move to another door, the ident of the new door
		// should be in TargetDoor
		SetPauseLevel(1);
		
		if (FadeTo(0, 0, 0, 160))	// Fade to black
		{
			FadeTo(255, 255, 255, 160);
			SetPauseLevel(0);
				
			// Screen is black, get the new position of the player
			GetNewPosition( TargetDoor );
		}
	}
}

//----------------------------------------
// Name: GetNewPosition()
//----------------------------------------
public GetNewPosition( TargetDoor[] )
{
	new x = GetX(TargetDoor);
	new y = GetY(TargetDoor);
	new dir = GetDirection(TargetDoor);
	
	// Get the sprite code of the door
	new DoorSprite[20];
	GetImage(TargetDoor, DoorSprite );
		
	// Work out the Width and Height of the target door from it's sprite
	new width = GetWidth(DoorSprite);
	new height= GetHeight(DoorSprite);
	

	// If the DoorSprite wasnt found then the width and height
	// Will be set to 1, in this case use the defaults for a noral 32x24 door
	// This is why all non-standard sized doors must have their main image
	// set in SetImage()
	if ( width == 1 || height == 1 || width == 0 || height == 0 )
	{
		width = 32;
		height = 24;
	}
	
	// If the entity doesnt exist then the x and y will be set to -999 each
	if ( x == -999 )
		return;
		
	// Adjust the position the player should be placed according to the direction
	// The new door is facing
	if ( dir == north )
	{
		x += width / 2 - 8;
		y += 16;
		SetDirection("player1", south);
	}
	if ( dir == east )
	{
		y += height / 2 - 8;
		SetDirection("player1", west);
	}
	if ( dir == south )
	{
		x += width / 2 - 8;
		y -= 8;
		SetDirection("player1", north);
	}
	if ( dir == west )
	{
		x += 8;
		y += height / 2 - 8;
		SetDirection("player1", east);
	}
		
	// Set the position on the player to the other door
	SetPosition("player1", x, y);
}

