levelLoader={
	loaded=false
}
levelFiles 	= {}
totalLevels 	= 0

function levelLoader:preLoad()
	worldChecker=1
	levelChecker=1

	levelLoader:loadOpus()
end 

function levelLoader:loadOpus()
	-- GET TOTAL MOVEMENTS (FOR LOADING PROGRESS)
	for i=1, #worlds do 
		totalLevels= totalLevels+#worlds[i].levels
	end
end

function levelLoader:loadLoop() 
	if worldChecker <= #worlds then 
		self:loadLevels()
	else
		self.loaded=true 
		loader:saveDef("worlds",worlds)
	end
end	

function levelLoader:fastLoad()
	worldChecker=1
	levelChecker=1
	while worldChecker <= #worlds do
		levelLoader:loadLevels()	
	end
end

function levelLoader:loadLevels()	
	
	if levelChecker <= #worlds[worldChecker].levels then 
		
		level = worlds[worldChecker].levels[levelChecker]
		local levelName = LINK_DIR.."levels".."/"..level.key..".lua"
		local loadedM = false
		if lfs.exists(levelName) then 
			level  				= lfs.load(levelName)()
			levels[level.key] 	= level 
			levelLoader:getImageList(level)
			levelChecker 		= levelChecker + 1
		else
			if lfs.exists(levelName) then 
				os.remove 	(GAME_DIR..levelName)
			end 
			worlds[worldChecker].levels[levelChecker] = nil
			totalLevels = totalLevels-1
			levelChecker = levelChecker + 1
		end
		
	else

		worldChecker = worldChecker+1
		levelChecker = 1

	end
	
end

function levelLoader:getImageList(l)
	l.images 		= {}
	l.objectCount 	= 0

	for r,room in pairs(l.rooms) do

		for o,object in pairs(room.objects) do
			table.add(l.images,object.key)
			l.objectCount = l.objectCount + 1
		end
	end
end

function levelLoader:getDefault()
	defaultLevel.key = uuid()
	return defaultLevel 	 
end
