RoomFreeze = class(RoomShooter, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end

	RoomShooter.init(o, x, y, world, ...)

	o.settingName = options.settingName or 'room_freeze_'
	o.prefix = options.prefix or '1'
	o.freezeTime = game_settings[o.settingName .. "freeze_time"] or 10
	o.bulletRate = game_settings[o.settingName .. "shot_rate"]
	o.typecount = 5
	o.nextBullet = o.bulletRate
	o.currentAim = math.pi/2
	o.aimSpeed = math.rad(game_settings[o.settingName .. "aim_speed"] or 8)
	
	o:setHealth(game_settings[o.settingName .. "health"], game_settings[o.settingName .. "health"])
	
	o.drawType = 'svg'
	o.scale = 1
	o.svg = SvgLoader(svg_root .. "defence/cannon_base_" .. player.currentWorld.id .. ".svg", true)
	o.svg:setPosition(x, y)
	o.shotSvg = SvgLoader(svg_root .. "defence/laser_".. o.prefix .."_" .. player.currentWorld.id .. ".svg", true)
	o.shotSvg:setPosition(x+o.w/2, y+o.h/2)
	-- o.shootRadius = 100
	if o.shootRadius then
		o.radiusDisplay = circleSvgGen(o.shootRadius)
		o.radiusDisplay:setPosition(x+o.w/2, y+o.h/2)
		o.radiusDisplay:setColor(255, 255, 255, 100)
	end

end)
function RoomFreeze:update()
	Room.update(self)
	self.target = findNearest(self.world.enemies.members, self:center(), function(t) return not t:isFrozen() end)
	if self.target then
		-- rotate the aim so that it points in its direction
		-- get the angle to the enemy center
		local enemyDiff = self:center()
		enemyDiff:sub(self.target:center())
		local targetAim = math.pi - enemyDiff:angle()
		-- should check to make sure that the difference is less than pi, otherwise it would make sense to go the other way around the circle
		local aimspeed = self.aimSpeed*30*bludG.elapsed
		if targetAim > self.currentAim then
			if math.abs(targetAim-self.currentAim) < math.pi*2 then
				self.currentAim = math.min(self.currentAim+aimspeed, targetAim)
			else
				self.currentAim = math.max(self.currentAim-aimspeed, targetAim)
			end
		elseif targetAim < self.currentAim then
			if math.abs(targetAim-self.currentAim) < math.pi*2 then
				self.currentAim = math.max(self.currentAim-aimspeed, targetAim)
			else
				self.currentAim = math.min(self.currentAim+aimspeed, targetAim)
			end
		end
	end
end

function RoomFreeze:draw()
	Room.draw(self)
	self.shotSvg:resetTransform()
	self.shotSvg:scale(self.scale, self.scale)
	self.shotSvg:rotate(self.currentAim+math.pi)
	self.shotSvg:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale)
	if self.radiusDisplay then
		self.radiusDisplay:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale)
	end
end
function RoomFreeze:sync()
	if self.alive then
		Room.sync(self)
		if self.nextBullet > 0 then self.nextBullet = self.nextBullet - 1 end
		if self.nextBullet <= 0 then
			self.nextBullet = self.bulletRate
			if self.target and self.target.alive and (not self.shootRadius or self:center():distance(self.target:center()) < self.shootRadius) then
				if not self.world.bullets then
					self.world.bullets = self.world:add(Group())
				end
				pd:startList("fromOF")
				pd:addSymbol("freezeShoot")
				pd:addFloat(1)
				pd:finish("fromOF")
				local aimPoint = Vec2(0,1)
				aimPoint:normalize()
				aimPoint:rotate(self.currentAim)
				aimPoint:mult(200)
				local b = self.world.bullets:addRecycle(FreezeBullet(self:center().x, self:center().y, self.freezeTime))
				b.velocity = aimPoint
				b.velocity:mult(r_scale)
				b:updateRotation()
			end
		end
	end
end

RoomFreeze.cost = 500
RoomFreeze.color = {r=255, g=255, b=255, a=255, rtype=RoomFreeze, typecount=5}
RoomFreeze.settingName = "room_shooter_"
RoomFreeze.rtype = RoomFreeze


RoomFreeze2 = class(RoomFreeze, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	mergeTable(options, {settingName='room_freeze2_', prefix='2'})
	RoomFreeze.init(o, x, y, world, options)
end)

RoomFreeze3 = class(RoomFreeze, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	mergeTable(options, {settingName='room_freeze3_', prefix='3'})
	RoomFreeze.init(o, x, y, world, options)
end)

RoomFreeze4 = class(RoomFreeze, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	mergeTable(options, {settingName='room_freeze4_', prefix='4'})
	RoomFreeze.init(o, x, y, world, options)
end)

RoomFreeze5 = class(RoomFreeze, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	mergeTable(options, {settingName='room_freeze5_', prefix='5'})
	RoomFreeze.init(o, x, y, world, options)
end)

function mergeTable(old, new)
	for k,v in pairs(new) do
		old[k] = v
	end
end

RoomFreeze2.rtype = RoomFreeze2
RoomFreeze3.rtype = RoomFreeze3
RoomFreeze4.rtype = RoomFreeze4
RoomFreeze5.rtype = RoomFreeze5