/***********************************************
 * _d5switch1 Entity Script
 * 
 * Author: G8orByte
 * Date:   Jun.19.2002
 *
 * Desc:   A floor Switch Script
 *
 * Usage:  If isActive, then sets the isPushed flag
 *         depending on whether the player is
 *         standing on it.
 *
 *         Use isPushed() to see if this switch is currently pressed.
 *         
 * Sprites: _D5SwitchSheet1.spt
 *
 ***********************************************/
#include <entity>
#include <general>
#include <float>
#include <counter>

//==================================
//   Global Data
//==================================
new UpSpr[20] = "_d5switch1";
new DnSpr[20] = "_d5switch1a";
new x;
new y;
new width;
new height;
new onswitch = false;
new delay = false;
new float: delaycount = 0.00;
new showup = false;

//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		// Set the active distance for this entity
		SetActiveDist("this", 320);

		// Set this entity's basic type
		SetType("this", otherType);

		// Record the width and height of the main sprite for later use
		width  = GetWidth(UpSpr);
		height = GetHeight(UpSpr);
		SetImage("this", UpSpr);
	}

	if (!onswitch)
		showup = false;

	// Draw the Switch if visible
	if (isVisible("this"))
	{
		x = GetX("this");
		y = GetY("this");
		if (delay)						//during delay
			PutSprite(DnSpr, x, y, 1);
		else if (showup)				//while still on the switch
			PutSprite(UpSpr, x, y, 1);
		else if (!isPushed("this"))
			PutSprite(UpSpr, x, y, 1);
		else
			PutSprite(DnSpr, x, y, 1);
	}

	if (isActive("this"))
	{
		x = GetX("this");
		y = GetY("this");
		// Check if the Player is over the switch
		if (NearPoint(x, y - (height / 3), GetX("player1"), GetY("player1"), width / 3))
		{
			if (!onswitch)
			{
				PlaySound("_d5switch1.wav", 240);
				SetPauseLevel(1);
				// Move Link down 2 pixels, like he stepped on it
				SetY("player1", GetY("player1") + 2);
				delay = true;
			}
			onswitch = true;
		}
		else
		{
			SetPushedFlag("this", false);
			if (!delay)
				onswitch = false;
		}
	}

	// Delay before setting to pushed
	if (delay)
	{
		delaycount += 2 * GetTimeDelta();
		if (delaycount > 1)
		{
			SetPauseLevel(0);
			SetPushedFlag("this", true);
			// Move Link back up 2 pixels
			SetY("player1", GetY("player1") - 2);
			delaycount = 0.00;
			delay = false;
			showup = true;  //show switch in up position
		}
	}
}
