MobGunner = {targetX = 0, targetY = 0, lifetime = 0, doStand = true, doWalk = false, doAttack = false, smashState = false, proximityAlert = false, Stunned = 0, attackSequence = 0, stopMovement = true, isFalling = false}
MobGunner.__index = MobGunner

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

function MobGunner:Create(me)
	SetHealth(me,8)
	--SetHealth(me,20)	
	SetPower(me,0)
	--ChangeAnimation(me,"Stand")
	--self.Update = Instance.CustomUpdate
	SetState(me,0)
end

function MobGunner:Update(me)
	--SetHealth(me, 20)  --Infinite life for debugging

	state = GetState(me)

	playerState = GetPlayerState(me)

	playerDirection = GetPlayerDirection(me)

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

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

	playerDistance = DistanceToTarget(me, playerX, playerY)

	if GetEnableState(me) == false  --Enemy is being thrown
	then
		--Do nothing
	elseif state == 6  --Enemy is airborne after
	then
		--Do nothing

	elseif state == 7  --Enemy is slammed
	then
		SetVelocity(me,0,0)
	elseif (playerState == 6 or playerState == 7) --Player is attacking
	then
		if playerDistance <= 64 --Player is within striking distance
		then
			if (myX > playerX and playerDirection == 1) or (myX < playerX and playerDirection == -1) --If Enemy is in position to be hit
			then
				if self.stopMovement == true
				then
					DebugNumberQuery(me, 10000000)

					self.stopMovement = false
					SetVelocity(me,0,0)
				end

				self.doAttack = false
				self.doWalk = false
				self.doStand = true
				ChangeAnimation(me,"Stand")
			end
		end
	elseif IsPlaying(me,"Stand") --Enemy is standing still
	then
		DebugNumberQuery(me, 0)

		if self.stopMovement == false
		then
			self.stopMovement = true
			--SetVelocity(me,0,0)
		end

		DebugBoolQuery(me, self.doAttack)
		DebugBoolQuery(me, self.doStand)

		if state == 1 and self.isFalling == true --In the air and supposed to be falling
		then
			--Do nothing
		elseif state == 0 and self.isFalling == true --On the ground and done falling
		then
			self.isFalling = false
		elseif self.doAttack == true
		then
			DebugNumberQuery(me, -1)
			self.doAttack = false
			ChangeAnimation(me, "Attack")
		elseif self.doStand == true and playerDistance <= 500
		then
			self.targetX = playerX
			self.targetY = playerY

			if self.targetX > myX
			then
				SetDirection(me, 1)
				self.targetX = self.targetX - 48.0;
			else
				SetDirection(me, -1)
				self.targetX = self.targetX + 48.0;
			end

			DebugNumberQuery(me, -2)

			if(self.targetX <= myX + 10 and self.targetX >= myX - 10 and self.targetY <= myY + 10 and self.targetY >= myY - 10)
			then
				self.doAttack = true
				self.doWalk = false
				self.doStand = false
				ChangeAnimation(me, "Attack")
			else
				self.doStand = false
				self.doWalk = true
				self.doAttack = false
				ChangeAnimation(me,"Walk")
			end
		end
	elseif IsPlaying(me,"Walk") --Enemy is walking
	then
		local distToTarget = DistanceToTarget(me, self.targetX, self.targetY)

		if playerDistance > distToTarget
		then

			self.targetX = playerX
			self.targetY = playerY

			if myX > self.targetX
			then
				SetDirection(me, -1)
				self.targetX = self.targetX + 48.0
			else
				SetDirection(me, 1)
				self.targetX = self.targetX - 48.0
			end
		end

		DebugNumberQuery(me, -4)

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

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

			if deltaLifetime >= 1
			then
				DebugNumberQuery(me, -6)

				if self.smashState == true
				then
					self.smashState = false

					self.doWalk = false
					self.doStand = true
					self.doAttack = false
				else
					if self.targetX <= playerX + 50 and self.targetX >= playerX - 50 and self.targetY <= playerY + 50 and self.targetY >= playerY - 50
					then
						self.doWalk = false
						self.doStand = false
						self.doAttack = true
					else
						self.doWalk = false
						self.doStand = true
						self.doAttack = false
					end
				end

				ChangeAnimation(me,"Stand")
			end
		elseif distToTarget < 5 and self.doWalk == true
		then
			DebugNumberQuery(me, -7)

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

			if ApproachTarget(me, self.targetX, self.targetY, 7.0) == false
			then
				DebugNumberQuery(me, -9)

				self.targetX = GetPosX(me)
				self.targetY = GetPosY(me)
				self.doWalk = false
			end
		end
	elseif IsPlaying(me, "Attack") --Enemy is attacking
	then
		--DebugNumberQuery(me, -13)

		if HitFrame(me,1)--EndOfAnimation(me)
		then
			DebugNumberQuery(me, -142)
			DebugNumberQuery(me, self.attackSequence)

			if self.attackSequence < 2
			then
				DebugNumberQuery(me, -14)

				child = CreateObject(me,"EnemyCollider","EnemyWeapons", 0,0,1,false)
				SetPosition(child, GetPosition(me))
				SetDirection(child,GetDirection(me))
				OffsetObject(child,50*GetDirection(me),0)
			else
				DebugNumberQuery(me, -143)

				child = CreateObject(me,"EnemyCollider2","EnemyWeapons", 0,0,1,false)
				SetPosition(child, GetPosition(me))
				SetDirection(child,GetDirection(me))
				OffsetObject(child,50*GetDirection(me),0)
				
				self.attackSequence = -1
			end

			self.attackSequence = self.attackSequence + 1
		end
		if EndOfAnimation(me)
		then
			if state == 1
			then
				self.isFalling = true
			end

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

function MobGunner:Collide(me, you)
	power = GetPower(you)
	TakeDamage(me, power)
	if power > 0
	then
		self.Stunned = 16
	end
end


function MobGunner:CollideWorld(me)
	ShakeScreen(8)
	TakeDamage(me, 2);
	SetVelocity(me,0,0)
	PlaySoundEffect(1)
end

function MobGunner:Death(me)
	x,y = GetPosition(me)
	y = y - 32
	CreateEmitter("Explosion", x,y)
end