/***********************************************
 * Spikes
 * 
 * Author: Littlebuddy 
 * Date:   today :p
 *
 * Desc:  Handles spikes and damage on them (NOTE: doesn't affect enemies, I belive these weren't close to enemies in LttP anyways :/)
 *
 * Usage: Everything is pre-set, nothing needed.
 *         
 ***********************************************/
#include <animation>
#include <entity>
#include <general>
#include <float>
#include <counter>

//stores the animation of the spike
new spikeanim[20];

//used for flashing
new float: flashcount = 0.00;
new flashing = false;

main()
{
	if (FirstRun())
	{
		//animations
		CreateCounterWithID(0,1,"FlashCounter");
		CreateAnim(8, spikeanim);
		AddAnimframe(spikeanim, 0, 0, "_spike_1");
		AddAnimframe(spikeanim, 0, 0, "_spike_2");
		AddAnimframe(spikeanim, 0, 0, "_spike_3");
		//animations ended
	}

	//base code, draws the entity, gets it's position and handles collision detection
	new x = GetInitialX("this");
	new y = GetInitialY("this");
	if(!isActive("this") || GetPauseLevel() != 0)		//if it's not active, don't do anything
	{
		if(isVisible("this"))
			DrawAnimNoInc(spikeanim, x, y, 0);
		return;
	}
	else
	{
		if(isVisible("this"))
			DrawAnim(spikeanim, x, y, 0);
		SetCollisionRect("this", 0, false, x, y, x + GetWidth("_spike_1"), y + GetHeight("_spike_1")-8);
	}
	//base code ended
	
	//handle hurting/flashing player (if there only was a library for this :sigh:)
	if(flashing == true) //check if the player is hurt
		PlayerFlash();		//if he is, flash him a bit
	else if(CollideAll("this", "player1") && GetCounterValue("FlashCounter") == false) //otherwise, check for a collision IF HE ISN'T HURT IN ANOTHER ONE (damn that bugged me a lot... w00t!)
		DamagePlayer();		//damage the player
	//player handling ended
}

DamagePlayer()
{
	SetHealth("player1", GetHealth("player1") - 50); //lower the players HP by 50 (1/2 heart)
	flashcount = 2.00;				//set the flashcounter to 2.00
	flashing = true;				//set the flashing state to true
	SetCounterValue("FlashCounter",true);
	if(GetHealth("player1") <= 0)	//This should be checked each frame instead in the player script... :/
	{
		SetPauseLevel(2);
		SetState("player1",dead);
		SetDeadFlag("this", true);
	}
}

PlayerFlash() 		// note, this part is ripped and modified from the player script :P
{		   		    // Couldn't use the existing due to the fact that it knocks you back.
	flashcount -= GetTimeDelta();
	
	// Make the Player flash by turning the visiblity flag and off
	if ( (floatround(flashcount * 40.00) % 2) == 0)
		SetVisibleFlag("player1", false);
	else
		SetVisibleFlag("player1", true);
	
	if (floatround(flashcount) <= 0)
	{
		SetVisibleFlag("player1", true);//make him visible when finished
		flashcount = 0.00;				//make sure the flashcount float is at 0.00
		flashing = false;				//set flashing to false
		SetCounterValue("FlashCounter",false); // set the flashcounter to false to enable hurting from spikes
	}
}