Instance = {}

function Instance:OnLoadLevel()
	LoadTexture("MobGunner")
	LoadTexture("AttackBot")
	LoadTexture("MegaMobGunner")
	LoadTexture("TriBot")
	LoadTexture("Gunner")
end

function Instance:CustomCreate(me)
	SetHealth(me,12)
end

function Instance:ChangePlayerAlertJump(me)
	RemoveArea(GetCurrentArea())
	ChangeAnimation(GetPlayerAlert(),"Jump")
	Gunner:Death(me)
	DestroyLevelBlockAt(64*41,64*3)
	DestroyLevelBlockAt(64*41,64*4)
	DestroyLevelBlockAt(64*41,64*5)
	CreateEmitter("Explosion", 64*42,64*2.5)
	CreateEmitter("Explosion", 64*42,64*3.5)
	CreateEmitter("Explosion", 64*42,64*4.5)
end

function Instance:EndPlayerAlert0(me)
	RemoveArea(GetCurrentArea())
	ChangeAnimation(GetPlayerAlert(),"Stand")
	Gunner:Death(me)
	DestroyLevelBlockAt(64*16,64*26)
	DestroyLevelBlockAt(64*16,64*27)

	CreateEmitter("Explosion", 64*17,64*25.5)
	CreateEmitter("Explosion", 64*17,64*26.5)
end

function Instance:EndPlayerAlert(me)
	RemoveArea(GetCurrentArea())
	ChangeAnimation(GetPlayerAlert(),"Stand")
	Gunner:Death(me)
	DestroyLevelBlockAt(64*50,64*25)
	DestroyLevelBlockAt(64*50,64*26)
	DestroyLevelBlockAt(64*50,64*27)

	CreateEmitter("Explosion", 64*51,64*24.5)
	CreateEmitter("Explosion", 64*51,64*25.5)
	CreateEmitter("Explosion", 64*51,64*26.5)
end

function Instance:EndPlayerAlert2(me)
	RemoveArea(GetCurrentArea())
	ChangeAnimation(GetPlayerAlert(),"Stand")
	Gunner:Death(me)
	DestroyLevelBlockAt(64*68,64*5)
	DestroyLevelBlockAt(64*68,64*6)
	DestroyLevelBlockAt(64*68,64*7)
	DestroyLevelBlockAt(64*68,64*8)

	CreateEmitter("Explosion", 64*69,64*4.5)
	CreateEmitter("Explosion", 64*69,64*5.5)
	CreateEmitter("Explosion", 64*69,64*6.5)
	CreateEmitter("Explosion", 64*69,64*7.5)
end

function Instance:EndPlayerAlert3(me)
	--RemoveArea(GetCurrentArea())
	ChangeAnimation(GetPlayerAlert(),"Stand")
	Gunner:Death(me)
	DestroyLevelBlockAt(64*123,64*25)
	DestroyLevelBlockAt(64*124,64*25)
	DestroyLevelBlockAt(64*125,64*25)

	CreateEmitter("Explosion", 64*124,64*24.5)
	CreateEmitter("Explosion", 64*125,64*24.5)
	CreateEmitter("Explosion", 64*126,64*24.5)
end

function Instance:EndPlayerAlert4(me)
	RemoveArea(GetCurrentArea())
	ChangeAnimation(GetPlayerAlert(),"Stand")
	TriBot:Death(me)
	DestroyLevelBlockAt(64*157,64*26)
	DestroyLevelBlockAt(64*157,64*27)
	DestroyLevelBlockAt(64*157,64*28)

	CreateEmitter("Explosion", 64*158,64*25.5)
	CreateEmitter("Explosion", 64*158,64*26.5)
	CreateEmitter("Explosion", 64*158,64*27.5)
end

function Instance:Create6Health(me)
	SetHealth(me,6)
end

function Instance:CollideUppercut(me,you)
	if( CheckCombo("Uppercut") == true)
	then
		Gunner:Collide(me,you)
	
	else
		SetVelocity(me,0,0)
		SetState(me,0)
	end

end


function Instance:DashUpdate(me)
	--SetHealth(me, 20)

	if self.Stunned > 0
	then
		self.Stunned = self.Stunned - 1

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

	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

			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)

			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 > 64
		then

			self.targetX = playerX
			self.targetY = playerY

			
				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

		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 playerDist < 64 and self.doWalk == true
		then
			DebugNumberQuery(me, -7)

			self.lifetime = GetLifetime(me)
			self.doWalk = false
			SetVelocity(me,0,0)
		elseif playerDist >= 64 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
		

		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



function Instance:CreateNewBots(me)
	instance = CreateObject(me,"Gunner","Enemies", 0,0,1,false,"DashGunner")
	UseGravity(instance,true)
	SetPosition(instance,1000,2000)
	--GunnerDash2 = {}
	--setmetatable(GunnerDash2,Gunner)
	--SetInstance(instance,"GunnerDash2")
	--GunnerDash2.Update = Instance.DashUpdate
end

function Instance:CreateInstance1(me)
	--self.Death = Instance.ChangePlayerAlertJump
	self.Death = Instance.EndPlayerAlert0

end

function Instance:CreateInstance2(me)
	self.Death = Instance.EndPlayerAlert

end

function Instance:CreateInstance4(me)
	self.Death = Instance.EndPlayerAlert3
end

function Instance:CreateInstance5(me)
	self.Death = Instance.EndPlayerAlert4
end
