房间内玩家对象管理单独做进新模块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"
VK= require"parts.virtualKey"
PLY= require"parts.player"
netPLY= require"parts.netPlayer"
AIFUNC= require"parts.ai"
AIBUILDER= require"parts.AITemplate"
MODES= require"parts.modes"

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
local data=love.data
local ins,rem=table.insert,table.remove
local rem=table.remove
local WS,TIME=WS,TIME
local NET={
connected=false,
allow_online=false,
@@ -352,28 +353,28 @@ function NET.updateWS_play()
elseif res.action==2 then--Player join
if res.type=='Self'then
--Enter new room
TABLE.cut(PLY_NET)
netPLY.clear()
if d.players then
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,
username=p.username,
sid=p.sid,
ready=p.ready,
config=p.config,
})
}
end
end
loadGame('netBattle',true,true)
else
--Load other players
ins(PLY_NET,{
netPLY.add{
uid=d.uid,
username=d.username,
sid=d.sid,
ready=d.ready,
config=d.config,
})
}
if SCN.socketRead then SCN.socketRead('Join',d)end
NET.allReady=false
end
@@ -384,12 +385,7 @@ function NET.updateWS_play()
NET.unlock('quit')
SCN.back()
else
for i=1,#PLY_NET do
if PLY_NET[i].sid==d.sid then
rem(PLY_NET,i)
break
end
end
netPLY.remove(d.sid)
for i=1,#PLAYERS do
if PLAYERS[i].sid==d.sid then
rem(PLAYERS,i)
@@ -407,36 +403,9 @@ function NET.updateWS_play()
elseif res.action==4 then--Player talk
if SCN.socketRead then SCN.socketRead('Talk',d)end
elseif res.action==5 then--Player change settings
if tostring(USER.uid)~=d.uid then
for i=1,#PLY_NET do
if PLY_NET[i].uid==d.uid then
PLY_NET[i].config=d.config
break
end
end
end
netPLY.setConf(d.uid,d.config)
elseif res.action==6 then--One ready
for i,p in next,PLY_NET do
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
netPLY.setReady(d.uid,d.ready)
elseif res.action==7 then--All Ready
SFX.play('reach',.6)
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()
end
function PLY.newRemotePlayer(id,mini,data)
function PLY.newRemotePlayer(id,mini,uid)
local P=newEmptyPlayer(id,mini)
P.type='remote'
P.update=PLY.update.remote_alive
@@ -382,11 +382,11 @@ function PLY.newRemotePlayer(id,mini,data)
P.stream={}
P.streamProgress=1
data.p=P
P.uid=data.uid
P.username=data.username
P.sid=data.sid
loadRemoteEnv(P,data.config)
netPLY.setPlayerObj(uid,P)
P.uid=uid
P.username=netPLY.getUsername(uid)
P.sid=netPLY.getSID(uid)
loadRemoteEnv(P,netPLY.getConfig(uid))
applyGameEnv(P)
end

View File

@@ -1,7 +1,7 @@
local gc,tc=love.graphics,love.touch
local ins=table.insert
local SCR,VK,NET=SCR,VK,NET
local PLAYERS,PLY_NET,GAME=PLAYERS,PLY_NET,GAME
local SCR,VK,NET,netPLY=SCR,VK,NET,netPLY
local PLAYERS,GAME=PLAYERS,GAME
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
else
if key=="space"then
NET.signal_ready(not PLY_NET[1].ready)
NET.signal_ready(not netPLY.getReady(1))
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')
end
end
@@ -159,9 +159,7 @@ function scene.socketRead(cmd,d)
elseif cmd=="Go"then
if not playing then
playing=true
for i=1,#PLY_NET do
PLY_NET[i].ready=false
end
netPLY.resetReady()
lastUpstreamTime=0
upstreamProgress=1
resetGameData('n',d.seed)
@@ -177,12 +175,8 @@ function scene.socketRead(cmd,d)
break
end
end
if not winnerUID then return end
for _,p in next,PLY_NET do
if p.uid==winnerUID then
TEXT.show(text.champion:gsub("$1",p.username),640,260,80,'zoomout',.26)
break
end
if winnerUID then
TEXT.show(text.champion:gsub("$1",netPLY.getUsername(winnerUID)),640,260,80,'zoomout',.26)
end
elseif cmd=="Stream"then
if d.uid~=USER.uid and playing then
@@ -205,30 +199,32 @@ function scene.update(dt)
NET.wsclose_stream()
SCN.back()
end
if not playing then return end
if playing then
local P1=PLAYERS[1]
local P1=PLAYERS[1]
touchMoveLastFrame=false
VK.update()
touchMoveLastFrame=false
VK.update()
--Update players
for p=1,#PLAYERS do PLAYERS[p]:update(dt)end
--Update players
for p=1,#PLAYERS do PLAYERS[p]:update(dt)end
--Warning check
checkWarning()
--Warning check
checkWarning()
--Upload stream
if P1.frameRun-lastUpstreamTime>8 then
local stream
stream,upstreamProgress=DATA.dumpRecording(GAME.rep,upstreamProgress)
if #stream>0 then
NET.uploadRecStream(stream)
else
ins(GAME.rep,P1.frameRun)
ins(GAME.rep,0)
--Upload stream
if P1.frameRun-lastUpstreamTime>8 then
local stream
stream,upstreamProgress=DATA.dumpRecording(GAME.rep,upstreamProgress)
if #stream>0 then
NET.uploadRecStream(stream)
else
ins(GAME.rep,P1.frameRun)
ins(GAME.rep,0)
end
lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99
end
lastUpstreamTime=PLAYERS[1].alive and P1.frameRun or 1e99
else
netPLY.update(dt)
end
end
@@ -247,26 +243,8 @@ function scene.draw()
--Warning
drawWarning()
else
for i=1,#PLY_NET do
local p=PLY_NET[i]
--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
--Users
netPLY.draw()
--Ready & Set mark
gc.setColor(.1,1,0,.9)
@@ -290,13 +268,13 @@ function scene.draw()
end
scene.widgetList={
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",
hide=function()
return
playing or
NET.serverGaming or
PLY_NET[1].ready or
netPLY.getReady(1)or
NET.getlock('ready')
end},
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
playing or
NET.serverGaming or
not PLY_NET[1].ready or
not netPLY.getReady(1)or
NET.getlock('ready')
end},
WIDGET.newKey{name="hideChat",fText="...",x=380,y=35,w=60,font=35,code=pressKey"\\"},