-- SPORE SEED

dofile("scripts/entities/entityinclude.lua")

done = false
lifeTime = 0

STATE_WITHER = 1001

function init(me)
	setupEntity(me)
	entity_initSkeletal(me, "NatureFormFlowers")
		
	entity_setCollideRadius(me, 0)
	
	entity_setInternalOffset(me, 0, -32)	
	
	lifeTime = 5
	
	entity_alpha(me, 0)
	entity_alpha(me, 1, 0.1)
	
	entity_scale(me, 0.5, 0)
	v = math.random(100)/250.0
	entity_scale(me, 0.95+v, 0.95+v, 0.5, 0, 0, 1)
	
	--entity_clampToSurface(me)
	entity_setState(me, STATE_IDLE)
	
	c = (math.random(100)/100.0)*0.1 + 0.9
	entity_setColor(me, c, c, c)

	
	if chance(50) then
		entity_fh(me)
	end
	
	--debugLog("HEREE!!!")
end

function postInit(me)
	entity_clampToSurface(me)
	entity_rotateToSurfaceNormal(me)
	--entity_rotateToSurfaceNormal(me)
	--c = ((math.random(200)-100)/100.0)*45
	--entity_rotate(me, entity_getRotation(me) + c)	
end

function songNote(me, note)
end

function update(me, dt)
	if entity_isState(me, STATE_IDLE) then
		if not done then
			lifeTime = lifeTime - dt
			if lifeTime < 0 then
				done = true
				entity_setState(me, STATE_WITHER)
			end
		end
	end
end

function hitSurface(me)
end

function enterState(me)
	if entity_isState(me, STATE_IDLE) then
		entity_animate(me, "idle", -1)
	elseif entity_isState(me, STATE_WITHER) then
		entity_setStateTime(me, entity_animate(me, "wither")+2)
	end
end

function damage(me, attacker, bone, damageType, dmg)
	return false
end

function exitState(me)
	if entity_isState(me, STATE_WITHER) then
		x,y = entity_getScale(me)
		entity_scale(me, x, 0.4, 3)
		entity_delete(me, 3)
	end
end
