/***********************************************
 * _greengrass Entity Script
 * 
 * Author: VIRUS
 * Date:   08/03/02
 *
 * Desc:   A grass script
 *
 * Usage:  N/A
 *
 * Uses _itemlib Library
 *
 * Sprites: _Grass.spt
 *         
 ***********************************************/
#include <entity>
#include <general>
#include <float>
#include <animation>

//   Global Data
new mainSpr[20] = "_greengrass";
new underSpr[20]= "_cutgreengrass2";
new walkanim[20];
new justDestroyed = false;
new float: DestroyCount;
new float: SoundCount;
new width;
new height;
new cut = false;  // CUT = FALSE


//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		// Set this entity's basic type
		SetType("this", otherType);

		// Setup some general parameters
		SetImage("this", mainSpr);
		SetWeight("this", 80);
		SetItem("this", " ");
		SetDamage("this", 50);
		SetActiveInGroups("this", true);

		width  = GetWidth(mainSpr);
		height = GetHeight(mainSpr);

		CreateAnim(6, walkanim);
		AddAnimframe(walkanim, 0, 0, "grasswalk1");
		AddAnimframe(walkanim, 0, 0, "grasswalk2");
		AddAnimframe(walkanim, 0, 0, "grasswalk3");
	}

	new yDepth = 2;
	new x = GetX("this");
	new y = GetY("this");
	
	if (!isActive("this"))
		return;

	if (isVisible("this"))
	{ 
		if (isActive("this"))
		{
			SetCollisionRect("this", 0, false, x, y, x + width, y + height); //was true
			PutSprite(mainSpr, x, y, yDepth );
		}
		if ( cut )
			PutSprite(underSpr, GetInitialX("this"), GetInitialY("this"), 1);

		if (CollideAll("player1", "this"))
		{
			if (GetState("player1") == standing)
				DrawAnimNoInc(walkanim, GetX("player1"), GetY("player1") + 8, 999999);
			else
				DrawAnim(walkanim, GetX("player1"), GetY("player1") + 8, 999999);
			SetSpeedMod("player1", -30);
			SoundCount += GetTimeDelta();
			if (SoundCount >= 0.3)
			{
				PlaySound("_boomerang.wav", 240);
				SoundCount = 0.00;
			}
		} else {
			SetSpeedMod("player1", 0);
		}
	}

	if (justDestroyed)
		Destroy();
}

//----------------------------------------
// Name: HitByWeapon(wtype[], damage)
//----------------------------------------
public HitByWeapon(wtype[], damage)
{
	// We should see what kind of weapon is hitting this entity
	// from the wtype param and make a decision based on that.
	
	// Get the item string if there is one, we need to pass it to GetRandomItem()
	new item[20];
	GetItem("this", item);

	// Make a random Item Appear beneath the grass
	CallFunction("_itemlib", false, "GetRandomItem", "nns", \
		GetX("this") + width / 2, GetY("this") + height / 2, item);

	BeginDestroy();
}

//----------------------------------------
// Name: BeginDestroy()
//----------------------------------------
public BeginDestroy()
{
	cut = true;
	SetActiveFlag("this", false);
	ClearCollisionRect("this", 0);
	justDestroyed = true;
	DestroyCount = 0.00;
	PlaySound("_bushdestroy.wav", 240);  //NEED'S SOUND
}

//----------------------------------------
// Name: Destroy()
//----------------------------------------
public Destroy() 
{
	new n;
	new x = GetX("this") + 8;
	new y = GetY("this");

	const MaxFrames = 7;
	new DestroyImages[2][] = { "_cutgreengrass0", "_cutgreengrass1" };

	// 4 leaves over MaxFrames frames of animation
	new xpos[4][MaxFrames]  = { {-4,-3,-5,-6,-8,-8,-11}, {2,0,-3,-6,-10,-14,-17}, {-2,-2,0,0,1,3,-4}, {-12,-7,-7,-2,0,2,-2} };
	new ypos[4][MaxFrames]  = { {-9,-10,-11,-12,-13,-16,-20}, {-3,-1,2,1,1,0,-2}, {1,5,7,9,11,11,15}, {-2,0,1,1,5,4,12} };
	new frame[4][MaxFrames] = { {0,0,0,0,0,0,0}, {1,1,1,1,1,0,1}, {0,0,1,0,0,0,1}, {0,0,0,0,0,0,1} };

	// Advance our animation counter
	DestroyCount += 12.0 * GetTimeDelta();

	// Draw the image of each leaf
	for( n = 0; n < 4; n++ )
	{
		PutSprite( DestroyImages[ frame[n][floatround(DestroyCount)] ], 
			x + xpos[n][ floatround(DestroyCount) ],   
			y + ypos[n][ floatround(DestroyCount) ], 
			y + ypos[n][ floatround(DestroyCount) ]);
	}
    
	// if our animation counter goes too high then end the animation         
	if (DestroyCount >= MaxFrames -1)
	{
		// Set the coordinates of the entity back to the initial coordinates
		SetX("this", GetInitialX("this"));
		SetY("this", GetInitialY("this"));
		Respawn("this", 20);
		justDestroyed = false;
   }  
}








