基本完成中途加入的观战

修复从联网游戏房间退出不会把GAME.playing置false
checkWarning的玩家改成输入而不是自动获取
整理代码
This commit is contained in:
MrZ_26
2022-11-03 18:06:28 +08:00
parent b2b5c99425
commit 43649c54a3
4 changed files with 63 additions and 51 deletions

View File

@@ -800,13 +800,12 @@ do-- function resetGameData(args)
collectgarbage()
end
end
do-- function checkWarning()
do-- function checkWarning(P,dt)
local max=math.max
function checkWarning(dt)
local P1=PLAYERS[1]
if P1.alive then
if P1.frameRun%26==0 then
local F=P1.field
function checkWarning(P,dt)
if P.alive then
if P.frameRun%26==0 then
local F=P.field
local height=0-- Max height of row 4~7
for x=4,7 do
for y=#F,1,-1 do
@@ -818,7 +817,7 @@ do-- function checkWarning()
end
end
end
GAME.warnLVL0=math.log(height-(P1.gameEnv.fieldH-5)+P1.atkBufferSum*.8)
GAME.warnLVL0=math.log(height-(P.gameEnv.fieldH-5)+P.atkBufferSum*.8)
end
local _=GAME.warnLVL
if _<GAME.warnLVL0 then
@@ -827,7 +826,7 @@ do-- function checkWarning()
_=max(_-.026,0)
end
GAME.warnLVL=_
if GAME.warnLVL>1.126 and P1.frameRun%30==0 then
if GAME.warnLVL>1.126 and P.frameRun%30==0 then
SFX.fplay('warn_beep',SETTING.sfx_warn)
end
elseif GAME.warnLVL>0 then

View File

@@ -3,7 +3,7 @@ local WS=WS
local NET={
uid=false,
uid_sid={},
storedStream={},
storedStream=false,
roomState={-- A copy of room structure on server
info={
@@ -440,19 +440,19 @@ end
--Remove player when leave
local function _playerLeaveRoom(uid)
if SCN.cur=='net_game' then
for i=1,#PLAYERS do if PLAYERS[i].uid==uid then table.remove(PLAYERS,i) break end end
for i=1,#PLY_ALIVE do if PLY_ALIVE[i].uid==uid then table.remove(PLY_ALIVE,i) break end end
if uid==USER.uid then
SCN.backTo('net_menu')
else
NETPLY.remove(uid)
end
if SCN.cur~='net_game' then return end
for i=1,#PLAYERS do if PLAYERS[i].uid==uid then table.remove(PLAYERS,i) break end end
for i=1,#PLY_ALIVE do if PLY_ALIVE[i].uid==uid then table.remove(PLY_ALIVE,i) break end end
if uid==USER.uid then
GAME.playing=false
SCN.backTo('net_menu')
else
NETPLY.remove(uid)
end
end
--Push stream data to players
local function _pumpStream(d)
function NET.pumpStream(d)
if d.playerId==USER.uid then return end
for _,P in next,PLAYERS do
if P.uid==d.playerId then
@@ -586,14 +586,13 @@ function NET.wsCallBack.global_getOnlineCount(body)
NET.onlineCount=tonumber(body.data) or "_"
end
function NET.wsCallBack.room_chat(body)
if SCN.cur=='net_game' then
TASK.unlock('receiveMessage')
TASK.lock('receiveMessage',1)
NET.textBox:push{
COLOR.Z,_getFullName(body.data.playerId),
COLOR.N,body.data.message,
}
end
if SCN.cur~='net_game' then return end
TASK.unlock('receiveMessage')
TASK.lock('receiveMessage',1)
NET.textBox:push{
COLOR.Z,_getFullName(body.data.playerId),
COLOR.N,body.data.message,
}
end
function NET.wsCallBack.room_create(body)
MES.new('check',text.createRoomSuccessed)
@@ -625,7 +624,6 @@ function NET.wsCallBack.room_enter(body)
NET.roomState=body.data
NETPLY.clear()
destroyPlayers()
TABLE.cut(NET.storedStream)
loadGame('netBattle',true,true)
for _,p in next,body.data.players do
NETPLY.add{
@@ -638,6 +636,7 @@ function NET.wsCallBack.room_enter(body)
}
end
if NET.roomState.state=='Playing' then
NET.storedStream={}
for _,p in next,body.data.players do
table.insert(NET.storedStream,{
playerId=p.playerId,
@@ -660,7 +659,7 @@ function NET.wsCallBack.room_enter(body)
config=p.config,
}
NET.textBox:push{COLOR.Y,text.joinRoom:repD(_getFullName(p.playerId))}
if not GAME.playing then
if not TASK.getLock('netPlaying') then
SFX.play('connected')
NET.freshRoomAllReady()
end
@@ -723,7 +722,7 @@ function NET.wsCallBack.player_setState(body)-- not used
end
function NET.wsCallBack.player_stream(body)
if SCN.cur~='net_game' then return end
_pumpStream(body.data)
NET.pumpStream(body.data)
end
function NET.wsCallBack.player_setPlayMode(body)
if SCN.cur~='net_game' then return end
@@ -773,6 +772,7 @@ function NET.ws_update()
TEST.yieldT(1/26)
if WS.status('game')=='dead' then
TEST.yieldUntilNextScene()
GAME.playing=false
SCN.backTo('main')
return
elseif WS.status('game')=='running' then
@@ -791,6 +791,7 @@ function NET.ws_update()
USER.uid=res.data
else
TEST.yieldUntilNextScene()
GAME.playing=false
SCN.backTo('main')
return
end
@@ -806,6 +807,7 @@ function NET.ws_update()
if WS.status('game')=='dead' then
TEST.yieldUntilNextScene()
GAME.playing=false
SCN.backTo('main')
return
end
@@ -824,12 +826,13 @@ function NET.ws_update()
if msg and msg.message then LOG(msg.message) end
end
TEST.yieldUntilNextScene()
GAME.playing=false
SCN.backTo('main')
return
elseif msg then
msg=JSON.decode(msg)
-- print(("Recv: <-- $1 err:$2"):repD(msg.action,msg.errno))
print(("Recv: <-- $1 err:$2"):repD(msg.action,msg.errno)) print(TABLE.dump(msg),"\n")
-- print(("Recv: <-- $1 err:$2"):repD(msg.action,msg.errno)) print(TABLE.dump(msg),"\n")
if msg.errno~=0 then
parseError(msg.message~=nil and msg.message or msg)
else

View File

@@ -296,7 +296,7 @@ local function _update_common(dt)
end
-- Warning check
checkWarning(dt)
checkWarning(PLAYERS[1],dt)
end
function scene.update(dt)
trigGameRate=trigGameRate+gameRate

View File

@@ -41,6 +41,7 @@ end
local function _quit()
if tryBack() then
NET.room_leave()
GAME.playing=false
SCN.back()
end
end
@@ -218,32 +219,34 @@ function scene.update(dt)
NET.freshRoomAllReady()
return
else
local P1=PLAYERS[1]
touchMoveLastFrame=false
VK.update(dt)
-- Update players
for p=1,#PLAYERS do PLAYERS[p]:update(dt) end
if #PLAYERS>0 then
-- Update players
for p=1,#PLAYERS do PLAYERS[p]:update(dt) end
-- Warning check
checkWarning(dt)
local P1=PLAYERS[1]
-- Upload stream
if not NET.spectate and P1.frameRun-lastUpstreamTime>8 then
local stream
if not GAME.rep[upstreamProgress] then
ins(GAME.rep,P1.frameRun)
ins(GAME.rep,0)
-- Warning check
checkWarning(P1,dt)
-- Upload stream
if not NET.spectate and P1.frameRun-lastUpstreamTime>8 then
local stream
if not GAME.rep[upstreamProgress] then
ins(GAME.rep,P1.frameRun)
ins(GAME.rep,0)
end
stream,upstreamProgress=DATA.dumpRecording(GAME.rep,upstreamProgress)
if #stream%3==1 then
stream=stream.."\0\0"
elseif #stream%3==2 then
stream=stream.."\0\0\0\0"
end
NET.player_stream(stream)
lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99
end
stream,upstreamProgress=DATA.dumpRecording(GAME.rep,upstreamProgress)
if #stream%3==1 then
stream=stream.."\0\0"
elseif #stream%3==2 then
stream=stream.."\0\0\0\0"
end
NET.player_stream(stream)
lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99
end
end
else
@@ -256,10 +259,17 @@ function scene.update(dt)
upstreamProgress=1
resetGameData('n',NET.seed)
NETPLY.mouseMove(0,0)
for i=1,#NETPLY.list do
NETPLY.list[i].readyMode='Playing'
end
NET.spectate=PLAYERS[1].uid~=USER.uid
if NET.storedStream then
for i=1,#NET.storedStream do
NET.pumpStream(NET.storedStream[i])
end
NET.storedStream=false
end
end
end
end