//Torch Slug

//OnAttacked
sound.playSound3D("sound.torchSlugHit", p.x, p.y, p.z);
entity.setHealth(entity.getHealth() - attackingDamage);

entity.setVelocity(0, 0.4, 0);

false;

//OnCreated

// Create the model
var m = Model();

m.addBoxExpanded("body1", -8, -6, -10, 15, -4, 20, 0, 0, 0.0);
m.addBoxExpanded("body2", -7, -5, -9, 13, -4, 18, 0, 0, 0.0);
m.addBoxExpanded("body3", -6, -4, -9, 11, -4, 15, 0, 0, 0.0);

m.y = 0.7;

var eye1 = Model();
eye1.addBoxExpanded("eye1", 2, 6, 5, 1, 3, 1, 0, 0, 0.0);
eye1.addBoxExpanded("eye2", -4, 6, 5, 1, 3, 1, 0, 0, 0.0);
eye1.setRotation(0,10,0);

var eye2 = Model();
eye2.addBoxExpanded("eye1", 2, 10, -2, 1, 3, 1, 0, 0, 0.0);
eye2.addBoxExpanded("eye2", -4, 10, -2, 1, 3, 1, 0, 0, 0.0);
eye2.setRotation(0,50,0);

// Assign texture
m.texture = "/models/mob/Slug.png";
eye1.texture = "/models/mob/slugEye.png";
eye2.texture = "/models/mob/slugEye.png";

// Attach to this entity
m.attachedTo = entity;
eye1.attachedTo = entity;
eye2.attachedTo = entity;

//OnDeath

// Explosion particles
for (var i=0; i<20; i++)
{
	entity.spawnExplosionParticle();
	sound.playSoundUI("sound.torchSlugDead");
}

// Insert Item Drop Code in here somewhere

m.removeFromRendering();
eye1.removeFromRendering();
eye2.removeFromRendering();

//OnUpdate

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

//Adjust collision box
entity.height = 0.5;
entity.width = 0.6;

//HurtTime
entity.hurtTime = 8;

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

		effect.spawnParticle("flame", tx, ty, tz, 0.00, 0.00, 0.00);
	}

}
else
{
	m.texture = "/models/mob/SlugNoFlames.png";
}

//Checks if player is still in range

//Make slug move
if (h > 7)
{
	entity.pathToEntity(player);
	entity.setMoveSpeed(0.5);
}
else if (h < 7)
{ 
entity.setMoveSpeed(0.7);
}



// 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;
			}





//Hits player and puts on fire
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
	{
		player.attackEntityFrom(player, 1);
	}
}


// Heals when on fire
if (entity.isBurning())
{
	entity.heal(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();
}
