lightsources = {}

function ChangeLightRadius( light, radius )
	if config.lights == 0 then
		return
	end
	if light.radius == radius then
		return
	end
	local rad
	if config.lights == 1 then
		rad = 16
	else
		rad = 256
	end
	rad = radius/rad
	SetObjSpriteSize( light.sprite, rad )
end

function CreateLightSource( object, color, radius )
	if config.lights == 0 then
		return
	end
	local lspr
	local rad
	if config.lights == 1 then
		lspr = CreateSprite( "circle_small", object.x, object.y )
		rad = 16
	else
		lspr = CreateSprite( "circle", object.x, object.y )
		rad = 256
	end
	SetObjSpriteSize( lspr, radius/rad )
	SetObjSpriteColor( lspr, color )
	local tab = 
		{ 
			sprite = lspr,
			color = color,
			radius = radius,
			x = object.x,
			y = object.y,
			object = object,
			type = "attached"
		} 
	if not object.lightsources then
		object.lightsources = {}
	end
	table.insert( object.lightsources, tab )
	table.insert( lightsources, tab )
	return tab
end

function CreateRayLight( x, y, object, color, radius )
	if config.lights == 0 then
		return
	end
	local lspr
	local rad
	if config.lights == 1 then
		lspr = CreateSprite( "circle_small", x, y )
		rad = 16
	else
		lspr = CreateSprite( "circle", x, y )
		rad = 256
	end
	SetObjSpriteSize( lspr, radius/rad )
	SetObjSpriteColor( lspr, color )
	local tab = 
		{ 
			sprite = lspr,
			color = color,
			radius = radius,
			x = x,
			y = y,
			object = object,
			type = "ray"
		} 
	if not object.lightsources then
		object.lightsources = {}
	end
	table.insert( object.lightsources, tab )
	table.insert( lightsources, tab )
	return tab
end

function CreateFlash( x, y, color, radius, vel_y )
	if config.lights == 0  then
		return
	end
	local lspr
	local rad
	if config.lights == 1 then
		lspr = CreateSprite( "circle_small", x, y )
		rad = 16
	else
		lspr = CreateSprite( "circle", x, y )
		rad = 256
	end
	SetObjSpriteSize( lspr, radius/rad )
	SetObjSpriteColor( lspr, color )
	local tab = 
		{ 
			sprite = lspr,
			color = color,
			radius = radius,
			x = x,
			y = y,
			vel = {0, vel_y},
			type = "flash"
		} 
	table.insert( lightsources, tab )
	return tab
end

function ProcessLights()
	if config.lights == 0  then
		return
	end
	for key, value in pairs( lightsources ) do
		SetObjPos( value.sprite, value.x, value.y )
		if value.type == "attached" then
			if not value.object.dead then
				value.x = value.object.x
				value.y = value.object.y
			else
				value.type = "flash"
			end
		elseif value.type == "ray" then
			if value.object.dead then
				value.type = "flash"
			end
		elseif value.type == "flash" then
			value.color[4] = value.color[4] + 0.03
			if value.color[4] >= 0 then
				delete( lightsources, key )
			end
		end
	end
end

function ResetLights()
	lightsources = {}
end
