From b4dc431b6f7d655142295e33b9228be243ab2969 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 30 Jan 2023 20:17:41 +0000 Subject: switch to using json for packet formatting --- client/main.lua | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) (limited to 'client/main.lua') diff --git a/client/main.lua b/client/main.lua index 14392d1..b2e2b3f 100644 --- a/client/main.lua +++ b/client/main.lua @@ -1,5 +1,5 @@ local enet = require"enet" -local words = require"common.words" +local json = require"common.dkjson" -- local SERVER_HOSTNAME = "ubq323.website" local SERVER_HOSTNAME = "localhost" @@ -64,7 +64,7 @@ end local function sync_local_player(pl,peer) -- send updated info about local player to server if pl.pos_dirty then - peer:send(words.join("ppos",pl.pos.x,pl.pos.y)) + peer:send(json.encode{t="ppos",x=pl.pos.x,y=pl.pos.y}) end end @@ -78,38 +78,22 @@ function love.update(dt) repeat local ev = host:service() if ev and ev.type == "receive" then - local w = words.split(ev.data) - local op = w[1] + local j = json.decode(ev.data) + local op = j.t if op == "join" then - local id,x,y,r,g,b = unpack(w,2) - -- blegh - id=tonumber(id) - x=tonumber(x) - y=tonumber(y) - r=tonumber(r) - g=tonumber(g) - b=tonumber(b) - remote_players[id] = {pos=coords.Pos.make(x,y),color={r,g,b},id=id} + local pl = j.pl + remote_players[pl.id] = {pos=coords.Pos.make(pl.x,pl.y),color=pl.color,id=pl.id} elseif op == "leave" then - local id = tonumber(w[2]) + local id = j.id remote_players[id]=nil elseif op == "move" then - local id,x,y = unpack(w,2) - id=tonumber(id) - x=tonumber(x) - y=tonumber(y) - assert(remote_players[id],"wheeze") + local id,x,y = j.id,j.x,j.y + assert(remote_players[id],"wheeze "..id) remote_players[id].pos.x = x remote_players[id].pos.y = y elseif op == "you" then - local id,x,y,r,g,b = unpack(w,2) - id=tonumber(id) - x=tonumber(x) - y=tonumber(y) - r=tonumber(r) - g=tonumber(g) - b=tonumber(b) - local_player = {pos=coords.Pos.make(x,y),color={r,g,b},id=id} + local pl = j.pl + local_player = {pos=coords.Pos.make(pl.x,pl.y),color=pl.color,id=pl.id} end end until not ev @@ -143,7 +127,7 @@ function love.draw() love.graphics.origin() love.graphics.scale(2) - love.graphics.setColor(1,1,1) + love.graphics.setColor(0,0,0) love.graphics.print(table.concat( {"sm "..tostring(sm), "wm "..tostring(wm), -- cgit v1.2.3