Gunner = {targetX = 0, targetY = 0, teleporting = false, lifetime = 0, doStand = true, doWalk = false, doAttack = false}
Gunner.__index = Gunner

function Gunner.LoadAssets(me)
	LoadModel(me,"Gunner","Animated")
end

function Gunner:Create(me)
	--LoadModel(me,"Gunner","Animated")
	--SetHealth(me,5)
	SetHealth(me,math.random(2,6)) --Add in some spice
	--SetHealth(me, 100000000.0)
	SetPalette(me,14)
	SetPower(me,3)
	StopAnimation(me)
end

function Gunner:Collide(me, you)
	power = GetPower(you)
	TakeDamage(me, power)
end

function Gunner:Death(me)
	child = CreateObject(me,"Death","EnemyFX", 0,11,1,false)
	SetSFX(child,true)
	MoveObject(child,32,0)
end

function Gunner:Update(me)
	state = GetState(me)

	playerX = GetPlayerX(me)
	playerY = GetPlayerY(me)

	myX = GetPosX(me);
	myY = GetPosY(me);

	if state == 7
	then
		DebugNumberQuery(me, -50)

		local randomNum = GetRandomNumber(me, 0, 99)

		if randomNum >= 70
		then
			DebugNumberQuery(me, -100)

			if myX >= playerX
			then
				Gunner.targetX = playerX + 100
				Gunner.targerY = playerY
			else
				Gunner.targetX = playerX - 100
				Gunner.targetY = playerY
			end

			Gunner.doWalk = true
			Gunner.doStand = false
			Gunner.doAttack = false

			ChangeAnimation(me, "Walk")
		end
	elseif IsPlaying(me,"Stand")
	then
		--SetContextSensitiveTargets(me)		

		local playerDist = DistanceToTarget(me, playerX, playerY)

		if Gunner.doAttack == true
		then
			DebugNumberQuery(me, -1)
			Gunner.doAttack = false
			ChangeAnimation(me, "Attack")
		elseif playerDist < 250 and Gunner.doStand == true
		then
			randomNumber = GetRandomNumber(me, 0, 99);
			if randomNumber >= 50
			then
				Gunner.teleporting = false
			else
				Gunner.teleporting = true
			end

			Gunner.targetX = playerX
			Gunner.targetY = playerY

			if Gunner.teleporting == false
			then
				if Gunner.targetX > myX
				then
					SetDirection(me, 1)
					Gunner.targetX = Gunner.targetX - 64.0;
				else
					SetDirection(me, -1)
					Gunner.targetX = Gunner.targetX + 64.0;
				end
			else
				if Gunner.targetX > myX
				then
					SetDirection(me, 1)
					Gunner.targetX = Gunner.targerX + 64.0
				else
					SetDirection(me, -1)
					Gunner.targetX = Gunner.targetX - 64.0
				end
			end
			DebugNumberQuery(me, -2)
			Gunner.doStand = false
			Gunner.doWalk = true
			Gunner.doAttack = false
			ChangeAnimation(me,"Walk")
		end
	elseif IsPlaying(me,"Walk")
	then
		local dist = DistanceToTarget(me, Gunner.targetX, Gunner.targetY)

		DebugNumberQuery(me, -4)

		if Gunner.doWalk == false
		then
			DebugNumberQuery(me, -5)

			local currLifetime = GetLifetime(me)
			local deltaLifetime = currLifetime - Gunner.lifetime
			DebugNumberQuery(me, deltaLifetime)

			if deltaLifetime >= 50
			then
				DebugNumberQuery(me, -6)
				Gunner.doWalk = false
				Gunner.doStand = false
				Gunner.doAttack = true
				ChangeAnimation(me,"Stand")
			end
		elseif dist < 5 and Gunner.doWalk == true
		then
			DebugNumberQuery(me, -7)

			Gunner.lifetime = GetLifetime(me)
			Gunner.doWalk = false
			SetVelocity(me,0,0)
		elseif dist >= 5 and Gunner.teleporting == false and Gunner.doWalk == true
		then
			DebugNumberQuery(me, -8)

			if ApproachTarget(me, Gunner.targetX, Gunner.targetY) == false
			then
				Gunner.targetX = GetPosX(me)
				Gunner.targetY = GetPosY(me)
			end
		elseif dist >= 5 and Gunner.teleporting == true and Gunner.doWalk == true
		then
			DebugNumberQuery(me, -9)

			if TeleportToTarget(me, Gunner.targetX, Gunner.targetY) == false
			then
				Gunner.targetX = GetPosX(me)
				Gunner.targetY = GetPosY(me)
			end
		end
	elseif IsPlaying(me, "Attack")
	then
		DebugNumberQuery(me, -10)

		if EndOfAnimation(me)
		then
			DebugNumberQuery(me, -11)

			Gunner.doAttack = false
			Gunner.doStand = true
			Gunner.doWalk = false
			ChangeAnimation(me,"Stand")
		end
	end
end
