房间内玩家对象管理单独做进新模块netPlayer,为未来消息气泡和发表情做准备

This commit is contained in:
MrZ626
2021-05-04 02:21:52 +08:00
parent a21e7a5e8d
commit 9836bda502
7 changed files with 211 additions and 111 deletions

View File

@@ -77,6 +77,7 @@ USERS= require"parts.users"
NET= require"parts.net" NET= require"parts.net"
VK= require"parts.virtualKey" VK= require"parts.virtualKey"
PLY= require"parts.player" PLY= require"parts.player"
netPLY= require"parts.netPlayer"
AIFUNC= require"parts.ai" AIFUNC= require"parts.ai"
AIBUILDER= require"parts.AITemplate" AIBUILDER= require"parts.AITemplate"
MODES= require"parts.modes" MODES= require"parts.modes"

View File

@@ -144,7 +144,6 @@ end
--Game tables --Game tables
PLAYERS={}--Players data PLAYERS={}--Players data
PLY_ALIVE={} PLY_ALIVE={}
PLY_NET={}
FIELD={}--Field(s) for custom game FIELD={}--Field(s) for custom game
BAG={}--Sequence for custom game BAG={}--Sequence for custom game
MISSION={}--Clearing mission for custom game MISSION={}--Clearing mission for custom game

View File

@@ -10,14 +10,9 @@ return{
}, },
load=function() load=function()
PLY.newPlayer(1) PLY.newPlayer(1)
local N=2 PLAYERS[1].sid=netPLY.getSID(1)
for i=1,#PLY_NET do for i=2,netPLY.getCount()do
if PLY_NET[i].uid==USER.uid then PLY.newRemotePlayer(i,false,netPLY.getUID(i))
PLAYERS[1].sid=PLY_NET[1].sid
else
PLY.newRemotePlayer(N,false,PLY_NET[i])
N=N+1
end
end end
end, end,
} }

View File

@@ -1,6 +1,7 @@
local data=love.data local data=love.data
local ins,rem=table.insert,table.remove local rem=table.remove
local WS,TIME=WS,TIME local WS,TIME=WS,TIME
local NET={ local NET={
connected=false, connected=false,
allow_online=false, allow_online=false,
@@ -352,28 +353,28 @@ function NET.updateWS_play()
elseif res.action==2 then--Player join elseif res.action==2 then--Player join
if res.type=='Self'then if res.type=='Self'then
--Enter new room --Enter new room
TABLE.cut(PLY_NET) netPLY.clear()
if d.players then if d.players then
for _,p in next,d.players do for _,p in next,d.players do
ins(PLY_NET,p.uid==USER.uid and 1 or #PLY_NET+1,{ netPLY.add{
uid=p.uid, uid=p.uid,
username=p.username, username=p.username,
sid=p.sid, sid=p.sid,
ready=p.ready, ready=p.ready,
config=p.config, config=p.config,
}) }
end end
end end
loadGame('netBattle',true,true) loadGame('netBattle',true,true)
else else
--Load other players --Load other players
ins(PLY_NET,{ netPLY.add{
uid=d.uid, uid=d.uid,
username=d.username, username=d.username,
sid=d.sid, sid=d.sid,
ready=d.ready, ready=d.ready,
config=d.config, config=d.config,
}) }
if SCN.socketRead then SCN.socketRead('Join',d)end if SCN.socketRead then SCN.socketRead('Join',d)end
NET.allReady=false NET.allReady=false
end end
@@ -384,12 +385,7 @@ function NET.updateWS_play()
NET.unlock('quit') NET.unlock('quit')
SCN.back() SCN.back()
else else
for i=1,#PLY_NET do netPLY.remove(d.sid)
if PLY_NET[i].sid==d.sid then
rem(PLY_NET,i)
break
end
end
for i=1,#PLAYERS do for i=1,#PLAYERS do
if PLAYERS[i].sid==d.sid then if PLAYERS[i].sid==d.sid then
rem(PLAYERS,i) rem(PLAYERS,i)
@@ -407,36 +403,9 @@ function NET.updateWS_play()
elseif res.action==4 then--Player talk elseif res.action==4 then--Player talk
if SCN.socketRead then SCN.socketRead('Talk',d)end if SCN.socketRead then SCN.socketRead('Talk',d)end
elseif res.action==5 then--Player change settings elseif res.action==5 then--Player change settings
if tostring(USER.uid)~=d.uid then netPLY.setConf(d.uid,d.config)
for i=1,#PLY_NET do
if PLY_NET[i].uid==d.uid then
PLY_NET[i].config=d.config
break
end
end
end
elseif res.action==6 then--One ready elseif res.action==6 then--One ready
for i,p in next,PLY_NET do netPLY.setReady(d.uid,d.ready)
if p.uid==d.uid then
if p.ready~=d.ready then
p.ready=d.ready
if not d.ready then NET.allReady=false end
SFX.play('spin_0',.6)
if i==1 then
NET.unlock('ready')
elseif not PLY_NET[1].ready then
for j=2,#PLY_NET do
if not PLY_NET[j].ready then
goto BREAK_notAllReady
end
end
SFX.play('blip_2',.5)
::BREAK_notAllReady::
end
end
break
end
end
elseif res.action==7 then--All Ready elseif res.action==7 then--All Ready
SFX.play('reach',.6) SFX.play('reach',.6)
NET.allReady=true NET.allReady=true

158
parts/netPlayer.lua Normal file
View File

@@ -0,0 +1,158 @@
local gc=love.graphics
local ins,rem=table.insert,table.remove
local posLists={
--1~5
(function()
local L={}
for i=1,5 do
L[i]={x=40,y=65+50*i,w=1000,h=46}
end
return L
end)(),
--6~17
(function()
local L={}
for i=1,17 do
L[i]={x=40,y=65+50*i,w=1000,h=46}
end
return L
end)(),
--18~31
(function()
local L={}
for i=1,31 do
L[i]={x=40,y=65+50*i,w=1000,h=46}
end
return L
end)(),
--32~49
(function()
local L={}
for i=1,49 do
L[i]={x=40,y=65+50*i,w=1000,h=46}
end
return L
end)(),
}
local posList
local PLY=setmetatable({},{
__index=function(self,uid)
for _,p in next,self do
if p.uid==uid then
return p
end
end
end
})
local netPLY={list=PLY}
local function freshPosList()
if #PLY<=5 then
posList=posLists[1]
elseif #PLY<=15 then
posList=posLists[2]
elseif #PLY<=30 then
posList=posLists[3]
end
end
function netPLY.clear()
while PLY[1]do rem(PLY)end
end
function netPLY.add(p)
ins(PLY,p.uid==USER.uid and 1 or #PLY+1,p)
p.x,p.y,p.w,p.h=640,2600,0,0
freshPosList()
end
function netPLY.remove(sid)
for i=1,#PLY do
if PLY[i].sid==sid then
rem(PLY,i)
break
end
end
freshPosList()
end
function netPLY.getCount()return #PLY end
function netPLY.getUID(i)return PLY[i].uid end
function netPLY.getUsername(uid)return PLY[uid].username end
function netPLY.getSID(i)return PLY[i].sid end
function netPLY.getReady(i)return PLY[i].ready end
function netPLY.getConfig(i)return PLY[i].config end
function netPLY.setPlayerObj(uid,p)
PLY[uid].p=p
end
function netPLY.setConf(uid,config)
if tostring(USER.uid)~=uid then
PLY[uid].config=config
end
end
function netPLY.setReady(uid,ready)
for i,p in next,PLY do
if p.uid==uid then
if p.ready~=ready then
p.ready=ready
if not ready then NET.allReady=false end
SFX.play('spin_0',.6)
if i==1 then
NET.unlock('ready')
elseif not PLY[1].ready then
for j=2,#PLY do
if not PLY[j].ready then
return
end
end
SFX.play('blip_2',.5)
end
end
return
end
end
end
function netPLY.resetReady()
for i=1,#PLY do
PLY[i].ready=false
end
end
function netPLY.update(dt)
for i=1,#PLY do
local p=PLY[i]
local t=posList[i]
p.x=p.x*.9+t.x*.1
p.y=p.y*.9+t.y*.1
p.w=p.w*.9+t.w*.1
p.h=p.h*.9+t.h*.1
end
end
function netPLY.draw()
for i=1,#PLY do
local p=PLY[i]
gc.translate(p.x,p.y)
--Rectangle
gc.setColor(COLOR[p.ready and'G'or'Z'])
gc.setLineWidth(2)
gc.rectangle('line',0,0,p.w,p.h)
--UID
setFont(40)
gc.setColor(.5,.5,.5)
gc.print("#"..p.uid,10,-5)
--Avatar
gc.setColor(1,1,1)
gc.draw(USERS.getAvatar(p.uid),160,3,nil,.3125)
--Username
gc.print(p.username,210,-5)
gc.translate(-p.x,-p.y)
end
end
return netPLY

View File

@@ -372,7 +372,7 @@ function PLY.newDemoPlayer(id)
} }
P:popNext() P:popNext()
end end
function PLY.newRemotePlayer(id,mini,data) function PLY.newRemotePlayer(id,mini,uid)
local P=newEmptyPlayer(id,mini) local P=newEmptyPlayer(id,mini)
P.type='remote' P.type='remote'
P.update=PLY.update.remote_alive P.update=PLY.update.remote_alive
@@ -382,11 +382,11 @@ function PLY.newRemotePlayer(id,mini,data)
P.stream={} P.stream={}
P.streamProgress=1 P.streamProgress=1
data.p=P netPLY.setPlayerObj(uid,P)
P.uid=data.uid P.uid=uid
P.username=data.username P.username=netPLY.getUsername(uid)
P.sid=data.sid P.sid=netPLY.getSID(uid)
loadRemoteEnv(P,data.config) loadRemoteEnv(P,netPLY.getConfig(uid))
applyGameEnv(P) applyGameEnv(P)
end end

View File

@@ -1,7 +1,7 @@
local gc,tc=love.graphics,love.touch local gc,tc=love.graphics,love.touch
local ins=table.insert local ins=table.insert
local SCR,VK,NET=SCR,VK,NET local SCR,VK,NET,netPLY=SCR,VK,NET,netPLY
local PLAYERS,PLY_NET,GAME=PLAYERS,PLY_NET,GAME local PLAYERS,GAME=PLAYERS,GAME
local textBox=WIDGET.newTextBox{name="texts",x=340,y=80,w=600,h=550,hide=false} local textBox=WIDGET.newTextBox{name="texts",x=340,y=80,w=600,h=550,hide=false}
@@ -93,9 +93,9 @@ function scene.keyDown(key)
end end
else else
if key=="space"then if key=="space"then
NET.signal_ready(not PLY_NET[1].ready) NET.signal_ready(not netPLY.getReady(1))
elseif key=="s"then elseif key=="s"then
if not(PLY_NET[1].ready or NET.getlock('ready'))then if not(netPLY.getReady(1)or NET.getlock('ready'))then
SCN.go('setting_game') SCN.go('setting_game')
end end
end end
@@ -159,9 +159,7 @@ function scene.socketRead(cmd,d)
elseif cmd=="Go"then elseif cmd=="Go"then
if not playing then if not playing then
playing=true playing=true
for i=1,#PLY_NET do netPLY.resetReady()
PLY_NET[i].ready=false
end
lastUpstreamTime=0 lastUpstreamTime=0
upstreamProgress=1 upstreamProgress=1
resetGameData('n',d.seed) resetGameData('n',d.seed)
@@ -177,12 +175,8 @@ function scene.socketRead(cmd,d)
break break
end end
end end
if not winnerUID then return end if winnerUID then
for _,p in next,PLY_NET do TEXT.show(text.champion:gsub("$1",netPLY.getUsername(winnerUID)),640,260,80,'zoomout',.26)
if p.uid==winnerUID then
TEXT.show(text.champion:gsub("$1",p.username),640,260,80,'zoomout',.26)
break
end
end end
elseif cmd=="Stream"then elseif cmd=="Stream"then
if d.uid~=USER.uid and playing then if d.uid~=USER.uid and playing then
@@ -205,30 +199,32 @@ function scene.update(dt)
NET.wsclose_stream() NET.wsclose_stream()
SCN.back() SCN.back()
end end
if not playing then return end if playing then
local P1=PLAYERS[1]
local P1=PLAYERS[1] touchMoveLastFrame=false
VK.update()
touchMoveLastFrame=false --Update players
VK.update() for p=1,#PLAYERS do PLAYERS[p]:update(dt)end
--Update players --Warning check
for p=1,#PLAYERS do PLAYERS[p]:update(dt)end checkWarning()
--Warning check --Upload stream
checkWarning() if P1.frameRun-lastUpstreamTime>8 then
local stream
--Upload stream stream,upstreamProgress=DATA.dumpRecording(GAME.rep,upstreamProgress)
if P1.frameRun-lastUpstreamTime>8 then if #stream>0 then
local stream NET.uploadRecStream(stream)
stream,upstreamProgress=DATA.dumpRecording(GAME.rep,upstreamProgress) else
if #stream>0 then ins(GAME.rep,P1.frameRun)
NET.uploadRecStream(stream) ins(GAME.rep,0)
else end
ins(GAME.rep,P1.frameRun) lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99
ins(GAME.rep,0)
end end
lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99 else
netPLY.update(dt)
end end
end end
@@ -247,26 +243,8 @@ function scene.draw()
--Warning --Warning
drawWarning() drawWarning()
else else
for i=1,#PLY_NET do --Users
local p=PLY_NET[i] netPLY.draw()
--Rectangle
gc.setColor(COLOR[p.ready and'G'or'Z'])
gc.setLineWidth(2)
gc.rectangle('line',40,65+50*i,1000,46)
--UID
setFont(40)
gc.setColor(.5,.5,.5)
gc.print("#"..p.uid,50,60+50*i)
--Avatar
gc.setColor(1,1,1)
gc.draw(USERS.getAvatar(p.uid),200,68+50*i,nil,.3125)
--Username
gc.print(p.username,240,60+50*i)
end
--Ready & Set mark --Ready & Set mark
gc.setColor(.1,1,0,.9) gc.setColor(.1,1,0,.9)
@@ -290,13 +268,13 @@ function scene.draw()
end end
scene.widgetList={ scene.widgetList={
textBox, textBox,
WIDGET.newKey{name="setting",fText=TEXTURE.setting,x=1200,y=160,w=90,h=90,code=pressKey"s",hide=function()return playing or PLY_NET[1].ready or NET.getlock('ready')end}, WIDGET.newKey{name="setting",fText=TEXTURE.setting,x=1200,y=160,w=90,h=90,code=pressKey"s",hide=function()return playing or netPLY.getReady(1)or NET.getlock('ready')end},
WIDGET.newKey{name="ready",x=900,y=560,w=400,h=100,color='lB',font=40,code=pressKey"space", WIDGET.newKey{name="ready",x=900,y=560,w=400,h=100,color='lB',font=40,code=pressKey"space",
hide=function() hide=function()
return return
playing or playing or
NET.serverGaming or NET.serverGaming or
PLY_NET[1].ready or netPLY.getReady(1)or
NET.getlock('ready') NET.getlock('ready')
end}, end},
WIDGET.newKey{name="cancel",x=900,y=560,w=400,h=100,color='H',font=40,code=pressKey"space", WIDGET.newKey{name="cancel",x=900,y=560,w=400,h=100,color='H',font=40,code=pressKey"space",
@@ -304,7 +282,7 @@ scene.widgetList={
return return
playing or playing or
NET.serverGaming or NET.serverGaming or
not PLY_NET[1].ready or not netPLY.getReady(1)or
NET.getlock('ready') NET.getlock('ready')
end}, end},
WIDGET.newKey{name="hideChat",fText="...",x=380,y=35,w=60,font=35,code=pressKey"\\"}, WIDGET.newKey{name="hideChat",fText="...",x=380,y=35,w=60,font=35,code=pressKey"\\"},