联网推进(修改增减玩家相关)

This commit is contained in:
MrZ626
2021-04-06 01:06:32 +08:00
parent 3b5d21fcd4
commit 9c12048964
2 changed files with 46 additions and 57 deletions

View File

@@ -1,4 +1,5 @@
local data=love.data
local ins,rem=table.insert,table.remove
local NET={
login=false,
allow_online=false,
@@ -294,21 +295,49 @@ function NET.updateWS_play()
if res.type=="Self"then
--Create room
TABLE.clear(PLY_NET)
local d=res.data
table.insert(PLY_NET,{
conf=dumpBasicConfig(),
name=USER.name,
uid=USER.id,
sid=d.sid,
ready=d.ready,
})
if data.players then
for _,p in next,data.players do
ins(PLY_NET,{
sid=p.sid,
uid=p.uid,
name=p.username,
conf=p.config,
ready=p.ready,
})
end
end
loadGame("netBattle",true,true)
_unlock("enterRoom")
else
--Others join room
--Load other players
ins(PLY_NET,{
sid=data.sid,
uid=data.uid,
name=data.username,
conf=data.config,
ready=data.ready,
})
SCN.socketRead("Join",res.data)
end
elseif res.action==3 then--Player leave
for i=1,#PLY_NET do
if PLY_NET[i].id==data.uid then
rem(PLY_NET,i)
break
end
end
for i=1,#PLAYERS do
if PLAYERS[i].userID==data.uid then
rem(PLAYERS,i)
break
end
end
for i=1,#PLY_ALIVE do
if PLY_ALIVE[i].userID==data.uid then
rem(PLY_ALIVE,i)
break
end
end
SCN.socketRead("Leave",res.data)
elseif res.action==4 then--Player talk
SCN.socketRead("Talk",res.data)

View File

@@ -1,7 +1,7 @@
local gc=love.graphics
local tc=love.touch
local ins,rem=table.insert,table.remove
local ins=table.insert
local SCR=SCR
local VK=virtualkey
@@ -9,8 +9,6 @@ local onVirtualkey=onVirtualkey
local pressVirtualkey=pressVirtualkey
local updateVirtualkey=updateVirtualkey
local PLY_NET=PLY_NET
local hideChatBox
local textBox=WIDGET.newTextBox{name="texts",x=340,y=80,w=600,h=550,hide=function()return hideChatBox end}
@@ -139,33 +137,11 @@ end
function scene.socketRead(cmd,data)
if cmd=="Join"then
if playerInitialized then
textBox:push{
COLOR.lR,data.username,
COLOR.dY,"#"..data.uid.." ",
COLOR.Y,text.joinRoom,
}
end
if data.players then
for _,p in next,data.players do
ins(PLY_NET,{
sid=p.sid,
uid=p.uid,
name=p.username,
conf=p.config,
ready=p.ready,
})
end
else
ins(PLY_NET,{
sid=data.sid,
uid=data.uid,
name=data.username,
conf=data.config,
ready=data.ready,
})
end
playerInitialized=true
textBox:push{
COLOR.lR,data.username,
COLOR.dY,"#"..data.uid.." ",
COLOR.Y,text.joinRoom,
}
SFX.play("click")
if not playing then
resetGameData("qn")
@@ -176,25 +152,9 @@ function scene.socketRead(cmd,data)
COLOR.dY,"#"..data.uid.." ",
COLOR.Y,text.leaveRoom,
}
for i=1,#PLY_NET do
if PLY_NET[i].id==data.uid then
rem(PLY_NET,i)
break
end
if not playing then
initPlayerPosition(true)
end
for i=1,#PLAYERS do
if PLAYERS[i].userID==data.uid then
rem(PLAYERS,i)
break
end
end
for i=1,#PLY_ALIVE do
if PLY_ALIVE[i].userID==data.uid then
rem(PLY_ALIVE,i)
break
end
end
initPlayerPosition(true)
elseif cmd=="Talk"then
textBox:push{
COLOR.W,data.username,