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

n = 0
dir = 0

STATE_FLYING = 1000
STATE_FALLING = 1001
inity = 0
drownTimer = 0
drownTime = 6

function init(me)
	setupEntity(me)
	entity_setEntityType(me, ET_ENEMY)
	entity_initSkeletal(me, "Parrot")	
	--entity_setAllDamageTargets(me, true)
	--entity_setEntityLayer(me, 0)

	entity_setCollideRadius(me, 32)
	
	entity_setHealth(me, 5)
	
	entity_setState(me, STATE_FLYING)
	
	entity_setCanLeaveWater(me, true)
	entity_setMaxSpeed(me, 800)
	entity_scale(me, 0.8, 0.8)
	entity_setDeathParticleEffect(me, "ParrotHit")
end

function postInit(me)
	n = getNaija()
	entity_setTarget(me, n)
	inity = entity_y(me)
end

function update(me, dt)
	entity_handleShotCollisions(me)
	entity_touchAvatarDamage(me, entity_getCollideRadius(me), 0.5, 1000, 0)
	
	if entity_isState(me, STATE_FLYING) then
		if dir == 0 then
			entity_addVel(me, -800*dt, 0)
		else
			entity_addVel(me, 800*dt, 0)
		end
		entity_flipToVel(me)
	end
	if not entity_isState(me, STATE_IDLE) then	
		entity_updateMovement(me, dt)
		--entity_setPosition(me, entity_x(me), inity, 0)
	end
	if entity_isUnderWater(me) then
		if not entity_isState(me, STATE_FALLING) then
			entity_setState(me, STATE_FALLING)
		end
		drownTimer = drownTimer + dt
		if drownTimer > drownTime then
			entity_damage(me, me, 999)
		end
	end
end

function enterState(me)
	if entity_isState(me, STATE_IDLE) then
		entity_animate(me, "idle", -1)
	elseif entity_isState(me, STATE_FLYING) then
		entity_animate(me, "fly", -1)		
		entity_setWeight(me, 0)
	elseif entity_isState(me, STATE_FALLING) then
		entity_setWeight(me, 300)
		entity_addVel(me, -entity_velx(me)/2, 400)
		entity_setMaxSpeedLerp(me, 2, 0.1)
		entity_rotate(me, 160, 2, 0, 0, 1)
		entity_animate(me, "idle", -1)
		--spawnParticleEffect("ParrotHit", entity_x(me), entity_y(me))
	end
end

function exitState(me)
end

function damage(me, attacker, bone, damageType, dmg)
	if attacker == me then return true end
	--debugLog(string.format("parrot health: %d", entity_getHealth(me)))
	spawnParticleEffect("ParrotHit", entity_x(me), entity_y(me))
	if damageType == DT_AVATAR_BITE then
		entity_changeHealth(me, -50)
		return true
	elseif damageType == DT_AVATAR_ENERGYBLAST or damageType == DT_AVATAR_SHOCK or damageType == DT_CRUSH then
		entity_setState(me, STATE_FALLING)
		spawnParticleEffect("ParrotHit", entity_x(me), entity_y(me))
		return true
	end
	return false
end

function animationKey(me, key)
end

function hitSurface(me)
	if dir == 0 then
		dir = 1
	else
		dir = 0
	end
end

function songNote(me, note)
end

function songNoteDone(me, note)
end

function song(me, song)
end

function activate(me)
end

