Light = {}
LoadedObjects.Lights = {}

function Light:new(x,y,range,flicker,color,lum)
	local o = {}
	o.pos = {
		x = x,
		y = y
	}
	o.range = range
	o.lum = lum or 1
	o.color = color or {1,1,1}
	o.flicker_amount = flicker or 2
	o.flicker_value = 0
	o.dim = 0
	o.flicker_time = 60/12
	o.flicker_timer = 0

	table.insert(LoadedObjects.Lights,o)
	o.id = #LoadedObjects.Lights

	setmetatable(o, self)
	self.__index = self
	return o
end

function Light:kill()
	if self.id ~= nil then
		for _, e in pairs(LoadedObjects.Lights) do
			if e.id > self.id then
				e.id = e.id - 1
			end
		end
		table.remove(LoadedObjects.Lights,self.id)
	end
	self = nil
end

function Light:flicker()
	self.flicker_timer = self.flicker_timer + 1

	if self.flicker_timer >= self.flicker_time then
		self.flicker_timer = self.flicker_timer - self.flicker_time
		self.flicker_value = math.random(0,1)
		self.flicker_value = math.min(math.max(self.flicker_value, -self.flicker_amount), self.flicker_amount)
	end
end
