微调资源加载,整理代码,多一点点log

This commit is contained in:
MrZ626
2021-10-21 15:51:50 +08:00
parent ea02bc470a
commit 89fb7a7659
3 changed files with 19 additions and 15 deletions

View File

@@ -1,5 +1,7 @@
local Sources={}
local volume=1
local BGM={ local BGM={
vol=1,
default=false, default=false,
getList=function()error("Cannot getList before initialize!")end, getList=function()error("Cannot getList before initialize!")end,
getCount=function()return 0 end, getCount=function()return 0 end,
@@ -13,7 +15,7 @@ local BGM={
local function task_fadeOut(src) local function task_fadeOut(src)
while true do while true do
coroutine.yield() coroutine.yield()
local v=src:getVolume()-.025*BGM.vol local v=src:getVolume()-.025*volume
src:setVolume(v>0 and v or 0) src:setVolume(v>0 and v or 0)
if v<=0 then if v<=0 then
src:pause() src:pause()
@@ -24,10 +26,10 @@ end
local function task_fadeIn(src) local function task_fadeIn(src)
while true do while true do
coroutine.yield() coroutine.yield()
local v=BGM.vol local v=volume
v=math.min(v,src:getVolume()+.025*v) v=math.min(v,src:getVolume()+.025*v)
src:setVolume(v) src:setVolume(v)
if v>=BGM.vol then if v>=volume then
return true return true
end end
end end
@@ -43,11 +45,10 @@ function BGM.setChange(func)
end end
function BGM.setVol(v) function BGM.setVol(v)
assert(type(v)=='number'and v>=0 and v<=1) assert(type(v)=='number'and v>=0 and v<=1)
BGM.vol=v volume=v
end end
function BGM.init(list) function BGM.init(list)
BGM.init=nil BGM.init=nil
local Sources={}
local simpList={} local simpList={}
for _,v in next,list do for _,v in next,list do
@@ -57,6 +58,7 @@ function BGM.init(list)
table.sort(simpList) table.sort(simpList)
function BGM.getList()return simpList end function BGM.getList()return simpList end
local count=#simpList local count=#simpList
LOG(count.." BGM files loaded")
function BGM.getCount()return count end function BGM.getCount()return count end
local function _load(name) local function _load(name)
@@ -77,10 +79,10 @@ function BGM.init(list)
end end
function BGM.setVol(v) function BGM.setVol(v)
assert(type(v)=='number'and v>=0 and v<=1) assert(type(v)=='number'and v>=0 and v<=1)
BGM.vol=v volume=v
if BGM.playing then if BGM.playing then
if BGM.vol>0 then if volume>0 then
BGM.playing:setVolume(BGM.vol) BGM.playing:setVolume(volume)
BGM.playing:play() BGM.playing:play()
elseif BGM.nowPlay then elseif BGM.nowPlay then
BGM.playing:pause() BGM.playing:pause()
@@ -95,7 +97,7 @@ function BGM.init(list)
function BGM.play(name) function BGM.play(name)
name=name or BGM.default name=name or BGM.default
if not _load(name)then return end if not _load(name)then return end
if BGM.vol==0 then if volume==0 then
BGM.nowPlay=name BGM.nowPlay=name
BGM.playing=Sources[name] BGM.playing=Sources[name]
return true return true

View File

@@ -40,14 +40,17 @@ function SFX.init(list)
for i=1,#list do ins(sfxList,list[i])end for i=1,#list do ins(sfxList,list[i])end
end end
function SFX.load(path) function SFX.load(path)
local c=0
for i=1,#sfxList do for i=1,#sfxList do
local fullPath=path..sfxList[i]..'.ogg' local fullPath=path..sfxList[i]..'.ogg'
if love.filesystem.getInfo(fullPath)then if love.filesystem.getInfo(fullPath)then
Sources[sfxList[i]]={love.audio.newSource(fullPath,'static')} Sources[sfxList[i]]={love.audio.newSource(fullPath,'static')}
c=c+1
else else
LOG("No SFX: "..sfxList[i]..'.ogg',.1) LOG("No SFX: "..sfxList[i]..'.ogg',.1)
end end
end end
LOG(c.."/"..#sfxList.." SFX files loaded")
end end
function SFX.loadSample(pack) function SFX.loadSample(pack)
assert(type(pack)=='table',"Usage: SFX.loadsample([table])") assert(type(pack)=='table',"Usage: SFX.loadsample([table])")
@@ -61,7 +64,7 @@ function SFX.loadSample(pack)
Sources[pack.name..num]={love.audio.newSource(pack.path..'/'..num..'.ogg','static')} Sources[pack.name..num]={love.audio.newSource(pack.path..'/'..num..'.ogg','static')}
num=num+1 num=num+1
end end
LOG("Pack "..pack.name.." loaded, "..(num-1).." files") LOG((num-1).." "..pack.name.." samples loaded")
end end
function SFX.getCount() function SFX.getCount()

View File

@@ -23,10 +23,9 @@ local loadingThread=coroutine.wrap(function()
end end
YIELD() YIELD()
YIELD('loadSFX')SFX.load('media/effect/'..SETTING.sfxPack..'/') YIELD('loadSFX')SFX.load('media/effect/'..SETTING.sfxPack..'/')
YIELD('loadSample') YIELD('loadSample')SFX.loadSample{name='bass',path='media/sample/bass',base='A2'}
SFX.loadSample{name='bass',path='media/sample/bass',base='A2'} YIELD('loadSample')SFX.loadSample{name='lead',path='media/sample/lead',base='A3'}
SFX.loadSample{name='lead',path='media/sample/lead',base='A3'} YIELD('loadSample')SFX.loadSample{name='bell',path='media/sample/bell',base='A4'}
SFX.loadSample{name='bell',path='media/sample/bell',base='A4'}
YIELD('loadVoice')VOC.load('media/vocal/'..SETTING.vocPack..'/') YIELD('loadVoice')VOC.load('media/vocal/'..SETTING.vocPack..'/')
YIELD('loadFont')for i=1,17 do getFont(15+5*i)end YIELD('loadFont')for i=1,17 do getFont(15+5*i)end