联网对战(初版,不可用)

This commit is contained in:
MrZ626
2021-01-29 01:28:55 +08:00
parent c8f4faaa95
commit 2cc74ea946
7 changed files with 167 additions and 84 deletions

View File

@@ -477,18 +477,23 @@ function applyCustomGame()--Apply CUSTOMENV, BAG, MISSION
GAME.modeEnv.mission=nil
end
end
function loadGame(M,ifQuickPlay)--Load a mode and go to game scene
function loadGame(M,ifQuickPlay,ifNet)--Load a mode and go to game scene
freshDate()
if legalGameTime()then
if MODES[M].score then STAT.lastPlay=M end
GAME.curModeName=M
GAME.curMode=MODES[M]
GAME.modeEnv=GAME.curMode.env
drawableText.modeName:set(text.modes[M][1])
drawableText.levelName:set(text.modes[M][2])
GAME.init=true
SCN.go("play",ifQuickPlay and"swipeD"or"fade_togame")
SFX.play("enter")
GAME.net=ifNet
if ifNet then
SCN.go("net_game","swipeD")
else
drawableText.modeName:set(text.modes[M][1])
drawableText.levelName:set(text.modes[M][2])
SCN.go("play",ifQuickPlay and"swipeD"or"fade_togame")
SFX.play("enter")
end
end
end
function initPlayerPosition(sudden)--Set initial position for every player
@@ -506,26 +511,32 @@ function initPlayerPosition(sudden)--Set initial position for every player
if L[3]then L[3][method](L[3],965,30,.5)end
if L[4]then L[4][method](L[4],20,390,.5)end
if L[5]then L[5][method](L[5],20,30,.5)end
elseif #L==49 then
elseif #L<=49 then
local n=2
for i=1,4 do for j=1,6 do
if not L[n]then return end
L[n][method](L[n],78*i-54,115*j-98,.09)
n=n+1
end end
for i=9,12 do for j=1,6 do
if not L[n]then return end
L[n][method](L[n],78*i+267,115*j-98,.09)
n=n+1
end end
elseif #L==99 then
elseif #L<=99 then
local n=2
for i=1,7 do for j=1,7 do
if not L[n]then return end
L[n][method](L[n],46*i-36,97*j-72,.068)
n=n+1
end end
for i=15,21 do for j=1,7 do
if not L[n]then return end
L[n][method](L[n],46*i+264,97*j-72,.068)
n=n+1
end end
else
error("TOO MANY PLAYERS!")
end
end
do--function resetGameData(args)
@@ -568,7 +579,7 @@ do--function resetGameData(args)
end
return S
end
function resetGameData(args)
function resetGameData(args,playerData)
if not args then args=""end
if PLAYERS[1]and not GAME.replaying and(GAME.frame>400 or GAME.result)then
mergeStat(STAT,PLAYERS[1].stat)
@@ -583,7 +594,7 @@ do--function resetGameData(args)
GAME.recording=false
GAME.replaying=1
else
GAME.frame=150-SETTING.reTime*15
GAME.frame=args:find("n")and 0 or 150-SETTING.reTime*15
GAME.seed=rnd(1046101471,2662622626)
GAME.pauseTime=0
GAME.pauseCount=0
@@ -597,7 +608,7 @@ do--function resetGameData(args)
end
destroyPlayers()
GAME.curMode.load()
GAME.curMode.load(playerData)
initPlayerPosition(args:find("q"))
restoreVirtualKey()
if GAME.modeEnv.task then
@@ -685,19 +696,11 @@ function dumpRecording(list,ptr)
return out..buffer
end
function pumpRecording(str,L)
-- str=data.decode("string","base64",str)
str=data.decode("string","base64",str)
local len=#str
local p=1
local list,curFrm
if L then
list=L
curFrm=L[#L-1]or 0
else
list={}
curFrm=0
end
local curFrm=L[#L-1]or 0
while p<=len do
--Read delta time
::nextByte::
@@ -708,17 +711,16 @@ function pumpRecording(str,L)
goto nextByte
end
curFrm=curFrm+b
list[#list+1]=curFrm
L[#L+1]=curFrm
p=p+1
b=byte(str,p)
if b>127 then
b=b-256
end
list[#list+1]=b
L[#L+1]=b
p=p+1
end
return list
end
do--function saveRecording()
local noRecList={"custom","solo","round","techmino"}

View File

@@ -211,6 +211,7 @@ CUSTOMENV={--gameEnv for cutsom game
}
GAME={--Global game data
init=false, --If need initializing game when enter scene-play
net=false, --If play net game
frame=0, --Frame count
result=false, --Game result (string)

View File

@@ -105,4 +105,6 @@ return{
{name="custom_puzzle"},
{name="custom_clear"},
{name="netBattle"},
}

16
parts/modes/netBattle.lua Normal file
View File

@@ -0,0 +1,16 @@
return{
color=COLOR.white,
env={
--TODO: ?
},
load=function(playerData)
PLY.newPlayer(1)
local N=2
for i=1,#playerData do
if playerData[i].id~=USER.id then
PLY.newRemotePlayer(N,false,playerData[i])
N=N+1
end
end
end,
}

View File

@@ -332,10 +332,12 @@ function PLY.newDemoPlayer(id)
}
P:popNext()
end
function PLY.newRemotePlayer(id,mini)
function PLY.newRemotePlayer(id,mini,userInfo)
local P=newEmptyPlayer(id,mini)
P.type="remote"
P.update=PLY.update.remote_alive
P.userName=userInfo.name
P.userID=userInfo.id
P.stream={}
P.streamProgress=1

View File

@@ -2,6 +2,8 @@ local gc=love.graphics
local gc_setColor,gc_circle=gc.setColor,gc.circle
local tc=love.touch
local playerData
local ins,rem=table.insert,table.remove
local max,sin=math.max,math.sin
local SCR=SCR
@@ -22,19 +24,29 @@ local function onVirtualkey(x,y)
return nearest
end
local hideChatBox=false
local textBox=WIDGET.newTextBox{name="texts",x=980,y=20,w=290,h=300,hide=function()return hideChatBox end}
local playing
local lastBackTime=0
local noTouch=false
local noTouch,noKey=false,false
local touchMoveLastFrame=false
local scene={}
function scene.sceneBack()
wsWrite("Q")
WSCONN=false
LOG.print(text.wsDisconnected,"warn")
end
function scene.sceneInit()
love.keyboard.setKeyRepeat(false)
if GAME.init then
resetGameData()
GAME.init=false
end
TASK.new(TICK_wsRead)
textBox:clear()
playerData={}
noTouch=not SETTING.VKSwitch
playing=false
end
function scene.touchDown(_,x,y)
@@ -102,54 +114,119 @@ end
function scene.keyDown(key)
if key=="escape"then
if TIME()-lastBackTime<1 then
WSCONN=false
SCN.back()
else
lastBackTime=TIME()
LOG.print(text.sureQuit,COLOR.orange)
end
else
local m=keyMap
for k=1,20 do
if key==m[1][k]or key==m[2][k]then
PLAYERS[1]:pressKey(k)
VK[k].isDown=true
VK[k].pressTime=10
return
end
if noKey then return end
local k=keyMap.keyboard[key]
if k and k>0 then
PLAYERS[1]:pressKey(k)
VK[k].isDown=true
VK[k].pressTime=10
end
end
end
function scene.keyUp(key)
local m=keyMap
for k=1,20 do
if key==m[1][k]or key==m[2][k]then
PLAYERS[1]:releaseKey(k)
VK[k].isDown=false
return
end
if noKey then return end
local k=keyMap.keyboard[key]
if k and k>0 then
PLAYERS[1]:releaseKey(k)
VK[k].isDown=false
end
end
function scene.gamepadDown(key)
local m=keyMap
for k=1,20 do
if key==m[3][k]or key==m[4][k]then
if key=="back"then
if TIME()-lastBackTime<1 then
WSCONN=false
SCN.back()
else
lastBackTime=TIME()
LOG.print(text.sureQuit,COLOR.orange)
end
else
if noKey then return end
local k=keyMap.joystick[key]
if k and k>0 then
PLAYERS[1]:pressKey(k)
VK[k].isDown=true
VK[k].pressTime=10
return
end
end
if key=="back"then pauseGame()end
end
function scene.gamepadUp(key)
local m=keyMap
for k=1,20 do
if key==m[3][k]or key==m[4][k]then
PLAYERS[1]:releaseKey(k)
VK[k].isDown=false
return
if noKey then return end
local k=keyMap.joystick[key]
if k and k>0 then
PLAYERS[1]:releaseKey(k)
VK[k].isDown=false
return
end
end
function scene.socketRead(mes)
local cmd=mes:sub(1,1)
local args=splitStr(mes:sub(2),":")
LOG.print(cmd,table.concat(args, " ; "))-------DEBUG PRINT
if cmd=="J"or cmd=="L"then
textBox:push{
COLOR.lR,args[1],
COLOR.dY,args[2].." ",
COLOR.Y,text[cmd=="J"and"chatJoin"or"chatLeave"]
}
if cmd=="J"then
ins(playerData,{name=args[1],id=args[2],conf=false})
if not playing then
resetGameData("n",playerData)
end
else
for i=1,#playerData do
if playerData[i].id==args[2]then
rem(playerData,i)
break
end
end
for i=1,#PLAYERS do
if PLAYERS[i].userID==args[2]then
rem(PLAYERS,i)
break
end
end
for i=1,#PLAYERS.alive do
if PLAYERS.alive[i].userID==args[2]then
rem(PLAYERS,i)
break
end
end
end
elseif cmd=="T"then
textBox:push{
COLOR.W,args[1],
COLOR.dY,args[2].." ",
COLOR.sky,args[3]
}
elseif cmd=="C"then
for i=1,#playerData do
if playerData[i].id==args[1]then
playerData[i].conf=args[2]
return
end
end
elseif cmd=="S"then
for _,P in next,PLAYERS do
if P.userID==args[1]then
pumpRecording(args[2],P.stream)
end
end
elseif cmd=="B"then
playing=true
resetGameData("n",playerData)
elseif cmd=="F"then
playing=false
else
LOG.print("Illegal message: ["..mes.."]",30,COLOR.green)
end
end
@@ -157,7 +234,6 @@ function scene.update(dt)
local _
local P1=PLAYERS[1]
local GAME=GAME
GAME.frame=GAME.frame+1
touchMoveLastFrame=false
@@ -171,26 +247,10 @@ function scene.update(dt)
end
end
--Replay
if GAME.replaying then
_=GAME.replaying
local L=GAME.rep
while GAME.frame==L[_]do
local k=L[_+1]
if k>0 then
P1:pressKey(k)
VK[k].isDown=true
VK[k].pressTime=10
else
VK[-k].isDown=false
P1:releaseKey(-k)
end
_=_+2
end
GAME.replaying=_
end
if not playing then return end
--Counting,include pre-das,directy RETURN,or restart counting
GAME.frame=GAME.frame+1
if GAME.frame<180 then
if GAME.frame==179 then
gameStart()
@@ -297,11 +357,6 @@ function scene.draw()
end
end
--Mode info
gc_setColor(1,1,1,.8)
gc.draw(drawableText.modeName,485,10)
gc.draw(drawableText.levelName,511+drawableText.modeName:getWidth(),10)
--Warning
gc.push("transform")
gc.origin()
@@ -314,7 +369,9 @@ function scene.draw()
gc.pop()
end
scene.widgetList={
WIDGET.newKey{name="quit",x=1235,y=45,w=80,font=20,code=pressKey"escape"},
textBox,
WIDGET.newKey{name="hideChat",fText="[..]",x=410,y=40,w=60,font=35,code=function()hideChatBox=not hideChatBox end},
WIDGET.newKey{name="quit",fText="X",x=870,y=40,w=60,font=40,code=pressKey"escape"},
}
return scene

View File

@@ -38,8 +38,7 @@ local function task_enterRoom(task)
local wsconn,connErr=client.poll(task)
if wsconn then
WSCONN=wsconn
SCN.go("net_game")
loadGame("sprint_40l")
loadGame("netBattle",true,true)
LOG.print(text.wsSuccessed,"warn")
return
elseif connErr then
@@ -103,6 +102,10 @@ function scene.keyDown(k)
end
end
elseif k=="return"then
if rooms[selected].private then
LOG.print("Can't enter private room now")
return
end
wsConnect(
task_enterRoom,
PATH.socket..PATH.play_room..