notsanequarium/lib/assets.lua
2021-01-23 19:37:37 +03:00

86 lines
No EOL
2.3 KiB
Lua
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

require 'lib.audio'
local assets = {}
function assets.clear()
print(" clearing assets")
sprites = {}
sound_path = {}
music_path = {}
end
function assets.load(base)
print(" loading " .. base)
assets.addSprites(base)
print("✓ added sprites")
assets.addAudio(base)
print("✓ added audio")
end
function assets.addSprites(base, d)
local dir = base.."/sprites"
if d then
dir = dir .. "/" .. d
end
local files = love.filesystem.getDirectoryItems(dir)
for _,file in ipairs(files) do
if string.sub(file, -4) == ".png" then
local spritename = string.sub(file, 1, -5)
local sprite = love.graphics.newImage(dir .. "/" .. file)
if d then
spritename = d .. "/" .. spritename
end
sprites[spritename] = sprite
--print(colr.cyan(" added sprite "..spritename))
elseif love.filesystem.getInfo(dir .. "/" .. file).type == "directory" then
--print(colr.cyan(" found sprite dir: " .. file))
local newdir = file
if d then
newdir = d .. "/" .. newdir
end
assets.addSprites(base, newdir)
end
end
end
function assets.addAudio(base, d, type)
local dir = base.."/audio"
if d then
dir = dir .. "/" .. d
end
local files = love.filesystem.getDirectoryItems(dir)
for _,file in ipairs(files) do
if love.filesystem.getInfo(dir .. "/" .. file).type == "directory" then
local newdir = file
if d then
newdir = d .. "/" .. newdir
end
assets.addAudio(base, newdir, type or file)
else
local audioname = file
if file:ends(".wav") then audioname = file:sub(1, -5) end
if file:ends(".mp3") then audioname = file:sub(1, -5) end
if file:ends(".ogg") then audioname = file:sub(1, -5) end
if file:ends(".flac") then audioname = file:sub(1, -5) end
if file:ends(".xm") then audioname = file:sub(1, -4) end
--[[if d then
audioname = d .. "/" .. audioname
end]]
if type == "sfx" then
sound_path[audioname] = dir .. "/" .. file
if sounds then
registerSound(audioname, (sounds[audioname] or {}).volume or 1)
end
elseif type == "bgm" then
music_path[audioname] = dir .. "/" .. file
end
--print(" audio "..audioname.." added")
end
end
end
return assets