--------------------------------------------------------------------------------------------------
RoomHeal = 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_heal_'
	o.prefix = options.prefix or '1'
	Room.init(o, x, y, world, ...)
	o.typecount = 4
	o.drawType = 'svg'
	
	o.svg = SvgLoader(svg_root .. "defence/heal_base_" .. player.currentWorld.id .. ".svg", true)
	o.svg:setPosition(x, y)

	o.healOn = SvgLoader(svg_root .. "defence/heal_" .. o.prefix .. "_on.svg", true)
	o.healOn:setPosition(x, y)

	o.healOff = SvgLoader(svg_root .. "defence/heal_" .. o.prefix .. "_off.svg", true)
	o.healOff:setPosition(x, y)

	Room.setHealth(o, game_settings[o.settingName .. "health"], game_settings[o.settingName .. "health"])
	o.healRate = game_settings[o.settingName .. "rate"]
	o.nextHeal = o.healRate
end)
function RoomHeal:draw()
	-- draw the main room
	Room.draw(self)
	if self.healing then
		self.healOn:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale)
	else
		self.healOff:draw(bludG.camera.scroll.x, bludG.camera.scroll.y, bludG.camera.scale)
	end
end
function RoomHeal:setPosition(x, y)
	Room.setPosition(self, x, y)
	if self.healOn then
		self.healOn:setPosition(x, y)
		self.healOff:setPosition(x, y)
	end
end
function RoomHeal:setScrollFactor(x, y)
	Room.setScrollFactor(self, x, y)
	if self.healOn then
		self.healOn:setScrollFactor(x, y)
		self.healOff:setScrollFactor(x, y)
	end
end
function RoomHeal:setScale(s)
	Room.setScale(self, s)
	self.healOff:scale(s, s)
	self.healOn:scale(s, s)
end
function RoomHeal:sync()
	self.healing = false
	if self.alive then
		Room.sync(self)
		if self.nextHeal > 0 then self.nextHeal = self.nextHeal - 1 end
		if self.nextHeal == 0 then
			self.nextHeal = self.healRate
			-- find the nearest room in need of healing and heal it
			for i,v in ipairs(self.world.rooms.members) do
				if v.alive and v ~= self and v.health < v.maxHealth and not v:is_a(RoomHeal) then
					self.healing = true
					pd:startList("fromOF")
					pd:addSymbol("healRoom")
					pd:addFloat(1.0)
					pd:finish("fromOF")
					v:setHealth(math.min(v.health+game_settings[self.settingName .. "power"], v.maxHealth))
					mainState.particles:addRecycle(PlayerLightning(self, v))
					mainState.particles:addRecycle(ParticleZip(self.pos.x, self.pos.y, v:center().x, v:center().y, "particle_data_heal.lua", sprites['tiles']['heal_cross.png'], {preroll=0.25, lifetime=0.5}))
					-- should only heal one other room now
					break;
				end
			end
		end
	end
end
RoomHeal.rtype = RoomHeal

RoomHeal.cost = 20
RoomHeal.color = {r=255, g=255, b=255, a=255, rtype=RoomHeal, typecount=4}
RoomHeal.settingName = "room_heal_"

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

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

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

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

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

RoomHeal2.rtype = RoomHeal2
RoomHeal3.rtype = RoomHeal3
RoomHeal4.rtype = RoomHeal4
RoomHeal5.rtype = RoomHeal5