-- LUA Script - precede every function and global member with lowercase name of script + '_main'
-- Door Prompts 'Closed' can be opened
  
plantattack = plantattack or {}
plantidle = plantidle or {}
attacking = attacking or {}
idling = idling or {}
death = death or {}
plantscale = plantscale or {}
attackdist = attackdist or {}

function plant_grow_init(e)
plantattack[e] = 0
plantidle[e] = 0
attacking[e] = 0
idling[e] = 0
death[e] = 0
plantscale[e] = 100
attackdist[e] = 150
end
       
function plant_grow_main(e)
 PlayerDX = g_Entity[e]['x'] - g_PlayerPosX;
 PlayerDY = g_Entity[e]['y'] - g_PlayerPosY;
 PlayerDZ = g_Entity[e]['z'] - g_PlayerPosZ;
 PlayerDist = math.sqrt(math.abs(PlayerDX*PlayerDX)+math.abs(PlayerDY*PlayerDY)+math.abs(PlayerDZ*PlayerDZ));


if g_Entity[e]['health'] == 0  then
	plantscale[e] = plantscale[e] + 10
		if plantscale[e] > 250 then
			plantscale[e] = 250
		end
	attackdist[e] = attackdist[e] + 10
	if attackdist[e] > 250 then
		attackdist[e] = 250
end
	SetEntityHealth(e,25)
	Scale(e,plantscale[e]);

end
  		
		

  
if death[e] == 0 then
	RotateToPlayer(e);
end
plantattack[e] = ((math.random(3)-1));
plantidle[e] = ((math.random(2)+2));

if PlayerDist > attackdist[e] and death[e] == 0 then
  	  idling[e] = 1
   	 SetAnimation(plantidle[e]);
    if g_Entity[e]['animating'] == 0 then
 		 PlayAnimation(e);
 		 g_Entity[e]['animating'] = 1
end
end

 if PlayerDist < attackdist[e] and death[e] == 0 then
	attacking[e] = 1
        SetAnimation(plantattack[e]);
    if g_Entity[e]['animating'] == 0 then
 		 PlayAnimation(e);
  		 g_Entity[e]['animating'] = 1
end
   if death[e] == 0 and GetAnimationFrame(e) == 20 or GetAnimationFrame(e) == 50 or GetAnimationFrame(e) == 70 and PlayerDist < 150 then
  	 PlaySound(e,0)
  	 HurtPlayer(e,100)
   
	end
if death[e] == 0 and PlayerDist < attackdist[e] and idling[e] == 1 then
 	StopAnimation(e)
	idling[e] = 0
end
if death == 0 and PlayerDist > attackdist[e] and attacking[e] == 1 then
	attacking[e] = 0
	idling[e] = 1
end
end
end
