task模块再升级,修复bgm模块没跟上task模块更新的bug

This commit is contained in:
MrZ626
2020-12-07 01:54:32 +08:00
parent 7bed61f210
commit c4d787cb27
3 changed files with 25 additions and 42 deletions

View File

@@ -1,9 +1,8 @@
local min=math.min
local function fadeOut(id)
local function fadeOut(src)
while true do
coroutine.yield()
local src=BGM.list[id]
local v=src:getVolume()-.025*SETTING.bgm
src:setVolume(v>0 and v or 0)
if v<=0 then
@@ -12,10 +11,9 @@ local function fadeOut(id)
end
end
end
local function fadeIn(id)
local function fadeIn(src)
while true do
coroutine.yield()
local src=BGM.list[id]
local v=SETTING.bgm
v=min(v,src:getVolume()+.025*v)
src:setVolume(v)
@@ -24,10 +22,12 @@ local function fadeIn(id)
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]
--suspend=[str:pausing ID]
--playing=[src:playing SRC]
}
function BGM.set(L)
@@ -52,19 +52,17 @@ function BGM.loadAll()
end
function BGM.play(s)
if SETTING.bgm==0 then
BGM.nowPlay=s
BGM.playing=BGM.list[s]
BGM.suspend,BGM.nowPlay=s
return
elseif not(s and BGM.list[s])then
return
end
if BGM.nowPlay~=s then
if BGM.nowPlay then TASK.new(fadeOut,BGM.nowPlay)end
TASK.changeCode(fadeIn,fadeOut)
TASK.removeTask_data(s)
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)
BGM.nowPlay,BGM.suspend=s
TASK.new(fadeIn,s)
TASK.new(fadeIn,BGM.list[s])
BGM.nowPlay=s
BGM.playing=BGM.list[s]
BGM.playing:play()
end
@@ -74,23 +72,15 @@ function BGM.freshVolume()
local v=SETTING.bgm
if v>0 then
BGM.playing:setVolume(v)
if BGM.suspend then
BGM.playing:play()
BGM.nowPlay,BGM.suspend=BGM.suspend
end
else
if BGM.nowPlay then
BGM.playing:pause()
BGM.suspend,BGM.nowPlay=BGM.nowPlay
end
BGM.playing:play()
elseif BGM.nowPlay then
BGM.playing:pause()
end
end
end
function BGM.stop()
if BGM.nowPlay then
TASK.new(fadeOut,BGM.nowPlay)
end
TASK.changeCode(fadeIn,fadeOut)
BGM.playing,BGM.nowPlay=nil
TASK.removeTask_code(fadeIn)
if BGM.nowPlay then TASK.new(fadeOut,BGM.nowPlay)end
BGM.nowPlay,BGM.playing=nil
end
return BGM

View File

@@ -28,6 +28,7 @@ function TASK.new(code,...)
tasks[#tasks+1]={
thread=thread,
code=code,
args={...},
}
end
end
@@ -38,17 +39,11 @@ function TASK.newNet(code,...)
tasks[#tasks+1]={
thread=thread,
code=code,
args={...},
net=true,
}
end
end
function TASK.changeCode(c1,c2)
for i=#tasks,1,-1 do
if tasks[i].thread==c1 then
tasks[i].thread=c2
end
end
end
function TASK.removeTask_code(code)
for i=#tasks,1,-1 do
if tasks[i].code==code then
@@ -56,9 +51,9 @@ function TASK.removeTask_code(code)
end
end
end
function TASK.removeTask_data(data)
function TASK.removeTask_iterate(func,...)
for i=#tasks,1,-1 do
if tasks[i].data==data then
if func(tasks[i],...)then
rem(tasks,i)
end
end

View File

@@ -35,10 +35,8 @@ function scene.keyDown(key)
end
elseif key=="return"or key=="space"then
if BGM.nowPlay~=BGM.list[S]then
if SETTING.bgm>0 then
SFX.play("click")
BGM.play(BGM.list[S])
end
BGM.play(BGM.list[S])
if SETTING.bgm>0 then SFX.play("click")end
else
BGM.stop()
end
@@ -79,7 +77,7 @@ scene.widgetList={
WIDGET.newText{name="now", x=700, y=500,font=50,align="R",hide=function()return not BGM.nowPlay end},
WIDGET.newSlider{name="bgm", x=760, y=80,w=400, font=35,disp=WIDGET.lnk_SETval("bgm"),code=function(v)SETTING.bgm=v BGM.freshVolume()end},
WIDGET.newButton{name="up", x=200, y=250,w=120, font=55,code=WIDGET.lnk_pressKey("up"),hide=function()return sceneTemp==1 end},
WIDGET.newButton{name="play", x=200, y=390,w=120, font=35,code=WIDGET.lnk_pressKey("space"),hide=function()return SETTING.bgm==0 end},
WIDGET.newButton{name="play", x=200, y=390,w=120, font=35,code=WIDGET.lnk_pressKey("space")},
WIDGET.newButton{name="down", x=200, y=530,w=120, font=55,code=WIDGET.lnk_pressKey("down"),hide=function()return sceneTemp==BGM.len end},
WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80, font=40,code=WIDGET.lnk_BACK},
}