整理代码,部分协程创建/执行改用wrap更加简洁

This commit is contained in:
MrZ626
2021-04-01 14:47:54 +08:00
parent 5d728573cd
commit 78b7dfcc36
7 changed files with 35 additions and 43 deletions

View File

@@ -1,6 +1,5 @@
local rem=table.remove local rem=table.remove
local assert=assert local assert,resume,status=assert,coroutine.resume,coroutine.status
local resume,status=coroutine.resume,coroutine.status
local tasks={} local tasks={}
local TASK={} local TASK={}

View File

@@ -1,6 +1,6 @@
local int,ceil,min,abs,rnd,modf=math.floor,math.ceil,math.min,math.abs,math.random,math.modf local int,ceil,min,abs,rnd,modf=math.floor,math.ceil,math.min,math.abs,math.random,math.modf
local ins,rem=table.insert,table.remove local ins,rem=table.insert,table.remove
local resume,yield=coroutine.resume,coroutine.yield local yield=coroutine.yield
-- controlname: -- controlname:
-- 1~5:mL,mR,rR,rL,rF, -- 1~5:mL,mR,rR,rL,rF,
-- 6~10:hD,sD,H,A,R, -- 6~10:hD,sD,H,A,R,
@@ -79,7 +79,7 @@ if type(_CC)=="table"then
P.cur=rem(P.nextQueue,1) P.cur=rem(P.nextQueue,1)
P.curX,P.curY=blockPos[P.cur.id],int(P.gameEnv.fieldH+1-modf(P.cur.sc[1]))+ceil(P.fieldBeneath/30) P.curX,P.curY=blockPos[P.cur.id],int(P.gameEnv.fieldH+1-modf(P.cur.sc[1]))+ceil(P.fieldBeneath/30)
assert(resume(P.newNext)) P.newNext()
local id=CCblockID[P.nextQueue[P.AIdata.next].id] local id=CCblockID[P.nextQueue[P.AIdata.next].id]
if id then if id then
CC.addNext(P.AI_bot,id) CC.addNext(P.AI_bot,id)

View File

@@ -178,7 +178,7 @@ local function newEmptyPlayer(id,mini)
P.type="none" P.type="none"
P.sound=false P.sound=false
-- P.newNext=false--Coroutine to get new next, loaded in applyGameEnv() -- P.newNext=false--Warped coroutine to get new next, loaded in applyGameEnv()
P.keyPressing={}for i=1,12 do P.keyPressing[i]=false end P.keyPressing={}for i=1,12 do P.keyPressing[i]=false end
P.movDir,P.moving,P.downing=0,0,0--Last move key,DAS charging,downDAS charging P.movDir,P.moving,P.downing=0,0,0--Last move key,DAS charging,downDAS charging
@@ -312,8 +312,8 @@ local function applyGameEnv(P)--Finish gameEnv processing
if ENV.nextCount==0 then ENV.nextPos=false end if ENV.nextCount==0 then ENV.nextPos=false end
P.newNext=coroutine.create(getSeqGen(P)) P.newNext=coroutine.wrap(getSeqGen(P))
assert(coroutine.resume(P.newNext,P,P.gameEnv.seqData)) P.newNext(P,P.gameEnv.seqData)
if P.mini then if P.mini then
ENV.lockFX=false ENV.lockFX=false

View File

@@ -6,7 +6,7 @@ local Player={}--Player class
local int,ceil,rnd=math.floor,math.ceil,math.random local int,ceil,rnd=math.floor,math.ceil,math.random
local max,min,modf=math.max,math.min,math.modf local max,min,modf=math.max,math.min,math.modf
local ins,rem=table.insert,table.remove local ins,rem=table.insert,table.remove
local resume,yield=coroutine.resume,coroutine.yield local resume,yield,status=coroutine.resume,coroutine.yield,coroutine.status
local kickList=require"parts.kickList" local kickList=require"parts.kickList"
@@ -121,8 +121,8 @@ function Player:RND(a,b)
end end
function Player:newTask(code,...) function Player:newTask(code,...)
local thread=coroutine.create(code) local thread=coroutine.create(code)
coroutine.resume(thread,self,...) resume(thread,self,...)
if coroutine.status(thread)~="dead"then if status(thread)~="dead"then
self.tasks[#self.tasks+1]={ self.tasks[#self.tasks+1]={
thread=thread, thread=thread,
code=code, code=code,
@@ -763,7 +763,7 @@ function Player:popNext(ifhold)--Pop nextQueue to hand
self.ctrlCount=0 self.ctrlCount=0
self.cur=rem(self.nextQueue,1) self.cur=rem(self.nextQueue,1)
assert(resume(self.newNext)) self.newNext()
if self.cur then if self.cur then
self.pieceCount=self.pieceCount+1 self.pieceCount=self.pieceCount+1
if self.AI_mode=="CC"then if self.AI_mode=="CC"then
@@ -1512,8 +1512,8 @@ function Player:loadAI(data)--Load AI params
else else
self:setRS("TRS") self:setRS("TRS")
end end
self.AI_thread=coroutine.create(AIFUNC[data.type]) self.AI_thread=coroutine.wrap(AIFUNC[data.type])
coroutine.resume(self.AI_thread,self,self.AI_keys) self.AI_thread(self,self.AI_keys)
end end
--------------------------</Methods>-------------------------- --------------------------</Methods>--------------------------

View File

@@ -1,8 +1,7 @@
local max,min=math.max,math.min local max,min=math.max,math.min
local int,abs,rnd=math.floor,math.abs,math.random local int,abs,rnd=math.floor,math.abs,math.random
local rem=table.remove local rem=table.remove
local resume=coroutine.resume local assert,resume,status=assert,coroutine.resume,coroutine.status
local status=coroutine.status
local function updateLine(P)--Attacks, line pushing, cam moving local function updateLine(P)--Attacks, line pushing, cam moving
local bf=P.atkBuffer local bf=P.atkBuffer
@@ -100,16 +99,13 @@ local function updateFXs(P,dt)
TEXT.update(P.bonus) TEXT.update(P.bonus)
end end
end end
local updateTasks do--updateTasks(P) local function updateTasks(P)
local assert=assert local L=P.tasks
function updateTasks(P) for i=#L,1,-1 do
local L=P.tasks local tr=L[i].thread
for i=#L,1,-1 do assert(resume(tr))
local tr=L[i].thread if status(tr)=="dead"then
assert(resume(tr)) rem(L,i)
if status(tr)=="dead"then
rem(L,i)
end
end end
end end
end end
@@ -150,8 +146,8 @@ function update.alive(P,dt)
local C=P.AI_keys local C=P.AI_keys
P.AI_delay=P.AI_delay-1 P.AI_delay=P.AI_delay-1
if not C[1]then if not C[1]then
if status(P.AI_thread)=="suspended"then if P.AI_thread and not pcall(P.AI_thread)then
resume(P.AI_thread) P.AI_thread=false
end end
elseif P.AI_delay<=0 then elseif P.AI_delay<=0 then
P:pressKey(C[1])P:releaseKey(C[1]) P:pressKey(C[1])P:releaseKey(C[1])

View File

@@ -36,7 +36,7 @@ local function upFloor()
SFX.play("click",.3) SFX.play("click",.3)
end end
end end
local loadingThread=coroutine.create(function() local loadingThread=coroutine.wrap(function()
for i=1,SFX.getCount()do for i=1,SFX.getCount()do
SFX.loadOne() SFX.loadOne()
if i%3==0 then YIELD()end if i%3==0 then YIELD()end
@@ -175,7 +175,6 @@ local loadingThread=coroutine.create(function()
logoColor2={COLOR.rainbow_light(r)} logoColor2={COLOR.rainbow_light(r)}
end end
STAT.run=STAT.run+1 STAT.run=STAT.run+1
LOADED=true
--Connect to server --Connect to server
TASK.new(NET.TICK_WS_app) TASK.new(NET.TICK_WS_app)
@@ -194,10 +193,10 @@ local loadingThread=coroutine.create(function()
upFloor() upFloor()
end end
if progress==25 then if progress==25 then
loadingThread=false
SFX.play("welcome_sfx") SFX.play("welcome_sfx")
VOC.play("welcome_voc") VOC.play("welcome_voc")
THEME.fresh() THEME.fresh()
LOADED=true
return return
end end
YIELD() YIELD()
@@ -256,8 +255,8 @@ function scene.update(dt)
if progress<25 then if progress<25 then
local p=progress local p=progress
repeat repeat
assert(coroutine.resume(loadingThread)) loadingThread()
until not loadingThread or skip<=0 or progress~=p until LOADED or skip<=0 or progress~=p
if skip>0 then skip=skip-1 end if skip>0 then skip=skip-1 end
else else
openTime=openTime+dt openTime=openTime+dt

View File

@@ -12,15 +12,13 @@ local widgetX0={
1290,1290,1290,1290, 1290,1290,1290,1290,
} }
local cmdEntryThread=coroutine.create(function() local cmdEntryThread=coroutine.wrap(function()
while true do while true do
while true do if
if YIELD()~="c"then break end YIELD()=="c"and(SFX.play("ren_6")or 1)and
SFX.play("ren_6") YIELD()=="m"and(SFX.play("ren_9")or 1)and
if YIELD()~="m"then break end YIELD()=="d"and(SFX.play("ren_11")or 1)
SFX.play("ren_9") then
if YIELD()~="d"then break end
SFX.play("ren_11")
SCN.go("app_cmd") SCN.go("app_cmd")
end end
end end
@@ -30,7 +28,7 @@ function scene.sceneInit()
scrollX=tipLength scrollX=tipLength
BG.set() BG.set()
coroutine.resume(cmdEntryThread) cmdEntryThread()
--Set quick-play-button text --Set quick-play-button text
scene.widgetList[2].text=text.WidgetText.main.qplay..": "..text.modes[STAT.lastPlay][1] scene.widgetList[2].text=text.WidgetText.main.qplay..": "..text.modes[STAT.lastPlay][1]
@@ -47,7 +45,7 @@ end
function scene.mouseDown(x,y) function scene.mouseDown(x,y)
if x>=520 and x<=760 and y>=140 and y<=620 then if x>=520 and x<=760 and y>=140 and y<=620 then
coroutine.resume(cmdEntryThread, cmdEntryThread(
x<520+80 and y>620-80 and"c"or x<520+80 and y>620-80 and"c"or
x>760-80 and y>620-80 and"m"or x>760-80 and y>620-80 and"m"or
x<520+80 and y<140+80 and"d" x<520+80 and y<140+80 and"d"
@@ -121,7 +119,7 @@ function scene.keyDown(key)
SCN.back() SCN.back()
end end
else else
coroutine.resume(cmdEntryThread,key) cmdEntryThread(key)
end end
end end