84 lines
No EOL
2.3 KiB
Lua
84 lines
No EOL
2.3 KiB
Lua
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 and sounds[audioname] then
|
||
registerSound(audioname, sounds[audioname].volume)
|
||
end
|
||
elseif type == "bgm" then
|
||
music_path[audioname] = dir .. "/" .. file
|
||
end
|
||
--print("ℹ️ audio "..audioname.." added")
|
||
end
|
||
end
|
||
end
|
||
|
||
return assets |