Something = {}

function Something:Blah(me)
	SetHealth(me,20)
end

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

function Gunner1:Update(me)
	--SetHealth(me, 20)

	state = GetState(me)

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

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

	if GetEnableState(me) == false
	then
		--Do nothing
	elseif state == 7 and self.smashState == false
	then
		DebugNumberQuery(me, -50)

		self.smashState = true

		local randomNum = GetRandomNumber(me, 0, 99)

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

			if myX >= playerX
			then
				self.targetX = playerX - 150
				self.targerY = playerY
			else
				self.targetX = playerX + 150
				self.targetY = playerY
			end

			self.doWalk = true
			self.doStand = false
			self.doAttack = false

			--SetState(me, 0)

			ChangeAnimation(me, "Walk")
		end
	elseif IsPlaying(me,"Stand")
	then
		DebugNumberQuery(me, 0)

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

		SetContextSensitiveTargets(me)	

		local playerDist = DistanceToTarget(me, playerX, playerY)

		local frontObj = GetFrontObj(me)
		local backObj = GetBackObj(me)
		local aboveFrontObj = GetAboveFrontObj(me)
		local aboveBackObj = GetAboveBackObj(me)
		local aboveObj = GetAboveObj(me)

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

		if self.doAttack == true
		then
			DebugNumberQuery(me, -1)
			self.doAttack = false
			ChangeAnimation(me, "Attack")
		elseif self.doStand == true and (frontObj == 17 or backObj == 17 or aboveFrontObj == 17 or aboveBackObj == 17 or aboveObj == 17)
		then
			randomNumber = GetRandomNumber(me, 0, 99);
			if randomNumber >= 5
			then
				self.teleporting = false
			else
				self.teleporting = true
			end

			self.targetX = playerX
			self.targetY = playerY

			if self.teleporting == false
			then
				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
			else
				local playerDir = GetPlayerDirection(me)

				if playerDir == -1 and myX < self.targetX
				then
					SetDirection(me, -1)
					self.targetX = self.targetX + 48.0
				elseif playerDir == 1 and myX > self.targetX
				then
					SetDirection(me, 1)
					self.targetX = self.targetX - 48.0
				elseif playerDir == -1 and myX > self.targetX
				then
					SetDirection(me, 1)
					self.targetX = self.targetX - 96.0
				elseif playerDir == 1 and myX < self.targetX
				then
					SetDirection(me, -1)
					self.targetX = self.targetX + 96.0
				end
			end

			DebugNumberQuery(me, -2)

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

			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")
	then
		local playerX = GetPlayerX(me)
		local playerY = GetPlayerY(me)

		local distToTarget = DistanceToTarget(me, self.targetX, self.targetY)
		local playerDist = DistanceToTarget(me, playerX, playerY)

		if playerDist > distToTarget
		then

			self.targetX = playerX
			self.targetY = playerY

			if self.teleporting == false
			then
				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
			else
				local playerDir = GetPlayerDirection(me)

				if playerDir == -1
				then
					SetDirection(me, -1)
					self.targetX = self.targetX + 48.0
				else
					SetDirection(me, 1)
					self.targetX = self.targetX - 48.0
				end
			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
					local pX = GetPlayerX(me)
					local pY = GetPlayerY(me)

					if self.targetX <= pX + 50 and self.targetX >= pX - 50 and self.targetY <= pY + 50 and self.targetY >= pY - 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.teleporting == false and self.doWalk == true
		then
			DebugNumberQuery(me, -8)

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

				self.targetX = GetPosX(me)
				self.targetY = GetPosY(me)
				self.doWalk = false
			end
		elseif distToTarget >= 5 and self.teleporting == true and self.doWalk == true
		then
			DebugNumberQuery(me, -10)
			SetVelocity(me, 0, 0)

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

			--DebugNumberQuery(me, myX)
			--DebugNumberQuery(me, myY)
			--DebugNumberQuery(me, self.targetX)
			--DebugNumberQuery(me, self.targetY)

			--if self.targetX <= myX + 50 and self.targetX >= myX - 50 and self.targetY <= myY + 50 and self.targetY >= myY - 50
			--if self.targetX == myX and self.targetY == myY
			--then
			--	DebugNumberQuery(me, -11)
			--	self.doWalk = false
			if TeleportToTarget(me, self.targetX, self.targetY) == false
			then
				DebugNumberQuery(me, -12)

				self.targetX = GetPosX(me)
				self.targetY = GetPosY(me)
				self.doWalk = false
			else
				CreateEmitter("TeleportFX", myX, myY)
				CreateEmitter("TeleportFX2", self.targetX, self.targetY)
			end
		end
	elseif IsPlaying(me, "Attack")
	then
		DebugNumberQuery(me, -13)

		if HitFrame(me,1)--EndOfAnimation(me)
		then
			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
			self.doAttack = false
			self.doStand = true
			self.doWalk = false
			ChangeAnimation(me,"Stand")
		end
		
	end
end