多媒体模块全体高度封装大型升级
This commit is contained in:
@@ -1,86 +1,93 @@
|
||||
local min=math.min
|
||||
|
||||
local function fadeOut(src)
|
||||
while true do
|
||||
coroutine.yield()
|
||||
local v=src:getVolume()-.025*SETTING.bgm
|
||||
src:setVolume(v>0 and v or 0)
|
||||
if v<=0 then
|
||||
src:stop()
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
local function fadeIn(src)
|
||||
while true do
|
||||
coroutine.yield()
|
||||
local v=SETTING.bgm
|
||||
v=min(v,src:getVolume()+.025*v)
|
||||
src:setVolume(v)
|
||||
if v>=SETTING.bgm then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
local function removeCurFadeOut(task,code,src)
|
||||
return task.code==code and task.args[1]==src
|
||||
end
|
||||
|
||||
local BGM={
|
||||
--nowPlay=[str:playing ID]
|
||||
--playing=[src:playing SRC]
|
||||
}
|
||||
function BGM.set(L)
|
||||
BGM.list=L
|
||||
BGM.len=#L
|
||||
end
|
||||
function BGM.loadOne(N)
|
||||
N=BGM.list[N]
|
||||
local file="media/BGM/"..N..".ogg"
|
||||
if love.filesystem.getInfo(file)then
|
||||
BGM.list[N]=love.audio.newSource(file,"stream")
|
||||
BGM.list[N]:setLooping(true)
|
||||
BGM.list[N]:setVolume(0)
|
||||
else
|
||||
LOG.print("No BGM file: "..N,5,COLOR.orange)
|
||||
end
|
||||
end
|
||||
function BGM.loadAll()
|
||||
for i=1,#BGM.list do
|
||||
BGM.loadOne(i)
|
||||
end
|
||||
end
|
||||
function BGM.play(s)
|
||||
if SETTING.bgm==0 then
|
||||
BGM.nowPlay=s
|
||||
BGM.playing=BGM.list[s]
|
||||
return
|
||||
end
|
||||
if s and BGM.list[s]and BGM.nowPlay~=s then
|
||||
if BGM.nowPlay then TASK.new(fadeOut,BGM.playing)end
|
||||
TASK.removeTask_iterate(removeCurFadeOut,fadeOut,BGM.list[s])
|
||||
TASK.removeTask_code(fadeIn)
|
||||
|
||||
TASK.new(fadeIn,BGM.list[s])
|
||||
BGM.nowPlay=s
|
||||
BGM.playing=BGM.list[s]
|
||||
BGM.playing:play()
|
||||
end
|
||||
end
|
||||
function BGM.freshVolume()
|
||||
if BGM.playing then
|
||||
local v=SETTING.bgm
|
||||
if v>0 then
|
||||
BGM.playing:setVolume(v)
|
||||
BGM.playing:play()
|
||||
elseif BGM.nowPlay then
|
||||
BGM.playing:pause()
|
||||
function BGM.init(list)
|
||||
BGM.init=nil
|
||||
local min=math.min
|
||||
local Sources={}function BGM.getList()return Sources end
|
||||
|
||||
local count=#list function BGM.getCount()return count end
|
||||
local function fadeOut(src)
|
||||
while true do
|
||||
coroutine.yield()
|
||||
local v=src:getVolume()-.025*SETTING.bgm
|
||||
src:setVolume(v>0 and v or 0)
|
||||
if v<=0 then
|
||||
src:stop()
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function BGM.stop()
|
||||
TASK.removeTask_code(fadeIn)
|
||||
if BGM.nowPlay then TASK.new(fadeOut,BGM.playing)end
|
||||
BGM.nowPlay,BGM.playing=nil
|
||||
local function fadeIn(src)
|
||||
while true do
|
||||
coroutine.yield()
|
||||
local v=SETTING.bgm
|
||||
v=min(v,src:getVolume()+.025*v)
|
||||
src:setVolume(v)
|
||||
if v>=SETTING.bgm then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
local function removeCurFadeOut(task,code,src)
|
||||
return task.code==code and task.args[1]==src
|
||||
end
|
||||
|
||||
BGM.loadOne=coroutine.wrap(function(skip)
|
||||
BGM.loadAll=nil
|
||||
for i=1,count do
|
||||
local file="media/BGM/"..list[i]..".ogg"
|
||||
if love.filesystem.getInfo(file)then
|
||||
Sources[list[i]]=love.audio.newSource(file,"stream")
|
||||
Sources[list[i]]:setLooping(true)
|
||||
Sources[list[i]]:setVolume(0)
|
||||
else
|
||||
LOG.print("No BGM file: "..list[i],5,COLOR.orange)
|
||||
end
|
||||
if not skip and i~=count then
|
||||
coroutine.yield()
|
||||
end
|
||||
end
|
||||
BGM.loadOne=nil
|
||||
|
||||
function BGM.play(s)
|
||||
if SETTING.bgm==0 then
|
||||
BGM.nowPlay=s
|
||||
BGM.playing=Sources[s]
|
||||
return
|
||||
end
|
||||
if s and Sources[s]and BGM.nowPlay~=s then
|
||||
if BGM.nowPlay then TASK.new(fadeOut,BGM.playing)end
|
||||
TASK.removeTask_iterate(removeCurFadeOut,fadeOut,Sources[s])
|
||||
TASK.removeTask_code(fadeIn)
|
||||
|
||||
TASK.new(fadeIn,Sources[s])
|
||||
BGM.nowPlay=s
|
||||
BGM.playing=Sources[s]
|
||||
BGM.playing:play()
|
||||
end
|
||||
end
|
||||
function BGM.freshVolume()
|
||||
if BGM.playing then
|
||||
local v=SETTING.bgm
|
||||
if v>0 then
|
||||
BGM.playing:setVolume(v)
|
||||
BGM.playing:play()
|
||||
elseif BGM.nowPlay then
|
||||
BGM.playing:pause()
|
||||
end
|
||||
end
|
||||
end
|
||||
function BGM.stop()
|
||||
TASK.removeTask_code(fadeIn)
|
||||
if BGM.nowPlay then TASK.new(fadeOut,BGM.playing)end
|
||||
BGM.nowPlay,BGM.playing=nil
|
||||
end
|
||||
end)
|
||||
function BGM.loadAll()
|
||||
BGM.loadOne(true)
|
||||
end
|
||||
end
|
||||
return BGM
|
||||
Reference in New Issue
Block a user