
/**********************************************************
* _itemrupee#.zes Entity Script
* 
* Author: GD
* Date:   16 June 2002
*
* Desc:   A rupee item which can be dropped or placed in a 
*		   Chest etc.. it uses a parameter to distinguise the
*         colour of the rupee.
*		   g = green, r = red, b = blue, p = purple
*
* Usage:  use in chests, with the _itemlib library or place
*         on its own.
*
* Sprites: _ItemSheet1.spt
*         
**********************************************************/
#include <entity>
#include <general>
#include <counter>
#include <float>
#include <animation>

new itemAnim[20];   			// String to store the Identifier of the rupee animation
new bouncing = false;
new initialBounce;
new willTimeOut = false;
new TempBounce;    
new ImageAlpha = 255;		
new float: bounceVelocity = 0.00;
new float: bounceY;
new param;
new width;
new height;
new RupeeWorth = 0;

//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		new n;
		new FirstFrame[20];
		
		// Allocate 1 text string, 300 characters long 
		AllocateStrings("this", 1, 300);
		
		// Create the Main Animation
      	CreateAnim(7, itemAnim);
      	      	
		// Save the parameter passed
		param = GetParam("this");
			
		// Do different things depending on the parameter
		if ( param == 'g' ) // green
		{
			SetImage("this", "_itemrupeeg");
			SetString("this", 0, "You found a Green Rupee, this is only worth 1 rupee");  // Set a description for this item
			AddAnimframe(itemAnim, 0, 0, "_itemrupeeg");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeega");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeegb");
			RupeeWorth = 1;
		}
		if ( param == 'b' ) // blue
		{
			SetImage("this", "_itemrupeeb");
			SetString("this", 0, "You found a Blue Rupee, this is worth only 5 rupees");  // Set a description for this item
			AddAnimframe(itemAnim, 0, 0, "_itemrupeeb");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeeba");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeebb");
			RupeeWorth = 5;
		}
		if ( param == 'r' ) // red
		{
			SetImage("this", "_itemrupeer");
			SetString("this", 0, "You found a Red Rupee, this is worth 20 rupees!");  // Set a description for this item
			AddAnimframe(itemAnim, 0, 0, "_itemrupeer");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeera");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeerb");
			RupeeWorth = 20;
		}
		if ( param == 'p' ) // purple
		{
			SetImage("this", "_itemrupeep");
			SetString("this", 0, "You found a Purple Rupee, this is worth a whopping 50 rupees!");  // Set a description for this item
			AddAnimframe(itemAnim, 0, 0, "_itemrupeep");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeepa");
			AddAnimframe(itemAnim, 0, 0, "_itemrupeepb");
			RupeeWorth = 50;
		}
		
		// Add some extra frames so the animation doesnt constantly loop
		for (n = 0; n < 5; n++)
		{
			GetImage("this", FirstFrame);
			AddAnimframe(itemAnim, 0, 0, FirstFrame);
		}
		
		// Get the width and Height of the anim for future use
		width  = GetAnimWidth(itemAnim);
		height = GetAnimHeight(itemAnim);
		SetActiveDist("this", 190);
	}	
	
	if ( !isTaken("this"))
	{
		if ( Collide("this", "player1") && isPickable("this") )
			ItemTake();
		
		new x = GetX("this");
		new y = GetY("this");
		
		// Set up 1 collision rectangle for the item
		SetCollisionRect("this", 0, false, x, y, x + width, y + height);
		
		// If the Item is visible then draw it
		if (isVisible("this") && !FirstRun("this"))
		{
			DrawAnim(itemAnim, x, y - floatround(bounceY), y + height, 0, 255, 255, 255, ImageAlpha, 0, 100);
			PutSprite("shadow3", x, y + height - 7, 2, 0, 255, 255, 255, ImageAlpha);
		}
		
		// If the Item has been out for too long then make it disappear
		if (willTimeOut)
			TimeOut();
	}
	
	if (bouncing)
		Bounce();
}

//----------------------------------------
// Name: ItemTake()
//----------------------------------------
ItemTake()
{
	// Make the Item disappear when taken
	SetVisibleFlag("this", false);
	SetTakenFlag("this", true);
	
	// This is a Rupee so add an amount to the rupee count
	IncCounterTarget("rupees", RupeeWorth);
	
	// Other scripts can disable the sound effect on items by setting their first value to 1
	if ( GetValue("this", 0) == 0 )
		PlaySound("_getrupee.wav", 240);
	
	// Delete this entity if its been taken
	DeleteEntity("this");
}

//----------------------------------------
// Name: TimeOut()
//----------------------------------------
TimeOut()
{
	static float: timer = 0.00;
	static float: alpha = 255.00;
	
	timer += GetTimeDelta();
	
	if (timer > 10)
	{
		alpha -= (10 * GetTimeDelta());
		ImageAlpha = floatround(alpha);
		
		// If the Timer runs out then delete the item
		if (alpha < 0.00)
			DeleteEntity("this");
	}
}

//----------------------------------------
// Name: StartBounce()
//----------------------------------------
public StartBounce()
{
/* StartBounce is called from another script using CallFunction, it should be
	called just after the item is created to make it "bounce" a bit. */
	
	// While this is bouncing it can't be picked up by the player
	SetPickableFlag("this", false);
	
	bouncing = true;
	bounceVelocity = 160.00;
	bounceY = 0.00;
	TempBounce = 120;
	
	// Record the position of the item before it bounces
	initialBounce = GetY("this");
	willTimeOut = true;
}


//----------------------------------------
// Name: Bounce()
//----------------------------------------
Bounce()
{
	bounceVelocity -= (700 * GetTimeDelta());
	bounceY += (bounceVelocity * GetTimeDelta());
		
	// Check if the item has finished bouncing
	if (initialBounce - floatround(bounceY) > initialBounce)
	{
		// Check if the item has more bounce left in it
		if (TempBounce < 20)
		{
			bouncing = false;
			SetPickableFlag("this", true);
		}   
		else
		{ 
			bounceVelocity = float(TempBounce);
			TempBounce = TempBounce / 2;
			bounceY = 0.00;
		} 
	}
}
