DeathStarEnemy = class(CloudBossEnemy, function(o, x, y, world, ...)
	pd:startList("fromOF")
	pd:addSymbol("alien_boss_on")
	pd:addFloat(1)
	pd:finish("fromOF")
	CloudBossEnemy.init(o,x,y,world, {animation=deathStarAnimation, settingName='star_boss_'})

	o.moveRate = game_settings[o.settingName .. "move_rate"] or 4
	o.moveCount = o.moveRate

	o.shootRate = game_settings[o.settingName .. "shoot_rate"] or 20
	o.shootCount = o.shootRate
	o.hitPower = game_settings[o.settingName.."hit_power"] or 10

	o.direction = 1
	if math.random()>0.5 then o.direction = -1 end
	o.elasticity = 0.75
	o.immovable = false
	o.shooting = false
	o.rot = 0
	bludG.camera:shake(1.5)
end)

function DeathStarEnemy:sync()
	if not Enemy.sync(self) then return end
	if not self.alive then return end
	if not self.shooting then
		if self.moveRate > 0 then
			self.moveRate = self.moveRate - 1
		else
			self.moveRate = self.moveCount
			self:move()
		end
		if self.shootRate > 0 then
			self.shootRate = self.shootRate - 1
		else
			self.shootRate = self.shootCount
			self:shoot()
		end
	end
end
function DeathStarEnemy:shoot()
  -- first, rotate into the correct position for shooting at the building
  local angle = math.pi-self:center():sub(self.world.rootRoom:center()):normalize():angle()
  self.shooting = true
  if self.rotTween then Tweener:removeTween(self.rotTween) end
  self.line = PlayerLine(self.pos, self.world.rootRoom:center())
  self.line.sprite = sprites['tiles']['circleBlur.png']
  self.line:setColor(100, 255, 150)
  self.rotTween = Tweener:addTween(self, {rot=angle}, {time=3,func="outElastic", onComplete=function()
  	-- break if dead
  	if not self.alive then return end
    -- when you are done shooting turn off the lock, and rotate back
    self.shooting = false
    local nearestBlock, lineStart, dirVec = self:findTarget()
    if nearestBlock then
		nearestBlock[1]:hit(self.hitPower)
		dirVec:mult(nearestBlock[2]):add(lineStart)
		local l = mainState.particles:addRecycle(PlayerLine(lineStart, dirVec, true))
		local e = mainState.particles:addRecycle(Explosion(lineStart.x, lineStart.y, 10))
		e:setColor(100, 255, 150)
		e.sprite = sprites['tiles']['circleBlur.png']
		l.sprite = sprites['tiles']['circleBlur.png']
		l.w = 2*r_scale
		Tweener:addTween(l, {w=20*r_scale})
	    pd:startList("fromOF")
		pd:addSymbol("alien_boss_shoot")
		pd:addFloat(1)
		pd:finish("fromOF")
    end
    Tweener:removeTween(self.rotTween)
    self.rotTween = Tweener:addTween(self, {rot=0}, {time=3})
  end})
  -- as you are doing that, charge up the gun
end
function DeathStarEnemy:kill()
	if not self.alive then return end
	self.svg = self.currentAnimation:getSprite()
	self.svg:resetTransform()
	self.svg:rotateAround(self.rot, self.boss_w/2, self.boss_h/2)

	self.svg:explode()
	pd:startList("fromOF")
	pd:addSymbol("alien_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
function DeathStarEnemy:move()
	-- determine the max radius of the building
	local limits = self.world:getTowerLimits()
	local c = self.world.rootRoom:center()
	local radius = math.max(math.abs(c.y-limits.minY), math.max(math.abs(c.x-limits.minX), limits.maxX-c.x))
	-- determine the target position
	local tpos = self:center():sub(self.world.rootRoom:center()):normalize()
	tpos:rotate(0.4*self.direction)
	tpos:mult(radius+self.boss_h)
	tpos:add(self.world.rootRoom:center())
	-- push towards the next position on the outer circle
	local pushPower = tpos:sub(self:center())
	self.velocity:add(pushPower)
	pd:startList("fromOF")
	pd:addSymbol("alien_boss_move")
	pd:addFloat(1)
	pd:finish("fromOF")
end
function DeathStarEnemy:update()
	if self.alive then
		self.svg = self.currentAnimation:getSprite()
	end
	self:overlap(self.world.groundPlane, function()
		self.direction = self.direction*-1
	end, Object.separate)
	Enemy.update(self)
	self.velocity:mult(0.95)
end
function DeathStarEnemy:findTarget()
		-- determine the endpoint for the line
	local lineStart = Vec2(0, self.boss_h/2):rotate(self.rot):add(self:center())
	local dirVec = lineStart:copy():sub(self:center()):normalize()
	local nearestBlock = nil
	for i,v in ipairs(self.world.rooms.members) do
		if v.alive then
			local hit, firstHit = Rectangle.raycast(v, lineStart, dirVec)
			if hit then
				if not nearestBlock then
					nearestBlock = {v, firstHit};
				elseif nearestBlock[2] > firstHit then
					nearestBlock[1], nearestBlock[2] = v, firstHit
				end
			end
		end
	end
	return nearestBlock, lineStart, dirVec
end
function DeathStarEnemy:draw()
	if self.alive then
		if self.line and self.shooting then
			local nearestBlock, lineStart, dirVec = self:findTarget()
			if nearestBlock then
				dirVec:mult(nearestBlock[2]):add(lineStart)
				self.line:drawLine(lineStart, dirVec, 4)
			end
		end
		if self.svg then
			self.svg:resetTransform()
			self.svg:rotateAround(self.rot, self.boss_w/2, self.boss_h/2)
		end
	end
	Object.draw(self)
end

-- mainState = WrapperState(Group());
-- mainState:changeState(TitleScreenState())
-- mainState.substate.playButton.onPress()
-- mainState.substate.playButton.onPress()
-- mainState.substate.playButton.onPress()

-- mainState.substate.enemies:addRecycle(DeathStarEnemy(0, bludG.camera.h/2, mainState.substate))