var target = entity.getEntitiesWithinRange(2);
var h = entity.getHealth();
var p = entity.getPosition();
var temp = 0;

entity.height = 0.5;
entity.width = 0.6;

//HurtTime
entity.hurtTime = 8;


entity.immuneToFire = true;
entity.setMaxHealth(15);

//Creates the Particles
if(h > 7)
{
	for (var i=0; i<10; i++)
	{
	var tx = entity.position.x + ((Math.random() * 0.8) - 0.575);
	var ty = entity.position.y + ((Math.random() * 0.8) - 0.0675);
	var tz = entity.position.z + ((Math.random() * 0.7) - 0.575);

	effect.spawnParticle("flame", tx, ty, tz, 0.02, 0.02, 0.02);
	}
	
	shape1.texture = "/models/mob/Slug.png";
	shape2.texture = "/models/mob/Slug.png";
	shape3.texture = "/models/mob/Slug.png";

}
else
{
	shape1.texture = "/models/mob/SlugNoFlames.png";
	shape2.texture = "/models/mob/SlugNoFlames.png";
	shape3.texture = "/models/mob/SlugNoFlames.png";
}

//Make slug move
if(entity.isInsideOfLava())
{
	//chat.print("Inlava");
	tpsLave++;
	if(tpsLave == 20)
	{
		entity.setHealth(h+4);
		tpsLave = 0;
	}
	entity.pathToEntity(player);
	entity.setMoveSpeed(4);
}

if (h > 7 && !entity.isInsideOfLava())
{
	//chat.print("OutLavaVie");
	entity.pathToEntity(player);
	entity.setMoveSpeed(0.5);
}
else if (h < 7 && !entity.isInsideOfLava())
{
	//chat.print("OutLavaMort");
	entity.pathToEntity(player);
	entity.setMoveSpeed(1.2);
}

entity.pathToEntity(player);

// Make Torch Slug Sounds

temp++;
if(temp%100 == 0) {
	Sounds = Math.floor(Math.random() * 2);
	switch(Sounds) {
		case 0 :
			sound.playSound3D("sound.LikeLikeMove", p.x, p.y, p.z);
			break;
	}
	temp = 0;
}


for(var j = 0; j < target.length; j++)
{
	var entj = target[j];
	if (entj.getClassType() == "Player" && h > 7)
	{
		player.setFireLevel(50)
		player.attackEntityFrom(player, 1);
		entity.setVelocity(0.1, 0.1, 0.1);
	}
	else if(entj.getClassType() == "Player" && h < 7)
	{
		player.attackEntityFrom(player, 1);
	}
}




// Face the player
var pPos = player.getPosition();
var angle = Math.atan2(p.z - pPos.z, p.x - pPos.x);
angle = (angle / Math.PI) * 180;
angle += 90;
entity.setRotation(angle, entity.rotation.pitch);

// Make entity dead if it doesn't have health. Prevents onDeath script from being delayed for over a second.
if (entity.health <= 0)
{
entity.setDead();
}
