/***********************************************
 * Crystal Switch
 * 
 * Author: Littlebuddy, TileSheet by TheChemist
 * Date:   today :p
 *
 * Desc:  Handles Crystal switching
 *
 * Usage: Use SetCounterValue("CrystalState",n) to set the color
 * 		  If n is 0, the crystal will be red, 1 = blue
 ***********************************************/
#include <entity>
#include <general>
#include <float>
#include <counter>
new hbw = 0;	    //stores wether the crystal has been hit or not
new float: HitCount;//goes up when the switch is hit
new val;
main()
{
	new x = GetX("this");
	new y = GetY("this");
	new h = GetHeight("cry_red");
	new w  = GetWidth( "cry_red");
	if (FirstRun()) // this only needs to be done once
	{
		SetType("this", otherType); // it's othertype, in this case, a switch
		SetCollisionRect("this",0,true,x,y,x+w,y+h); // set the collision rectangle
		CreateCounterWithID(0,1,"CrystalState");
	}
	if (!isActive("this")) // stop here if it's not active
	{	
		ClearCollisionRect("this",0);
		return;
	}
	if(GetCounterValue("CrystalState") == 1)
		PutSprite("cry_blue", x, y, y+h);
	else //CrystalState == 0
		PutSprite("cry_red" , x, y, y+h);
	
	if (hbw == 1) // this updates HitCount after the orb has been hit
		hitS();
}

public HitByWeapon(wtype[], damage, x, y) // currently it reacts the same way for every weapon, I really don't know what does what, so I'll leave it as that.
{
	if(hbw == 1 || !isActive("this"))
		return;
			
	if(GetCounterValue("CrystalState") == 1)
		SetCounterValue("CrystalState", 0);
	else
		SetCounterValue("CrystalState", 1);
	PlaySound("blocks.wav", 240); //block movement sound is played here, to stop any doubles & wierd volumes
	HitCount = 0.00;
	hbw = 1;
}

hitS() //called when hbw is 1, to return it to 0 after a set time(0.23) to enable attacking again.
{
	HitCount += GetTimeDelta();
	SetInteractingFlag("this",true);
	if(HitCount > 0.25)
		hbw = 0;
}