
RoomMine = class(Room, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	o.settingName = options.settingName or 'room_mine_'
	o.prefix = options.prefix or '1'

	Room.init(o, x, y, world, ...)
	o.bulletRate = game_settings[o.settingName .. "shot_rate"]
	o.typecount = 3
	o.nextBullet = o.bulletRate
	o.currentAim = math.pi/2
	o.aimSpeed = math.rad(game_settings[o.settingName .. "aim_speed"])
	Room.setHealth(o, game_settings[o.settingName .. "health"], game_settings[o.settingName .. "health"])
	o.drawType = 'svg'
	o.svg = SvgLoader(svg_root .. "defence/cannon_base_" .. player.currentWorld.id ..".svg", true)
	o.svg:setPosition(x, y)
	o.shotSvg = SvgLoader(svg_root .. "defence/mine_launcher_" .. o.prefix .. "_" .. player.currentWorld.id ..".svg", true)
	o.shotSvg:setPosition(x+o.w/2, y+o.h/2)
	o.scale = 1
	o.overlaySvg = SvgLoader(svg_root .. "defence/mine_launcher_base_" .. o.prefix .. "_" .. player.currentWorld.id ..".svg", true)
	o.overlaySvg:setPosition(x+o.w/2, y+o.h/2)

end)
function RoomMine:setScale(s)
	Room.setScale(self, s)
	self.shotSvg:scale(s, s)
	self.overlaySvg:scale(s, s)
	self.scale = s
	self.shotSvg:setPosition(self.pos.x+self.w/2, self.pos.y+self.h/2)
	self.overlaySvg:setPosition(self.pos.x+self.w/2, self.pos.y+self.h/2)
end
function RoomMine:setPosition(x, y)
	Room.setPosition(self, x, y)
	self.shotSvg:setPosition(x+self.w/2, y+self.h/2)
	self.overlaySvg:setPosition(x+self.w/2, y+self.h/2)
end

function RoomMine:setScrollFactor(x, y)
	Room.setScrollFactor(self, x, y)
	self.shotSvg:setScrollFactor(x, y)
	self.overlaySvg:setScrollFactor(x, y)
end
function RoomMine:draw()
	Room.draw(self)
	self.shotSvg:resetTransform()
	if self.scale then
		self.shotSvg:scale(self.scale, self.scale)
	end
	self.shotSvg:rotate(self.currentAim+math.pi)
	self.shotSvg:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale)
	self.overlaySvg:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale)
end
function RoomMine:update()
	Room.update(self)
	self.target = findNearest(self.world.enemies.members, self:center())
	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 RoomMine: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 then
				if not self.world.bullets then
					self.world.bullets = self.world:add(Group())
				end
				pd:startList("fromOF")
				pd:addSymbol("aimMine")
				pd:addFloat(self.currentAim)
				pd:finish("fromOF")
				local aimPoint = Vec2(0,1)
				aimPoint:normalize()
				aimPoint:rotate(self.currentAim)
				aimPoint:mult(300)
				local b = self.world.bullets:addRecycle(Mine(self:center().x, self:center().y, self.world, game_settings[self.settingName .. "fuse_start"], game_settings[self.settingName .. "fuse_end"], game_settings[self.settingName .. "radius"], game_settings[self.settingName .. "power"]))
				b.velocity = aimPoint
				b.velocity:mult(r_scale)
			end
		end
	end
end
RoomMine.rtype = RoomMine
RoomMine.cost = 500
RoomMine.color = {r=255, g=255, b=255, a=255, rtype=RoomMine, typecount=3}
RoomMine.settingName = "room_mine_"

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

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

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

RoomMine5 = class(RoomMine, function(o, x, y, world, ...)
	local arg = {...}
	if arg[1] == nil then
		options = {}
	else
		options = arg[1]
	end
	mergeTable(options, {settingName='room_mine5_', prefix='5'})
	RoomMine.init(o, x, y, world, options)
end)
RoomMine2.rtype = RoomMine2
RoomMine3.rtype = RoomMine3
RoomMine4.rtype = RoomMine4
RoomMine5.rtype = RoomMine5