整理tick函数,大多数不再是全局,而是直接放到使用的场景作为local变量

This commit is contained in:
MrZ626
2020-12-06 02:53:00 +08:00
parent ebd2cce1db
commit bcacee9d6e
7 changed files with 319 additions and 314 deletions

View File

@@ -462,6 +462,21 @@ function loadGame(M,ifQuickPlay)
SFX.play("enter")
end
end
local function tick_showMods()
local time=0
while true do
coroutine.yield()
time=time+1
if time%20==0 then
local M=GAME.mod[time/20]
if M then
TEXT.show(M.id,700+(time-20)%120*4,36,45,"spin",.5)
else
return
end
end
end
end
function resetGameData(replaying)
if PLAYERS[1]and not GAME.replaying then
mergeStat(STAT,PLAYERS[1].stat)
@@ -489,7 +504,6 @@ function resetGameData(replaying)
GAME.seed=rnd(1046101471,2662622626)
end
TASK.removeTask_code(TICK.autoPause)
destroyPlayers()
GAME.curMode.load()
restoreVirtualKey()
@@ -516,8 +530,8 @@ function resetGameData(replaying)
end
STAT.game=STAT.game+1
FREEROW.reset(30*#PLAYERS)
TASK.removeTask_code(TICK.showMods)
TASK.new(TICK.showMods)
TASK.removeTask_code(tick_showMods)
TASK.new(tick_showMods)
SFX.play("ready")
collectgarbage()
end

View File

@@ -1354,6 +1354,96 @@ function Player.loadAI(P,data)--Load AI params
end
--------------------------</Methods>--------------------------
--------------------------<Ticks>--------------------------
local function tick_throwBadge(ifAI,sender,time)
while true do
coroutine.yield()
time=time-1
if time%4==0 then
local S,R=sender,sender.lastRecv
local x1,y1,x2,y2
if S.small then
x1,y1=S.centerX,S.centerY
else
x1,y1=S.x+308*S.size,S.y+450*S.size
end
if R.small then
x2,y2=R.centerX,R.centerY
else
x2,y2=R.x+66*R.size,R.y+344*R.size
end
--Generate badge object
SYSFX.newBadge(x1,y1,x2,y2)
if not ifAI and time%8==0 then
SFX.play("collect")
end
end
if time<=0 then return end
end
end
local function tick_finish(P)
while true do
coroutine.yield()
P.endCounter=P.endCounter+1
if P.endCounter<40 then
--Make field visible
for j=1,#P.field do for i=1,10 do
if P.visTime[j][i]<20 then P.visTime[j][i]=P.visTime[j][i]+.5 end
end end
elseif P.endCounter==60 then
return
end
end
end
local function tick_lose(P)
while true do
coroutine.yield()
P.endCounter=P.endCounter+1
if P.endCounter<40 then
--Make field visible
for j=1,#P.field do for i=1,10 do
if P.visTime[j][i]<20 then P.visTime[j][i]=P.visTime[j][i]+.5 end
end end
elseif P.endCounter>80 then
for i=1,#P.field do
for j=1,10 do
if P.visTime[i][j]>0 then
P.visTime[i][j]=P.visTime[i][j]-1
end
end
end
if P.endCounter==120 then
for _=#P.field,1,-1 do
FREEROW.discard(P.field[_])
FREEROW.discard(P.visTime[_])
P.field[_],P.visTime[_]=nil
end
return
end
end
if not GAME.modeEnv.royaleMode and #PLAYERS>1 then
P.y=P.y+P.endCounter*.26
P.absFieldY=P.absFieldY+P.endCounter*.26
end
end
end
function tick_autoPause()
local time=0
while true do
coroutine.yield()
time=time+1
if SCN.cur~="play"or GAME.frame<180 then
return
elseif time==120 then
pauseGame()
return
end
end
end
--------------------------</Ticks>--------------------------
--------------------------<Events>--------------------------
local function gameOver()--Save record
if GAME.replaying then return end
@@ -1453,12 +1543,12 @@ function Player.win(P,result)
end
if P.human then
gameOver()
TASK.new(TICK.autoPause)
TASK.new(tick_autoPause)
if MARKING then
P:showTextF(text.marking,0,-226,25,"appear",.4,.0626)
end
end
P:newTask(TICK.finish)
P:newTask(tick_finish)
end
function Player.lose(P,force)
if P.result then return end
@@ -1532,7 +1622,7 @@ function Player.lose(P,force)
end
P.lastRecv=A
if P.id==1 or A.id==1 then
TASK.new(TICK.throwBadge,not A.human,P,max(3,P.badge)*4)
TASK.new(tick_throwBadge,not A.human,P,max(3,P.badge)*4)
end
end
else
@@ -1560,13 +1650,13 @@ function Player.lose(P,force)
end
end
gameOver()
P:newTask(#PLAYERS>1 and TICK.lose or TICK.finish)
TASK.new(TICK.autoPause)
P:newTask(#PLAYERS>1 and tick_lose or tick_finish)
TASK.new(tick_autoPause)
if MARKING then
P:showTextF(text.marking,0,-226,25,"appear",.4,.0626)
end
else
P:newTask(TICK.lose)
P:newTask(tick_lose)
end
if #PLAYERS.alive==1 then
PLAYERS.alive[1]:win()

View File

@@ -4,6 +4,69 @@ local Timer=love.timer.getTime
local max,min,sin=math.max,math.min,math.sin
local function tick_httpREQ_launch(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
if res then
if response.code==200 then
LOG.print(res.notice,360,COLOR.sky)
if VERSION_CODE>=res.version_code then
LOG.print(text.versionIsNew,360,COLOR.sky)
else
LOG.print(string.gsub(text.versionIsOld,"$1",res.version_name),"warn")
end
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.getNoticeFail..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function tick_httpREQ_autoLogin(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
LOGIN=true
local res=json.decode(response.body)
if res then
LOG.print(text.loginSuccessed)
end
else
LOGIN=false
local err=json.decode(response.body)
if err then
LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.sceneInit()
@@ -118,10 +181,10 @@ function scene.update()
LOADED=true
SFX.play("welcome_sfx")
VOC.play("welcome_voc")
httpRequest(TICK.httpREQ_launch,PATH.api..PATH.appInfo)
httpRequest(tick_httpREQ_launch,PATH.api..PATH.appInfo)
if ACCOUNT.auth_token and ACCOUNT.email then
httpRequest(
TICK.httpREQ_autoLogin,
tick_httpREQ_autoLogin,
PATH.api..PATH.auth,
"GET",
{["Content-Type"]="application/json"},

View File

@@ -1,15 +1,55 @@
local scene={}
local function tick_httpREQ_newLogin(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
LOGIN=response.code==200
if res then
if LOGIN then
LOG.print(text.loginSuccessed)
ACCOUNT.email=res.email
ACCOUNT.auth_token=res.auth_token
FILE.save(ACCOUNT,"account","")
function login()
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local function login()
local email= WIDGET.active.email.value
local password= WIDGET.active.password.value
if #email==0 or not email:match("^[a-zA-Z0-9_]+@[a-zA-Z0-9_-]+%.[a-zA-Z0-9_]+$") then
if #email==0 or not email:match("^[a-zA-Z0-9_]+@[a-zA-Z0-9_-]+%.[a-zA-Z0-9_]+$")then
LOG.print(text.wrongEmail)return
elseif #password==0 then
LOG.print(text.noPassword)return
end
httpRequest(
TICK.httpREQ_newLogin,
tick_httpREQ_newLogin,
PATH.api..PATH.auth,
"GET",
{["Content-Type"]="application/json"},
@@ -20,6 +60,8 @@ function login()
)
end
local scene={}
function scene.keyDown(key)
if key=="escape"then
SCN.back()

View File

@@ -1,5 +1,45 @@
local gc=love.graphics
local function tick_httpREQ_checkAccessToken(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
LOG.print(text.accessSuccessed)
SCN.pop()
SCN.go("netgame")
elseif response.code==403 or response.code==401 then
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
)
else
local err=json.decode(response.body)
if err then
LOG.print(text.netErrorCode..response.code..": "..err.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.sceneInit()
@@ -41,7 +81,7 @@ scene.widgetList={
if LOGIN then
if ACCOUNT.access_token then
httpRequest(
TICK.httpREQ_checkAccessToken,
tick_httpREQ_checkAccessToken,
PATH.api..PATH.access,
"GET",
{["Content-Type"]="application/json"},

View File

@@ -1,3 +1,30 @@
local function tick_httpREQ_register(task)
local time=0
while true do
coroutine.yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
if res then
if response.code==200 then
LOG.print(text.registerSuccessed..": "..res.message)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
local scene={}
function scene.keyDown(key)
@@ -16,7 +43,7 @@ function scene.keyDown(key)
LOG.print(text.diffPassword)return
end
httpRequest(
TICK.httpREQ_register,
tick_httpREQ_register,
PATH.api..PATH.auth,
"POST",
{["Content-Type"]="application/json"},

View File

@@ -1,280 +1,8 @@
local yield=coroutine.yield
local Tick={}
function Tick.showMods()
local time=0
while true do
yield()
time=time+1
if time%20==0 then
local M=GAME.mod[time/20]
if M then
TEXT.show(M.id,700+(time-20)%120*4,36,45,"spin",.5)
else
return
end
end
end
end
function Tick.finish(P)
while true do
yield()
P.endCounter=P.endCounter+1
if P.endCounter<40 then
--Make field visible
for j=1,#P.field do for i=1,10 do
if P.visTime[j][i]<20 then P.visTime[j][i]=P.visTime[j][i]+.5 end
end end
elseif P.endCounter==60 then
return
end
end
end
function Tick.lose(P)
while true do
yield()
P.endCounter=P.endCounter+1
if P.endCounter<40 then
--Make field visible
for j=1,#P.field do for i=1,10 do
if P.visTime[j][i]<20 then P.visTime[j][i]=P.visTime[j][i]+.5 end
end end
elseif P.endCounter>80 then
for i=1,#P.field do
for j=1,10 do
if P.visTime[i][j]>0 then
P.visTime[i][j]=P.visTime[i][j]-1
end
end
end
if P.endCounter==120 then
for _=#P.field,1,-1 do
FREEROW.discard(P.field[_])
FREEROW.discard(P.visTime[_])
P.field[_],P.visTime[_]=nil
end
return
end
end
if not GAME.modeEnv.royaleMode and #PLAYERS>1 then
P.y=P.y+P.endCounter*.26
P.absFieldY=P.absFieldY+P.endCounter*.26
end
end
end
function Tick.throwBadge(ifAI,sender,time)
while true do
yield()
time=time-1
if time%4==0 then
local S,R=sender,sender.lastRecv
local x1,y1,x2,y2
if S.small then
x1,y1=S.centerX,S.centerY
else
x1,y1=S.x+308*S.size,S.y+450*S.size
end
if R.small then
x2,y2=R.centerX,R.centerY
else
x2,y2=R.x+66*R.size,R.y+344*R.size
end
--Generate badge object
SYSFX.newBadge(x1,y1,x2,y2)
if not ifAI and time%8==0 then
SFX.play("collect")
end
end
if time<=0 then return end
end
end
function Tick.autoPause()
local time=0
while true do
yield()
time=time+1
if SCN.cur~="play"then
return
elseif time==120 then
pauseGame()
return
end
end
end
function Tick.httpREQ_launch(task)
local time=0
while true do
yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
if res then
if response.code==200 then
LOG.print(res.notice,360,COLOR.sky)
if VERSION_CODE>=res.version_code then
LOG.print(text.versionIsNew,360,COLOR.sky)
else
LOG.print(string.gsub(text.versionIsOld,"$1",res.version_name),"warn")
end
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.getNoticeFail..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function Tick.httpREQ_register(task)
local time=0
while true do
yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
if res then
if response.code==200 then
LOG.print(text.registerSuccessed..": "..res.message)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function Tick.httpREQ_newLogin(task)
local time=0
while true do
yield()
local response,request_error=client.poll(task)
if response then
local res=json.decode(response.body)
LOGIN=response.code==200
if res then
if LOGIN then
LOG.print(text.loginSuccessed)
ACCOUNT.email=res.email
ACCOUNT.auth_token=res.auth_token
FILE.save(ACCOUNT,"account","")
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
)
else
LOG.print(text.netErrorCode..response.code..": "..res.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function Tick.httpREQ_autoLogin(task)
local time=0
while true do
yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
LOGIN=true
local res=json.decode(response.body)
if res then
LOG.print(text.loginSuccessed)
end
else
LOGIN=false
local err=json.decode(response.body)
if err then
LOG.print(text.loginFailed..": "..text.netErrorCode..response.code.."-"..err.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function Tick.httpREQ_checkAccessToken(task)
local time=0
while true do
yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
LOG.print(text.accessSuccessed)
SCN.pop()
SCN.go("netgame")
elseif response.code==403 or response.code==401 then
httpRequest(
TICK.httpREQ_getAccessToken,
PATH.api..PATH.access,
"POST",
{["Content-Type"]="application/json"},
json.encode{
email=ACCOUNT.email,
auth_token=ACCOUNT.auth_token,
}
)
else
local err=json.decode(response.body)
if err then
LOG.print(text.netErrorCode..response.code..": "..err.message,"warn")
end
end
return
elseif request_error then
LOG.print(text.loginFailed..": "..request_error,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function Tick.httpREQ_getAccessToken(task)
local time=0
while true do
yield()
coroutine.yield()
local response,request_error=client.poll(task)
if response then
if response.code==200 then
@@ -315,30 +43,10 @@ function Tick.httpREQ_getAccessToken(task)
end
end
end
function Tick.wsCONN_connect(task)
local time=0
local function tick_wsCONN_read()
while true do
yield()
local wsconn,connErr=client.poll(task)
if wsconn then
WSCONN=wsconn
TASK.new(Tick.wsCONN_read)
LOG.print(text.wsSuccessed,"warn")
return
elseif connErr then
LOG.print(text.wsFailed..": "..connErr,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
function Tick.wsCONN_read()
while true do
yield()
coroutine.yield()
if not WSCONN then return end
local messages,readErr=client.read(WSCONN)
if messages then
@@ -353,9 +61,30 @@ function Tick.wsCONN_read()
end
end
end
function Tick.wsCONN_connect(task)
local time=0
while true do
coroutine.yield()
local wsconn,connErr=client.poll(task)
if wsconn then
WSCONN=wsconn
TASK.new(tick_wsCONN_read)
LOG.print(text.wsSuccessed,"warn")
return
elseif connErr then
LOG.print(text.wsFailed..": "..connErr,"warn")
return
end
time=time+1
if time>360 then
LOG.print(text.httpTimeout,"message")
return
end
end
end
-- function Tick.wsCONN_write()
-- while true do
-- local message=yield()
-- local message=coroutine.yield()
-- if message then
-- local writeErr=client.write(WSCONN,message)
-- if writeErr then