/**********************************************************
 * _itemheart Entity Script
 * 
 * Author: GD
 * Date:   17/12/01
 *
 * Desc:   A Heart Item which can be dropped
 *         or placed in chests etc. To make this item into
 *         a different item just change its appearance and
 *         the contents of ItemTake()
 *
 * Usage:  use in chests, with the _itemlib library or place
 *         on its own.
 *
 * Sprites: _ItemSheet1.spt
 *         
 **********************************************************/
#include <entity>
#include <general>
#include <counter>
#include <float>

// Define a String with the image name
new ImgStr[20] = "_itemheart";

new bouncing = false;
new initialBounce;
new willTimeOut = false;
new TempBounce;    
new ImageAlpha = 255;		
new float: bounceVelocity = 0.00;
new float: bounceY;


//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
   if (FirstRun())
   {
      SetImage("this", ImgStr);

      // Allocate 1 text string, 300 characters long 
      AllocateStrings("this", 1, 300);

      // Set a description for this item
      SetString("this", 0, "You found a heart! this will add to your life total");
   }

   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 + GetWidth(ImgStr), y + GetHeight(ImgStr));

      // If the Item is visible then draw it
      if (isVisible("this") && !FirstRun("this")) 
      {
         PutSprite(ImgStr, x, y - floatround(bounceY), y + GetHeight(ImgStr), 0, 255,255,255, ImageAlpha);
         PutSprite("shadow3", x, y + GetHeight(ImgStr) - 6, 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 heart - so update the Players health
   SetHealth("player1", GetHealth("player1") + 100);

   // Other scripts can disable the sound effect on items by setting their first value to 1
   // This is mainly for chests
   if ( GetValue("this", 0) == 0 )
   	 PlaySound("_powerup.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;
      } 
   }
}




