整理代码,部分协程创建/执行改用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

@@ -178,7 +178,7 @@ local function newEmptyPlayer(id,mini)
P.type="none"
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.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
P.newNext=coroutine.create(getSeqGen(P))
assert(coroutine.resume(P.newNext,P,P.gameEnv.seqData))
P.newNext=coroutine.wrap(getSeqGen(P))
P.newNext(P,P.gameEnv.seqData)
if P.mini then
ENV.lockFX=false

View File

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

View File

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