-- ai sequence
-- run towards building
-- run away from building
-- throw bombs
-- jump over building
-- repeat

bossCrabIdleAnimationSvgs = {}
bossCrabAttackAnimationSvgs = {}
bossCrabLeftAnimationSvgs = {}
bossCrabRightAnimationSvgs = {}

CrabBossEnemyPreload = function()
	if bossCrabIdleAnimationSvgs[1] then return end
	for i,v in ipairs(bossCrabIdleAnimation) do
		bossCrabIdleAnimationSvgs[i] = SvgLoader(svg_root .. "boss/" .. v, true)
	end
	for i,v in ipairs(bossCrabAttackAnimation) do
		bossCrabAttackAnimationSvgs[i] = SvgLoader(svg_root .. "boss/" .. v, true)
	end
	for i,v in ipairs(bossCrabLeftAnimation) do
		bossCrabLeftAnimationSvgs[i] = SvgLoader(svg_root .. "boss/" .. v, true)
	end
	for i,v in ipairs(bossCrabRightAnimation) do
		bossCrabRightAnimationSvgs[i] = SvgLoader(svg_root .. "boss/" .. v, true)
	end
end

CrabBossEnemyUnload = function()
	bossCrabIdleAnimationSvgs = {}
	bossCrabAttackAnimationSvgs = {}
	bossCrabLeftAnimationSvgs = {}
	bossCrabRightAnimationSvgs = {}
end
CrabBossEnemy = class(Enemy, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end

	o.settingName = options.settingName or 'crab_boss_'

	Enemy.init(o,x,y,394*r_scale*0.5,319*r_scale*0.5)
	if not bossCrabIdleAnimationSvgs[1] then CrabBossEnemyPreload() end
	o.idleAnimation = AnimationModel(bossCrabIdleAnimationSvgs)
	o.attackAnimation = AnimationModel(bossCrabAttackAnimationSvgs)
	o.leftAnimation = AnimationModel(bossCrabLeftAnimationSvgs)
	o.rightAnimation = AnimationModel(bossCrabRightAnimationSvgs)

	o.currentAnimation = o.idleAnimation

	o.drawType = 'svg'
	o.sprite = sprites['tiles']['white.png']
	o.moves = true
	o.world = world
	o.immovable = false
	
	o.health = game_settings[o.settingName.."health"] or 90
	
	o.hitPower = game_settings[o.settingName.."power"] or 10
	o.jumpDistance = game_settings[o.settingName.."jump_distance"] or 100
	o.jumpDistance = o.jumpDistance * r_scale
	o.jumpPower = game_settings[o.settingName.."jump_power"] or 600
	-- this is the gravity
	o.y_speed = 1200
	o.x_speed = game_settings[o.settingName.."run_rate"] or 120
	o.y_speed = o.y_speed *r_scale / 2; o.x_speed = r_scale*o.x_speed/2;
	o.velocity = Vec2(0, 0)
	-- can't load the vertical acceleration until after the first run
	o.firstRun = true
	o.acceleration = Vec2(0, 0)
	o.facingDirection = 'left'
	o:initialPosition()
	o.aiState = 'toBuilding'
	o.healthbar = o.world:add(BossHealthBar())
	o.healthbar:setValue(1)

	pd:startList("fromOF")
	pd:addSymbol("crab_boss_on")
	pd:addFloat(1)
	pd:finish("fromOF")
	bludG.camera:shake(1.5)
end)

function CrabBossEnemy:sync()
	if not Enemy.sync(self) then return end
	if self.alive then
	end
end
function CrabBossEnemy:draw()
	if self.alive then
		self.svg = self.currentAnimation:getSprite()
		self.svg:resetTransform()
		if self.facingDirection == 'right' then
			self.pos.x = self.pos.x+self.w
			self.svg:scale(-1, 1)
		end
		Object.draw(self)
		if self.facingDirection == 'right' then
			self.pos.x = self.pos.x-self.w
		end
	else
		Object.draw(self)
	end
end
function CrabBossEnemy:kill()
	if not self.alive then return end
	self.svg = self.currentAnimation:getSprite()
	self.svg:resetTransform()
	if self.moveDirection == 'left' then
		self.pos.x = self.pos.x+self.w
		self.svg:scale(-1, 1)
	end

	self.svg:explode()
	pd:startList("fromOF")
	pd:addSymbol("crab_boss_kill")
	pd:addFloat(1)
	pd:finish("fromOF")
	-- can't immediately kill because I want the parts to fade out
	self.alive = false
	self.killb2d = true
	Tweener:addTween(self.tint, {a=255}, {time=1, onComplete=function()
		Tweener:addTween(self.tint, {a=0}, {time=2.5})
	end})
	Tweener:addTween(self.pos, {y=self.pos.y+200}, {time=3.5, func='inExpo', onComplete=function()
		self.exists = false
	end})
	-- Object.kill(self)
end
-- move the missile to a random distance from the center of the building
function CrabBossEnemy:initialPosition()
	local limits = self.world:getTowerLimits()
	limits.min = Vec2(limits.minX, limits.minY)
	limits.max = Vec2(limits.maxX, limits.maxY)
	if math.random() < 0.5 then
		self.pos.x, self.pos.y=limits.min.x-math.random(100, 200)-self.w,-self.h-300*r_scale
		self.acceleration.x = self.x_speed
	else
		self.facingDirection = 'right'
		self.pos.x, self.pos.y=limits.max.x+math.random(100, 200)+self.w,-self.h-300*r_scale
		self.acceleration.x = -self.x_speed
	end
	-- start by running towards the building
end

function CrabBossEnemy:update()
	Enemy.update(self)
	self.world.groundPlane:overlap(self, nil, Object.separate)
	self.world.rooms:overlap(self, nil, Object.separate) 
	if self.alive and not self.world.over then
		if self[self.aiState] then self[self.aiState](self) end
	end
	if self.firstRun then self.firstRun = false; self.acceleration.y = self.y_speed; end
end

function CrabBossEnemy:toBuilding()
	-- check to see if we have hit the building yet... when we do, attack it
	if self.touching.other then
		for i,v in ipairs(self.touching.other) do
			if v:is_a(Room) then
				self.acceleration.x = 0
				self.aiState = 'attack'
				self.currentAnimation = self.attackAnimation
				self.attackAnimation.loop = false
				self.attackAnimation:startAnimation()

				-- attacks on frame 19
				mainState.particles:addRecycle(TimeWait(function()
					if self.alive then
						pd:startList("fromOF")
						pd:addSymbol("crab_boss_hit")
						pd:addFloat(1)
						pd:finish("fromOF")
						v:hit(self.hitPower)
					end
				end, 19*self.attackAnimation.rate/1000))
				self.attackAnimation.endCallback = function()
					self.aiState = 'awayBuilding'
					self.currentAnimation = self.leftAnimation
					pd:startList("fromOF")
					pd:addSymbol("crab_boss_run")
					pd:addFloat(1)
					pd:finish("fromOF")
					if self.pos.x < self.world.rootRoom:center().x then
						self.acceleration.x = -self.x_speed
					else
						self.acceleration.x = self.x_speed
					end
				end
			end
		end
	end
end


function CrabBossEnemy:awayBuilding()
	local limits = self.world:getTowerLimits()
	limits.min = Vec2(limits.minX, limits.minY)
	limits.max = Vec2(limits.maxX, limits.maxY)
	if self.pos.x < self.world.rootRoom:center().x then
		if self.pos.x < limits.min.x - self.jumpDistance then
			self.jumpCount = 0
			self.aiState = 'jump'
			self.acceleration.x = self.x_speed
		end
	else
		if self.pos.x > limits.max.x + self.jumpDistance then
			self.jumpCount = 0
			self.aiState = 'jump'
			self.acceleration.x = -self.x_speed
		end
	end
end

function CrabBossEnemy:jump()
	if self.touching.DOWN then
		pd:startList("fromOF")
		pd:addSymbol("crab_boss_jump")
		pd:addFloat(1)
		pd:finish("fromOF")
		self.jumpCount = self.jumpCount + 1
		self.velocity.y = -self.jumpPower*r_scale
		-- just jump twice then back up and try again
		if self.jumpCount > 2 then
			-- check to see our distance from the building, and if it is less then the jump distance, then move us away from the building
			self.aiState = 'distanceReset'
			if self.pos.x < self.world.rootRoom:center().x then
				self.acceleration.x = -self.x_speed
			else
				self.acceleration.x = self.x_speed
			end
		end
	end
end
function CrabBossEnemy:distanceReset()
	local limits = self.world:getTowerLimits()
	limits.min = Vec2(limits.minX, limits.minY)
	limits.max = Vec2(limits.maxX, limits.maxY)
	if self:center().x < self.world.rootRoom:center().x then
		if self.pos.x < limits.min.x - self.jumpDistance then
			self.facingDirection = 'left'
			self.aiState = 'toBuilding'
		pd:startList("fromOF")
		pd:addSymbol("crab_boss_run")
		pd:addFloat(1)
		pd:finish("fromOF")
			self.acceleration.x = self.x_speed
		end
	else
		if self.pos.x > limits.max.x + self.jumpDistance then
			self.facingDirection = 'right'
			self.aiState = 'toBuilding'
		pd:startList("fromOF")
		pd:addSymbol("crab_boss_run")
		pd:addFloat(1)
		pd:finish("fromOF")
			self.acceleration.x = -self.x_speed
		end
	end	
end
