//UI Code
width = ui.getWidth();
height = ui.getHeight();
manaX = 5;
manaY = height - 20;
manaLabel = UILabel("Sanity:" , manaX, manaY);
manaLabelWidth = ui.getStringWidth("Mana: ");
manaAmount = UILabel(90, manaLabelWidth + manaX, manaY);
maxMana = 100;
generateManaMod = 15;
loseManaMod = 15;

 //Sound Variables
sanity1="sound.babyinsanity"
sanity2="sound.babyinsanity2"
sanity3="sound.babyinsanity3"
sanity4="sound.babyinsanity4"
 
//Functions + Definitions
updateMana = function()
{
    pPos = player.getPosition();
if (world.getLightValue(pPos.x, pPos.y, pPos.z) > 3)
{
     generateMana();
}
else            
{
    loseMana();
}
}

loseMana = function()
{
    loseTime = time.getTickCount() % loseManaMod;
    manaCurrent = parseInt(manaAmount.text);
        if(loseTime == 0 && manaCurrent > 0)
        {
                manaCurrent--;
                manaAmount.text = manaCurrent;
        }
		if(manaCurrent == 50)
		{
		
		   sound.playSoundUI(sanity1);
		   }
		   if(manaCurrent == 65)
		   {
		      sound.playSoundUI(sanity3);
		   }
		 if(manaCurrent == 30)
		    {
			  
			  sound.playSoundUI(sanity4);
			}
			if(manaCurrent == 10)
			{
			  
			  sound.playSoundUI(sanity2);
			  
			}
		if(manaCurrent == 0)
		{
		    player.setHealth(0)
		}
}

generateMana = function()
{
        generateTime = time.getTickCount() % generateManaMod;
        manaCurrent = parseInt(manaAmount.text);
        if(generateTime == 0 && manaCurrent != maxMana)
        {
                manaCurrent++;
                manaAmount.text = manaCurrent;
        }
}     
